circomlib/test/eddsa.js
test/eddsa.jsRaw
1const chai = require("chai");
2const path = require("path");
3
4const tester = require("circom").tester;
5const bigInt = require("big-integer");
6
7const eddsa = require("../src/eddsa.js");
8const babyJub = require("../src/babyjub.js");
9
10const assert = chai.assert;
11
12function print(circuit, w, s) {
13 console.log(s + ": " + w[circuit.getSignalIdx(s)]);
14}
15
16function buffer2bits(buff) {
17 const res = [];
18 for (let i=0; i<buff.length; i++) {
19 for (let j=0; j<8; j++) {
20 if ((buff[i]>>j)&1) {
21 res.push(bigInt.one);
22 } else {
23 res.push(bigInt.zero);
24 }
25 }
26 }
27 return res;
28}
29
30
31describe("EdDSA test", function () {
32 let circuit;
33
34 this.timeout(100000);
35
36 before( async () => {
37 circuit = await tester(path.join(__dirname, "circuits", "eddsa_test.circom"));
38 });
39
40 it("Sign a single 10 bytes from 0 to 9", async () => {
41 const msg = Buffer.from("00010203040506070809", "hex");
42
43// const prvKey = crypto.randomBytes(32);
44
45 const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
46
47 const pubKey = eddsa.prv2pub(prvKey);
48
49 const pPubKey = babyJub.packPoint(pubKey);
50
51 const signature = eddsa.sign(prvKey, msg);
52
53 const pSignature = eddsa.packSignature(signature);
54 const uSignature = eddsa.unpackSignature(pSignature);
55
56 assert(eddsa.verify(msg, uSignature, pubKey));
57
58 const msgBits = buffer2bits(msg);
59 const r8Bits = buffer2bits(pSignature.slice(0, 32));
60 const sBits = buffer2bits(pSignature.slice(32, 64));
61 const aBits = buffer2bits(pPubKey);
62
63 const w = await circuit.calculateWitness({A: aBits, R8: r8Bits, S: sBits, msg: msgBits});
64
65 await circuit.checkConstraints(w);
66 });
67});