circomlib/test/pedersen2.js
test/pedersen2.jsRaw
1const chai = require("chai");
2const path = require("path");
3const snarkjs = require("snarkjs");
4const compiler = require("circom");
5
6const assert = chai.assert;
7
8const bigInt = snarkjs.bigInt;
9
10const babyJub = require("../src/babyjub.js");
11const pedersen = require("../src/pedersenHash.js");
12
13
14describe("Pedersen test", function() {
15 let circuit;
16 this.timeout(100000);
17 before( async() => {
18 const cirDef = await compiler(path.join(__dirname, "circuits", "pedersen2_test.circom"));
19
20 circuit = new snarkjs.Circuit(cirDef);
21
22 console.log("NConstrains Pedersen2: " + circuit.nConstraints);
23 });
24 it("Should pedersen at zero", async () => {
25
26 let w, xout, yout;
27
28 w = circuit.calculateWitness({ in: 0});
29
30 xout = w[circuit.getSignalIdx("main.out[0]")];
31 yout = w[circuit.getSignalIdx("main.out[1]")];
32
33 const b = Buffer.alloc(32);
34
35 const h = pedersen.hash(b);
36 const hP = babyJub.unpackPoint(h);
37
38 /*
39 console.log(`[${xout.toString()}, ${yout.toString()}]`);
40 console.log(`[${hP[0].toString()}, ${hP[1].toString()}]`);
41 */
42
43 assert(xout.equals(hP[0]));
44 assert(yout.equals(hP[1]));
45 });
46 it("Should pedersen with 253 ones", async () => {
47
48 let w, xout, yout;
49
50 const n = bigInt.one.shl(253).sub(bigInt.one);
51 console.log(n.toString(16));
52
53 w = circuit.calculateWitness({ in: n});
54
55 xout = w[circuit.getSignalIdx("main.out[0]")];
56 yout = w[circuit.getSignalIdx("main.out[1]")];
57
58 const b = Buffer.alloc(32);
59 for (let i=0; i<31; i++) b[i] = 0xFF;
60 b[31] = 0x1F;
61
62
63 const h = pedersen.hash(b);
64 const hP = babyJub.unpackPoint(h);
65
66 /*
67 console.log(`[${xout.toString()}, ${yout.toString()}]`);
68 console.log(`[${hP[0].toString()}, ${hP[1].toString()}]`);
69 */
70
71 assert(xout.equals(hP[0]));
72 assert(yout.equals(hP[1]));
73 });
74});