commit 732cf258e3fce3dfb2c5e323e4038a533b2d5788
parent 649ad61a4d6acd7fd1d3014523155533148a5cf1
Author: Florian Dold <dold@taler.net>
Date: Mon, 20 Jul 2026 13:58:59 +0200
util: fix negative divisors in amount division
Diffstat:
3 files changed, 14 insertions(+), 4 deletions(-)
diff --git a/packages/taler-util/src/amounts.test.ts b/packages/taler-util/src/amounts.test.ts
@@ -188,3 +188,15 @@ test("amount division", (t) => {
"EUR:1" as AmountString,
);
});
+
+test("Amounts.divide rejects non-positive or non-integer divisors", (t) => {
+ const a = jAmt(10, 0, "EUR");
+
+ const r = Amounts.divide(a, 2);
+ assert.strictEqual(r.value, 5);
+ assert.strictEqual(r.fraction, 0);
+
+ assert.throws(() => Amounts.divide(a, 0));
+ assert.throws(() => Amounts.divide(a, -3));
+ assert.throws(() => Amounts.divide(a, 1.5));
+});
diff --git a/packages/taler-util/src/amounts.ts b/packages/taler-util/src/amounts.ts
@@ -430,8 +430,8 @@ export class Amounts {
* Divide an amount. Throws on division by zero.
*/
static divide(a: AmountJson, n: number): AmountJson {
- if (n === 0) {
- throw Error(`Division by 0`);
+ if (!Number.isInteger(n) || n <= 0) {
+ throw Error(`Amount can only be divided by a positive integer`);
}
if (n === 1) {
return { value: a.value, fraction: a.fraction, currency: a.currency };
diff --git a/packages/taler-util/src/taler-crypto.test.ts b/packages/taler-util/src/taler-crypto.test.ts
@@ -415,8 +415,6 @@ test("age restriction commitCompare accepts a matching derived commitment", asyn
const salt = getRandomBytes(32);
const derived = await AgeRestriction.commitmentDerive(proof, salt);
- // By definition (see doc comment on commitCompare): derived.commitment
- // should equal proof.commitment * salt.
const ok = await AgeRestriction.commitCompare(
derived.commitment,
proof.commitment,