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