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.

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