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.

100 lines
3.7 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
6 years ago
6 years ago
6 years ago
  1. const chai = require("chai");
  2. const path = require("path");
  3. const snarkjs = require("snarkjs");
  4. const compiler = require("circom");
  5. const assert = chai.assert;
  6. const bigInt = require("big-integer");
  7. describe("Baby Jub test", function () {
  8. let circuitAdd;
  9. let circuitTest;
  10. this.timeout(100000);
  11. before( async() => {
  12. const cirDefAdd = await compiler(path.join(__dirname, "circuits", "babyadd_tester.circom"));
  13. circuitAdd = new snarkjs.Circuit(cirDefAdd);
  14. console.log("NConstrains BabyAdd: " + circuitAdd.nConstraints);
  15. const cirDefTest = await compiler(path.join(__dirname, "circuits", "babycheck_test.circom"));
  16. circuitTest = new snarkjs.Circuit(cirDefTest);
  17. console.log("NConstrains BabyTest: " + circuitTest.nConstraints);
  18. });
  19. it("Should add point (0,1) and (0,1)", async () => {
  20. const input={
  21. x1: snarkjs.bigInt(0),
  22. y1: snarkjs.bigInt(1),
  23. x2: snarkjs.bigInt(0),
  24. y2: snarkjs.bigInt(1)
  25. };
  26. const w = circuitAdd.calculateWitness(input);
  27. const xout = w[circuitAdd.getSignalIdx("main.xout")];
  28. const yout = w[circuitAdd.getSignalIdx("main.yout")];
  29. assert(xout.equals(0));
  30. assert(yout.equals(1));
  31. });
  32. it("Should add 2 same numbers", async () => {
  33. const input={
  34. x1: snarkjs.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
  35. y1: snarkjs.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
  36. x2: snarkjs.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
  37. y2: snarkjs.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475")
  38. };
  39. const w = circuitAdd.calculateWitness(input);
  40. const xout = w[circuitAdd.getSignalIdx("main.xout")];
  41. const yout = w[circuitAdd.getSignalIdx("main.yout")];
  42. assert(xout.equals(snarkjs.bigInt("6890855772600357754907169075114257697580319025794532037257385534741338397365")));
  43. assert(yout.equals(snarkjs.bigInt("4338620300185947561074059802482547481416142213883829469920100239455078257889")));
  44. });
  45. it("Should add 2 different numbers", async () => {
  46. const input={
  47. x1: snarkjs.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
  48. y1: snarkjs.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
  49. x2: snarkjs.bigInt("16540640123574156134436876038791482806971768689494387082833631921987005038935"),
  50. y2: snarkjs.bigInt("20819045374670962167435360035096875258406992893633759881276124905556507972311")
  51. };
  52. const w = circuitAdd.calculateWitness(input);
  53. const xout = w[circuitAdd.getSignalIdx("main.xout")];
  54. const yout = w[circuitAdd.getSignalIdx("main.yout")];
  55. /*
  56. console.log(xout.toString());
  57. console.log(yout.toString());
  58. */
  59. assert(xout.equals(snarkjs.bigInt("7916061937171219682591368294088513039687205273691143098332585753343424131937")));
  60. assert(yout.equals(snarkjs.bigInt("14035240266687799601661095864649209771790948434046947201833777492504781204499")));
  61. });
  62. it("Should check 0 is a valid poiny", async() => {
  63. const w = circuitTest.calculateWitness({x: 0, y:1});
  64. assert(circuitTest.checkWitness(w));
  65. });
  66. it("Should check 0 is an invalid poiny", async() => {
  67. try {
  68. circuitTest.calculateWitness({x: 1, y: 0});
  69. assert(false, "Should be a valid point");
  70. } catch(err) {
  71. assert.equal(err.message, "Constraint doesn't match: 168700 != 1");
  72. }
  73. });
  74. });