2018-11-27 20:30:33 +03:00
|
|
|
const chai = require("chai");
|
|
|
|
const path = require("path");
|
2019-12-13 20:35:29 +03:00
|
|
|
|
|
|
|
const tester = require("circom").tester;
|
2020-04-18 23:33:59 +03:00
|
|
|
const Fr = require("ffjavascript").bn128.Fr;
|
2018-11-27 20:30:33 +03:00
|
|
|
|
|
|
|
const eddsa = require("../src/eddsa.js");
|
|
|
|
const babyJub = require("../src/babyjub.js");
|
|
|
|
|
|
|
|
const assert = chai.assert;
|
|
|
|
|
|
|
|
function print(circuit, w, s) {
|
|
|
|
console.log(s + ": " + w[circuit.getSignalIdx(s)]);
|
|
|
|
}
|
|
|
|
|
|
|
|
function buffer2bits(buff) {
|
|
|
|
const res = [];
|
|
|
|
for (let i=0; i<buff.length; i++) {
|
|
|
|
for (let j=0; j<8; j++) {
|
|
|
|
if ((buff[i]>>j)&1) {
|
2020-04-18 23:33:59 +03:00
|
|
|
res.push(Fr.one);
|
2018-11-27 20:30:33 +03:00
|
|
|
} else {
|
2020-04-18 23:33:59 +03:00
|
|
|
res.push(Fr.zero);
|
2018-11-27 20:30:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
describe("EdDSA test", function () {
|
|
|
|
let circuit;
|
|
|
|
|
|
|
|
this.timeout(100000);
|
|
|
|
|
|
|
|
before( async () => {
|
2019-12-13 20:35:29 +03:00
|
|
|
circuit = await tester(path.join(__dirname, "circuits", "eddsa_test.circom"));
|
2018-11-27 20:30:33 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it("Sign a single 10 bytes from 0 to 9", async () => {
|
|
|
|
const msg = Buffer.from("00010203040506070809", "hex");
|
|
|
|
|
2019-02-12 14:21:44 +03:00
|
|
|
// const prvKey = crypto.randomBytes(32);
|
2018-11-27 20:30:33 +03:00
|
|
|
|
|
|
|
const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
|
|
|
|
|
|
|
|
const pubKey = eddsa.prv2pub(prvKey);
|
|
|
|
|
|
|
|
const pPubKey = babyJub.packPoint(pubKey);
|
|
|
|
|
|
|
|
const signature = eddsa.sign(prvKey, msg);
|
|
|
|
|
|
|
|
const pSignature = eddsa.packSignature(signature);
|
|
|
|
const uSignature = eddsa.unpackSignature(pSignature);
|
|
|
|
|
|
|
|
assert(eddsa.verify(msg, uSignature, pubKey));
|
|
|
|
|
|
|
|
const msgBits = buffer2bits(msg);
|
|
|
|
const r8Bits = buffer2bits(pSignature.slice(0, 32));
|
|
|
|
const sBits = buffer2bits(pSignature.slice(32, 64));
|
|
|
|
const aBits = buffer2bits(pPubKey);
|
|
|
|
|
2020-03-26 21:24:20 +03:00
|
|
|
const w = await circuit.calculateWitness({A: aBits, R8: r8Bits, S: sBits, msg: msgBits}, true);
|
2018-11-27 20:30:33 +03:00
|
|
|
|
2019-12-16 23:35:52 +03:00
|
|
|
await circuit.checkConstraints(w);
|
2018-11-27 20:30:33 +03:00
|
|
|
});
|
|
|
|
});
|