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.

286 lines
9.8 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. // decode-tx
  129. // ISOnChain indicates if tx is L1 (true) or L2 (false)
  130. ISOnChain []*big.Int // bool, len: [nTx - 1]
  131. // ISOutIdx current index account for each Tx
  132. ISOutIdx []*big.Int // uint64 (max nLevels bits), len: [nTx - 1]
  133. // rollup-tx
  134. // ISStateRoot root at the moment of the Tx, the state root value once the Tx is processed into the state tree
  135. ISStateRoot []*big.Int // Hash, len: [nTx - 1]
  136. // ISExitTree root at the moment of the Tx the value once the Tx is processed into the exit tree
  137. ISExitRoot []*big.Int // Hash, len: [nTx - 1]
  138. // ISAccFeeOut accumulated fees once the Tx is processed
  139. ISAccFeeOut [][]*big.Int // big.Int, len: [nTx - 1][maxFeeTx]
  140. // fee-tx
  141. // ISStateRootFee root at the moment of the Tx, the state root value once the Tx is processed into the state tree
  142. ISStateRootFee []*big.Int // Hash, len: [maxFeeTx - 1]
  143. // ISInitStateRootFee state root once all L1-L2 tx are processed (before computing the fees-tx)
  144. ISInitStateRootFee *big.Int // Hash
  145. // ISFinalAccFee final accumulated fees (before computing the fees-tx)
  146. ISFinalAccFee []*big.Int // big.Int, len: [maxFeeTx - 1]
  147. }
  148. // NewZKInputs returns a pointer to an initialized struct of ZKInputs
  149. func NewZKInputs(nTx, maxFeeTx, nLevels int) *ZKInputs {
  150. zki := &ZKInputs{}
  151. // General
  152. zki.OldLastIdx = big.NewInt(0)
  153. zki.OldStateRoot = big.NewInt(0)
  154. zki.GlobalChainID = big.NewInt(0)
  155. zki.FeeIdxs = newSlice(maxFeeTx)
  156. zki.FeePlanTokens = newSlice(maxFeeTx)
  157. // Txs
  158. zki.TxCompressedData = newSlice(nTx)
  159. zki.TxCompressedDataV2 = newSlice(nTx)
  160. zki.FromIdx = newSlice(nTx)
  161. zki.AuxFromIdx = newSlice(nTx)
  162. zki.ToIdx = newSlice(nTx)
  163. zki.AuxToIdx = newSlice(nTx)
  164. zki.ToBJJAy = newSlice(nTx)
  165. zki.ToEthAddr = newSlice(nTx)
  166. zki.OnChain = newSlice(nTx)
  167. zki.NewAccount = newSlice(nTx)
  168. // L1
  169. zki.LoadAmountF = newSlice(nTx)
  170. zki.FromEthAddr = newSlice(nTx)
  171. zki.FromBJJCompressed = make([][256]*big.Int, nTx)
  172. for i := 0; i < len(zki.FromBJJCompressed); i++ {
  173. // zki.FromBJJCompressed[i] = newSlice(256)
  174. for j := 0; j < 256; j++ {
  175. zki.FromBJJCompressed[i][j] = big.NewInt(0)
  176. }
  177. }
  178. // L2
  179. zki.RqOffset = newSlice(nTx)
  180. zki.RqTxCompressedDataV2 = newSlice(nTx)
  181. zki.RqToEthAddr = newSlice(nTx)
  182. zki.RqToBJJAy = newSlice(nTx)
  183. zki.S = newSlice(nTx)
  184. zki.R8x = newSlice(nTx)
  185. zki.R8y = newSlice(nTx)
  186. // State MerkleTree Leafs transitions
  187. zki.TokenID1 = newSlice(nTx)
  188. zki.Nonce1 = newSlice(nTx)
  189. zki.Sign1 = newSlice(nTx)
  190. zki.Ay1 = newSlice(nTx)
  191. zki.Balance1 = newSlice(nTx)
  192. zki.EthAddr1 = newSlice(nTx)
  193. zki.Siblings1 = make([][]*big.Int, nTx)
  194. for i := 0; i < len(zki.Siblings1); i++ {
  195. zki.Siblings1[i] = newSlice(nLevels + 1)
  196. }
  197. zki.IsOld0_1 = newSlice(nTx)
  198. zki.OldKey1 = newSlice(nTx)
  199. zki.OldValue1 = newSlice(nTx)
  200. zki.TokenID2 = newSlice(nTx)
  201. zki.Nonce2 = newSlice(nTx)
  202. zki.Sign2 = newSlice(nTx)
  203. zki.Ay2 = newSlice(nTx)
  204. zki.Balance2 = newSlice(nTx)
  205. zki.EthAddr2 = newSlice(nTx)
  206. zki.Siblings2 = make([][]*big.Int, nTx)
  207. for i := 0; i < len(zki.Siblings2); i++ {
  208. zki.Siblings2[i] = newSlice(nLevels + 1)
  209. }
  210. zki.NewExit = newSlice(nTx)
  211. zki.IsOld0_2 = newSlice(nTx)
  212. zki.OldKey2 = newSlice(nTx)
  213. zki.OldValue2 = newSlice(nTx)
  214. zki.TokenID3 = newSlice(maxFeeTx)
  215. zki.Nonce3 = newSlice(maxFeeTx)
  216. zki.Sign3 = newSlice(maxFeeTx)
  217. zki.Ay3 = newSlice(maxFeeTx)
  218. zki.Balance3 = newSlice(maxFeeTx)
  219. zki.EthAddr3 = newSlice(maxFeeTx)
  220. zki.Siblings3 = make([][]*big.Int, maxFeeTx)
  221. for i := 0; i < len(zki.Siblings3); i++ {
  222. zki.Siblings3[i] = newSlice(nLevels + 1)
  223. }
  224. // Intermediate States
  225. zki.ISOnChain = newSlice(nTx - 1)
  226. zki.ISOutIdx = newSlice(nTx - 1)
  227. zki.ISStateRoot = newSlice(nTx - 1)
  228. zki.ISExitRoot = newSlice(nTx - 1)
  229. zki.ISAccFeeOut = make([][]*big.Int, nTx-1)
  230. for i := 0; i < len(zki.ISAccFeeOut); i++ {
  231. zki.ISAccFeeOut[i] = newSlice(maxFeeTx)
  232. }
  233. zki.ISStateRootFee = newSlice(maxFeeTx - 1)
  234. zki.ISInitStateRootFee = big.NewInt(0)
  235. zki.ISFinalAccFee = newSlice(maxFeeTx - 1)
  236. return zki
  237. }
  238. // newSlice returns a []*big.Int slice of length n with values initialized at
  239. // 0.
  240. // Is used to initialize all *big.Ints of the ZKInputs data structure, so when
  241. // the transactions are processed and the ZKInputs filled, there is no need to
  242. // set all the elements, and if a transaction does not use a parameter, can be
  243. // leaved as it is in the ZKInputs, as will be 0, so later when using the
  244. // ZKInputs to generate the zkSnark proof there is no 'nil'/'null' values.
  245. func newSlice(n int) []*big.Int {
  246. s := make([]*big.Int, n)
  247. for i := 0; i < len(s); i++ {
  248. s[i] = big.NewInt(0)
  249. }
  250. return s
  251. }