circomlib/test/pedersen.js
test/pedersen.jsRaw
1const chai = require("chai");
2const path = require("path");
3
4const Fr = require("ffjavascript").bn128.Fr;
5const tester = require("circom").tester;
6
7const babyJub = require("../src/babyjub.js");
8
9const PBASE =
10 [
11 [Fr.e("7688621503272331394947188562469131124099290577812125474996268020905176040083"),Fr.e("6637287939860384587467947982369268811366630904563077767287326262235485629411")],
12 [Fr.e("11549681895645637778324638856880330712650895608496649854094912415387988201330"),Fr.e("5771732722784528537721081267383956005090479808901717812009343940574217488577")],
13 [Fr.e("18790245153471844934157747708238883966079935875787657036767664036124524381945"),Fr.e("18300275459419441151064576487317481499516933849631632883767173501999997278432")],
14 [Fr.e("16301069151422548986850494139112207641738464387919729729324473657161689764196"),Fr.e("8215273507373494014441104012907835625670941526105528197815397741007626226499")],
15 [Fr.e("12597665704678284488008395353749282149622295037737374782196049599390683534185"),Fr.e("4072455241781501621593714139281767473040087753548015968773801065193764079468")]
16 ];
17
18describe("Double Pedersen test", function() {
19 let circuit;
20 this.timeout(100000);
21 before( async() => {
22
23 circuit = await tester(path.join(__dirname, "circuits", "pedersen_test.circom"));
24
25 });
26 it("Should pedersen at zero", async () => {
27
28 let w;
29
30 w = await circuit.calculateWitness({ in: ["0", "0"]}, true);
31
32 await circuit.assertOut(w, {out: [0,1]});
33
34 });
35 it("Should pedersen at one first generator", async () => {
36 let w;
37
38 w = await circuit.calculateWitness({ in: ["1", "0"]}, true);
39
40 await circuit.assertOut(w, {out: PBASE[0]});
41
42 });
43 it("Should pedersen at one second generator", async () => {
44 let w;
45
46 w = await circuit.calculateWitness({ in: ["0", "1"]}, true);
47
48 await circuit.assertOut(w, {out: PBASE[1]});
49
50 });
51 it("Should pedersen at mixed generators", async () => {
52 let w;
53 w = await circuit.calculateWitness({ in: ["3", "7"]}, true);
54
55 const r = babyJub.addPoint(
56 babyJub.mulPointEscalar(PBASE[0], 3),
57 babyJub.mulPointEscalar(PBASE[1], 7)
58 );
59
60 await circuit.assertOut(w, {out: r});
61
62 });
63 it("Should pedersen all ones", async () => {
64 let w;
65
66 const allOnes = Fr.sub(Fr.shl(Fr.e("1"), Fr.e(250)), Fr.e("1"));
67 w = await circuit.calculateWitness({ in: [allOnes, allOnes]}, true);
68
69
70 const r2 = babyJub.addPoint(
71 babyJub.mulPointEscalar(PBASE[0], allOnes),
72 babyJub.mulPointEscalar(PBASE[1], allOnes)
73 );
74
75 await circuit.assertOut(w, {out: r2});
76 });
77});