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.

103 lines
3.0 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 Poseidon 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", "eddsaposeidon_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.signPoseidon(prvKey, msg);
  24. assert(eddsa.verifyPoseidon(msg, signature, pubKey));
  25. const input = {
  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)
  33. };
  34. // console.log(JSON.stringify(utils.stringifyBigInts(input)));
  35. const w = await circuit.calculateWitness(input, true);
  36. await circuit.checkConstraints(w);
  37. });
  38. it("Detect Invalid signature", async () => {
  39. const msg = F.e(1234);
  40. const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
  41. const pubKey = eddsa.prv2pub(prvKey);
  42. const signature = eddsa.signPoseidon(prvKey, msg);
  43. assert(eddsa.verifyPoseidon(msg, signature, pubKey));
  44. try {
  45. await circuit.calculateWitness({
  46. enabled: 1,
  47. Ax: F.toObject(pubKey[0]),
  48. Ay: F.toObject(pubKey[1]),
  49. R8x: F.toObject(F.add(signature.R8[0], F.e(1))),
  50. R8y: F.toObject(signature.R8[1]),
  51. S: signature.S,
  52. M: F.toObject(msg)}, true);
  53. assert(false);
  54. } catch(err) {
  55. assert(err.message.includes("Assert Failed"));
  56. }
  57. });
  58. it("Test a dissabled circuit with a bad signature", async () => {
  59. const msg = F.e(1234);
  60. const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
  61. const pubKey = eddsa.prv2pub(prvKey);
  62. const signature = eddsa.signPoseidon(prvKey, msg);
  63. assert(eddsa.verifyPoseidon(msg, signature, pubKey));
  64. const w = await circuit.calculateWitness({
  65. enabled: 0,
  66. Ax: F.toObject(pubKey[0]),
  67. Ay: F.toObject(pubKey[1]),
  68. R8x: F.toObject(F.add(signature.R8[0], F.e(1))),
  69. R8y: F.toObject(signature.R8[1]),
  70. S: signature.S,
  71. M: F.toObject(msg)}, true);
  72. await circuit.checkConstraints(w);
  73. });
  74. });