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