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.

98 lines
3.6 KiB

6 years ago
  1. const chai = require("chai");
  2. const path = require("path");
  3. const zkSnark = require("zksnark");
  4. const compiler = require("circom");
  5. const assert = chai.assert;
  6. const bigInt = require("big-integer");
  7. describe("Baby Jub test", () => {
  8. it("Should add point (0,1) and (0,1)", async () => {
  9. const cirDef = await compiler(path.join(__dirname, "circuits", "babyadd_tester.circom"));
  10. // console.log(JSON.stringify(cirDef, null, 1));
  11. // assert.equal(cirDef.nVars, 2);
  12. const circuit = new zkSnark.Circuit(cirDef);
  13. console.log("NConstrains: " + circuit.nConstraints);
  14. const input={
  15. x1: zkSnark.bigInt(0),
  16. y1: zkSnark.bigInt(1),
  17. x2: zkSnark.bigInt(0),
  18. y2: zkSnark.bigInt(1)
  19. }
  20. const w = circuit.calculateWitness(input);
  21. const xout = w[circuit.getSignalIdx("main.xout")];
  22. const yout = w[circuit.getSignalIdx("main.yout")];
  23. assert(xout.equals(0));
  24. assert(yout.equals(1));
  25. });
  26. it("Should add 2 same numbers", async () => {
  27. const cirDef = await compiler(path.join(__dirname, "circuits", "babyadd_tester.circom"));
  28. // console.log(JSON.stringify(cirDef, null, 1));
  29. // assert.equal(cirDef.nVars, 2);
  30. const circuit = new zkSnark.Circuit(cirDef);
  31. console.log("NConstrains: " + circuit.nConstraints);
  32. const input={
  33. x1: zkSnark.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
  34. y1: zkSnark.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
  35. x2: zkSnark.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
  36. y2: zkSnark.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475")
  37. }
  38. const w = circuit.calculateWitness(input);
  39. const xout = w[circuit.getSignalIdx("main.xout")];
  40. const yout = w[circuit.getSignalIdx("main.yout")];
  41. assert(xout.equals(zkSnark.bigInt("6890855772600357754907169075114257697580319025794532037257385534741338397365")));
  42. assert(yout.equals(zkSnark.bigInt("4338620300185947561074059802482547481416142213883829469920100239455078257889")));
  43. });
  44. it("Should add 2 different numbers", async () => {
  45. const cirDef = await compiler(path.join(__dirname, "circuits", "babyadd_tester.circom"));
  46. // console.log(JSON.stringify(cirDef, null, 1));
  47. // assert.equal(cirDef.nVars, 2);
  48. const circuit = new zkSnark.Circuit(cirDef);
  49. console.log("NConstrains: " + circuit.nConstraints);
  50. const input={
  51. x1: zkSnark.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
  52. y1: zkSnark.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
  53. x2: zkSnark.bigInt("16540640123574156134436876038791482806971768689494387082833631921987005038935"),
  54. y2: zkSnark.bigInt("20819045374670962167435360035096875258406992893633759881276124905556507972311")
  55. }
  56. const w = circuit.calculateWitness(input);
  57. const xout = w[circuit.getSignalIdx("main.xout")];
  58. const yout = w[circuit.getSignalIdx("main.yout")];
  59. console.log(xout.toString());
  60. console.log(yout.toString());
  61. assert(xout.equals(zkSnark.bigInt("7916061937171219682591368294088513039687205273691143098332585753343424131937")));
  62. assert(yout.equals(zkSnark.bigInt("14035240266687799601661095864649209771790948434046947201833777492504781204499")));
  63. });
  64. });