arnaucube
arnaucube/circomlibPublic Self-hosted repository
circomlib/test/babyjub.js
test/babyjub.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 = require("big-integer");
  9
 10
 11describe("Baby Jub test", function () {
 12    let circuitAdd;
 13    let circuitTest;
 14
 15    this.timeout(100000);
 16
 17    before( async() => {
 18        const cirDefAdd = await compiler(path.join(__dirname, "circuits", "babyadd_tester.circom"));
 19        circuitAdd = new snarkjs.Circuit(cirDefAdd);
 20        console.log("NConstrains BabyAdd: " + circuitAdd.nConstraints);
 21
 22        const cirDefTest = await compiler(path.join(__dirname, "circuits", "babycheck_test.circom"));
 23        circuitTest = new snarkjs.Circuit(cirDefTest);
 24        console.log("NConstrains BabyTest: " + circuitTest.nConstraints);
 25    });
 26
 27    it("Should add point (0,1) and (0,1)", async () => {
 28
 29        const input={
 30            x1: snarkjs.bigInt(0),
 31            y1: snarkjs.bigInt(1),
 32            x2: snarkjs.bigInt(0),
 33            y2: snarkjs.bigInt(1)
 34        };
 35
 36        const w = circuitAdd.calculateWitness(input);
 37
 38        const xout = w[circuitAdd.getSignalIdx("main.xout")];
 39        const yout = w[circuitAdd.getSignalIdx("main.yout")];
 40
 41        assert(xout.equals(0));
 42        assert(yout.equals(1));
 43    });
 44
 45    it("Should add 2 same numbers", async () => {
 46
 47        const input={
 48            x1: snarkjs.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
 49            y1: snarkjs.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
 50            x2: snarkjs.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
 51            y2: snarkjs.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475")
 52        };
 53
 54        const w = circuitAdd.calculateWitness(input);
 55
 56        const xout = w[circuitAdd.getSignalIdx("main.xout")];
 57        const yout = w[circuitAdd.getSignalIdx("main.yout")];
 58
 59        assert(xout.equals(snarkjs.bigInt("6890855772600357754907169075114257697580319025794532037257385534741338397365")));
 60        assert(yout.equals(snarkjs.bigInt("4338620300185947561074059802482547481416142213883829469920100239455078257889")));
 61    });
 62
 63    it("Should add 2 different numbers", async () => {
 64
 65        const input={
 66            x1: snarkjs.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
 67            y1: snarkjs.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
 68            x2: snarkjs.bigInt("16540640123574156134436876038791482806971768689494387082833631921987005038935"),
 69            y2: snarkjs.bigInt("20819045374670962167435360035096875258406992893633759881276124905556507972311")
 70        };
 71
 72        const w = circuitAdd.calculateWitness(input);
 73
 74        const xout = w[circuitAdd.getSignalIdx("main.xout")];
 75        const yout = w[circuitAdd.getSignalIdx("main.yout")];
 76
 77        /*
 78        console.log(xout.toString());
 79        console.log(yout.toString());
 80        */
 81
 82        assert(xout.equals(snarkjs.bigInt("7916061937171219682591368294088513039687205273691143098332585753343424131937")));
 83        assert(yout.equals(snarkjs.bigInt("14035240266687799601661095864649209771790948434046947201833777492504781204499")));
 84    });
 85
 86    it("Should check 0 is a valid poiny", async() => {
 87        const w = circuitTest.calculateWitness({x: 0, y:1});
 88        assert(circuitTest.checkWitness(w));
 89    });
 90
 91    it("Should check 0 is an invalid poiny", async() => {
 92        try {
 93            circuitTest.calculateWitness({x: 1, y: 0});
 94            assert(false, "Should be a valid point");
 95        } catch(err) {
 96            assert.equal(err.message, "Constraint doesn't match: 168700 != 1");
 97        }
 98    });
 99
100});