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. "2610057752638682202795145288373380503107623443963127956230801721756904484787");
  17. assert.equal(pubKey[1].toString(),
  18. "16617171478497210597712478520507818259149717466230047843969353176573634386897");
  19. const pPubKey = babyJub.packPoint(pubKey);
  20. const signature = eddsa.signMiMC(prvKey, msg);
  21. assert.equal(signature.R8[0].toString(),
  22. "4974729414807584049518234760796200867685098748448054182902488636762478901554");
  23. assert.equal(signature.R8[1].toString(),
  24. "18714049394522540751536514815950425694461287643205706667341348804546050128733");
  25. assert.equal(signature.S.toString(),
  26. "2171284143457722024136077617757713039502332290425057126942676527240038689549");
  27. const pSignature = eddsa.packSignature(signature);
  28. assert.equal(pSignature.toString("hex"), ""+
  29. "5dfb6f843c023fe3e52548ccf22e55c81b426f7af81b4f51f7152f2fcfc65f29"+
  30. "0dab19c5a0a75973cd75a54780de0c3a41ede6f57396fe99b5307fff3ce7cc04");
  31. const uSignature = eddsa.unpackSignature(pSignature);
  32. assert(eddsa.verifyMiMC(msg, uSignature, pubKey));
  33. });
  34. });