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.

102 lines
2.9 KiB

  1. const chai = require("chai");
  2. const path = require("path");
  3. const wasm_tester = require("circom_tester").wasm;
  4. const buildEddsa = require("circomlibjs").buildEddsa;
  5. const buildBabyjub = require("circomlibjs").buildBabyjub;
  6. const assert = chai.assert;
  7. describe("EdDSA MiMC test", function () {
  8. let circuit;
  9. let eddsa;
  10. let babyJub;
  11. let F;
  12. this.timeout(100000);
  13. before( async () => {
  14. eddsa = await buildEddsa();
  15. babyJub = await buildBabyjub();
  16. F = babyJub.F;
  17. circuit = await wasm_tester(path.join(__dirname, "circuits", "eddsamimc_test.circom"));
  18. });
  19. it("Sign a single number", async () => {
  20. const msg = F.e(1234);
  21. const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
  22. const pubKey = eddsa.prv2pub(prvKey);
  23. const signature = eddsa.signMiMC(prvKey, msg);
  24. assert(eddsa.verifyMiMC(msg, signature, pubKey));
  25. const w = await circuit.calculateWitness({
  26. enabled: 1,
  27. Ax: F.toObject(pubKey[0]),
  28. Ay: F.toObject(pubKey[1]),
  29. R8x: F.toObject(signature.R8[0]),
  30. R8y: F.toObject(signature.R8[1]),
  31. S: signature.S,
  32. M: F.toObject(msg)}, true);
  33. await circuit.checkConstraints(w);
  34. });
  35. it("Detect Invalid signature", async () => {
  36. const msg = F.e(1234);
  37. const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
  38. const pubKey = eddsa.prv2pub(prvKey);
  39. const signature = eddsa.signMiMC(prvKey, msg);
  40. assert(eddsa.verifyMiMC(msg, signature, pubKey));
  41. try {
  42. const w = await circuit.calculateWitness({
  43. enabled: 1,
  44. Ax: F.toObject(pubKey[0]),
  45. Ay: F.toObject(pubKey[1]),
  46. R8x: F.toObject(F.add(signature.R8[0], F.e(1))),
  47. R8y: F.toObject(signature.R8[1]),
  48. S: signature.S,
  49. M: F.toObject(msg)}, true);
  50. assert(false);
  51. } catch(err) {
  52. assert(err.message.includes("Assert Failed"));
  53. }
  54. });
  55. it("Test a dissabled circuit with a bad signature", async () => {
  56. const msg = F.e(1234);
  57. const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
  58. const pubKey = eddsa.prv2pub(prvKey);
  59. const signature = eddsa.signMiMC(prvKey, msg);
  60. assert(eddsa.verifyMiMC(msg, signature, pubKey));
  61. const w = await circuit.calculateWitness({
  62. enabled: 0,
  63. Ax: F.toObject(pubKey[0]),
  64. Ay: F.toObject(pubKey[1]),
  65. R8x: F.toObject(F.add(signature.R8[0], F.e(1))),
  66. R8y: F.toObject(signature.R8[1]),
  67. S: signature.S,
  68. M: F.toObject(msg)}, true);
  69. await circuit.checkConstraints(w);
  70. });
  71. });