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.

60 lines
1.9 KiB

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 snarkjs = require("snarkjs");
  4. const compiler = require("circom");
  5. var blake2b = require('blake2b');
  6. const poseidon = require("../src/poseidon.js");
  7. const assert = chai.assert;
  8. describe("Blake2b version test", function() {
  9. it("Should give the expected output for blake2b version", async () => {
  10. var output = new Uint8Array(32);
  11. var input = Buffer.from('poseidon_constants');
  12. h = blake2b(output.length).update(input).digest('hex')
  13. assert.equal('e57ba154fb2c47811dc1a2369b27e25a44915b4e4ece4eb8ec74850cb78e01b1', h);
  14. });
  15. });
  16. describe("Poseidon Circuit test", function () {
  17. let circuit;
  18. this.timeout(100000);
  19. before( async () => {
  20. const cirDef = await compiler(path.join(__dirname, "circuits", "poseidon_test.circom"));
  21. circuit = new snarkjs.Circuit(cirDef);
  22. console.log("Poseidon constraints: " + circuit.nConstraints);
  23. });
  24. it("Should check constrain of hash([1, 2])", async () => {
  25. const w = circuit.calculateWitness({inputs: [1, 2]});
  26. const res = w[circuit.getSignalIdx("main.out")];
  27. const hash = poseidon.createHash(6, 8, 57);
  28. const res2 = hash([1,2]);
  29. assert.equal('12242166908188651009877250812424843524687801523336557272219921456462821518061', res2.toString());
  30. assert.equal(res.toString(), res2.toString());
  31. assert(circuit.checkWitness(w));
  32. });
  33. it("Should check constrain of hash([3, 4])", async () => {
  34. const w = circuit.calculateWitness({inputs: [3, 4]});
  35. const res = w[circuit.getSignalIdx("main.out")];
  36. const hash = poseidon.createHash(6, 8, 57);
  37. const res2 = hash([3, 4]);
  38. assert.equal('17185195740979599334254027721507328033796809509313949281114643312710535000993', res2.toString());
  39. assert.equal(res.toString(), res2.toString());
  40. assert(circuit.checkWitness(w));
  41. });
  42. });