circomlib/test/pedersen.js
test/pedersen.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");
11
12const PBASE =
13 [
14 [ bigInt( "6842263847932328569390632736104801120816056295876316310227967232893658007436"),
15 bigInt("10520112236148895828506510766039255961372323270202387671483666293012156799229")],
16 [ bigInt( "7512553369533424708326990019377586455744651641787163924108944444109352325495"),
17 bigInt( "242060483180498555826438209654403949979206323274480625257315595534333598496")],
18 [ bigInt( "480315709862415282411588615813248553518720286084247594626493599605932342246"),
19 bigInt("15016559215090999873142530067666085992648246670781771102893391410239675444873")],
20 [ bigInt( "8311398801616893527636419786153024398643144699386228070202625261657263599049"),
21 bigInt("11125962584728296601438821974884453267303385157860713577195820780853779600315")],
22 [ bigInt( "1924118814882677827825936037840538695314492559747259292440881566152665343441"),
23 bigInt("17232376423406964731689089286495480735310130852288107159412732879983310795144")]
24 ];
25
26describe("Double Pedersen test", function() {
27 let circuit;
28 this.timeout(100000);
29 before( async() => {
30 const cirDef = await compiler(path.join(__dirname, "circuits", "pedersen_test.circom"));
31
32 circuit = new snarkjs.Circuit(cirDef);
33
34 console.log("NConstrains: " + circuit.nConstraints);
35 });
36 it("Should pedersen at zero", async () => {
37
38 let w, xout, yout;
39
40 w = circuit.calculateWitness({ in: ["0", "0"]});
41
42 xout = w[circuit.getSignalIdx("main.out[0]")];
43 yout = w[circuit.getSignalIdx("main.out[1]")];
44
45 assert(xout.equals("0"));
46 assert(yout.equals("1"));
47 });
48 it("Should pedersen at one first generator", async () => {
49 let w, xout, yout;
50
51 w = circuit.calculateWitness({ in: ["1", "0"]});
52
53 xout = bigInt(w[circuit.getSignalIdx("main.out[0]")]);
54 yout = bigInt(w[circuit.getSignalIdx("main.out[1]")]);
55
56 assert(xout.equals(PBASE[0][0]));
57 assert(yout.equals(PBASE[0][1]));
58 });
59 it("Should pedersen at one second generator", async () => {
60 let w, xout, yout;
61
62 w = circuit.calculateWitness({ in: ["0", "1"]});
63
64 xout = w[circuit.getSignalIdx("main.out[0]")];
65 yout = w[circuit.getSignalIdx("main.out[1]")];
66
67 assert(xout.equals(PBASE[1][0]));
68 assert(yout.equals(PBASE[1][1]));
69
70 });
71 it("Should pedersen at mixed generators", async () => {
72 let w, xout, yout;
73 w = circuit.calculateWitness({ in: ["3", "7"]});
74
75 xout = w[circuit.getSignalIdx("main.out[0]")];
76 yout = w[circuit.getSignalIdx("main.out[1]")];
77
78
79 const r = babyJub.addPoint(
80 babyJub.mulPointEscalar(PBASE[0], 3),
81 babyJub.mulPointEscalar(PBASE[1], 7)
82 );
83
84 assert(xout.equals(r[0]));
85 assert(yout.equals(r[1]));
86
87 });
88 it("Should pedersen all ones", async () => {
89 let w, xout, yout;
90
91 const allOnes = bigInt("1").shl(251).sub(bigInt("1"));
92 w = circuit.calculateWitness({ in: [allOnes, allOnes]});
93
94 xout = w[circuit.getSignalIdx("main.out[0]")];
95 yout = w[circuit.getSignalIdx("main.out[1]")];
96
97 const r2 = babyJub.addPoint(
98 babyJub.mulPointEscalar(PBASE[0], allOnes),
99 babyJub.mulPointEscalar(PBASE[1], allOnes)
100 );
101
102 assert(xout.equals(r2[0]));
103 assert(yout.equals(r2[1]));
104 });
105});