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.

287 lines
9.9 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. // NewAccount boolean (0/1) flag set 'true' when L1 tx creates a new account (fromIdx==0)
  55. NewAccount []*big.Int // bool, len: [nTx]
  56. //
  57. // Txs/L1Txs
  58. //
  59. // transaction L1
  60. // LoadAmountF encoded as float16
  61. LoadAmountF []*big.Int // uint16, len: [nTx]
  62. // FromEthAddr
  63. FromEthAddr []*big.Int // ethCommon.Address, len: [nTx]
  64. // FromBJJCompressed boolean encoded where each value is a *big.Int
  65. FromBJJCompressed [][256]*big.Int // bool array, len: [nTx][256]
  66. //
  67. // Txs/L2Txs
  68. //
  69. // RqOffset relative transaction position to be linked. Used to perform atomic transactions.
  70. RqOffset []*big.Int // uint8 (max 3 bits), len: [nTx]
  71. // transaction L2 request data
  72. // RqTxCompressedDataV2
  73. RqTxCompressedDataV2 []*big.Int // big.Int (max 251 bits), len: [nTx]
  74. // RqToEthAddr
  75. RqToEthAddr []*big.Int // ethCommon.Address, len: [nTx]
  76. // RqToBJJAy
  77. RqToBJJAy []*big.Int // big.Int, len: [nTx]
  78. // transaction L2 signature
  79. // S
  80. S []*big.Int // big.Int, len: [nTx]
  81. // R8x
  82. R8x []*big.Int // big.Int, len: [nTx]
  83. // R8y
  84. R8y []*big.Int // big.Int, len: [nTx]
  85. //
  86. // State MerkleTree Leafs transitions
  87. //
  88. // state 1, value of the sender (from) account leaf
  89. TokenID1 []*big.Int // uint32, len: [nTx]
  90. Nonce1 []*big.Int // uint64 (max 40 bits), len: [nTx]
  91. Sign1 []*big.Int // bool, len: [nTx]
  92. Balance1 []*big.Int // big.Int (max 192 bits), len: [nTx]
  93. Ay1 []*big.Int // big.Int, len: [nTx]
  94. EthAddr1 []*big.Int // ethCommon.Address, len: [nTx]
  95. Siblings1 [][]*big.Int // big.Int, len: [nTx][nLevels + 1]
  96. // Required for inserts and deletes, values of the CircomProcessorProof (smt insert proof)
  97. IsOld0_1 []*big.Int // bool, len: [nTx]
  98. OldKey1 []*big.Int // uint64 (max 40 bits), len: [nTx]
  99. OldValue1 []*big.Int // Hash, len: [nTx]
  100. // state 2, value of the receiver (to) account leaf
  101. // if Tx is an Exit, state 2 is used for the Exit Merkle Proof
  102. TokenID2 []*big.Int // uint32, len: [nTx]
  103. Nonce2 []*big.Int // uint64 (max 40 bits), len: [nTx]
  104. Sign2 []*big.Int // bool, len: [nTx]
  105. Balance2 []*big.Int // big.Int (max 192 bits), len: [nTx]
  106. Ay2 []*big.Int // big.Int, len: [nTx]
  107. EthAddr2 []*big.Int // ethCommon.Address, len: [nTx]
  108. Siblings2 [][]*big.Int // big.Int, len: [nTx][nLevels + 1]
  109. // newExit determines if an exit transaction has to create a new leaf in the exit tree
  110. NewExit []*big.Int // bool, len: [nTx]
  111. // Required for inserts and deletes, values of the CircomProcessorProof (smt insert proof)
  112. IsOld0_2 []*big.Int // bool, len: [nTx]
  113. OldKey2 []*big.Int // uint64 (max 40 bits), len: [nTx]
  114. OldValue2 []*big.Int // Hash, len: [nTx]
  115. // state 3, value of the account leaf receiver of the Fees
  116. // fee tx
  117. // State fees
  118. TokenID3 []*big.Int // uint32, len: [maxFeeTx]
  119. Nonce3 []*big.Int // uint64 (max 40 bits), len: [maxFeeTx]
  120. Sign3 []*big.Int // bool, len: [maxFeeTx]
  121. Balance3 []*big.Int // big.Int (max 192 bits), len: [maxFeeTx]
  122. Ay3 []*big.Int // big.Int, len: [maxFeeTx]
  123. EthAddr3 []*big.Int // ethCommon.Address, len: [maxFeeTx]
  124. Siblings3 [][]*big.Int // Hash, len: [maxFeeTx][nLevels + 1]
  125. //
  126. // Intermediate States
  127. //
  128. // Intermediate States to parallelize witness computation
  129. // decode-tx
  130. // ISOnChain indicates if tx is L1 (true) or L2 (false)
  131. ISOnChain []*big.Int // bool, len: [nTx - 1]
  132. // ISOutIdx current index account for each Tx
  133. ISOutIdx []*big.Int // uint64 (max nLevels bits), len: [nTx - 1]
  134. // rollup-tx
  135. // ISStateRoot root at the moment of the Tx, the state root value once the Tx is processed into the state tree
  136. ISStateRoot []*big.Int // Hash, len: [nTx - 1]
  137. // ISExitTree root at the moment of the Tx the value once the Tx is processed into the exit tree
  138. ISExitRoot []*big.Int // Hash, len: [nTx - 1]
  139. // ISAccFeeOut accumulated fees once the Tx is processed
  140. ISAccFeeOut [][]*big.Int // big.Int, len: [nTx - 1][maxFeeTx]
  141. // fee-tx
  142. // ISStateRootFee root at the moment of the Tx, the state root value once the Tx is processed into the state tree
  143. ISStateRootFee []*big.Int // Hash, len: [maxFeeTx - 1]
  144. // ISInitStateRootFee state root once all L1-L2 tx are processed (before computing the fees-tx)
  145. ISInitStateRootFee *big.Int // Hash
  146. // ISFinalAccFee final accumulated fees (before computing the fees-tx)
  147. ISFinalAccFee []*big.Int // big.Int, len: [maxFeeTx - 1]
  148. }
  149. // NewZKInputs returns a pointer to an initialized struct of ZKInputs
  150. func NewZKInputs(nTx, maxFeeTx, nLevels int) *ZKInputs {
  151. zki := &ZKInputs{}
  152. // General
  153. zki.OldLastIdx = big.NewInt(0)
  154. zki.OldStateRoot = big.NewInt(0)
  155. zki.GlobalChainID = big.NewInt(0)
  156. zki.FeeIdxs = newSlice(maxFeeTx)
  157. zki.FeePlanTokens = newSlice(maxFeeTx)
  158. // Txs
  159. zki.TxCompressedData = newSlice(nTx)
  160. zki.TxCompressedDataV2 = newSlice(nTx)
  161. zki.FromIdx = newSlice(nTx)
  162. zki.AuxFromIdx = newSlice(nTx)
  163. zki.ToIdx = newSlice(nTx)
  164. zki.AuxToIdx = newSlice(nTx)
  165. zki.ToBJJAy = newSlice(nTx)
  166. zki.ToEthAddr = newSlice(nTx)
  167. zki.OnChain = newSlice(nTx)
  168. zki.NewAccount = newSlice(nTx)
  169. // L1
  170. zki.LoadAmountF = newSlice(nTx)
  171. zki.FromEthAddr = newSlice(nTx)
  172. zki.FromBJJCompressed = make([][256]*big.Int, nTx)
  173. for i := 0; i < len(zki.FromBJJCompressed); i++ {
  174. // zki.FromBJJCompressed[i] = newSlice(256)
  175. for j := 0; j < 256; j++ {
  176. zki.FromBJJCompressed[i][j] = big.NewInt(0)
  177. }
  178. }
  179. // L2
  180. zki.RqOffset = newSlice(nTx)
  181. zki.RqTxCompressedDataV2 = newSlice(nTx)
  182. zki.RqToEthAddr = newSlice(nTx)
  183. zki.RqToBJJAy = newSlice(nTx)
  184. zki.S = newSlice(nTx)
  185. zki.R8x = newSlice(nTx)
  186. zki.R8y = newSlice(nTx)
  187. // State MerkleTree Leafs transitions
  188. zki.TokenID1 = newSlice(nTx)
  189. zki.Nonce1 = newSlice(nTx)
  190. zki.Sign1 = newSlice(nTx)
  191. zki.Balance1 = newSlice(nTx)
  192. zki.Ay1 = newSlice(nTx)
  193. zki.EthAddr1 = newSlice(nTx)
  194. zki.Siblings1 = make([][]*big.Int, nTx)
  195. for i := 0; i < len(zki.Siblings1); i++ {
  196. zki.Siblings1[i] = newSlice(nLevels + 1)
  197. }
  198. zki.IsOld0_1 = newSlice(nTx)
  199. zki.OldKey1 = newSlice(nTx)
  200. zki.OldValue1 = newSlice(nTx)
  201. zki.TokenID2 = newSlice(nTx)
  202. zki.Nonce2 = newSlice(nTx)
  203. zki.Sign2 = newSlice(nTx)
  204. zki.Balance2 = newSlice(nTx)
  205. zki.Ay2 = newSlice(nTx)
  206. zki.EthAddr2 = newSlice(nTx)
  207. zki.Siblings2 = make([][]*big.Int, nTx)
  208. for i := 0; i < len(zki.Siblings2); i++ {
  209. zki.Siblings2[i] = newSlice(nLevels + 1)
  210. }
  211. zki.NewExit = newSlice(nTx)
  212. zki.IsOld0_2 = newSlice(nTx)
  213. zki.OldKey2 = newSlice(nTx)
  214. zki.OldValue2 = newSlice(nTx)
  215. zki.TokenID3 = newSlice(maxFeeTx)
  216. zki.Nonce3 = newSlice(maxFeeTx)
  217. zki.Sign3 = newSlice(maxFeeTx)
  218. zki.Balance3 = newSlice(maxFeeTx)
  219. zki.Ay3 = newSlice(maxFeeTx)
  220. zki.EthAddr3 = newSlice(maxFeeTx)
  221. zki.Siblings3 = make([][]*big.Int, maxFeeTx)
  222. for i := 0; i < len(zki.Siblings3); i++ {
  223. zki.Siblings3[i] = newSlice(nLevels + 1)
  224. }
  225. // Intermediate States
  226. zki.ISOnChain = newSlice(nTx - 1)
  227. zki.ISOutIdx = newSlice(nTx - 1)
  228. zki.ISStateRoot = newSlice(nTx - 1)
  229. zki.ISExitRoot = newSlice(nTx - 1)
  230. zki.ISAccFeeOut = make([][]*big.Int, nTx-1)
  231. for i := 0; i < len(zki.ISAccFeeOut); i++ {
  232. zki.ISAccFeeOut[i] = newSlice(maxFeeTx)
  233. }
  234. zki.ISStateRootFee = newSlice(maxFeeTx - 1)
  235. zki.ISInitStateRootFee = big.NewInt(0)
  236. zki.ISFinalAccFee = newSlice(maxFeeTx - 1)
  237. return zki
  238. }
  239. // newSlice returns a []*big.Int slice of length n with values initialized at
  240. // 0.
  241. // Is used to initialize all *big.Ints of the ZKInputs data structure, so when
  242. // the transactions are processed and the ZKInputs filled, there is no need to
  243. // set all the elements, and if a transaction does not use a parameter, can be
  244. // leaved as it is in the ZKInputs, as will be 0, so later when using the
  245. // ZKInputs to generate the zkSnark proof there is no 'nil'/'null' values.
  246. func newSlice(n int) []*big.Int {
  247. s := make([]*big.Int, n)
  248. for i := 0; i < len(s); i++ {
  249. s[i] = big.NewInt(0)
  250. }
  251. return s
  252. }