circomlib/test/eddsaposeidon.js

100 lines
2.7 KiB
JavaScript
Raw Normal View History

2019-07-08 15:08:04 +03:00
const chai = require("chai");
const path = require("path");
2019-12-14 22:32:45 +03:00
const tester = require("circom").tester;
const Fr = require("ffjavascript").bn128.Fr;
2019-07-08 15:08:04 +03:00
const eddsa = require("../src/eddsa.js");
const assert = chai.assert;
describe("EdDSA Poseidon test", function () {
let circuit;
this.timeout(100000);
before( async () => {
2019-12-14 22:32:45 +03:00
circuit = await tester(path.join(__dirname, "circuits", "eddsaposeidon_test.circom"));
2019-07-08 15:08:04 +03:00
});
it("Sign a single number", async () => {
const msg = Fr.e(1234);
2019-07-08 15:08:04 +03:00
const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
const pubKey = eddsa.prv2pub(prvKey);
const signature = eddsa.signPoseidon(prvKey, msg);
assert(eddsa.verifyPoseidon(msg, signature, pubKey));
2019-12-20 23:55:07 +03:00
const input = {
2019-07-08 15:08:04 +03:00
enabled: 1,
Ax: pubKey[0],
Ay: pubKey[1],
R8x: signature.R8[0],
R8y: signature.R8[1],
S: signature.S,
2019-12-20 23:55:07 +03:00
M: msg
};
// console.log(JSON.stringify(utils.stringifyBigInts(input)));
2020-03-26 21:24:20 +03:00
const w = await circuit.calculateWitness(input, true);
2019-07-08 15:08:04 +03:00
2019-12-16 23:35:52 +03:00
await circuit.checkConstraints(w);
2019-07-08 15:08:04 +03:00
});
it("Detect Invalid signature", async () => {
const msg = Fr.e(1234);
2019-07-08 15:08:04 +03:00
const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
const pubKey = eddsa.prv2pub(prvKey);
const signature = eddsa.signPoseidon(prvKey, msg);
assert(eddsa.verifyPoseidon(msg, signature, pubKey));
try {
2019-12-14 22:32:45 +03:00
await circuit.calculateWitness({
2019-07-08 15:08:04 +03:00
enabled: 1,
Ax: pubKey[0],
Ay: pubKey[1],
R8x: Fr.add(signature.R8[0], Fr.e(1)),
2019-07-08 15:08:04 +03:00
R8y: signature.R8[1],
S: signature.S,
2020-03-26 21:24:20 +03:00
M: msg}, true);
2019-07-08 15:08:04 +03:00
assert(false);
} catch(err) {
assert(/Constraint\sdoesn't\smatch(.*)1\s!=\s0/.test(err.message) );
2019-07-08 15:08:04 +03:00
}
});
it("Test a dissabled circuit with a bad signature", async () => {
const msg = Fr.e(1234);
2019-07-08 15:08:04 +03:00
const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
const pubKey = eddsa.prv2pub(prvKey);
const signature = eddsa.signPoseidon(prvKey, msg);
assert(eddsa.verifyPoseidon(msg, signature, pubKey));
2019-12-14 22:32:45 +03:00
const w = await circuit.calculateWitness({
2019-07-08 15:08:04 +03:00
enabled: 0,
Ax: pubKey[0],
Ay: pubKey[1],
R8x: Fr.add(signature.R8[0], Fr.e(1)),
2019-07-08 15:08:04 +03:00
R8y: signature.R8[1],
S: signature.S,
2020-03-26 21:24:20 +03:00
M: msg}, true);
2019-07-08 15:08:04 +03:00
2019-12-16 23:35:52 +03:00
await circuit.checkConstraints(w);
2019-07-08 15:08:04 +03:00
});
});