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 createBlakeHash = require("blake-hash");
  7const eddsa = require("../src/eddsa.js");
  8
  9const assert = chai.assert;
 10
 11const bigInt = require("snarkjs").bigInt;
 12
 13describe("Baby Jub test", function () {
 14    let circuitAdd;
 15    let circuitTest;
 16
 17    this.timeout(100000);
 18
 19    before( async() => {
 20        const cirDefAdd = await compiler(path.join(__dirname, "circuits", "babyadd_tester.circom"));
 21        circuitAdd = new snarkjs.Circuit(cirDefAdd);
 22        console.log("NConstrains BabyAdd: " + circuitAdd.nConstraints);
 23
 24        const cirDefTest = await compiler(path.join(__dirname, "circuits", "babycheck_test.circom"));
 25        circuitTest = new snarkjs.Circuit(cirDefTest);
 26        console.log("NConstrains BabyTest: " + circuitTest.nConstraints);
 27
 28        const cirDefPbk = await compiler(path.join(__dirname, "circuits", "babypbk_test.circom"));
 29        circuitPbk = new snarkjs.Circuit(cirDefPbk);
 30        console.log("NConstrains BabyPbk: " + circuitPbk.nConstraints);
 31
 32    });
 33
 34    it("Should add point (0,1) and (0,1)", async () => {
 35
 36        const input={
 37            x1: snarkjs.bigInt(0),
 38            y1: snarkjs.bigInt(1),
 39            x2: snarkjs.bigInt(0),
 40            y2: snarkjs.bigInt(1)
 41        };
 42
 43        const w = circuitAdd.calculateWitness(input);
 44
 45        const xout = w[circuitAdd.getSignalIdx("main.xout")];
 46        const yout = w[circuitAdd.getSignalIdx("main.yout")];
 47
 48        assert(xout.equals(0));
 49        assert(yout.equals(1));
 50    });
 51
 52    it("Should add 2 same numbers", async () => {
 53
 54        const input={
 55            x1: snarkjs.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
 56            y1: snarkjs.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
 57            x2: snarkjs.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
 58            y2: snarkjs.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475")
 59        };
 60
 61        const w = circuitAdd.calculateWitness(input);
 62
 63        const xout = w[circuitAdd.getSignalIdx("main.xout")];
 64        const yout = w[circuitAdd.getSignalIdx("main.yout")];
 65
 66        assert(xout.equals(snarkjs.bigInt("6890855772600357754907169075114257697580319025794532037257385534741338397365")));
 67        assert(yout.equals(snarkjs.bigInt("4338620300185947561074059802482547481416142213883829469920100239455078257889")));
 68    });
 69
 70    it("Should add 2 different numbers", async () => {
 71
 72        const input={
 73            x1: snarkjs.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
 74            y1: snarkjs.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
 75            x2: snarkjs.bigInt("16540640123574156134436876038791482806971768689494387082833631921987005038935"),
 76            y2: snarkjs.bigInt("20819045374670962167435360035096875258406992893633759881276124905556507972311")
 77        };
 78
 79        const w = circuitAdd.calculateWitness(input);
 80
 81        const xout = w[circuitAdd.getSignalIdx("main.xout")];
 82        const yout = w[circuitAdd.getSignalIdx("main.yout")];
 83
 84        /*
 85        console.log(xout.toString());
 86        console.log(yout.toString());
 87        */
 88
 89        assert(xout.equals(snarkjs.bigInt("7916061937171219682591368294088513039687205273691143098332585753343424131937")));
 90        assert(yout.equals(snarkjs.bigInt("14035240266687799601661095864649209771790948434046947201833777492504781204499")));
 91    });
 92
 93    it("Should check 0 is a valid poiny", async() => {
 94        const w = circuitTest.calculateWitness({x: 0, y:1});
 95        assert(circuitTest.checkWitness(w));
 96    });
 97
 98    it("Should check 0 is an invalid poiny", async() => {
 99        try {
100            circuitTest.calculateWitness({x: 1, y: 0});
101            assert(false, "Should be a valid point");
102        } catch(err) {
103            assert(/Constraint\sdoesn't\smatch(.*)168700\s!=\s1/.test(err.message) );
104        }
105    });
106
107    it("Should extract the public key from the private one", async () => {
108
109        const rawpvk = Buffer.from("0001020304050607080900010203040506070809000102030405060708090021", "hex");
110        const pvk    = eddsa.pruneBuffer(createBlakeHash("blake512").update(rawpvk).digest().slice(0,32));
111        const S      = bigInt.leBuff2int(pvk).shr(3);
112
113        const A      = eddsa.prv2pub(rawpvk);
114
115        const input = {
116            in : S,
117            Ax : A[0],
118            Ay : A[1]
119        }
120
121        const w = circuitPbk.calculateWitness(input);
122        assert(circuitPbk.checkWitness(w));
123    });
124
125});