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.

82 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 bigInt = require("big-integer");
  6. const utils = require("../src/utils.js");
  7. describe("EdDSA js test", function () {
  8. this.timeout(100000);
  9. it("Sign (using Mimc7) a single 10 bytes from 0 to 9", () => {
  10. const msgBuf = Buffer.from("00010203040506070809", "hex");
  11. const msg = utils.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. it("Sign (using Poseidon) a single 10 bytes from 0 to 9", () => {
  35. const msgBuf = Buffer.from("00010203040506070809", "hex");
  36. const msg = utils.leBuff2int(msgBuf);
  37. const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
  38. const pubKey = eddsa.prv2pub(prvKey);
  39. assert.equal(pubKey[0].toString(),
  40. "13277427435165878497778222415993513565335242147425444199013288855685581939618");
  41. assert.equal(pubKey[1].toString(),
  42. "13622229784656158136036771217484571176836296686641868549125388198837476602820");
  43. const pPubKey = babyJub.packPoint(pubKey);
  44. const signature = eddsa.signPoseidon(prvKey, msg);
  45. assert.equal(signature.R8[0].toString(),
  46. "11384336176656855268977457483345535180380036354188103142384839473266348197733");
  47. assert.equal(signature.R8[1].toString(),
  48. "15383486972088797283337779941324724402501462225528836549661220478783371668959");
  49. assert.equal(signature.S.toString(),
  50. "248298168863866362217836334079793350221620631973732197668910946177382043688");
  51. const pSignature = eddsa.packSignature(signature);
  52. assert.equal(pSignature.toString("hex"), ""+
  53. "dfedb4315d3f2eb4de2d3c510d7a987dcab67089c8ace06308827bf5bcbe02a2"+
  54. "28506bce274aa1b3f7e7c2fd7e4fe09bff8f9aa37a42def7994e98f322888c00");
  55. const uSignature = eddsa.unpackSignature(pSignature);
  56. assert(eddsa.verifyPoseidon(msg, uSignature, pubKey));
  57. });
  58. });