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 "13277427435165878497778222415993513565335242147425444199013288855685581939618");
27 assert.equal(pubKey[1].toString(),
28 "13622229784656158136036771217484571176836296686641868549125388198837476602820");
29
30 const pPubKey = babyJub.packPoint(pubKey);
31
32 const signature = eddsa.signMiMC(prvKey, msg);
33 assert.equal(signature.R8[0].toString(),
34 "11384336176656855268977457483345535180380036354188103142384839473266348197733");
35 assert.equal(signature.R8[1].toString(),
36 "15383486972088797283337779941324724402501462225528836549661220478783371668959");
37 assert.equal(signature.S.toString(),
38 "2523202440825208709475937830811065542425109372212752003460238913256192595070");
39
40 const pSignature = eddsa.packSignature(signature);
41 assert.equal(pSignature.toString("hex"), ""+
42 "dfedb4315d3f2eb4de2d3c510d7a987dcab67089c8ace06308827bf5bcbe02a2"+
43 "7ed40dab29bf993c928e789d007387998901a24913d44fddb64b1f21fc149405");
44
45 const uSignature = eddsa.unpackSignature(pSignature);
46 assert(eddsa.verifyMiMC(msg, uSignature, pubKey));
47
48 });
49});