Improve field tests

This commit is contained in:
Paul Miller 2022-12-31 06:49:09 +00:00
parent 5d42549acc
commit cc2c84f040
No known key found for this signature in database
GPG Key ID: 697079DA6878B89B
2 changed files with 246 additions and 520 deletions

@ -15,7 +15,252 @@ import { starkCurve } from '../lib/esm/stark.js';
import { pallas, vesta } from '../lib/esm/pasta.js'; import { pallas, vesta } from '../lib/esm/pasta.js';
import { bn254 } from '../lib/esm/bn.js'; import { bn254 } from '../lib/esm/bn.js';
import { jubjub } from '../lib/esm/jubjub.js'; import { jubjub } from '../lib/esm/jubjub.js';
import { bls12_381 } from '../lib/esm/bls12-381.js';
// Fields tests
const FIELDS = {
secp192r1: { Fp: [secp192r1.CURVE.Fp] },
secp224r1: { Fp: [secp224r1.CURVE.Fp] },
secp256r1: { Fp: [secp256r1.CURVE.Fp] },
secp521r1: { Fp: [secp521r1.CURVE.Fp] },
secp256k1: { Fp: [secp256k1.CURVE.Fp] },
stark: { Fp: [starkCurve.CURVE.Fp] },
jubjub: { Fp: [jubjub.CURVE.Fp] },
ed25519: { Fp: [ed25519.CURVE.Fp] },
ed448: { Fp: [ed448.CURVE.Fp] },
bn254: { Fp: [bn254.CURVE.Fp] },
pallas: { Fp: [pallas.CURVE.Fp] },
vesta: { Fp: [vesta.CURVE.Fp] },
bls12: {
Fp: [bls12_381.CURVE.Fp],
Fp2: [
bls12_381.CURVE.Fp2,
fc.array(fc.bigInt(1n, bls12_381.CURVE.Fp.ORDER - 1n), {
minLength: 2,
maxLength: 2,
}),
(Fp2, num) => Fp2.fromBigTuple([num[0], num[1]]),
],
// Fp6: [bls12_381.CURVE.Fp6],
Fp12: [
bls12_381.CURVE.Fp12,
fc.array(fc.bigInt(1n, bls12_381.CURVE.Fp.ORDER - 1n), {
minLength: 12,
maxLength: 12,
}),
(Fp12, num) => Fp12.fromBigTwelve(num),
],
},
};
for (const c in FIELDS) {
const curve = FIELDS[c];
for (const f in curve) {
const Fp = curve[f][0];
const name = `${c}/${f}:`;
const FC_BIGINT = curve[f][1] ? curve[f][1] : fc.bigInt(1n, Fp.ORDER - 1n);
const create = curve[f][2] ? curve[f][2].bind(null, Fp) : (num) => Fp.create(num);
should(`${name} equality`, () => {
fc.assert(
fc.property(FC_BIGINT, (num) => {
const a = create(num);
const b = create(num);
deepStrictEqual(Fp.equals(a, b), true);
deepStrictEqual(Fp.equals(b, a), true);
})
);
});
should(`${name} non-equality`, () => {
fc.assert(
fc.property(FC_BIGINT, FC_BIGINT, (num1, num2) => {
const a = create(num1);
const b = create(num2);
deepStrictEqual(Fp.equals(a, b), num1 === num2);
deepStrictEqual(Fp.equals(b, a), num1 === num2);
})
);
});
should(`${name} add/subtract/commutativity`, () => {
fc.assert(
fc.property(FC_BIGINT, FC_BIGINT, (num1, num2) => {
const a = create(num1);
const b = create(num2);
deepStrictEqual(Fp.add(a, b), Fp.add(b, a));
})
);
});
should(`${name} add/subtract/associativity`, () => {
fc.assert(
fc.property(FC_BIGINT, FC_BIGINT, FC_BIGINT, (num1, num2, num3) => {
const a = create(num1);
const b = create(num2);
const c = create(num3);
deepStrictEqual(Fp.add(a, Fp.add(b, c)), Fp.add(Fp.add(a, b), c));
})
);
});
should(`${name} add/subtract/x+0=x`, () => {
fc.assert(
fc.property(FC_BIGINT, (num) => {
const a = create(num);
deepStrictEqual(Fp.add(a, Fp.ZERO), a);
})
);
});
should(`${name} add/subtract/x-0=x`, () => {
fc.assert(
fc.property(FC_BIGINT, (num) => {
const a = create(num);
deepStrictEqual(Fp.sub(a, Fp.ZERO), a);
deepStrictEqual(Fp.sub(a, a), Fp.ZERO);
})
);
});
should(`${name} add/subtract/negate equality`, () => {
fc.assert(
fc.property(FC_BIGINT, (num1) => {
const a = create(num1);
const b = create(num1);
deepStrictEqual(Fp.sub(Fp.ZERO, a), Fp.negate(a));
deepStrictEqual(Fp.sub(a, b), Fp.add(a, Fp.negate(b)));
deepStrictEqual(Fp.sub(a, b), Fp.add(a, Fp.mul(b, Fp.create(-1n))));
})
);
});
should(`${name} add/subtract/negate`, () => {
fc.assert(
fc.property(FC_BIGINT, (num) => {
const a = create(num);
deepStrictEqual(Fp.negate(a), Fp.sub(Fp.ZERO, a));
deepStrictEqual(Fp.negate(a), Fp.mul(a, Fp.create(-1n)));
})
);
});
should(`${name} multiply/commutativity`, () => {
fc.assert(
fc.property(FC_BIGINT, FC_BIGINT, (num1, num2) => {
const a = create(num1);
const b = create(num2);
deepStrictEqual(Fp.mul(a, b), Fp.mul(b, a));
})
);
});
should(`${name} multiply/associativity`, () => {
fc.assert(
fc.property(FC_BIGINT, FC_BIGINT, FC_BIGINT, (num1, num2, num3) => {
const a = create(num1);
const b = create(num2);
const c = create(num3);
deepStrictEqual(Fp.mul(a, Fp.mul(b, c)), Fp.mul(Fp.mul(a, b), c));
})
);
});
should(`${name} multiply/distributivity`, () => {
fc.assert(
fc.property(FC_BIGINT, FC_BIGINT, FC_BIGINT, (num1, num2, num3) => {
const a = create(num1);
const b = create(num2);
const c = create(num3);
deepStrictEqual(Fp.mul(a, Fp.add(b, c)), Fp.add(Fp.mul(b, a), Fp.mul(c, a)));
})
);
});
should(`${name} multiply/add equality`, () => {
fc.assert(
fc.property(FC_BIGINT, (num) => {
const a = create(num);
deepStrictEqual(Fp.mul(a, 0n), Fp.ZERO);
deepStrictEqual(Fp.mul(a, Fp.ZERO), Fp.ZERO);
deepStrictEqual(Fp.mul(a, 1n), a);
deepStrictEqual(Fp.mul(a, Fp.ONE), a);
deepStrictEqual(Fp.mul(a, 2n), Fp.add(a, a));
deepStrictEqual(Fp.mul(a, 3n), Fp.add(Fp.add(a, a), a));
deepStrictEqual(Fp.mul(a, 4n), Fp.add(Fp.add(Fp.add(a, a), a), a));
})
);
});
should(`${name} multiply/square equality`, () => {
fc.assert(
fc.property(FC_BIGINT, (num) => {
const a = create(num);
deepStrictEqual(Fp.square(a), Fp.mul(a, a));
})
);
});
should(`${name} multiply/pow equality`, () => {
fc.assert(
fc.property(FC_BIGINT, (num) => {
const a = create(num);
deepStrictEqual(Fp.pow(a, 0n), Fp.ONE);
deepStrictEqual(Fp.pow(a, 1n), a);
deepStrictEqual(Fp.pow(a, 2n), Fp.mul(a, a));
deepStrictEqual(Fp.pow(a, 3n), Fp.mul(Fp.mul(a, a), a));
})
);
});
const isSquare = mod.FpIsSquare(Fp);
should(`${name} multiply/sqrt`, () => {
if (Fp === bls12_381.CURVE.Fp12) return; // Not implemented
fc.assert(
fc.property(FC_BIGINT, (num) => {
const a = create(num);
let root;
try {
root = Fp.sqrt(a);
} catch (e) {
deepStrictEqual(isSquare(a), false);
return;
}
deepStrictEqual(isSquare(a), true);
deepStrictEqual(Fp.equals(Fp.square(root), a), true, 'sqrt(a)^2 == a');
deepStrictEqual(Fp.equals(Fp.square(Fp.negate(root)), a), true, '(-sqrt(a))^2 == a');
})
);
});
should(`${name} div/division by one equality`, () => {
fc.assert(
fc.property(FC_BIGINT, (num) => {
const a = create(num);
if (Fp.equals(a, Fp.ZERO)) return; // No division by zero
deepStrictEqual(Fp.div(a, Fp.ONE), a);
deepStrictEqual(Fp.div(a, a), Fp.ONE);
})
);
});
should(`${name} zero division equality`, () => {
fc.assert(
fc.property(FC_BIGINT, (num) => {
const a = create(num);
deepStrictEqual(Fp.div(Fp.ZERO, a), Fp.ZERO);
})
);
});
should(`${name} div/division distributivity`, () => {
fc.assert(
fc.property(FC_BIGINT, FC_BIGINT, FC_BIGINT, (num1, num2, num3) => {
const a = create(num1);
const b = create(num2);
const c = create(num3);
deepStrictEqual(Fp.div(Fp.add(a, b), c), Fp.add(Fp.div(a, c), Fp.div(b, c)));
})
);
});
should(`${name} div/division and multiplication equality`, () => {
fc.assert(
fc.property(FC_BIGINT, FC_BIGINT, (num1, num2) => {
const a = create(num1);
const b = create(num2);
deepStrictEqual(Fp.div(a, b), Fp.mul(a, Fp.invert(b)));
})
);
});
}
}
// Group tests
// prettier-ignore // prettier-ignore
const CURVES = { const CURVES = {
secp192r1, secp224r1, secp256r1, secp384r1, secp521r1, secp192r1, secp224r1, secp256r1, secp384r1, secp521r1,
@ -29,6 +274,7 @@ const CURVES = {
}; };
const NUM_RUNS = 5; const NUM_RUNS = 5;
const getXY = (p) => ({ x: p.x, y: p.y }); const getXY = (p) => ({ x: p.x, y: p.y });
function equal(a, b, comment) { function equal(a, b, comment) {

@ -41,145 +41,7 @@ const getPubKey = (priv) => bls.getPublicKey(priv);
{ {
const Fp = bls.Fp; const Fp = bls.Fp;
const FC_BIGINT = fc.bigInt(1n, Fp.ORDER - 1n); const FC_BIGINT = fc.bigInt(1n, Fp.ORDER - 1n);
should('bls12-381/Fp/equality', () => {
fc.assert(
fc.property(FC_BIGINT, (num) => {
const a = Fp.create(num);
const b = Fp.create(num);
deepStrictEqual(Fp.equals(a, b), true);
deepStrictEqual(Fp.equals(b, a), true);
})
);
});
should('bls12-381/Fp/non-equality', () => {
fc.assert(
fc.property(FC_BIGINT, FC_BIGINT, (num1, num2) => {
const a = Fp.create(num1);
const b = Fp.create(num2);
deepStrictEqual(Fp.equals(a, b), num1 === num2);
deepStrictEqual(Fp.equals(b, a), num1 === num2);
})
);
});
should('bls12-381/Fp/add/subtract/commutativity', () => {
fc.assert(
fc.property(FC_BIGINT, FC_BIGINT, (num1, num2) => {
const a = Fp.create(num1);
const b = Fp.create(num2);
deepStrictEqual(Fp.add(a, b), Fp.add(b, a));
})
);
});
should('bls12-381/Fp/add/subtract/associativity', () => {
fc.assert(
fc.property(FC_BIGINT, FC_BIGINT, FC_BIGINT, (num1, num2, num3) => {
const a = Fp.create(num1);
const b = Fp.create(num2);
const c = Fp.create(num3);
deepStrictEqual(Fp.add(a, Fp.add(b, c)), Fp.add(Fp.add(a, b), c));
})
);
});
should('bls12-381/Fp/add/subtract/x+0=x', () => {
fc.assert(
fc.property(FC_BIGINT, (num) => {
const a = Fp.create(num);
deepStrictEqual(Fp.add(a, Fp.ZERO), a);
})
);
});
should('bls12-381/Fp/add/subtract/x-0=x', () => {
fc.assert(
fc.property(FC_BIGINT, (num) => {
const a = Fp.create(num);
deepStrictEqual(Fp.sub(a, Fp.ZERO), a);
deepStrictEqual(Fp.sub(a, a), Fp.ZERO);
})
);
});
should('bls12-381/Fp/add/subtract/negate equality', () => {
fc.assert(
fc.property(FC_BIGINT, (num1) => {
const a = Fp.create(num1);
const b = Fp.create(num1);
deepStrictEqual(Fp.sub(Fp.ZERO, a), Fp.negate(a));
deepStrictEqual(Fp.sub(a, b), Fp.add(a, Fp.negate(b)));
deepStrictEqual(Fp.sub(a, b), Fp.add(a, Fp.mul(b, Fp.create(-1n))));
})
);
});
should('bls12-381/Fp/add/subtract/negate', () => {
fc.assert(
fc.property(FC_BIGINT, (num) => {
const a = Fp.create(num);
deepStrictEqual(Fp.negate(a), Fp.sub(Fp.ZERO, a));
deepStrictEqual(Fp.negate(a), Fp.mul(a, Fp.create(-1n)));
})
);
});
should('bls12-381/Fp/multiply/commutativity', () => {
fc.assert(
fc.property(FC_BIGINT, FC_BIGINT, (num1, num2) => {
const a = Fp.create(num1);
const b = Fp.create(num2);
deepStrictEqual(Fp.mul(a, b), Fp.mul(b, a));
})
);
});
should('bls12-381/Fp/multiply/associativity', () => {
fc.assert(
fc.property(FC_BIGINT, FC_BIGINT, FC_BIGINT, (num1, num2, num3) => {
const a = Fp.create(num1);
const b = Fp.create(num2);
const c = Fp.create(num3);
deepStrictEqual(Fp.mul(a, Fp.mul(b, c)), Fp.mul(Fp.mul(a, b), c));
})
);
});
should('bls12-381/Fp/multiply/distributivity', () => {
fc.assert(
fc.property(FC_BIGINT, FC_BIGINT, FC_BIGINT, (num1, num2, num3) => {
const a = Fp.create(num1);
const b = Fp.create(num2);
const c = Fp.create(num3);
deepStrictEqual(Fp.mul(a, Fp.add(b, c)), Fp.add(Fp.mul(b, a), Fp.mul(c, a)));
})
);
});
should('bls12-381/Fp/multiply/add equality', () => {
fc.assert(
fc.property(FC_BIGINT, (num) => {
const a = Fp.create(num);
deepStrictEqual(Fp.mul(a, Fp.create(0n)), Fp.ZERO);
deepStrictEqual(Fp.mul(a, Fp.ZERO), Fp.ZERO);
deepStrictEqual(Fp.mul(a, Fp.create(1n)), a);
deepStrictEqual(Fp.mul(a, Fp.ONE), a);
deepStrictEqual(Fp.mul(a, Fp.create(2n)), Fp.add(a, a));
deepStrictEqual(Fp.mul(a, Fp.create(3n)), Fp.add(Fp.add(a, a), a));
deepStrictEqual(Fp.mul(a, Fp.create(4n)), Fp.add(Fp.add(Fp.add(a, a), a), a));
})
);
});
should('bls12-381/Fp/multiply/square equality', () => {
fc.assert(
fc.property(FC_BIGINT, (num) => {
const a = Fp.create(num);
deepStrictEqual(Fp.square(a), Fp.mul(a, a));
})
);
});
should('bls12-381/Fp/multiply/pow equality', () => {
fc.assert(
fc.property(FC_BIGINT, (num) => {
const a = Fp.create(num);
deepStrictEqual(Fp.pow(a, 0n), Fp.ONE);
deepStrictEqual(Fp.pow(a, 1n), a);
deepStrictEqual(Fp.pow(a, 2n), Fp.mul(a, a));
deepStrictEqual(Fp.pow(a, 3n), Fp.mul(Fp.mul(a, a), a));
})
);
});
should('bls12-381/Fp/multiply/sqrt', () => { should('bls12-381/Fp/multiply/sqrt', () => {
let sqr1 = Fp.sqrt(Fp.create(300855555557n)); let sqr1 = Fp.sqrt(Fp.create(300855555557n));
deepStrictEqual( deepStrictEqual(
@ -188,43 +50,6 @@ const getPubKey = (priv) => bls.getPublicKey(priv);
); );
throws(() => Fp.sqrt(Fp.create(72057594037927816n))); throws(() => Fp.sqrt(Fp.create(72057594037927816n)));
}); });
should('bls12-381/Fp/div/division by one equality', () => {
fc.assert(
fc.property(fc.bigInt(1n, Fp.ORDER - 1n), (num) => {
const a = Fp.create(num);
deepStrictEqual(Fp.div(a, Fp.ONE), a);
deepStrictEqual(Fp.div(a, a), Fp.ONE);
})
);
});
should('bls12-381/Fp/div/division by zero equality', () => {
fc.assert(
fc.property(FC_BIGINT, (num) => {
const a = Fp.create(num);
deepStrictEqual(Fp.div(Fp.ZERO, a), Fp.ZERO);
})
);
});
should('bls12-381/Fp/div/division distributivity', () => {
fc.assert(
fc.property(FC_BIGINT, FC_BIGINT, FC_BIGINT, (num1, num2, num3) => {
const a = Fp.create(num1);
const b = Fp.create(num2);
const c = Fp.create(num3);
deepStrictEqual(Fp.div(Fp.add(a, b), c), Fp.add(Fp.div(a, c), Fp.div(b, c)));
})
);
});
should('bls12-381/Fp/div/division and multiplication equality', () => {
fc.assert(
fc.property(FC_BIGINT, FC_BIGINT, (num1, num2) => {
const a = Fp.create(num1);
const b = Fp.create(num2);
deepStrictEqual(Fp.div(a, b), Fp.mul(a, Fp.invert(b)));
})
);
});
} }
// Fp2 // Fp2
@ -234,16 +59,6 @@ const getPubKey = (priv) => bls.getPublicKey(priv);
const FC_BIGINT = fc.bigInt(1n, Fp.ORDER - 1n); const FC_BIGINT = fc.bigInt(1n, Fp.ORDER - 1n);
const FC_BIGINT_2 = fc.array(FC_BIGINT, { minLength: 2, maxLength: 2 }); const FC_BIGINT_2 = fc.array(FC_BIGINT, { minLength: 2, maxLength: 2 });
should('bls12-381 Fp2/equality', () => {
fc.assert(
fc.property(FC_BIGINT_2, (num) => {
const a = Fp2.fromBigTuple([num[0], num[1]]);
const b = Fp2.fromBigTuple([num[0], num[1]]);
deepStrictEqual(Fp2.equals(a, b), true);
deepStrictEqual(Fp2.equals(b, a), true);
})
);
});
should('bls12-381 Fp2/non-equality', () => { should('bls12-381 Fp2/non-equality', () => {
fc.assert( fc.assert(
fc.property(FC_BIGINT_2, FC_BIGINT_2, (num1, num2) => { fc.property(FC_BIGINT_2, FC_BIGINT_2, (num1, num2) => {
@ -254,136 +69,7 @@ const getPubKey = (priv) => bls.getPublicKey(priv);
}) })
); );
}); });
should('bls12-381 Fp2/add/subtract/commutativity', () => {
fc.assert(
fc.property(FC_BIGINT_2, FC_BIGINT_2, (num1, num2) => {
const a = Fp2.fromBigTuple([num1[0], num1[1]]);
const b = Fp2.fromBigTuple([num2[0], num2[1]]);
deepStrictEqual(Fp2.add(a, b), Fp2.add(b, a));
})
);
});
should('bls12-381 Fp2/add/subtract/associativity', () => {
fc.assert(
fc.property(FC_BIGINT_2, FC_BIGINT_2, FC_BIGINT_2, (num1, num2, num3) => {
const a = Fp2.fromBigTuple([num1[0], num1[1]]);
const b = Fp2.fromBigTuple([num2[0], num2[1]]);
const c = Fp2.fromBigTuple([num3[0], num3[1]]);
deepStrictEqual(Fp2.add(a, Fp2.add(b, c)), Fp2.add(Fp2.add(a, b), c));
})
);
});
should('bls12-381 Fp2/add/subtract/x+0=x', () => {
fc.assert(
fc.property(FC_BIGINT_2, (num) => {
const a = Fp2.fromBigTuple([num[0], num[1]]);
deepStrictEqual(Fp2.add(a, Fp2.ZERO), a);
})
);
});
should('bls12-381 Fp2/add/subtract/x-0=x', () => {
fc.assert(
fc.property(FC_BIGINT_2, (num) => {
const a = Fp2.fromBigTuple([num[0], num[1]]);
deepStrictEqual(Fp2.sub(a, Fp2.ZERO), a);
deepStrictEqual(Fp2.sub(a, a), Fp2.ZERO);
})
);
});
should('bls12-381 Fp2/add/subtract/negate equality', () => {
fc.assert(
fc.property(FC_BIGINT_2, (num1) => {
const a = Fp2.fromBigTuple([num1[0], num1[1]]);
const b = Fp2.fromBigTuple([num1[0], num1[1]]);
deepStrictEqual(Fp2.sub(Fp2.ZERO, a), Fp2.negate(a));
deepStrictEqual(Fp2.sub(a, b), Fp2.add(a, Fp2.negate(b)));
deepStrictEqual(Fp2.sub(a, b), Fp2.add(a, Fp2.mul(b, -1n)));
})
);
});
should('bls12-381 Fp2/add/subtract/negate', () => {
fc.assert(
fc.property(FC_BIGINT_2, (num) => {
const a = Fp2.fromBigTuple([num[0], num[1]]);
deepStrictEqual(Fp2.negate(a), Fp2.sub(Fp2.ZERO, a));
deepStrictEqual(Fp2.negate(a), Fp2.mul(a, -1n));
})
);
});
should('bls12-381 Fp2/multiply/commutativity', () => {
fc.assert(
fc.property(FC_BIGINT_2, FC_BIGINT_2, (num1, num2) => {
const a = Fp2.fromBigTuple([num1[0], num1[1]]);
const b = Fp2.fromBigTuple([num2[0], num2[1]]);
deepStrictEqual(Fp2.mul(a, b), Fp2.mul(b, a));
})
);
});
should('bls12-381 Fp2/multiply/associativity', () => {
fc.assert(
fc.property(FC_BIGINT_2, FC_BIGINT_2, FC_BIGINT_2, (num1, num2, num3) => {
const a = Fp2.fromBigTuple([num1[0], num1[1]]);
const b = Fp2.fromBigTuple([num2[0], num2[1]]);
const c = Fp2.fromBigTuple([num3[0], num3[1]]);
deepStrictEqual(Fp2.mul(a, Fp2.mul(b, c)), Fp2.mul(Fp2.mul(a, b), c));
})
);
});
should('bls12-381 Fp2/multiply/distributivity', () => {
fc.assert(
fc.property(FC_BIGINT_2, FC_BIGINT_2, FC_BIGINT_2, (num1, num2, num3) => {
const a = Fp2.fromBigTuple([num1[0], num1[1]]);
const b = Fp2.fromBigTuple([num2[0], num2[1]]);
const c = Fp2.fromBigTuple([num3[0], num3[1]]);
deepStrictEqual(Fp2.mul(a, Fp2.add(b, c)), Fp2.add(Fp2.mul(b, a), Fp2.mul(c, a)));
})
);
});
should('bls12-381 Fp2/multiply/add equality', () => {
fc.assert(
fc.property(FC_BIGINT_2, (num) => {
const a = Fp2.fromBigTuple([num[0], num[1]]);
deepStrictEqual(Fp2.mul(a, 0n), Fp2.ZERO);
deepStrictEqual(Fp2.mul(a, Fp2.ZERO), Fp2.ZERO);
deepStrictEqual(Fp2.mul(a, 1n), a);
deepStrictEqual(Fp2.mul(a, Fp2.ONE), a);
deepStrictEqual(Fp2.mul(a, 2n), Fp2.add(a, a));
deepStrictEqual(Fp2.mul(a, 3n), Fp2.add(Fp2.add(a, a), a));
deepStrictEqual(Fp2.mul(a, 4n), Fp2.add(Fp2.add(Fp2.add(a, a), a), a));
})
);
});
should('bls12-381 Fp2/multiply/square equality', () => {
fc.assert(
fc.property(FC_BIGINT_2, (num) => {
const a = Fp2.fromBigTuple([num[0], num[1]]);
deepStrictEqual(Fp2.square(a), Fp2.mul(a, a));
})
);
});
should('bls12-381 Fp2/multiply/pow equality', () => {
fc.assert(
fc.property(FC_BIGINT_2, (num) => {
const a = Fp2.fromBigTuple([num[0], num[1]]);
deepStrictEqual(Fp2.pow(a, 0n), Fp2.ONE);
deepStrictEqual(Fp2.pow(a, 1n), a);
deepStrictEqual(Fp2.pow(a, 2n), Fp2.mul(a, a));
deepStrictEqual(Fp2.pow(a, 3n), Fp2.mul(Fp2.mul(a, a), a));
})
);
});
should('bls12-381 Fp2/div/distributivity', () => {
fc.assert(
fc.property(FC_BIGINT_2, FC_BIGINT_2, FC_BIGINT_2, (num1, num2, num3) => {
const a = Fp2.fromBigTuple([num1[0], num1[1]]);
const b = Fp2.fromBigTuple([num2[0], num2[1]]);
const c = Fp2.fromBigTuple([num3[0], num3[1]]);
deepStrictEqual(Fp2.div(Fp2.add(a, b), c), Fp2.add(Fp2.div(a, c), Fp2.div(b, c)));
})
);
});
should('bls12-381 Fp2/div/x/1=x', () => { should('bls12-381 Fp2/div/x/1=x', () => {
fc.assert( fc.assert(
fc.property(FC_BIGINT_2, (num) => { fc.property(FC_BIGINT_2, (num) => {
@ -394,23 +80,6 @@ const getPubKey = (priv) => bls.getPublicKey(priv);
}) })
); );
}); });
should('bls12-381 Fp2/div/ x/0=0', () => {
fc.assert(
fc.property(FC_BIGINT_2, (num) => {
const a = Fp2.fromBigTuple([num[0], num[1]]);
deepStrictEqual(Fp2.div(Fp2.ZERO, a), Fp2.ZERO);
})
);
});
should('bls12-381 Fp2/div/ multiply equality', () => {
fc.assert(
fc.property(FC_BIGINT_2, FC_BIGINT_2, (num1, num2) => {
const a = Fp2.fromBigTuple([num1[0], num1[1]]);
const b = Fp2.fromBigTuple([num2[0], num2[1]]);
deepStrictEqual(Fp2.div(a, b), Fp2.mul(a, Fp2.invert(b)));
})
);
});
should('bls12-381 Fp2/frobenius', () => { should('bls12-381 Fp2/frobenius', () => {
// expect(Fp2.FROBENIUS_COEFFICIENTS[0].equals(Fp.ONE)).toBe(true); // expect(Fp2.FROBENIUS_COEFFICIENTS[0].equals(Fp.ONE)).toBe(true);
@ -472,195 +141,6 @@ const getPubKey = (priv) => bls.getPublicKey(priv);
}); });
} }
// Fp12
{
const Fp = bls.Fp;
const Fp12 = bls.Fp12;
const FC_BIGINT = fc.bigInt(1n, Fp.ORDER - 1n);
const FC_BIGINT_12 = fc.array(FC_BIGINT, {
minLength: 12,
maxLength: 12,
});
should('bls12-381 Fp12/equality', () => {
fc.assert(
fc.property(FC_BIGINT_12, (num) => {
const a = Fp12.fromBigTwelve(num);
const b = Fp12.fromBigTwelve(num);
deepStrictEqual(Fp12.equals(a, b), true);
deepStrictEqual(Fp12.equals(b, a), true);
})
);
});
should('bls12-381 Fp12/non-equality', () => {
fc.assert(
fc.property(FC_BIGINT_12, FC_BIGINT_12, (num1, num2) => {
const a = Fp12.fromBigTwelve(num1);
const b = Fp12.fromBigTwelve(num2);
deepStrictEqual(Fp12.equals(a, b), num1[0] === num2[0] && num1[1] === num2[1]);
deepStrictEqual(Fp12.equals(b, a), num1[0] === num2[0] && num1[1] === num2[1]);
})
);
});
should('bls12-381 Fp12/add/subtract/commutativuty', () => {
fc.assert(
fc.property(FC_BIGINT_12, FC_BIGINT_12, (num1, num2) => {
const a = Fp12.fromBigTwelve(num1);
const b = Fp12.fromBigTwelve(num2);
deepStrictEqual(Fp12.add(a, b), Fp12.add(b, a));
})
);
});
should('bls12-381 Fp12/add/subtract/associativity', () => {
fc.assert(
fc.property(FC_BIGINT_12, FC_BIGINT_12, FC_BIGINT_12, (num1, num2, num3) => {
const a = Fp12.fromBigTwelve(num1);
const b = Fp12.fromBigTwelve(num2);
const c = Fp12.fromBigTwelve(num3);
deepStrictEqual(Fp12.add(a, Fp12.add(b, c)), Fp12.add(Fp12.add(a, b), c));
})
);
});
should('bls12-381 Fp12/add/subtract/x+0=x', () => {
fc.assert(
fc.property(FC_BIGINT_12, (num) => {
const a = Fp12.fromBigTwelve(num);
deepStrictEqual(Fp12.add(a, Fp12.ZERO), a);
})
);
});
should('bls12-381 Fp12/add/subtract/x-0=x', () => {
fc.assert(
fc.property(FC_BIGINT_12, (num) => {
const a = Fp12.fromBigTwelve(num);
deepStrictEqual(Fp12.sub(a, Fp12.ZERO), a);
deepStrictEqual(Fp12.sub(a, a), Fp12.ZERO);
})
);
});
should('bls12-381 Fp12/add/subtract/negate equality', () => {
fc.assert(
fc.property(FC_BIGINT_12, (num1) => {
const a = Fp12.fromBigTwelve(num1);
const b = Fp12.fromBigTwelve(num1);
deepStrictEqual(Fp12.sub(Fp12.ZERO, a), Fp12.negate(a));
deepStrictEqual(Fp12.sub(a, b), Fp12.add(a, Fp12.negate(b)));
deepStrictEqual(Fp12.sub(a, b), Fp12.add(a, Fp12.mul(b, -1n)));
})
);
});
should('bls12-381 Fp12/add/subtract/negate', () => {
fc.assert(
fc.property(FC_BIGINT_12, (num) => {
const a = Fp12.fromBigTwelve(num);
deepStrictEqual(Fp12.negate(a), Fp12.sub(Fp12.ZERO, a));
deepStrictEqual(Fp12.negate(a), Fp12.mul(a, -1n));
})
);
});
should('bls12-381 Fp12/multiply/commutativity', () => {
fc.assert(
fc.property(FC_BIGINT_12, FC_BIGINT_12, (num1, num2) => {
const a = Fp12.fromBigTwelve(num1);
const b = Fp12.fromBigTwelve(num2);
deepStrictEqual(Fp12.mul(a, b), Fp12.mul(b, a));
})
);
});
should('bls12-381 Fp12/multiply/associativity', () => {
fc.assert(
fc.property(FC_BIGINT_12, FC_BIGINT_12, FC_BIGINT_12, (num1, num2, num3) => {
const a = Fp12.fromBigTwelve(num1);
const b = Fp12.fromBigTwelve(num2);
const c = Fp12.fromBigTwelve(num3);
deepStrictEqual(Fp12.mul(a, Fp12.mul(b, c)), Fp12.mul(Fp12.mul(a, b), c));
})
);
});
should('bls12-381 Fp12/multiply/distributivity', () => {
fc.assert(
fc.property(FC_BIGINT_12, FC_BIGINT_12, FC_BIGINT_12, (num1, num2, num3) => {
const a = Fp12.fromBigTwelve(num1);
const b = Fp12.fromBigTwelve(num2);
const c = Fp12.fromBigTwelve(num3);
deepStrictEqual(Fp12.mul(a, Fp12.add(b, c)), Fp12.add(Fp12.mul(b, a), Fp12.mul(c, a)));
})
);
});
should('bls12-381 Fp12/multiply/add equality', () => {
fc.assert(
fc.property(FC_BIGINT_12, (num) => {
const a = Fp12.fromBigTwelve(num);
deepStrictEqual(Fp12.mul(a, 0n), Fp12.ZERO);
deepStrictEqual(Fp12.mul(a, Fp12.ZERO), Fp12.ZERO);
deepStrictEqual(Fp12.mul(a, 1n), a);
deepStrictEqual(Fp12.mul(a, Fp12.ONE), a);
deepStrictEqual(Fp12.mul(a, 2n), Fp12.add(a, a));
deepStrictEqual(Fp12.mul(a, 3n), Fp12.add(Fp12.add(a, a), a));
deepStrictEqual(Fp12.mul(a, 4n), Fp12.add(Fp12.add(Fp12.add(a, a), a), a));
})
);
});
should('bls12-381 Fp12/multiply/square equality', () => {
fc.assert(
fc.property(FC_BIGINT_12, (num) => {
const a = Fp12.fromBigTwelve(num);
deepStrictEqual(Fp12.square(a), Fp12.mul(a, a));
})
);
});
should('bls12-381 Fp12/multiply/pow equality', () => {
fc.assert(
fc.property(FC_BIGINT_12, (num) => {
const a = Fp12.fromBigTwelve(num);
deepStrictEqual(Fp12.pow(a, 0n), Fp12.ONE);
deepStrictEqual(Fp12.pow(a, 1n), a);
deepStrictEqual(Fp12.pow(a, 2n), Fp12.mul(a, a));
deepStrictEqual(Fp12.pow(a, 3n), Fp12.mul(Fp12.mul(a, a), a));
})
);
});
should('bls12-381 Fp12/div/x/1=x', () => {
fc.assert(
fc.property(FC_BIGINT_12, (num) => {
const a = Fp12.fromBigTwelve(num);
deepStrictEqual(Fp12.div(a, 1n), a);
deepStrictEqual(Fp12.div(a, Fp12.ONE), a);
deepStrictEqual(Fp12.div(a, a), Fp12.ONE);
})
);
});
should('bls12-381 Fp12/div/x/0=0', () => {
fc.assert(
fc.property(FC_BIGINT_12, (num) => {
const a = Fp12.fromBigTwelve(num);
deepStrictEqual(Fp12.div(Fp12.ZERO, a), Fp12.ZERO);
})
);
});
should('bls12-381 Fp12/div/distributivity', () => {
fc.assert(
fc.property(FC_BIGINT_12, FC_BIGINT_12, FC_BIGINT_12, (num1, num2, num3) => {
const a = Fp12.fromBigTwelve(num1);
const b = Fp12.fromBigTwelve(num2);
const c = Fp12.fromBigTwelve(num3);
deepStrictEqual(Fp12.div(Fp12.add(a, b), c), Fp12.add(Fp12.div(a, c), Fp12.div(b, c)));
})
);
});
should('bls12-381 Fp12/div/multiply equality', () => {
fc.assert(
fc.property(FC_BIGINT_12, FC_BIGINT_12, (num1, num2) => {
const a = Fp12.fromBigTwelve(num1);
const b = Fp12.fromBigTwelve(num2);
deepStrictEqual(Fp12.div(a, b), Fp12.mul(a, Fp12.invert(b)));
})
);
});
}
// Point // Point
{ {
const Fp = bls.Fp; const Fp = bls.Fp;