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.

159 lines
6.6 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. describe("keccakfRound test", function () {
  7. this.timeout(100000);
  8. // apt install nlohmann-json3-dev
  9. // apt install nasm
  10. // apt install libgmp3-dev
  11. it ("keccakfRound (testvector generated from go)", async () => {
  12. const cir = await c_tester(path.join(__dirname, "circuits", "keccakfRound0_test.circom"));
  13. await cir.loadConstraints();
  14. // console.log("n_constraints", cir.constraints.length);
  15. const input =
  16. [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];
  17. const expectedOut = utils.strsToBigInts(["26388279066651",
  18. "246290629787648", "26388279902208", "25165850", "246290605457408",
  19. "7784628352", "844424965783552", "2305843009213694083",
  20. "844432714760192", "2305843009249345539", "637534226", "14848",
  21. "641204224", "14354", "3670528", "6308236288", "2130304761856",
  22. "648518346341354496", "6309216256", "648520476645130240",
  23. "4611706359392501763", "792677514882318336", "20340965113972",
  24. "4611732197915754499", "792633534417207412"]);
  25. const stateIn = utils.u64ArrayToBits(input);
  26. const expectedOutBits = utils.u64ArrayToBits(expectedOut);
  27. const witness = await cir.calculateWitness({ "in": stateIn }, true);
  28. const stateOut = witness.slice(1, 1+(25*64));
  29. const stateOutU64 = utils.bitsToU64Array(stateOut);
  30. // console.log(stateOutU64, expectedOut);
  31. assert.deepEqual(stateOutU64, expectedOut);
  32. });
  33. it ("keccakfRound 20 (testvector generated from go)", async () => {
  34. const cir = await c_tester(path.join(__dirname, "circuits", "keccakfRound20_test.circom"));
  35. await cir.loadConstraints();
  36. // console.log("n_constraints", cir.constraints.length);
  37. const input = utils.strsToBigInts(["26388279066651", "246290629787648",
  38. "26388279902208", "25165850", "246290605457408", "7784628352",
  39. "844424965783552", "2305843009213694083", "844432714760192",
  40. "2305843009249345539", "637534226", "14848", "641204224", "14354",
  41. "3670528", "6308236288", "2130304761856", "648518346341354496",
  42. "6309216256", "648520476645130240", "4611706359392501763",
  43. "792677514882318336", "20340965113972", "4611732197915754499",
  44. "792633534417207412"]);
  45. const expectedOut = utils.strsToBigInts(["17728382861289829725",
  46. "13654073086381141005", "9912591532945168756",
  47. "2030068283137172501", "5084683018496047808", "151244976540463006",
  48. "11718217461613725815", "11636071286320763433",
  49. "15039144509240642782", "11629028282864249197",
  50. "2594633730779457624", "14005558505838459171",
  51. "4612881094252610438", "2828009553220809993",
  52. "4838578484623267135", "1006588603063111352",
  53. "11109191860075454495", "1187545859779038208",
  54. "14661669042642437042", "5345317080454741069",
  55. "8196674451365552863", "635818354583088260",
  56. "13515759754032305626", "1708499319988748543",
  57. "7509292798507899312"]);
  58. const stateIn = utils.u64ArrayToBits(input);
  59. const expectedOutBits = utils.u64ArrayToBits(expectedOut);
  60. const witness = await cir.calculateWitness({ "in": stateIn }, true);
  61. const stateOut = witness.slice(1, 1+(25*64));
  62. const stateOutU64 = utils.bitsToU64Array(stateOut);
  63. // console.log(stateOutU64, expectedOut);
  64. assert.deepEqual(stateOutU64, expectedOut);
  65. });
  66. });
  67. describe("keccakf test", function () {
  68. this.timeout(100000);
  69. let cir;
  70. before(async () => {
  71. cir = await c_tester(path.join(__dirname, "circuits", "keccakf_test.circom"));
  72. await cir.loadConstraints();
  73. console.log("n_constraints", cir.constraints.length);
  74. });
  75. it ("keccakf 1 (testvector generated from go)", async () => {
  76. const input =
  77. [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];
  78. const expectedOut = utils.strsToBigInts(["9472389783892099349",
  79. "2159377575142921216", "17826682512249813373",
  80. "2325963263767348549", "15086930817298358378",
  81. "11661812091723830419", "3517755057770134847",
  82. "5223775837645169598", "933274647126506074", "3451250694486589320",
  83. "825065683101361807", "6192414258352188799",
  84. "14426505790672879210", "3326742392640380689",
  85. "16749975585634164134", "17847697619892908514",
  86. "11598434253200954839", "6049795840392747215",
  87. "8610635351954084385", "18234131770974529925",
  88. "15330347418010067760", "12047099911907354591",
  89. "4763389569697138851", "6779624089296570504",
  90. "15083668107635345971"]);
  91. const stateIn = utils.u64ArrayToBits(input);
  92. const expectedOutBits = utils.u64ArrayToBits(expectedOut);
  93. const witness = await cir.calculateWitness({ "in": stateIn }, true);
  94. const stateOut = witness.slice(1, 1+(25*64));
  95. const stateOutU64 = utils.bitsToU64Array(stateOut);
  96. // console.log(stateOutU64, expectedOut);
  97. assert.deepEqual(stateOutU64, expectedOut);
  98. });
  99. it ("keccakf 2 (testvector generated from go)", async () => {
  100. const input = utils.strsToBigInts(["9472389783892099349",
  101. "2159377575142921216", "17826682512249813373",
  102. "2325963263767348549", "15086930817298358378",
  103. "11661812091723830419", "3517755057770134847",
  104. "5223775837645169598", "933274647126506074", "3451250694486589320",
  105. "825065683101361807", "6192414258352188799",
  106. "14426505790672879210", "3326742392640380689",
  107. "16749975585634164134", "17847697619892908514",
  108. "11598434253200954839", "6049795840392747215",
  109. "8610635351954084385", "18234131770974529925",
  110. "15330347418010067760", "12047099911907354591",
  111. "4763389569697138851", "6779624089296570504",
  112. "15083668107635345971"]);
  113. const expectedOut = utils.strsToBigInts(["269318771259381490",
  114. "15892848561416382510", "12485559500958802382",
  115. "4360182510883008729", "14284025675983944434",
  116. "8800366419087562177", "7881853509112258378",
  117. "9503857914080778528", "17110477940977988953",
  118. "13825318756568052601", "11460650932194163315",
  119. "13272167288297399439", "13599957064256729412",
  120. "12730838251751851758", "13736647180617564382",
  121. "5651695613583298166", "15496251216716036782",
  122. "9748494184433838858", "3637745438296580159",
  123. "3821184813198767406", "15603239432236101315",
  124. "3726326332491237029", "7819962668913661099",
  125. "2285898735263816116", "13518516210247555620"]);
  126. const stateIn = utils.u64ArrayToBits(input);
  127. const expectedOutBits = utils.u64ArrayToBits(expectedOut);
  128. const witness = await cir.calculateWitness({ "in": stateIn }, true);
  129. const stateOut = witness.slice(1, 1+(25*64));
  130. const stateOutU64 = utils.bitsToU64Array(stateOut);
  131. // console.log(stateOutU64, expectedOut);
  132. assert.deepEqual(stateOutU64, expectedOut);
  133. });
  134. });