arnaucube
arnaucube/circomlibPublic Self-hosted repository
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 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});
40
41        // TODO
42        // assert(circuit.checkWitness(w));
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.signPoseidon(prvKey, msg);
54
55        assert(eddsa.verifyPoseidon(msg, signature, pubKey));
56        try {
57            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.signPoseidon(prvKey, msg);
81
82        assert(eddsa.verifyPoseidon(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        // TODO
94        // assert(circuit.checkWitness(w));
95    });
96});