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.

125 lines
4.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
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 createBlakeHash = require("blake-hash");
  6. const eddsa = require("../src/eddsa.js");
  7. const assert = chai.assert;
  8. const bigInt = require("snarkjs").bigInt;
  9. describe("Baby Jub test", function () {
  10. let circuitAdd;
  11. let circuitTest;
  12. this.timeout(100000);
  13. before( async() => {
  14. const cirDefAdd = await compiler(path.join(__dirname, "circuits", "babyadd_tester.circom"));
  15. circuitAdd = new snarkjs.Circuit(cirDefAdd);
  16. console.log("NConstrains BabyAdd: " + circuitAdd.nConstraints);
  17. const cirDefTest = await compiler(path.join(__dirname, "circuits", "babycheck_test.circom"));
  18. circuitTest = new snarkjs.Circuit(cirDefTest);
  19. console.log("NConstrains BabyTest: " + circuitTest.nConstraints);
  20. const cirDefPbk = await compiler(path.join(__dirname, "circuits", "babypbk_test.circom"));
  21. circuitPbk = new snarkjs.Circuit(cirDefPbk);
  22. console.log("NConstrains BabyPbk: " + circuitPbk.nConstraints);
  23. });
  24. it("Should add point (0,1) and (0,1)", async () => {
  25. const input={
  26. x1: snarkjs.bigInt(0),
  27. y1: snarkjs.bigInt(1),
  28. x2: snarkjs.bigInt(0),
  29. y2: snarkjs.bigInt(1)
  30. };
  31. const w = circuitAdd.calculateWitness(input);
  32. const xout = w[circuitAdd.getSignalIdx("main.xout")];
  33. const yout = w[circuitAdd.getSignalIdx("main.yout")];
  34. assert(xout.equals(0));
  35. assert(yout.equals(1));
  36. });
  37. it("Should add 2 same numbers", async () => {
  38. const input={
  39. x1: snarkjs.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
  40. y1: snarkjs.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
  41. x2: snarkjs.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
  42. y2: snarkjs.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475")
  43. };
  44. const w = circuitAdd.calculateWitness(input);
  45. const xout = w[circuitAdd.getSignalIdx("main.xout")];
  46. const yout = w[circuitAdd.getSignalIdx("main.yout")];
  47. assert(xout.equals(snarkjs.bigInt("6890855772600357754907169075114257697580319025794532037257385534741338397365")));
  48. assert(yout.equals(snarkjs.bigInt("4338620300185947561074059802482547481416142213883829469920100239455078257889")));
  49. });
  50. it("Should add 2 different numbers", async () => {
  51. const input={
  52. x1: snarkjs.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
  53. y1: snarkjs.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
  54. x2: snarkjs.bigInt("16540640123574156134436876038791482806971768689494387082833631921987005038935"),
  55. y2: snarkjs.bigInt("20819045374670962167435360035096875258406992893633759881276124905556507972311")
  56. };
  57. const w = circuitAdd.calculateWitness(input);
  58. const xout = w[circuitAdd.getSignalIdx("main.xout")];
  59. const yout = w[circuitAdd.getSignalIdx("main.yout")];
  60. /*
  61. console.log(xout.toString());
  62. console.log(yout.toString());
  63. */
  64. assert(xout.equals(snarkjs.bigInt("7916061937171219682591368294088513039687205273691143098332585753343424131937")));
  65. assert(yout.equals(snarkjs.bigInt("14035240266687799601661095864649209771790948434046947201833777492504781204499")));
  66. });
  67. it("Should check 0 is a valid poiny", async() => {
  68. const w = circuitTest.calculateWitness({x: 0, y:1});
  69. assert(circuitTest.checkWitness(w));
  70. });
  71. it("Should check 0 is an invalid poiny", async() => {
  72. try {
  73. circuitTest.calculateWitness({x: 1, y: 0});
  74. assert(false, "Should be a valid point");
  75. } catch(err) {
  76. assert(/Constraint\sdoesn't\smatch(.*)168700\s!=\s1/.test(err.message) );
  77. }
  78. });
  79. it("Should extract the public key from the private one", async () => {
  80. const rawpvk = Buffer.from("0001020304050607080900010203040506070809000102030405060708090021", "hex");
  81. const pvk = eddsa.pruneBuffer(createBlakeHash("blake512").update(rawpvk).digest().slice(0,32));
  82. const S = bigInt.leBuff2int(pvk).shr(3);
  83. const A = eddsa.prv2pub(rawpvk);
  84. const input = {
  85. in : S,
  86. Ax : A[0],
  87. Ay : A[1]
  88. }
  89. const w = circuitPbk.calculateWitness(input);
  90. assert(circuitPbk.checkWitness(w));
  91. });
  92. });