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