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.

158 lines
5.7 KiB

  1. const path = require("path");
  2. const chai = require("chai");
  3. const assert = chai.assert;
  4. const c_tester = require("circom_tester").c;
  5. const utils = require("./utils");
  6. const keccak256 = require("keccak256");
  7. describe("Keccak 32bytes full hash test", function () {
  8. this.timeout(100000);
  9. let cir;
  10. before(async () => {
  11. cir = await c_tester(path.join(__dirname, "circuits", "keccak_256_256_test.circom"));
  12. await cir.loadConstraints();
  13. console.log("n_constraints", cir.constraints.length);
  14. });
  15. it ("Keccak 1 (testvector generated from go)", async () => {
  16. const input = [116, 101, 115, 116, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  17. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
  18. const expectedOut = [37, 17, 98, 135, 161, 178, 88, 97, 125, 150, 143,
  19. 65, 228, 211, 170, 133, 153, 9, 88, 212, 4, 212, 175, 238, 249,
  20. 210, 214, 116, 170, 85, 45, 21];
  21. const inIn = utils.bytesToBits(input);
  22. const witness = await cir.calculateWitness({ "in": inIn }, true);
  23. const stateOut = witness.slice(1, 1+(32*8));
  24. const stateOutBytes = utils.bitsToBytes(stateOut);
  25. // console.log(stateOutBytes, expectedOut);
  26. assert.deepEqual(stateOutBytes, expectedOut);
  27. });
  28. it ("Keccak 2 (testvector generated from go)", async () => {
  29. const input = [37, 17, 98, 135, 161, 178, 88, 97, 125, 150, 143, 65,
  30. 228, 211, 170, 133, 153, 9, 88, 212, 4, 212, 175, 238, 249, 210,
  31. 214, 116, 170, 85, 45, 21];
  32. const expectedOut = [182, 104, 121, 2, 8, 48, 224, 11, 238, 244, 73,
  33. 142, 67, 205, 166, 27, 10, 223, 142, 209, 10, 46, 171, 110, 239,
  34. 68, 111, 116, 164, 127, 103, 141];
  35. const inIn = utils.bytesToBits(input);
  36. const witness = await cir.calculateWitness({ "in": inIn }, true);
  37. const stateOut = witness.slice(1, 1+(32*8));
  38. const stateOutBytes = utils.bitsToBytes(stateOut);
  39. // console.log(stateOutBytes, expectedOut);
  40. assert.deepEqual(stateOutBytes, expectedOut);
  41. });
  42. it ("Keccak 3 (testvector generated from go)", async () => {
  43. const input = [182, 104, 121, 2, 8, 48, 224, 11, 238, 244, 73, 142, 67,
  44. 205, 166, 27, 10, 223, 142, 209, 10, 46, 171, 110, 239, 68, 111,
  45. 116, 164, 127, 103, 141];
  46. const expectedOut = [191, 235, 249, 254, 70, 24, 106, 244, 212, 163,
  47. 52, 240, 1, 128, 235, 61, 158, 52, 138, 60, 197, 80, 113, 36, 44,
  48. 217, 55, 211, 97, 231, 26, 7];
  49. const inIn = utils.bytesToBits(input);
  50. const witness = await cir.calculateWitness({ "in": inIn }, true);
  51. const stateOut = witness.slice(1, 1+(32*8));
  52. const stateOutBytes = utils.bitsToBytes(stateOut);
  53. // console.log(stateOutBytes, expectedOut);
  54. assert.deepEqual(stateOutBytes, expectedOut);
  55. });
  56. it ("Keccak 4 (testvector generated from go)", async () => {
  57. const input = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  58. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
  59. const expectedOut = [41, 13, 236, 217, 84, 139, 98, 168, 214, 3, 69,
  60. 169, 136, 56, 111, 200, 75, 166, 188, 149, 72, 64, 8, 246, 54, 47,
  61. 147, 22, 14, 243, 229, 99];
  62. const inIn = utils.bytesToBits(input);
  63. const witness = await cir.calculateWitness({ "in": inIn }, true);
  64. const stateOut = witness.slice(1, 1+(32*8));
  65. const stateOutBytes = utils.bitsToBytes(stateOut);
  66. // console.log(stateOutBytes, expectedOut);
  67. assert.deepEqual(stateOutBytes, expectedOut);
  68. });
  69. describe("Keccak256 circuit check with js version", function () {
  70. this.timeout(100000);
  71. it ("Keccak256 circom-js 1", async () => {
  72. let input, inputBits, expectedOut, witness, stateOut, stateOutBytes;
  73. input = Buffer.from("0000000000000000000000000000000000000000000000000000000000000000", "hex");
  74. for(let i=0; i<10; i++) {
  75. inputBits = utils.bytesToBits(input);
  76. let jsOutRaw = keccak256(input);
  77. expectedOut = utils.bufferToBytes(jsOutRaw);
  78. console.log(i, "in:", input.toString('hex'), "\n out:", jsOutRaw.toString('hex'));
  79. witness = await cir.calculateWitness({ "in": inputBits }, true);
  80. stateOut = witness.slice(1, 1+(32*8));
  81. stateOutBytes = utils.bitsToBytes(stateOut);
  82. assert.deepEqual(stateOutBytes, expectedOut);
  83. // assign output into input for next iteration
  84. input = jsOutRaw;
  85. }
  86. });
  87. });
  88. });
  89. describe("Keccak input: 4bytes, output: 32bytes, full hash test", function () {
  90. this.timeout(100000);
  91. let cir;
  92. before(async () => {
  93. cir = await c_tester(path.join(__dirname, "circuits", "keccak_32_256_test.circom"));
  94. await cir.loadConstraints();
  95. console.log("n_constraints", cir.constraints.length);
  96. });
  97. it ("Keccak inputSize==32bits: 1 (testvector generated from go)", async () => {
  98. const input = [116, 101, 115, 116];
  99. const expectedOut = [156, 34, 255, 95, 33, 240, 184, 27, 17, 62, 99,
  100. 247, 219, 109, 169, 79, 237, 239, 17, 178, 17, 155, 64, 136, 184,
  101. 150, 100, 251, 154, 60, 182, 88];
  102. const inIn = utils.bytesToBits(input);
  103. const witness = await cir.calculateWitness({ "in": inIn }, true);
  104. const stateOut = witness.slice(1, 1+(32*8));
  105. const stateOutBytes = utils.bitsToBytes(stateOut);
  106. // console.log(stateOutBytes, expectedOut);
  107. assert.deepEqual(stateOutBytes, expectedOut);
  108. });
  109. it ("Keccak256 inputSize==32bits, circom-js 1", async () => {
  110. let input, inputBits, expectedOut, witness, stateOut, stateOutBytes;
  111. input = Buffer.from("test");
  112. for(let i=0; i<10; i++) {
  113. inputBits = utils.bytesToBits(input);
  114. let jsOutRaw = keccak256(input);
  115. expectedOut = utils.bufferToBytes(jsOutRaw);
  116. console.log(i, "in:", input.toString('hex'), "\n out:", jsOutRaw.toString('hex'));
  117. witness = await cir.calculateWitness({ "in": inputBits }, true);
  118. stateOut = witness.slice(1, 1+(32*8));
  119. stateOutBytes = utils.bitsToBytes(stateOut);
  120. assert.deepEqual(stateOutBytes, expectedOut);
  121. // assign output[0:4] into input for next iteration
  122. input = jsOutRaw.slice(0, 4);
  123. }
  124. });
  125. });