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.

97 lines
2.6 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. const chai = require("chai");
  2. const path = require("path");
  3. const tester = require("circom").tester;
  4. const Fr = require("ffjavascript").bn128.Fr;
  5. const eddsa = require("../src/eddsa.js");
  6. const assert = chai.assert;
  7. describe("EdDSA MiMC test", function () {
  8. let circuit;
  9. this.timeout(100000);
  10. before( async () => {
  11. circuit = await tester(path.join(__dirname, "circuits", "eddsamimc_test.circom"));
  12. });
  13. it("Sign a single number", async () => {
  14. const msg = Fr.e(1234);
  15. const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
  16. const pubKey = eddsa.prv2pub(prvKey);
  17. const signature = eddsa.signMiMC(prvKey, msg);
  18. assert(eddsa.verifyMiMC(msg, signature, pubKey));
  19. const w = await circuit.calculateWitness({
  20. enabled: 1,
  21. Ax: pubKey[0],
  22. Ay: pubKey[1],
  23. R8x: signature.R8[0],
  24. R8y: signature.R8[1],
  25. S: signature.S,
  26. M: msg}, true);
  27. await circuit.checkConstraints(w);
  28. });
  29. it("Detect Invalid signature", async () => {
  30. const msg = Fr.e(1234);
  31. const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
  32. const pubKey = eddsa.prv2pub(prvKey);
  33. const signature = eddsa.signMiMC(prvKey, msg);
  34. assert(eddsa.verifyMiMC(msg, signature, pubKey));
  35. try {
  36. const w = await circuit.calculateWitness({
  37. enabled: 1,
  38. Ax: pubKey[0],
  39. Ay: pubKey[1],
  40. R8x: Fr.add(signature.R8[0], Fr.e(1)),
  41. R8y: signature.R8[1],
  42. S: signature.S,
  43. M: msg}, true);
  44. assert(false);
  45. } catch(err) {
  46. assert(/Constraint\sdoesn't\smatch(.*)1\s!=\s0/.test(err.message) );
  47. }
  48. });
  49. it("Test a dissabled circuit with a bad signature", async () => {
  50. const msg = Fr.e(1234);
  51. const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
  52. const pubKey = eddsa.prv2pub(prvKey);
  53. const signature = eddsa.signMiMC(prvKey, msg);
  54. assert(eddsa.verifyMiMC(msg, signature, pubKey));
  55. const w = await circuit.calculateWitness({
  56. enabled: 0,
  57. Ax: pubKey[0],
  58. Ay: pubKey[1],
  59. R8x: Fr.add(signature.R8[0], Fr.e(1)),
  60. R8y: signature.R8[1],
  61. S: signature.S,
  62. M: msg}, true);
  63. await circuit.checkConstraints(w);
  64. });
  65. });