circomlib/test/babyjub.js
test/babyjub.jsRaw
1const chai = require("chai");
2const path = require("path");
3
4const createBlakeHash = require("blake-hash");
5const eddsa = require("../src/eddsa.js");
6
7const assert = chai.assert;
8
9const bigInt = require("big-integer");
10const tester = require("circom").tester;
11const utils = require("../src/utils.js");
12
13describe("Baby Jub test", function () {
14 let circuitAdd;
15 let circuitTest;
16 let circuitPbk;
17
18 this.timeout(100000);
19
20 before( async() => {
21 circuitAdd = await tester(path.join(__dirname, "circuits", "babyadd_tester.circom"));
22
23 circuitTest = await tester(path.join(__dirname, "circuits", "babycheck_test.circom"));
24
25 circuitPbk = await tester(path.join(__dirname, "circuits", "babypbk_test.circom"));
26 });
27
28 it("Should add point (0,1) and (0,1)", async () => {
29
30 const input={
31 x1: bigInt(0),
32 y1: bigInt(1),
33 x2: bigInt(0),
34 y2: bigInt(1)
35 };
36
37 const w = await circuitAdd.calculateWitness(input);
38
39 await circuitAdd.assertOut(w, {xout: bigInt(0), yout: bigInt(1)});
40 });
41
42 it("Should add 2 same numbers", async () => {
43
44 const input={
45 x1: bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
46 y1: bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
47 x2: bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
48 y2: bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475")
49 };
50
51 const w = await circuitAdd.calculateWitness(input);
52
53 await circuitAdd.assertOut(w, {
54 xout: bigInt("6890855772600357754907169075114257697580319025794532037257385534741338397365"),
55 yout: bigInt("4338620300185947561074059802482547481416142213883829469920100239455078257889")
56 });
57
58 });
59
60 it("Should add 2 different numbers", async () => {
61
62 const input={
63 x1: bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
64 y1: bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
65 x2: bigInt("16540640123574156134436876038791482806971768689494387082833631921987005038935"),
66 y2: bigInt("20819045374670962167435360035096875258406992893633759881276124905556507972311")
67 };
68
69 const w = await circuitAdd.calculateWitness(input);
70
71 await circuitAdd.assertOut(w, {
72 xout: bigInt("7916061937171219682591368294088513039687205273691143098332585753343424131937"),
73 yout: bigInt("14035240266687799601661095864649209771790948434046947201833777492504781204499")
74 });
75
76 });
77
78 it("Should check (0,1) is a valid poiny", async() => {
79 const w = await circuitTest.calculateWitness({x: 0, y:1});
80
81 await circuitTest.checkConstraints(w);
82 });
83
84 it("Should check (1,0) is an invalid point", async() => {
85 try {
86 await circuitTest.calculateWitness({x: 1, y: 0});
87 assert(false, "Should be a valid point");
88 } catch(err) {
89 assert(/Constraint\sdoesn't\smatch(.*)168700\s!=\s1/.test(err.message) );
90 }
91 });
92
93 it("Should extract the public key from the private one", async () => {
94
95 const rawpvk = Buffer.from("0001020304050607080900010203040506070809000102030405060708090021", "hex");
96 const pvk = eddsa.pruneBuffer(createBlakeHash("blake512").update(rawpvk).digest().slice(0,32));
97 const S = utils.leBuff2int(pvk).shiftRight(3);
98
99 const A = eddsa.prv2pub(rawpvk);
100
101 const input = {
102 in : S
103 };
104
105 const w = await circuitPbk.calculateWitness(input);
106
107 await circuitPbk.assertOut(w, {Ax : A[0], Ay: A[1]});
108
109 await circuitPbk.checkConstraints(w);
110 });
111
112});