circomlib/test/eddsamimc.js

98 lines
2.6 KiB
JavaScript
Raw Permalink Normal View History

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