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.

81 lines
3.4 KiB

  1. const chai = require("chai");
  2. const eddsa = require("../src/eddsa.js");
  3. const babyJub = require("../src/babyjub.js");
  4. const assert = chai.assert;
  5. const utils = require("ffjavascript").utils;
  6. describe("EdDSA js test", function () {
  7. this.timeout(100000);
  8. it("Sign (using Mimc7) a single 10 bytes from 0 to 9", () => {
  9. const msgBuf = Buffer.from("00010203040506070809", "hex");
  10. const msg = utils.leBuff2int(msgBuf);
  11. // const prvKey = crypto.randomBytes(32);
  12. const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
  13. const pubKey = eddsa.prv2pub(prvKey);
  14. assert.equal(pubKey[0].toString(),
  15. "17579234973106307986399040784563986669343100608865726413246909559198451825625");
  16. assert.equal(pubKey[1].toString(),
  17. "21581828029826859845363968476425861244058376747493285816141526544272562145486");
  18. const pPubKey = babyJub.packPoint(pubKey);
  19. const signature = eddsa.signMiMC(prvKey, msg);
  20. assert.equal(signature.R8[0].toString(),
  21. "12672422877531089818651367820728973438446851190471722610781936061829103362897");
  22. assert.equal(signature.R8[1].toString(),
  23. "12052234579439634484237590306927118446073354173341433290934144373261241958718");
  24. assert.equal(signature.S.toString(),
  25. "1582013862333331285840015273849085014739146294568319205499642618291614907374");
  26. const pSignature = eddsa.packSignature(signature);
  27. assert.equal(pSignature.toString("hex"), ""+
  28. "3e417cd811f9c9c545a680b962e45d22ccb62b2284b4fe4bbc9fdb50b252a59a" +
  29. "eefbebe2b895393fa0e9b5b31b19e65a63fee5d7b6261d8d5b6b847c5b637f03");
  30. const uSignature = eddsa.unpackSignature(pSignature);
  31. assert(eddsa.verifyMiMC(msg, uSignature, pubKey));
  32. });
  33. it("Sign (using Poseidon) a single 10 bytes from 0 to 9", () => {
  34. const msgBuf = Buffer.from("00010203040506070809", "hex");
  35. const msg = utils.leBuff2int(msgBuf);
  36. const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
  37. const pubKey = eddsa.prv2pub(prvKey);
  38. assert.equal(pubKey[0].toString(),
  39. "17579234973106307986399040784563986669343100608865726413246909559198451825625");
  40. assert.equal(pubKey[1].toString(),
  41. "21581828029826859845363968476425861244058376747493285816141526544272562145486");
  42. const pPubKey = babyJub.packPoint(pubKey);
  43. const signature = eddsa.signPoseidon(prvKey, msg);
  44. assert.equal(signature.R8[0].toString(),
  45. "12672422877531089818651367820728973438446851190471722610781936061829103362897");
  46. assert.equal(signature.R8[1].toString(),
  47. "12052234579439634484237590306927118446073354173341433290934144373261241958718");
  48. assert.equal(signature.S.toString(),
  49. "2318334603430781860679872910160434499077270843466490702990199622594868564504");
  50. const pSignature = eddsa.packSignature(signature);
  51. assert.equal(pSignature.toString("hex"), ""+
  52. "3e417cd811f9c9c545a680b962e45d22ccb62b2284b4fe4bbc9fdb50b252a59a" +
  53. "1852c049fc6286138a0ddb57718049a09374fdf0390686c7ac5637b481212005");
  54. const uSignature = eddsa.unpackSignature(pSignature);
  55. assert(eddsa.verifyPoseidon(msg, uSignature, pubKey));
  56. });
  57. });