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