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.

283 lines
12 KiB

  1. const path = require("path");
  2. const chai = require("chai");
  3. const assert = chai.assert;
  4. const wasm_tester = require("circom_tester").wasm;
  5. const c_tester = require("circom_tester").c;
  6. const utils = require("./utils");
  7. describe("Keccak-Pad test", function () {
  8. this.timeout(100000);
  9. it ("Pad (testvector generated from go)", async () => {
  10. const cir = await wasm_tester(path.join(__dirname, "circuits", "pad_test.circom"));
  11. const input =
  12. [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31];
  13. const expectedOut = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  14. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
  15. 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  16. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 0, 0, 0, 0, 0, 0,
  18. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  19. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128];
  20. const stateIn = utils.bytesToBits(input);
  21. const witness = await cir.calculateWitness({ "in": stateIn }, true);
  22. const stateOut = witness.slice(1, 1+(136*8));
  23. const stateOutBytes = utils.bitsToBytes(stateOut);
  24. // console.log(stateOutBytes, expectedOut);
  25. assert.deepEqual(stateOutBytes, expectedOut);
  26. });
  27. });
  28. describe("absorb test", function () {
  29. this.timeout(100000);
  30. let cir;
  31. before(async () => {
  32. // const cir = await wasm_tester(path.join(__dirname, "circuits", "keccakf_test.circom"));
  33. cir = await c_tester(path.join(__dirname, "circuits", "absorb_test.circom"));
  34. await cir.loadConstraints();
  35. console.log("n_constraints", cir.constraints.length);
  36. });
  37. it ("absorb 1 (testvector generated from go)", async () => {
  38. const s = utils.strsToBigInts(["0", "1", "2", "3", "4", "5", "6", "7",
  39. "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18",
  40. "19", "20", "21", "22", "23", "24"]);
  41. const block = utils.strsToBigInts(["0", "1", "2", "3", "4", "5", "6",
  42. "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17",
  43. "18", "19", "20", "21", "22", "23", "24", "25", "26", "27",
  44. "28","29", "30", "31", "1", "0", "0", "0", "0", "0", "0", "0", "0",
  45. "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
  46. "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
  47. "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
  48. "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
  49. "0","0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
  50. "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
  51. "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
  52. "0", "0", "0", "128"]);
  53. const expectedOut = utils.strsToBigInts(["8342348566319207042",
  54. "319359607942176202", "14410076088654599075",
  55. "15666111399434436772", "9558421567405313402",
  56. "3396178318116504023", "794353847439963108",
  57. "12717011319735989377", "3503398863218919239",
  58. "5517201702366862678", "15999361614129160496",
  59. "1325524015888689985", "11971708408118944333",
  60. "14874486179441062217", "12554876384974234666",
  61. "11129975558302206043", "11257826431949606534",
  62. "2740710607956478714", "15000019752453010167",
  63. "15593606854132419294",
  64. "2598425978562809333","8872504799797239246", "1212062965004664308",
  65. "5443427421087086722", "10946808592826700411"]);
  66. const sIn = utils.u64ArrayToBits(s);
  67. const blockIn = utils.bytesToBits(block);
  68. const expectedOutBits = utils.u64ArrayToBits(expectedOut);
  69. const witness = await cir.calculateWitness({ "s": sIn, "block": blockIn }, true);
  70. const stateOut = witness.slice(1, 1+(25*64));
  71. const stateOutU64 = utils.bitsToU64Array(stateOut);
  72. // console.log(stateOutU64, expectedOut);
  73. assert.deepEqual(stateOutU64, expectedOut);
  74. });
  75. it ("absorb 2 (testvector generated from go)", async () => {
  76. const s = utils.strsToBigInts(["8342348566319207042",
  77. "319359607942176202", "14410076088654599075",
  78. "15666111399434436772", "9558421567405313402",
  79. "3396178318116504023", "794353847439963108",
  80. "12717011319735989377", "3503398863218919239",
  81. "5517201702366862678", "15999361614129160496",
  82. "1325524015888689985", "11971708408118944333",
  83. "14874486179441062217", "12554876384974234666",
  84. "11129975558302206043", "11257826431949606534",
  85. "2740710607956478714", "15000019752453010167",
  86. "15593606854132419294",
  87. "2598425978562809333","8872504799797239246", "1212062965004664308",
  88. "5443427421087086722", "10946808592826700411"]);
  89. const block = utils.strsToBigInts(["0", "1", "2", "3", "4", "5", "6",
  90. "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17",
  91. "18", "19", "20", "21", "22", "23", "24", "25", "26", "27",
  92. "28","29", "30", "31", "1", "0", "0", "0", "0", "0", "0", "0", "0",
  93. "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
  94. "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
  95. "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
  96. "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
  97. "0","0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
  98. "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
  99. "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
  100. "0", "0", "0", "128"]);
  101. const expectedOut = utils.strsToBigInts(["8909243822027471379",
  102. "1111840847970088140", "12093072708540612559",
  103. "11255033638786021658", "2082116894939842214",
  104. "12821085060245261575", "6901785969834988344",
  105. "3182430130277914993", "2164708585929408975",
  106. "14402143231999718904", "16231444410553803968",
  107. "1850945423480060493", "12856855675247400303",
  108. "1137248620532111171", "7389129221921446308",
  109. "12932467982741614601", "1350606937385760406",
  110. "10983682292859713641", "10305595434820307765",
  111. "13958651111365489854", "17206620388135196198",
  112. "4238113785249530092", "7230868147643218103", "603011106238724524",
  113. "16480095441097880488"]);
  114. const sIn = utils.u64ArrayToBits(s);
  115. const blockIn = utils.bytesToBits(block);
  116. const expectedOutBits = utils.u64ArrayToBits(expectedOut);
  117. const witness = await cir.calculateWitness({ "s": sIn, "block": blockIn }, true);
  118. const stateOut = witness.slice(1, 1+(25*64));
  119. const stateOutU64 = utils.bitsToU64Array(stateOut);
  120. // console.log(stateOutU64, expectedOut);
  121. assert.deepEqual(stateOutU64, expectedOut);
  122. });
  123. });
  124. describe("Keccak-Final test", function () {
  125. this.timeout(100000);
  126. let cir;
  127. before(async () => {
  128. cir = await c_tester(path.join(__dirname, "circuits", "final_test.circom"));
  129. await cir.loadConstraints();
  130. console.log("n_constraints", cir.constraints.length);
  131. });
  132. it ("Final 1 (testvector generated from go)", async () => {
  133. const input =
  134. [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31];
  135. const expectedOut = utils.strsToBigInts(["16953415415620100490",
  136. "7495738965189503699", "12723370805759944158",
  137. "3295955328722933810", "12121371508560456016",
  138. "174876831679863147", "15944933357501475584",
  139. "7502339663607726274", "12048918224562833898",
  140. "16715284461100269102", "15582559130083209842",
  141. "1743886467337678829", "2424196198791253761",
  142. "1116417308245482383", "10367365997906434042",
  143. "1849801549382613906", "13294939539683415102",
  144. "4478091053375708790", "2969967870313332958",
  145. "14618962068930014237", "2721742233407503451",
  146. "12003265593030191290", "8109318293656735684",
  147. "6346795302983965746", "12210038122000333046"]);
  148. const inIn = utils.bytesToBits(input);
  149. const expectedOutBits = utils.u64ArrayToBits(expectedOut);
  150. const witness = await cir.calculateWitness({ "in": inIn }, true);
  151. const stateOut = witness.slice(1, 1+(25*64));
  152. const stateOutU64 = utils.bitsToU64Array(stateOut);
  153. // console.log(stateOutU64, expectedOut);
  154. assert.deepEqual(stateOutU64, expectedOut);
  155. });
  156. it ("Final 2 (testvector generated from go)", async () => {
  157. const input = utils.strsToBigInts(["254", "254", "254", "254", "254",
  158. "254", "254", "254", "254", "254", "254", "254", "254", "254",
  159. "254", "254", "254", "254", "254", "254", "254", "254", "254",
  160. "254", "254", "254", "254", "254", "254", "254", "254", "254"]);
  161. const expectedOut = utils.strsToBigInts(["16852464862333879129",
  162. "9588646233186836430", "693207875935078627", "6545910230963382296",
  163. "3599194178366828471", "13130606490077331384",
  164. "10374798023615518933", "7285576075118720444",
  165. "4097382401500492461", "3968685317688314807",
  166. "3350659309646210303", "640023485234837464", "2550030127986774041",
  167. "8948768022010378840", "10678227883444996205",
  168. "1395278318096830339", "2744077813166753978",
  169. "13362598477502046010", "14601579319881128511",
  170. "4070707967569603186", "16833768365875755098",
  171. "1486295134719870048", "9161068934282437999",
  172. "8245604251371175619", "8421994351908003183"]);
  173. const inIn = utils.bytesToBits(input);
  174. const expectedOutBits = utils.u64ArrayToBits(expectedOut);
  175. const witness = await cir.calculateWitness({ "in": inIn }, true);
  176. const stateOut = witness.slice(1, 1+(25*64));
  177. const stateOutU64 = utils.bitsToU64Array(stateOut);
  178. // console.log(stateOutU64, expectedOut);
  179. assert.deepEqual(stateOutU64, expectedOut);
  180. });
  181. });
  182. describe("Keccak-Squeeze test", function () {
  183. this.timeout(100000);
  184. let cir;
  185. before(async () => {
  186. cir = await c_tester(path.join(__dirname, "circuits", "squeeze_test.circom"));
  187. await cir.loadConstraints();
  188. console.log("n_constraints", cir.constraints.length);
  189. });
  190. it ("Squeeze 1 (testvector generated from go)", async () => {
  191. const input = utils.strsToBigInts(["16852464862333879129",
  192. "9588646233186836430", "693207875935078627", "6545910230963382296",
  193. "3599194178366828471", "13130606490077331384",
  194. "10374798023615518933", "7285576075118720444",
  195. "4097382401500492461", "3968685317688314807",
  196. "3350659309646210303", "640023485234837464", "2550030127986774041",
  197. "8948768022010378840", "10678227883444996205",
  198. "1395278318096830339", "2744077813166753978",
  199. "13362598477502046010", "14601579319881128511",
  200. "4070707967569603186", "16833768365875755098",
  201. "1486295134719870048", "9161068934282437999",
  202. "8245604251371175619", "8421994351908003183"]);
  203. const expectedOut = [89, 195, 41, 13, 129, 251, 223, 233, 206, 31, 253,
  204. 61, 242, 182, 17, 133, 227, 8, 157, 240, 227, 196, 158, 9, 24, 232,
  205. 42, 96, 172, 190, 215, 90];
  206. const inIn = utils.u64ArrayToBits(input);
  207. const expectedOutBits = utils.bytesToBits(expectedOut);
  208. const witness = await cir.calculateWitness({ "s": inIn }, true);
  209. const stateOut = witness.slice(1, 1+(32*8));
  210. const stateOutBytes = utils.bitsToBytes(stateOut);
  211. // console.log(stateOutBytes, expectedOut);
  212. assert.deepEqual(stateOutBytes, expectedOut);
  213. });
  214. it ("Squeeze 2 (testvector generated from go)", async () => {
  215. const input = utils.strsToBigInts(["16953415415620100490",
  216. "7495738965189503699", "12723370805759944158",
  217. "3295955328722933810", "12121371508560456016",
  218. "174876831679863147", "15944933357501475584",
  219. "7502339663607726274", "12048918224562833898",
  220. "16715284461100269102", "15582559130083209842",
  221. "1743886467337678829", "2424196198791253761",
  222. "1116417308245482383", "10367365997906434042",
  223. "1849801549382613906", "13294939539683415102",
  224. "4478091053375708790", "2969967870313332958",
  225. "14618962068930014237", "2721742233407503451",
  226. "12003265593030191290", "8109318293656735684",
  227. "6346795302983965746", "12210038122000333046"]);
  228. const expectedOut = [138, 225, 170, 89, 127, 161, 70, 235, 211, 170,
  229. 44, 237, 223, 54, 6, 104, 222, 165, 229, 38, 86, 126, 146, 176, 50,
  230. 24, 22, 164, 232, 149, 189, 45];
  231. const inIn = utils.u64ArrayToBits(input);
  232. const expectedOutBits = utils.bytesToBits(expectedOut);
  233. const witness = await cir.calculateWitness({ "s": inIn }, true);
  234. const stateOut = witness.slice(1, 1+(32*8));
  235. const stateOutBytes = utils.bitsToBytes(stateOut);
  236. // console.log(stateOutBytes, expectedOut);
  237. assert.deepEqual(stateOutBytes, expectedOut);
  238. });
  239. });