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.

118 lines
3.8 KiB

  1. const chai = require("chai");
  2. const path = require("path");
  3. const createBlakeHash = require("blake-hash");
  4. const buildEddsa = require("circomlibjs").buildEddsa;
  5. const assert = chai.assert;
  6. const wasm_tester = require("circom_tester").wasm;
  7. const utils = require("ffjavascript").utils;
  8. const Scalar = require("ffjavascript").Scalar;
  9. describe("Baby Jub test", function () {
  10. let eddsa;
  11. let F;
  12. let circuitAdd;
  13. let circuitTest;
  14. let circuitPbk;
  15. this.timeout(100000);
  16. before( async() => {
  17. eddsa = await buildEddsa();
  18. F = eddsa.F;
  19. circuitAdd = await wasm_tester(path.join(__dirname, "circuits", "babyadd_tester.circom"));
  20. circuitTest = await wasm_tester(path.join(__dirname, "circuits", "babycheck_test.circom"));
  21. circuitPbk = await wasm_tester(path.join(__dirname, "circuits", "babypbk_test.circom"));
  22. });
  23. it("Should add point (0,1) and (0,1)", async () => {
  24. const input={
  25. x1: 0,
  26. y1: 1,
  27. x2: 0,
  28. y2: 1
  29. };
  30. const w = await circuitAdd.calculateWitness(input, true);
  31. await circuitAdd.assertOut(w, {xout: 0, yout: 1});
  32. });
  33. it("Should add 2 same numbers", async () => {
  34. const input={
  35. x1: 17777552123799933955779906779655732241715742912184938656739573121738514868268n,
  36. y1: 2626589144620713026669568689430873010625803728049924121243784502389097019475n,
  37. x2: 17777552123799933955779906779655732241715742912184938656739573121738514868268n,
  38. y2: 2626589144620713026669568689430873010625803728049924121243784502389097019475n
  39. };
  40. const w = await circuitAdd.calculateWitness(input, true);
  41. await circuitAdd.assertOut(w, {
  42. xout: 6890855772600357754907169075114257697580319025794532037257385534741338397365n,
  43. yout: 4338620300185947561074059802482547481416142213883829469920100239455078257889n
  44. });
  45. });
  46. it("Should add 2 different numbers", async () => {
  47. const input={
  48. x1: 17777552123799933955779906779655732241715742912184938656739573121738514868268n,
  49. y1: 2626589144620713026669568689430873010625803728049924121243784502389097019475n,
  50. x2: 16540640123574156134436876038791482806971768689494387082833631921987005038935n,
  51. y2: 20819045374670962167435360035096875258406992893633759881276124905556507972311n
  52. };
  53. const w = await circuitAdd.calculateWitness(input, true);
  54. await circuitAdd.assertOut(w, {
  55. xout: 7916061937171219682591368294088513039687205273691143098332585753343424131937n,
  56. yout: 14035240266687799601661095864649209771790948434046947201833777492504781204499n
  57. });
  58. });
  59. it("Should check (0,1) is a valid point", async() => {
  60. const w = await circuitTest.calculateWitness({x: 0, y:1}, true);
  61. await circuitTest.checkConstraints(w);
  62. });
  63. it("Should check (1,0) is an invalid point", async() => {
  64. try {
  65. await circuitTest.calculateWitness({x: 1, y: 0}, true);
  66. assert(false, "Should be a valid point");
  67. } catch(err) {
  68. assert(err.message.includes("Assert Failed"));
  69. }
  70. });
  71. it("Should extract the public key from the private one", async () => {
  72. const rawpvk = Buffer.from("0001020304050607080900010203040506070809000102030405060708090021", "hex");
  73. const pvk = eddsa.pruneBuffer(createBlakeHash("blake512").update(rawpvk).digest().slice(0,32));
  74. const S = Scalar.shr(utils.leBuff2int(pvk), 3);
  75. const A = eddsa.prv2pub(rawpvk);
  76. const input = {
  77. in : S
  78. };
  79. const w = await circuitPbk.calculateWitness(input, true);
  80. await circuitPbk.assertOut(w, {Ax : F.toObject(A[0]), Ay: F.toObject(A[1])});
  81. await circuitPbk.checkConstraints(w);
  82. });
  83. });