Skip to content

Commit 28e4a10

Browse files
committed
tests: add bls, bn, math
1 parent b754590 commit 28e4a10

File tree

3 files changed

+156
-0
lines changed

3 files changed

+156
-0
lines changed

test/test-vectors/bls.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { bls12_381 } from "ethereum-cryptography/bls";
2+
import { deepStrictEqual } from "./assert";
3+
4+
describe("bls12-381", () => {
5+
const PointG1 = bls12_381.G1.ProjectivePoint;
6+
const PointG2 = bls12_381.G2.ProjectivePoint;
7+
const { Fp, Fp12 } = bls12_381.fields;
8+
9+
it("basic", () => {
10+
const a = PointG1.fromAffine({
11+
x: Fp.create(
12+
0x17f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bbn
13+
),
14+
y: Fp.create(
15+
0x08b3f481e3aaa0f1a09e30ed741d8ae4fcf5e095d5d00af600db18cb2c04b3edd03cc744a2888ae40caa232946c5e7e1n
16+
),
17+
});
18+
a.assertValidity();
19+
});
20+
it("sign", () => {
21+
const [priv, msg] =
22+
"0d1bd9077705325666408124339dca98c0c842b35a90bc3cea8e0c36f2d35583:c43623:94f60dc44a4dbb2505befe346c0c143190fc877ded5e877418f0f890b8ae357a40e8fcc189139aaa509d2b6500f623a5".split(
23+
":"
24+
);
25+
const sig = bls12_381.signShortSignature(msg, priv);
26+
const pub = bls12_381.getPublicKeyForShortSignatures(priv);
27+
const res = bls12_381.verifyShortSignature(sig, msg, pub);
28+
deepStrictEqual(res, true, `${priv}-${msg}`);
29+
});
30+
it("pairing", () => {
31+
const p1 = bls12_381.pairing(PointG1.BASE, PointG2.BASE);
32+
deepStrictEqual(
33+
p1,
34+
// @ts-ignore
35+
Fp12.fromBigTwelve([
36+
0x1250ebd871fc0a92a7b2d83168d0d727272d441befa15c503dd8e90ce98db3e7b6d194f60839c508a84305aaca1789b6n,
37+
0x089a1c5b46e5110b86750ec6a532348868a84045483c92b7af5af689452eafabf1a8943e50439f1d59882a98eaa0170fn,
38+
0x1368bb445c7c2d209703f239689ce34c0378a68e72a6b3b216da0e22a5031b54ddff57309396b38c881c4c849ec23e87n,
39+
0x193502b86edb8857c273fa075a50512937e0794e1e65a7617c90d8bd66065b1fffe51d7a579973b1315021ec3c19934fn,
40+
0x01b2f522473d171391125ba84dc4007cfbf2f8da752f7c74185203fcca589ac719c34dffbbaad8431dad1c1fb597aaa5n,
41+
0x018107154f25a764bd3c79937a45b84546da634b8f6be14a8061e55cceba478b23f7dacaa35c8ca78beae9624045b4b6n,
42+
0x19f26337d205fb469cd6bd15c3d5a04dc88784fbb3d0b2dbdea54d43b2b73f2cbb12d58386a8703e0f948226e47ee89dn,
43+
0x06fba23eb7c5af0d9f80940ca771b6ffd5857baaf222eb95a7d2809d61bfe02e1bfd1b68ff02f0b8102ae1c2d5d5ab1an,
44+
0x11b8b424cd48bf38fcef68083b0b0ec5c81a93b330ee1a677d0d15ff7b984e8978ef48881e32fac91b93b47333e2ba57n,
45+
0x03350f55a7aefcd3c31b4fcb6ce5771cc6a0e9786ab5973320c806ad360829107ba810c5a09ffdd9be2291a0c25a99a2n,
46+
0x04c581234d086a9902249b64728ffd21a189e87935a954051c7cdba7b3872629a4fafc05066245cb9108f0242d0fe3efn,
47+
0x0f41e58663bf08cf068672cbd01a7ec73baca4d72ca93544deff686bfd6df543d48eaa24afe47e1efde449383b676631n,
48+
])
49+
);
50+
});
51+
});

