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