arnaucube
arnaucube/grapevine-sonobePublic Self-hosted repository
grapevine-sonobe/circom/node_modules/circomlib/test/pedersen.js
circom/node_modules/circomlib/test/pedersen.jsRaw
 1const chai = require("chai");
 2const path = require("path");
 3
 4const Scalar = require("ffjavascript").Scalar;
 5
 6const wasm_tester = require("circom_tester").wasm;
 7
 8const buildBabyjub = require("circomlibjs").buildBabyjub;
 9
10
11describe("Double Pedersen test", function() {
12    let babyJub;
13    let Fr;
14    let PBASE;
15    let circuit;
16    this.timeout(100000);
17    before( async() => {
18        babyJub = await buildBabyjub();
19        Fr = babyJub.F;
20        PBASE =
21        [
22            [Fr.e("10457101036533406547632367118273992217979173478358440826365724437999023779287"),Fr.e("19824078218392094440610104313265183977899662750282163392862422243483260492317")],
23            [Fr.e("2671756056509184035029146175565761955751135805354291559563293617232983272177"),Fr.e("2663205510731142763556352975002641716101654201788071096152948830924149045094")],
24            [Fr.e("5802099305472655231388284418920769829666717045250560929368476121199858275951"),Fr.e("5980429700218124965372158798884772646841287887664001482443826541541529227896")],
25            [Fr.e("7107336197374528537877327281242680114152313102022415488494307685842428166594"),Fr.e("2857869773864086953506483169737724679646433914307247183624878062391496185654")],
26            [Fr.e("20265828622013100949498132415626198973119240347465898028410217039057588424236"),Fr.e("1160461593266035632937973507065134938065359936056410650153315956301179689506")]
27        ];
28        circuit = await wasm_tester(path.join(__dirname, "circuits", "pedersen_test.circom"));
29
30    });
31
32    it("Should pedersen at zero", async () => {
33
34        let w;
35
36        w = await circuit.calculateWitness({ in: ["0", "0"]}, true);
37
38        await circuit.assertOut(w, {out: [0,1]});
39
40    });
41    it("Should pedersen at one first generator", async () => {
42        let w;
43
44        w = await circuit.calculateWitness({ in: ["1", "0"]}, true);
45
46        await circuit.assertOut(w, {out: [Fr.toObject(PBASE[0][0]), Fr.toObject(PBASE[0][1])]});
47
48    });
49    it("Should pedersen at one second generator", async () => {
50        let w;
51
52        w = await circuit.calculateWitness({ in: ["0", "1"]}, true);
53
54        await circuit.assertOut(w, {out: [Fr.toObject(PBASE[1][0]), Fr.toObject(PBASE[1][1])]});
55
56    });
57    it("Should pedersen at mixed generators", async () => {
58        let w;
59        w = await circuit.calculateWitness({ in: ["3", "7"]}, true);
60
61        const r = babyJub.addPoint(
62            babyJub.mulPointEscalar(PBASE[0], 3),
63            babyJub.mulPointEscalar(PBASE[1], 7)
64        );
65
66        await circuit.assertOut(w, {out: [Fr.toObject(r[0]), Fr.toObject(r[1])]});
67
68    });
69    it("Should pedersen all ones", async () => {
70        let w;
71
72        const allOnes = Scalar.sub(Scalar.shl(Scalar.e(1), 250), Scalar.e(1));
73        w = await circuit.calculateWitness({ in: [allOnes, allOnes]}, true);
74
75
76        const r2 = babyJub.addPoint(
77            babyJub.mulPointEscalar(PBASE[0], allOnes),
78            babyJub.mulPointEscalar(PBASE[1], allOnes)
79        );
80
81        await circuit.assertOut(w, {out: [Fr.toObject(r2[0]), Fr.toObject(r2[1])]});
82    });
83});