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