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