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.

115 lines
3.1 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. const chai = require("chai");
  2. const path = require("path");
  3. const crypto = require("crypto");
  4. const Fr = require("ffjavascript").bn128.Fr;
  5. const assert = chai.assert;
  6. const sha256 = require("./helpers/sha256");
  7. const tester = require("circom").tester;
  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", function () {
  28. this.timeout(100000);
  29. it("Should work bits to array and array to bits", async () => {
  30. const b = new Buffer.alloc(64);
  31. for (let i=0; i<64; i++) {
  32. b[i] = i+1;
  33. }
  34. const a = buffer2bitArray(b);
  35. const b2 = bitArray2buffer(a);
  36. assert.equal(b.toString("hex"), b2.toString("hex"), true);
  37. });
  38. it("Should calculate a hash of 1 compressor", async () => {
  39. const cir = await tester(path.join(__dirname, "circuits", "sha256_2_test.circom"));
  40. const witness = await cir.calculateWitness({ "a": "1", "b": "2" }, true);
  41. const b = new Buffer.alloc(54);
  42. b[26] = 1;
  43. b[53] = 2;
  44. const hash = crypto.createHash("sha256")
  45. .update(b)
  46. .digest("hex");
  47. const r = "0x" + hash.slice(10);
  48. const hash2 = sha256.hash(b.toString("hex"), {msgFormat: "hex-bytes"});
  49. assert.equal(hash, hash2);
  50. assert(Fr.eq(witness[1], Fr.e(r)));
  51. }).timeout(1000000);
  52. it("Should calculate a hash of 2 compressor", async () => {
  53. const cir = await tester(path.join(__dirname, "circuits", "sha256_test512.circom"));
  54. const b = new Buffer.alloc(64);
  55. for (let i=0; i<64; i++) {
  56. b[i] = i+1;
  57. }
  58. const hash = crypto.createHash("sha256")
  59. .update(b)
  60. .digest("hex");
  61. const arrIn = buffer2bitArray(b);
  62. const witness = await cir.calculateWitness({ "in": arrIn }, true);
  63. const arrOut = witness.slice(1, 257);
  64. const hash2 = bitArray2buffer(arrOut).toString("hex");
  65. assert.equal(hash, hash2);
  66. }).timeout(1000000);
  67. it ("Should calculate a hash of 2 compressor", async () => {
  68. const cir = await tester(path.join(__dirname, "circuits", "sha256_test448.circom"));
  69. const testStr = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  70. const b = Buffer.from(testStr, "utf8");
  71. const hash = crypto.createHash("sha256")
  72. .update(b)
  73. .digest("hex");
  74. const arrIn = buffer2bitArray(b);
  75. const witness = await cir.calculateWitness({ "in": arrIn }, true);
  76. const arrOut = witness.slice(1, 257);
  77. const hash2 = bitArray2buffer(arrOut).toString("hex");
  78. assert.equal(hash, hash2);
  79. });
  80. });