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        await circuit.checkConstraints(w);
42    });
43
44    it("Detect Invalid signature", async () => {
45        const msg = bigInt(1234);
46
47        const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
48
49        const pubKey = eddsa.prv2pub(prvKey);
50
51
52        const signature = eddsa.signPoseidon(prvKey, msg);
53
54        assert(eddsa.verifyPoseidon(msg, signature, pubKey));
55        try {
56            await circuit.calculateWitness({
57                enabled: 1,
58                Ax: pubKey[0],
59                Ay: pubKey[1],
60                R8x: signature.R8[0].add(bigInt(1)),
61                R8y: signature.R8[1],
62                S: signature.S,
63                M: msg});
64            assert(false);
65        } catch(err) {
66            assert(/Constraint\sdoesn't\smatch(.*)1\s!=\s0/.test(err.message) );
67        }
68    });
69
70
71    it("Test a dissabled circuit with a bad signature", async () => {
72        const msg = bigInt(1234);
73
74        const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
75
76        const pubKey = eddsa.prv2pub(prvKey);
77
78
79        const signature = eddsa.signPoseidon(prvKey, msg);
80
81        assert(eddsa.verifyPoseidon(msg, signature, pubKey));
82
83        const w = await circuit.calculateWitness({
84            enabled: 0,
85            Ax: pubKey[0],
86            Ay: pubKey[1],
87            R8x: signature.R8[0].add(bigInt(1)),
88            R8y: signature.R8[1],
89            S: signature.S,
90            M: msg});
91
92        await circuit.checkConstraints(w);
93    });
94});