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.3 KiB

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