You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.8 KiB

  1. const chai = require("chai");
  2. const snarkjs = require("snarkjs");
  3. const eddsa = require("../src/eddsa.js");
  4. const babyJub = require("../src/babyjub.js");
  5. const assert = chai.assert;
  6. const bigInt = snarkjs.bigInt;
  7. describe("EdDSA js test", function () {
  8. this.timeout(100000);
  9. it("Sign a single 10 bytes from 0 to 9", () => {
  10. const msgBuf = Buffer.from("00010203040506070809", "hex");
  11. const msg = bigInt.leBuff2int(msgBuf);
  12. // const prvKey = crypto.randomBytes(32);
  13. const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
  14. const pubKey = eddsa.prv2pub(prvKey);
  15. assert.equal(pubKey[0].toString(),
  16. "13277427435165878497778222415993513565335242147425444199013288855685581939618");
  17. assert.equal(pubKey[1].toString(),
  18. "13622229784656158136036771217484571176836296686641868549125388198837476602820");
  19. const pPubKey = babyJub.packPoint(pubKey);
  20. const signature = eddsa.signMiMC(prvKey, msg);
  21. assert.equal(signature.R8[0].toString(),
  22. "11384336176656855268977457483345535180380036354188103142384839473266348197733");
  23. assert.equal(signature.R8[1].toString(),
  24. "15383486972088797283337779941324724402501462225528836549661220478783371668959");
  25. assert.equal(signature.S.toString(),
  26. "2523202440825208709475937830811065542425109372212752003460238913256192595070");
  27. const pSignature = eddsa.packSignature(signature);
  28. assert.equal(pSignature.toString("hex"), ""+
  29. "dfedb4315d3f2eb4de2d3c510d7a987dcab67089c8ace06308827bf5bcbe02a2"+
  30. "7ed40dab29bf993c928e789d007387998901a24913d44fddb64b1f21fc149405");
  31. const uSignature = eddsa.unpackSignature(pSignature);
  32. assert(eddsa.verifyMiMC(msg, uSignature, pubKey));
  33. });
  34. });