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.

99 lines
2.7 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 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 Poseidon test", function () {
  8. let circuit;
  9. this.timeout(100000);
  10. before( async () => {
  11. circuit = await tester(path.join(__dirname, "circuits", "eddsaposeidon_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.signPoseidon(prvKey, msg);
  18. assert(eddsa.verifyPoseidon(msg, signature, pubKey));
  19. const input = {
  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
  27. };
  28. // console.log(JSON.stringify(utils.stringifyBigInts(input)));
  29. const w = await circuit.calculateWitness(input, true);
  30. await circuit.checkConstraints(w);
  31. });
  32. it("Detect Invalid signature", async () => {
  33. const msg = Fr.e(1234);
  34. const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
  35. const pubKey = eddsa.prv2pub(prvKey);
  36. const signature = eddsa.signPoseidon(prvKey, msg);
  37. assert(eddsa.verifyPoseidon(msg, signature, pubKey));
  38. try {
  39. await circuit.calculateWitness({
  40. enabled: 1,
  41. Ax: pubKey[0],
  42. Ay: pubKey[1],
  43. R8x: Fr.add(signature.R8[0], Fr.e(1)),
  44. R8y: signature.R8[1],
  45. S: signature.S,
  46. M: msg}, true);
  47. assert(false);
  48. } catch(err) {
  49. assert(/Constraint\sdoesn't\smatch(.*)1\s!=\s0/.test(err.message) );
  50. }
  51. });
  52. it("Test a dissabled circuit with a bad signature", async () => {
  53. const msg = Fr.e(1234);
  54. const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
  55. const pubKey = eddsa.prv2pub(prvKey);
  56. const signature = eddsa.signPoseidon(prvKey, msg);
  57. assert(eddsa.verifyPoseidon(msg, signature, pubKey));
  58. const w = await circuit.calculateWitness({
  59. enabled: 0,
  60. Ax: pubKey[0],
  61. Ay: pubKey[1],
  62. R8x: Fr.add(signature.R8[0], Fr.e(1)),
  63. R8y: signature.R8[1],
  64. S: signature.S,
  65. M: msg}, true);
  66. await circuit.checkConstraints(w);
  67. });
  68. });