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.

294 lines
10 KiB

  1. // Package common contains all the common data structures used at the
  2. // hermez-node, zk.go contains the zkSnark inputs used to generate the proof
  3. //nolint:deadcode,structcheck,unused
  4. package common
  5. import "math/big"
  6. // circuit parameters
  7. // absolute maximum of L1 or L2 transactions allowed
  8. type nTx uint32
  9. // merkle tree depth
  10. type nLevels uint32
  11. // absolute maximum of L1 transaction allowed
  12. type maxL1Tx uint32
  13. //absolute maximum of fee transactions allowed
  14. type maxFeeTx uint32
  15. // ZKInputs represents the inputs that will be used to generate the zkSNARK proof
  16. type ZKInputs struct {
  17. //
  18. // General
  19. //
  20. // inputs for final `hashGlobalInputs`
  21. // OldLastIdx is the last index assigned to an account
  22. OldLastIdx *big.Int // uint64 (max nLevels bits)
  23. // OldStateRoot is the current state merkle tree root
  24. OldStateRoot *big.Int // Hash
  25. // GlobalChainID is the blockchain ID (0 for Ethereum mainnet). This value can be get from the smart contract.
  26. GlobalChainID *big.Int // uint16
  27. // FeeIdxs is an array of merkle tree indexes where the coordinator will receive the accumulated fees
  28. FeeIdxs []*big.Int // uint64 (max nLevels bits), len: [maxFeeTx]
  29. // accumulate fees
  30. // FeePlanTokens contains all the tokenIDs for which the fees are being accumulated
  31. FeePlanTokens []*big.Int // uint32 (max 32 bits), len: [maxFeeTx]
  32. //
  33. // Txs (L1&L2)
  34. //
  35. // transaction L1-L2
  36. // TxCompressedData
  37. TxCompressedData []*big.Int // big.Int (max 251 bits), len: [nTx]
  38. // TxCompressedDataV2, only used in L2Txs, in L1Txs is set to 0
  39. TxCompressedDataV2 []*big.Int // big.Int (max 193 bits), len: [nTx]
  40. // FromIdx
  41. FromIdx []*big.Int // uint64 (max nLevels bits), len: [nTx]
  42. // AuxFromIdx is the Idx of the new created account which is consequence of a L1CreateAccountTx
  43. AuxFromIdx []*big.Int // uint64 (max nLevels bits), len: [nTx]
  44. // ToIdx
  45. ToIdx []*big.Int // uint64 (max nLevels bits), len: [nTx]
  46. // AuxToIdx is the Idx of the Tx that has 'toIdx==0', is the coordinator who will find which Idx corresponds to the 'toBJJAy' or 'toEthAddr'
  47. AuxToIdx []*big.Int // uint64 (max nLevels bits), len: [nTx]
  48. // ToBJJAy
  49. ToBJJAy []*big.Int // big.Int, len: [nTx]
  50. // ToEthAddr
  51. ToEthAddr []*big.Int // ethCommon.Address, len: [nTx]
  52. // OnChain determines if is L1 (1/true) or L2 (0/false)
  53. OnChain []*big.Int // bool, len: [nTx]
  54. //
  55. // Txs/L1Txs
  56. //
  57. // NewAccount boolean (0/1) flag set 'true' when L1 tx creates a new account (fromIdx==0)
  58. NewAccount []*big.Int // bool, len: [nTx]
  59. // LoadAmountF encoded as float16
  60. LoadAmountF []*big.Int // uint16, len: [nTx]
  61. // FromEthAddr
  62. FromEthAddr []*big.Int // ethCommon.Address, len: [nTx]
  63. // FromBJJCompressed boolean encoded where each value is a *big.Int
  64. FromBJJCompressed [][256]*big.Int // bool array, len: [nTx][256]
  65. //
  66. // Txs/L2Txs
  67. //
  68. // RqOffset relative transaction position to be linked. Used to perform atomic transactions.
  69. RqOffset []*big.Int // uint8 (max 3 bits), len: [nTx]
  70. // transaction L2 request data
  71. // RqTxCompressedDataV2
  72. RqTxCompressedDataV2 []*big.Int // big.Int (max 251 bits), len: [nTx]
  73. // RqToEthAddr
  74. RqToEthAddr []*big.Int // ethCommon.Address, len: [nTx]
  75. // RqToBJJAy
  76. RqToBJJAy []*big.Int // big.Int, len: [nTx]
  77. // transaction L2 signature
  78. // S
  79. S []*big.Int // big.Int, len: [nTx]
  80. // R8x
  81. R8x []*big.Int // big.Int, len: [nTx]
  82. // R8y
  83. R8y []*big.Int // big.Int, len: [nTx]
  84. //
  85. // State MerkleTree Leafs transitions
  86. //
  87. // state 1, value of the sender (from) account leaf
  88. TokenID1 []*big.Int // uint32, len: [nTx]
  89. Nonce1 []*big.Int // uint64 (max 40 bits), len: [nTx]
  90. Sign1 []*big.Int // bool, len: [nTx]
  91. Ay1 []*big.Int // big.Int, len: [nTx]
  92. Balance1 []*big.Int // big.Int (max 192 bits), len: [nTx]
  93. EthAddr1 []*big.Int // ethCommon.Address, len: [nTx]
  94. Siblings1 [][]*big.Int // big.Int, len: [nTx][nLevels + 1]
  95. // Required for inserts and deletes, values of the CircomProcessorProof (smt insert proof)
  96. IsOld0_1 []*big.Int // bool, len: [nTx]
  97. OldKey1 []*big.Int // uint64 (max 40 bits), len: [nTx]
  98. OldValue1 []*big.Int // Hash, len: [nTx]
  99. // state 2, value of the receiver (to) account leaf
  100. // if Tx is an Exit, state 2 is used for the Exit Merkle Proof
  101. TokenID2 []*big.Int // uint32, len: [nTx]
  102. Nonce2 []*big.Int // uint64 (max 40 bits), len: [nTx]
  103. Sign2 []*big.Int // bool, len: [nTx]
  104. Ay2 []*big.Int // big.Int, len: [nTx]
  105. Balance2 []*big.Int // big.Int (max 192 bits), len: [nTx]
  106. EthAddr2 []*big.Int // ethCommon.Address, len: [nTx]
  107. Siblings2 [][]*big.Int // big.Int, len: [nTx][nLevels + 1]
  108. // newExit determines if an exit transaction has to create a new leaf in the exit tree
  109. NewExit []*big.Int // bool, len: [nTx]
  110. // Required for inserts and deletes, values of the CircomProcessorProof (smt insert proof)
  111. IsOld0_2 []*big.Int // bool, len: [nTx]
  112. OldKey2 []*big.Int // uint64 (max 40 bits), len: [nTx]
  113. OldValue2 []*big.Int // Hash, len: [nTx]
  114. // state 3, value of the account leaf receiver of the Fees
  115. // fee tx
  116. // State fees
  117. TokenID3 []*big.Int // uint32, len: [maxFeeTx]
  118. Nonce3 []*big.Int // uint64 (max 40 bits), len: [maxFeeTx]
  119. Sign3 []*big.Int // bool, len: [maxFeeTx]
  120. Ay3 []*big.Int // big.Int, len: [maxFeeTx]
  121. Balance3 []*big.Int // big.Int (max 192 bits), len: [maxFeeTx]
  122. EthAddr3 []*big.Int // ethCommon.Address, len: [maxFeeTx]
  123. Siblings3 [][]*big.Int // Hash, len: [maxFeeTx][nLevels + 1]
  124. //
  125. // Intermediate States
  126. //
  127. // Intermediate States to parallelize witness computation
  128. // Note: the Intermediate States (IS) of the last transaction does not
  129. // exist. Meaning that transaction 3 (4th) will fill the parameters
  130. // FromIdx[3] and ISOnChain[3], but last transaction (nTx-1) will fill
  131. // FromIdx[nTx-1] but will not fill ISOnChain. That's why IS have
  132. // length of nTx-1, while the other parameters have length of nTx.
  133. // Last transaction does not need intermediate state since its output
  134. // will not be used.
  135. // decode-tx
  136. // ISOnChain indicates if tx is L1 (true) or L2 (false)
  137. ISOnChain []*big.Int // bool, len: [nTx - 1]
  138. // ISOutIdx current index account for each Tx
  139. ISOutIdx []*big.Int // uint64 (max nLevels bits), len: [nTx - 1]
  140. // rollup-tx
  141. // ISStateRoot root at the moment of the Tx, the state root value once the Tx is processed into the state tree
  142. ISStateRoot []*big.Int // Hash, len: [nTx - 1]
  143. // ISExitTree root at the moment of the Tx the value once the Tx is processed into the exit tree
  144. ISExitRoot []*big.Int // Hash, len: [nTx - 1]
  145. // ISAccFeeOut accumulated fees once the Tx is processed
  146. ISAccFeeOut [][]*big.Int // big.Int, len: [nTx - 1][maxFeeTx]
  147. // fee-tx
  148. // ISStateRootFee root at the moment of the Tx, the state root value once the Tx is processed into the state tree
  149. ISStateRootFee []*big.Int // Hash, len: [maxFeeTx - 1]
  150. // ISInitStateRootFee state root once all L1-L2 tx are processed (before computing the fees-tx)
  151. ISInitStateRootFee *big.Int // Hash
  152. // ISFinalAccFee final accumulated fees (before computing the fees-tx)
  153. ISFinalAccFee []*big.Int // big.Int, len: [maxFeeTx - 1]
  154. }
  155. // NewZKInputs returns a pointer to an initialized struct of ZKInputs
  156. func NewZKInputs(nTx, maxFeeTx, nLevels int) *ZKInputs {
  157. zki := &ZKInputs{}
  158. // General
  159. zki.OldLastIdx = big.NewInt(0)
  160. zki.OldStateRoot = big.NewInt(0)
  161. zki.GlobalChainID = big.NewInt(0)
  162. zki.FeeIdxs = newSlice(maxFeeTx)
  163. zki.FeePlanTokens = newSlice(maxFeeTx)
  164. // Txs
  165. zki.TxCompressedData = newSlice(nTx)
  166. zki.TxCompressedDataV2 = newSlice(nTx)
  167. zki.FromIdx = newSlice(nTx)
  168. zki.AuxFromIdx = newSlice(nTx)
  169. zki.ToIdx = newSlice(nTx)
  170. zki.AuxToIdx = newSlice(nTx)
  171. zki.ToBJJAy = newSlice(nTx)
  172. zki.ToEthAddr = newSlice(nTx)
  173. zki.OnChain = newSlice(nTx)
  174. zki.NewAccount = newSlice(nTx)
  175. // L1
  176. zki.LoadAmountF = newSlice(nTx)
  177. zki.FromEthAddr = newSlice(nTx)
  178. zki.FromBJJCompressed = make([][256]*big.Int, nTx)
  179. for i := 0; i < len(zki.FromBJJCompressed); i++ {
  180. // zki.FromBJJCompressed[i] = newSlice(256)
  181. for j := 0; j < 256; j++ {
  182. zki.FromBJJCompressed[i][j] = big.NewInt(0)
  183. }
  184. }
  185. // L2
  186. zki.RqOffset = newSlice(nTx)
  187. zki.RqTxCompressedDataV2 = newSlice(nTx)
  188. zki.RqToEthAddr = newSlice(nTx)
  189. zki.RqToBJJAy = newSlice(nTx)
  190. zki.S = newSlice(nTx)
  191. zki.R8x = newSlice(nTx)
  192. zki.R8y = newSlice(nTx)
  193. // State MerkleTree Leafs transitions
  194. zki.TokenID1 = newSlice(nTx)
  195. zki.Nonce1 = newSlice(nTx)
  196. zki.Sign1 = newSlice(nTx)
  197. zki.Ay1 = newSlice(nTx)
  198. zki.Balance1 = newSlice(nTx)
  199. zki.EthAddr1 = newSlice(nTx)
  200. zki.Siblings1 = make([][]*big.Int, nTx)
  201. for i := 0; i < len(zki.Siblings1); i++ {
  202. zki.Siblings1[i] = newSlice(nLevels + 1)
  203. }
  204. zki.IsOld0_1 = newSlice(nTx)
  205. zki.OldKey1 = newSlice(nTx)
  206. zki.OldValue1 = newSlice(nTx)
  207. zki.TokenID2 = newSlice(nTx)
  208. zki.Nonce2 = newSlice(nTx)
  209. zki.Sign2 = newSlice(nTx)
  210. zki.Ay2 = newSlice(nTx)
  211. zki.Balance2 = newSlice(nTx)
  212. zki.EthAddr2 = newSlice(nTx)
  213. zki.Siblings2 = make([][]*big.Int, nTx)
  214. for i := 0; i < len(zki.Siblings2); i++ {
  215. zki.Siblings2[i] = newSlice(nLevels + 1)
  216. }
  217. zki.NewExit = newSlice(nTx)
  218. zki.IsOld0_2 = newSlice(nTx)
  219. zki.OldKey2 = newSlice(nTx)
  220. zki.OldValue2 = newSlice(nTx)
  221. zki.TokenID3 = newSlice(maxFeeTx)
  222. zki.Nonce3 = newSlice(maxFeeTx)
  223. zki.Sign3 = newSlice(maxFeeTx)
  224. zki.Ay3 = newSlice(maxFeeTx)
  225. zki.Balance3 = newSlice(maxFeeTx)
  226. zki.EthAddr3 = newSlice(maxFeeTx)
  227. zki.Siblings3 = make([][]*big.Int, maxFeeTx)
  228. for i := 0; i < len(zki.Siblings3); i++ {
  229. zki.Siblings3[i] = newSlice(nLevels + 1)
  230. }
  231. // Intermediate States
  232. zki.ISOnChain = newSlice(nTx - 1)
  233. zki.ISOutIdx = newSlice(nTx - 1)
  234. zki.ISStateRoot = newSlice(nTx - 1)
  235. zki.ISExitRoot = newSlice(nTx - 1)
  236. zki.ISAccFeeOut = make([][]*big.Int, nTx-1)
  237. for i := 0; i < len(zki.ISAccFeeOut); i++ {
  238. zki.ISAccFeeOut[i] = newSlice(maxFeeTx)
  239. }
  240. zki.ISStateRootFee = newSlice(maxFeeTx - 1)
  241. zki.ISInitStateRootFee = big.NewInt(0)
  242. zki.ISFinalAccFee = newSlice(maxFeeTx - 1)
  243. return zki
  244. }
  245. // newSlice returns a []*big.Int slice of length n with values initialized at
  246. // 0.
  247. // Is used to initialize all *big.Ints of the ZKInputs data structure, so when
  248. // the transactions are processed and the ZKInputs filled, there is no need to
  249. // set all the elements, and if a transaction does not use a parameter, can be
  250. // leaved as it is in the ZKInputs, as will be 0, so later when using the
  251. // ZKInputs to generate the zkSnark proof there is no 'nil'/'null' values.
  252. func newSlice(n int) []*big.Int {
  253. s := make([]*big.Int, n)
  254. for i := 0; i < len(s); i++ {
  255. s[i] = big.NewInt(0)
  256. }
  257. return s
  258. }