grapevine-sonobe/circom/node_modules/circomlib/test/pedersen2.js
circom/node_modules/circomlib/test/pedersen2.jsRaw
1const path = require("path");
2
3const Scalar = require("ffjavascript").Scalar;
4
5const buildPedersenHash = require("circomlibjs").buildPedersenHash;
6const buildBabyJub = require("circomlibjs").buildBabyjub;
7
8const wasm_tester = require("circom_tester").wasm;
9
10
11describe("Pedersen test", function() {
12 let babyJub
13 let pedersen;
14 let F;
15 let circuit;
16 this.timeout(100000);
17 before( async() => {
18
19 babyJub = await buildBabyJub();
20 F = babyJub.F;
21 pedersen = await buildPedersenHash();
22 circuit = await wasm_tester(path.join(__dirname, "circuits", "pedersen2_test.circom"));
23 });
24 it("Should pedersen at zero", async () => {
25
26 let w;
27
28 w = await circuit.calculateWitness({ in: 0}, true);
29
30 const b = Buffer.alloc(32);
31
32 const h = pedersen.hash(b);
33 const hP = babyJub.unpackPoint(h);
34
35 await circuit.assertOut(w, {out: [F.toObject(hP[0]), F.toObject(hP[1])] });
36
37 });
38 it("Should pedersen with 253 ones", async () => {
39
40 let w;
41
42 const n = F.e(Scalar.sub(Scalar.shl(Scalar.e(1), 253), Scalar.e(1)));
43
44 w = await circuit.calculateWitness({ in: F.toObject(n)}, true);
45
46 const b = Buffer.alloc(32);
47 for (let i=0; i<31; i++) b[i] = 0xFF;
48 b[31] = 0x1F;
49
50 const h = pedersen.hash(b);
51 const hP = babyJub.unpackPoint(h);
52
53 await circuit.assertOut(w, {out: [F.toObject(hP[0]), F.toObject(hP[1])] });
54
55 });
56});