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