test/test-vectors/bn.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { bn254 } from "ethereum-cryptography/bn";
2+
import { deepStrictEqual } from "./assert";
3+
4+
describe("bls12-381", () => {
5+
const PointG1 = bn254.G1.ProjectivePoint;
6+
const PointG2 = bn254.G2.ProjectivePoint;
7+
const { Fp12 } = bn254.fields;
8+
9+
it("basic", () => {
10+
const g1 =
11+
PointG1.BASE.multiply(
12+
18097487326282793650237947474982649264364522469319914492172746413872781676n
13+
);
14+
g1.assertValidity();
15+
deepStrictEqual(g1.toAffine(), {
16+
x: 0x16f7535f91f50bb2227f483b54850a63b38206f28e0a1a65c83d0c90762442a9n,
17+
y: 0x0b46dd0c40725b6b4a298576629d77b41a545060adb4358eabec939e80691a05n,
18+
});
19+
const g2 =
20+
PointG2.BASE.multiply(
21+
20390255904278144451778773028944684152769293537511418234311120800877067946n
22+
);
23+
g2.assertValidity();
24+
deepStrictEqual(g2.toAffine(), {
25+
x: {
26+
c0: 0x1ecfd2dff2aad18798b64bdb0c2b50c9d73e6c05619e04cbf5b448fd98726880n,
27+
c1: 0x0e16c8d96362720af0916592be1b839a26f5e6b710f3ede0d8840d9a70eaf97fn,
28+
},
29+
y: {
30+
c0: 0x2aa778acda9e7d4925c60ad84c12fb3b4f2b9539d5699934b0e6fdd10cc2c0e1n,
31+
c1: 0x1e8f2c1f441fed039bb46d6bfb91236cf7ba240c75080cedbe40e049c46b26ben,
32+
},
33+
});
34+
});
35+
it("pairing", () => {
36+
const g1 =
37+
PointG1.BASE.multiply(
38+
18097487326282793650237947474982649264364522469319914492172746413872781676n
39+
);
40+
const g2 =
41+
PointG2.BASE.multiply(
42+
20390255904278144451778773028944684152769293537511418234311120800877067946n
43+
);
44+
deepStrictEqual(
45+
bn254.pairing(g1, g2, true),
46+
// @ts-ignore
47+
Fp12.fromBigTwelve([
48+
7520311483001723614143802378045727372643587653754534704390832890681688842501n,
49+
20265650864814324826731498061022229653175757397078253377158157137251452249882n,
50+
11942254371042183455193243679791334797733902728447312943687767053513298221130n,
51+
759657045325139626991751731924144629256296901790485373000297868065176843620n,
52+
16045761475400271697821392803010234478356356448940805056528536884493606035236n,
53+
4715626119252431692316067698189337228571577552724976915822652894333558784086n,
54+
14901948363362882981706797068611719724999331551064314004234728272909570402962n,
55+
11093203747077241090565767003969726435272313921345853819385060670210834379103n,
56+
17897835398184801202802503586172351707502775171934235751219763553166796820753n,
57+
1344517825169318161285758374052722008806261739116142912817807653057880346554n,
58+
11123896897251094532909582772961906225000817992624500900708432321664085800838n,
59+
17453370448280081813275586256976217762629631160552329276585874071364454854650n,
60+
])
61+
);
62+
});
63+
});

test/test-vectors/math.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { modPow, modInvert } from "ethereum-cryptography/math";
2+
import { deepStrictEqual, throws } from "./assert";
3+
4+
describe("math", () => {
5+
it("pow", () => {
6+
deepStrictEqual(modPow(123n, 456n, 789n), 699n);
7+
deepStrictEqual(modPow(123n, 0n, 789n), 1n);
8+
deepStrictEqual(modPow(2n, 5n, 789n), 32n);
9+
deepStrictEqual(modPow(123n, 456n, 1n), 0n);
10+
deepStrictEqual(
11+
modPow(
12+
0x17f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bbn,
13+
0x08b3f481e3aaa0f1a09e30ed741d8ae4fcf5e095d5d00af600db18cb2c04b3edd03cc744a2888ae40caa232946c5e7e1n,
14+
0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaabn
15+
),
16+
436876548127983101943682984672944697156319180922804969694242509081341800477678465744409641708610886843942671311740n
17+
);
18+
throws(() => modPow(123n, 456n, 0n));
19+
throws(() => modPow(123n, 456n, -1n));
20+
throws(() => modPow(123n, -1n, 789n));
21+
});
22+
it("invert", () => {
23+
// basic
24+
deepStrictEqual(modInvert(3n, 11n), 4n);
25+
deepStrictEqual(modInvert(10n, 17n), 12n);
26+
deepStrictEqual(modInvert(22n, 5n), 3n); // bigger than modulo
27+
deepStrictEqual(
28+
modInvert(
29+
0x17f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bbn,
30+
0x08b3f481e3aaa0f1a09e30ed741d8ae4fcf5e095d5d00af600db18cb2c04b3edd03cc744a2888ae40caa232946c5e7e1n
31+
),
32+
291256306712195702191844365537370801710916620404828242975224254728724473918830780018590872057480493707327142420858n
33+
); // big
34+
// zero
35+
throws(() => modInvert(0n, 5n));
36+
throws(() => modInvert(5n, 0n));
37+
deepStrictEqual(modInvert(-1n, 5n), 4n);
38+
throws(() => modInvert(5n, -1n));
39+
throws(() => modInvert(2n, 4n)); // gcd is not 1
40+
throws(() => modInvert(7n, 7n)); // number and modulo same
41+
});
42+
});

0 commit comments

Comments
 (0)