arnaucube
arnaucube/grapevine-sonobePublic Self-hosted repository
grapevine-sonobe/circom/node_modules/circomlib/test/eddsamimc.js
circom/node_modules/circomlib/test/eddsamimc.jsRaw
  1const chai = require("chai");
  2const path = require("path");
  3const wasm_tester = require("circom_tester").wasm;
  4
  5const buildEddsa = require("circomlibjs").buildEddsa;
  6const buildBabyjub = require("circomlibjs").buildBabyjub;
  7
  8const assert = chai.assert;
  9
 10describe("EdDSA MiMC test", function () {
 11    let circuit;
 12    let eddsa;
 13    let babyJub;
 14    let F;
 15
 16    this.timeout(100000);
 17
 18    before( async () => {
 19        eddsa = await buildEddsa();
 20        babyJub = await buildBabyjub();
 21        F = babyJub.F;
 22
 23        circuit = await wasm_tester(path.join(__dirname, "circuits", "eddsamimc_test.circom"));
 24    });
 25
 26    it("Sign a single number", async () => {
 27        const msg = F.e(1234);
 28
 29        const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
 30
 31        const pubKey = eddsa.prv2pub(prvKey);
 32
 33        const signature = eddsa.signMiMC(prvKey, msg);
 34
 35        assert(eddsa.verifyMiMC(msg, signature, pubKey));
 36
 37        const w = await circuit.calculateWitness({
 38            enabled: 1,
 39            Ax: F.toObject(pubKey[0]),
 40            Ay: F.toObject(pubKey[1]),
 41            R8x: F.toObject(signature.R8[0]),
 42            R8y: F.toObject(signature.R8[1]),
 43            S: signature.S,
 44            M: F.toObject(msg)}, true);
 45
 46
 47        await circuit.checkConstraints(w);
 48
 49    });
 50
 51    it("Detect Invalid signature", async () => {
 52        const msg = F.e(1234);
 53
 54        const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
 55
 56        const pubKey = eddsa.prv2pub(prvKey);
 57
 58
 59        const signature = eddsa.signMiMC(prvKey, msg);
 60
 61        assert(eddsa.verifyMiMC(msg, signature, pubKey));
 62        try {
 63            const w = await circuit.calculateWitness({
 64                enabled: 1,
 65                Ax: F.toObject(pubKey[0]),
 66                Ay: F.toObject(pubKey[1]),
 67                R8x: F.toObject(F.add(signature.R8[0], F.e(1))),
 68                R8y: F.toObject(signature.R8[1]),
 69                S: signature.S,
 70                M: F.toObject(msg)}, true);
 71            assert(false);
 72        } catch(err) {
 73	    assert(err.message.includes("Assert Failed"));
 74        }
 75    });
 76
 77
 78    it("Test a dissabled circuit with a bad signature", async () => {
 79        const msg = F.e(1234);
 80
 81        const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
 82
 83        const pubKey = eddsa.prv2pub(prvKey);
 84
 85
 86        const signature = eddsa.signMiMC(prvKey, msg);
 87
 88        assert(eddsa.verifyMiMC(msg, signature, pubKey));
 89
 90        const w = await  circuit.calculateWitness({
 91            enabled: 0,
 92            Ax: F.toObject(pubKey[0]),
 93            Ay: F.toObject(pubKey[1]),
 94            R8x: F.toObject(F.add(signature.R8[0], F.e(1))),
 95            R8y: F.toObject(signature.R8[1]),
 96            S: signature.S,
 97            M: F.toObject(msg)}, true);
 98
 99        await circuit.checkConstraints(w);
100
101    });
102});