circomlib/test/eddsa_js.js
test/eddsa_js.jsRaw
1const chai = require("chai");
2const snarkjs = require("snarkjs");
3
4const eddsa = require("../src/eddsa.js");
5const babyJub = require("../src/babyjub.js");
6
7const assert = chai.assert;
8
9const bigInt = snarkjs.bigInt;
10
11describe("EdDSA js test", function () {
12
13 this.timeout(100000);
14
15 it("Sign a single 10 bytes from 0 to 9", () => {
16 const msgBuf = Buffer.from("00010203040506070809", "hex");
17 const msg = bigInt.leBuff2int(msgBuf);
18
19 // const prvKey = crypto.randomBytes(32);
20
21 const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
22
23 const pubKey = eddsa.prv2pub(prvKey);
24
25 assert.equal(pubKey[0].toString(),
26 "2610057752638682202795145288373380503107623443963127956230801721756904484787");
27 assert.equal(pubKey[1].toString(),
28 "16617171478497210597712478520507818259149717466230047843969353176573634386897");
29
30 const pPubKey = babyJub.packPoint(pubKey);
31
32 const signature = eddsa.signMiMC(prvKey, msg);
33 assert.equal(signature.R8[0].toString(),
34 "4974729414807584049518234760796200867685098748448054182902488636762478901554");
35 assert.equal(signature.R8[1].toString(),
36 "18714049394522540751536514815950425694461287643205706667341348804546050128733");
37 assert.equal(signature.S.toString(),
38 "2171284143457722024136077617757713039502332290425057126942676527240038689549");
39
40 const pSignature = eddsa.packSignature(signature);
41 assert.equal(pSignature.toString("hex"), ""+
42 "5dfb6f843c023fe3e52548ccf22e55c81b426f7af81b4f51f7152f2fcfc65f29"+
43 "0dab19c5a0a75973cd75a54780de0c3a41ede6f57396fe99b5307fff3ce7cc04");
44
45 const uSignature = eddsa.unpackSignature(pSignature);
46 assert(eddsa.verifyMiMC(msg, uSignature, pubKey));
47
48 });
49});