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.

112 lines
3.9 KiB

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