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
41        await circuit.checkConstraints(w);
42
43    });
44
45    it("Detect Invalid signature", async () => {
46        const msg = bigInt(1234);
47
48        const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
49
50        const pubKey = eddsa.prv2pub(prvKey);
51
52
53        const signature = eddsa.signMiMC(prvKey, msg);
54
55        assert(eddsa.verifyMiMC(msg, signature, pubKey));
56        try {
57            const w = await circuit.calculateWitness({
58                enabled: 1,
59                Ax: pubKey[0],
60                Ay: pubKey[1],
61                R8x: signature.R8[0].add(bigInt(1)),
62                R8y: signature.R8[1],
63                S: signature.S,
64                M: msg});
65            assert(false);
66        } catch(err) {
67            assert(/Constraint\sdoesn't\smatch(.*)1\s!=\s0/.test(err.message) );
68        }
69    });
70
71
72    it("Test a dissabled circuit with a bad signature", async () => {
73        const msg = bigInt(1234);
74
75        const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
76
77        const pubKey = eddsa.prv2pub(prvKey);
78
79
80        const signature = eddsa.signMiMC(prvKey, msg);
81
82        assert(eddsa.verifyMiMC(msg, signature, pubKey));
83
84        const w = await  circuit.calculateWitness({
85            enabled: 0,
86            Ax: pubKey[0],
87            Ay: pubKey[1],
88            R8x: signature.R8[0].add(bigInt(1)),
89            R8y: signature.R8[1],
90            S: signature.S,
91            M: msg});
92
93        await circuit.checkConstraints(w);
94
95    });
96});