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.

187 lines
5.2 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. // Copyright (c) 2018 Jordi Baylina
  2. // License: LGPL-3.0+
  3. //
  4. const Contract = require("./evmasm");
  5. const { unstringifyBigInts } = require("ffjavascript").utils;
  6. const Web3Utils = require("web3-utils");
  7. const { C:K, M } = unstringifyBigInts(require("./poseidon_constants.json"));
  8. const N_ROUNDS_F = 8;
  9. const N_ROUNDS_P = [56, 57, 56, 60, 60, 63, 64, 63];
  10. function toHex256(a) {
  11. let S = a.toString(16);
  12. while (S.length < 64) S="0"+S;
  13. return "0x" + S;
  14. }
  15. function createCode(nInputs) {
  16. if (( nInputs<1) || (nInputs>8)) throw new Error("Invalid number of inputs. Must be 1<=nInputs<=8");
  17. const t = nInputs + 1;
  18. const nRoundsF = N_ROUNDS_F;
  19. const nRoundsP = N_ROUNDS_P[t - 2];
  20. const C = new Contract();
  21. function saveM() {
  22. for (let i=0; i<t; i++) {
  23. for (let j=0; j<t; j++) {
  24. C.push(toHex256(M[t-2][j][i]));
  25. C.push((1+i*t+j)*32);
  26. C.mstore();
  27. }
  28. }
  29. }
  30. function ark(r) { // st, q
  31. for (let i=0; i<t; i++) {
  32. C.dup(t); // q, st, q
  33. C.push(toHex256(K[t-2][r*t+i])); // K, q, st, q
  34. C.dup(2+i); // st[i], K, q, st, q
  35. C.addmod(); // newSt[i], st, q
  36. C.swap(1 + i); // xx, st, q
  37. C.pop();
  38. }
  39. }
  40. function sigma(p) {
  41. // sq, q
  42. C.dup(t); // q, st, q
  43. C.dup(1+p); // st[p] , q , st, q
  44. C.dup(1); // q, st[p] , q , st, q
  45. C.dup(0); // q, q, st[p] , q , st, q
  46. C.dup(2); // st[p] , q, q, st[p] , q , st, q
  47. C.dup(0); // st[p] , st[p] , q, q, st[p] , q , st, q
  48. C.mulmod(); // st2[p], q, st[p] , q , st, q
  49. C.dup(0); // st2[p], st2[p], q, st[p] , q , st, q
  50. C.mulmod(); // st4[p], st[p] , q , st, q
  51. C.mulmod(); // st5[p], st, q
  52. C.swap(1+p);
  53. C.pop(); // newst, q
  54. }
  55. function mix() {
  56. C.label("mix");
  57. for (let i=0; i<t; i++) {
  58. for (let j=0; j<t; j++) {
  59. if (j==0) {
  60. C.dup(i+t); // q, newSt, oldSt, q
  61. C.push((1+i*t+j)*32);
  62. C.mload(); // M, q, newSt, oldSt, q
  63. C.dup(2+i+j); // oldSt[j], M, q, newSt, oldSt, q
  64. C.mulmod(); // acc, newSt, oldSt, q
  65. } else {
  66. C.dup(1+i+t); // q, acc, newSt, oldSt, q
  67. C.push((1+i*t+j)*32);
  68. C.mload(); // M, q, acc, newSt, oldSt, q
  69. C.dup(3+i+j); // oldSt[j], M, q, acc, newSt, oldSt, q
  70. C.mulmod(); // aux, acc, newSt, oldSt, q
  71. C.dup(2+i+t); // q, aux, acc, newSt, oldSt, q
  72. C.swap(2); // acc, aux, q, newSt, oldSt, q
  73. C.addmod(); // acc, newSt, oldSt, q
  74. }
  75. }
  76. }
  77. for (let i=0; i<t; i++) {
  78. C.swap((t -i) + (t -i-1));
  79. C.pop();
  80. }
  81. C.push(0);
  82. C.mload();
  83. C.jmp();
  84. }
  85. // Check selector
  86. C.push("0x0100000000000000000000000000000000000000000000000000000000");
  87. C.push(0);
  88. C.calldataload();
  89. C.div();
  90. C.push(Web3Utils.keccak256(`poseidon(uint256[${nInputs}])`).slice(0, 10)); // poseidon(uint256[])
  91. C.eq();
  92. C.jmpi("start");
  93. C.invalid();
  94. C.label("start");
  95. saveM();
  96. C.push("0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001"); // q
  97. // Load t values from the call data.
  98. // The function has a single array param param
  99. // [Selector (4)] [item1 (32)] [item2 (32)] ....
  100. // Stack positions 0-nInputs.
  101. for (let i=0; i<t; i++) {
  102. C.push(0x04+(0x20*(nInputs-i)));
  103. C.calldataload();
  104. }
  105. for (let i=0; i<nRoundsF+nRoundsP-1; i++) {
  106. ark(i);
  107. if ((i<nRoundsF/2) || (i>=nRoundsP+nRoundsF/2)) {
  108. for (let j=0; j<t; j++) {
  109. sigma(j);
  110. }
  111. } else {
  112. sigma(0);
  113. }
  114. const strLabel = "aferMix"+i;
  115. C._pushLabel(strLabel);
  116. C.push(0);
  117. C.mstore();
  118. C.jmp("mix");
  119. C.label(strLabel);
  120. }
  121. C.push(toHex256(K[t-2][(nRoundsF+nRoundsP-1)*t])); // K, st, q
  122. C.dup(t+1); // q, K, st, q
  123. C.swap(2); // st[0], K, q, st\st[0]
  124. C.addmod(); // st q
  125. sigma(0);
  126. C.push("0x00");
  127. C.mstore(); // Save it to pos 0;
  128. C.push("0x20");
  129. C.push("0x00");
  130. C.return();
  131. mix();
  132. return C.createTxData();
  133. }
  134. function generateABI(nInputs) {
  135. return [
  136. {
  137. "constant": true,
  138. "inputs": [
  139. {
  140. "internalType": `uint256[${nInputs}]`,
  141. "name": "input",
  142. "type": `uint256[${nInputs}]`
  143. }
  144. ],
  145. "name": "poseidon",
  146. "outputs": [
  147. {
  148. "internalType": "uint256",
  149. "name": "",
  150. "type": "uint256"
  151. }
  152. ],
  153. "payable": false,
  154. "stateMutability": "pure",
  155. "type": "function"
  156. }
  157. ];
  158. }
  159. module.exports.generateABI = generateABI;
  160. module.exports.createCode = createCode;