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.

136 lines
3.8 KiB

  1. const chai = require("chai");
  2. const path = require("path");
  3. const snarkjs = require("snarkjs");
  4. const crypto = require("crypto");
  5. const compiler = require("circom");
  6. const assert = chai.assert;
  7. const sha256 = require("./helpers/sha256");
  8. // const printSignal = require("./helpers/printsignal");
  9. function buffer2bitArray(b) {
  10. const res = [];
  11. for (let i=0; i<b.length; i++) {
  12. for (let j=0; j<8; j++) {
  13. res.push((b[i] >> (7-j) &1));
  14. }
  15. }
  16. return res;
  17. }
  18. function bitArray2buffer(a) {
  19. const len = Math.floor((a.length -1 )/8)+1;
  20. const b = new Buffer.alloc(len);
  21. for (let i=0; i<a.length; i++) {
  22. const p = Math.floor(i/8);
  23. b[p] = b[p] | (Number(a[i]) << ( 7 - (i%8) ));
  24. }
  25. return b;
  26. }
  27. describe("SHA256 test", () => {
  28. it("Should work bits to array and array to bits", async () => {
  29. const b = new Buffer.alloc(64);
  30. for (let i=0; i<64; i++) {
  31. b[i] = i+1;
  32. }
  33. const a = buffer2bitArray(b);
  34. const b2 = bitArray2buffer(a);
  35. assert.equal(b.toString("hex"), b2.toString("hex"));
  36. });
  37. it("Should calculate a hash of 1 compressor", async () => {
  38. const cirDef = await compiler(path.join(__dirname, "circuits", "sha256_2_test.circom"));
  39. const circuit = new snarkjs.Circuit(cirDef);
  40. console.log("Vars: "+circuit.nVars);
  41. console.log("Constraints: "+circuit.nConstraints);
  42. const witness = circuit.calculateWitness({ "a": "1", "b": "2" });
  43. const b = new Buffer.alloc(54);
  44. b[26] = 1;
  45. b[53] = 2;
  46. const hash = crypto.createHash("sha256")
  47. .update(b)
  48. .digest("hex");
  49. const r = "0x" + hash.slice(10);
  50. const hash2 = sha256.hash(b.toString("hex"), {msgFormat: "hex-bytes"});
  51. assert.equal(hash, hash2);
  52. assert(witness[1].equals(snarkjs.bigInt(r)));
  53. }).timeout(1000000);
  54. it("Should calculate a hash of 2 compressor", async () => {
  55. const cirDef = await compiler(path.join(__dirname, "circuits", "sha256_test512.circom"), {reduceConstraints:false} );
  56. const circuit = new snarkjs.Circuit(cirDef);
  57. console.log("Vars: "+circuit.nVars);
  58. console.log("Constraints: "+circuit.nConstraints);
  59. /*
  60. const testStr = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  61. const b = Buffer.from(testStr, 'utf8');
  62. */
  63. const b = new Buffer.alloc(64);
  64. for (let i=0; i<64; i++) {
  65. b[i] = i+1;
  66. }
  67. const hash = crypto.createHash("sha256")
  68. .update(b)
  69. .digest("hex");
  70. const arrIn = buffer2bitArray(b);
  71. const witness = circuit.calculateWitness({ "in": arrIn } /*, {logOutput: true} */);
  72. const arrOut = witness.slice(1, 257);
  73. const hash2 = bitArray2buffer(arrOut).toString("hex");
  74. assert.equal(hash, hash2);
  75. }).timeout(1000000);
  76. it("Should calculate a hash of 2 compressor", async () => {
  77. const cirDef = await compiler(path.join(__dirname, "circuits", "sha256_test448.circom"), {reduceConstraints:false} );
  78. const circuit = new snarkjs.Circuit(cirDef);
  79. console.log("Vars: "+circuit.nVars);
  80. console.log("Constraints: "+circuit.nConstraints);
  81. const testStr = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  82. const b = Buffer.from(testStr, 'utf8');
  83. for (let i=0; i<64; i++) {
  84. b[i] = i+1;
  85. }
  86. const hash = crypto.createHash("sha256")
  87. .update(b)
  88. .digest("hex");
  89. const arrIn = buffer2bitArray(b);
  90. const witness = circuit.calculateWitness({ "in": arrIn } /*, {logOutput: true} */);
  91. const arrOut = witness.slice(1, 257);
  92. const hash2 = bitArray2buffer(arrOut).toString("hex");
  93. assert.equal(hash, hash2);
  94. }).timeout(1000000);
  95. });