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. "13277427435165878497778222415993513565335242147425444199013288855685581939618");
  16. assert.equal(pubKey[1].toString(),
  17. "13622229784656158136036771217484571176836296686641868549125388198837476602820");
  18. const pPubKey = babyJub.packPoint(pubKey);
  19. const signature = eddsa.signMiMC(prvKey, msg);
  20. assert.equal(signature.R8[0].toString(),
  21. "11384336176656855268977457483345535180380036354188103142384839473266348197733");
  22. assert.equal(signature.R8[1].toString(),
  23. "15383486972088797283337779941324724402501462225528836549661220478783371668959");
  24. assert.equal(signature.S.toString(),
  25. "2523202440825208709475937830811065542425109372212752003460238913256192595070");
  26. const pSignature = eddsa.packSignature(signature);
  27. assert.equal(pSignature.toString("hex"), ""+
  28. "dfedb4315d3f2eb4de2d3c510d7a987dcab67089c8ace06308827bf5bcbe02a2"+
  29. "7ed40dab29bf993c928e789d007387998901a24913d44fddb64b1f21fc149405");
  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. "13277427435165878497778222415993513565335242147425444199013288855685581939618");
  40. assert.equal(pubKey[1].toString(),
  41. "13622229784656158136036771217484571176836296686641868549125388198837476602820");
  42. const pPubKey = babyJub.packPoint(pubKey);
  43. const signature = eddsa.signPoseidon(prvKey, msg);
  44. assert.equal(signature.R8[0].toString(),
  45. "11384336176656855268977457483345535180380036354188103142384839473266348197733");
  46. assert.equal(signature.R8[1].toString(),
  47. "15383486972088797283337779941324724402501462225528836549661220478783371668959");
  48. assert.equal(signature.S.toString(),
  49. "248298168863866362217836334079793350221620631973732197668910946177382043688");
  50. const pSignature = eddsa.packSignature(signature);
  51. assert.equal(pSignature.toString("hex"), ""+
  52. "dfedb4315d3f2eb4de2d3c510d7a987dcab67089c8ace06308827bf5bcbe02a2"+
  53. "28506bce274aa1b3f7e7c2fd7e4fe09bff8f9aa37a42def7994e98f322888c00");
  54. const uSignature = eddsa.unpackSignature(pSignature);
  55. assert(eddsa.verifyPoseidon(msg, uSignature, pubKey));
  56. });
  57. });