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.

368 lines
13 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. package common
  4. import (
  5. "encoding/json"
  6. "math/big"
  7. "github.com/hermeznetwork/hermez-node/log"
  8. "github.com/mitchellh/mapstructure"
  9. )
  10. // ZKMetadata contains ZKInputs metadata that is not used directly in the
  11. // ZKInputs result, but to calculate values for Hash check
  12. type ZKMetadata struct {
  13. // Circuit parameters
  14. // absolute maximum of L1 or L2 transactions allowed
  15. NTx uint32
  16. // merkle tree depth
  17. NLevels uint32
  18. MaxLevels uint32
  19. // absolute maximum of L1 transaction allowed
  20. MaxL1Tx uint32
  21. // Maximum number of Idxs where Fees can be send in a batch (currently
  22. // is constant for all circuits: 64)
  23. MaxFeeIdxs uint32
  24. L1TxsData [][]byte
  25. L2TxsData [][]byte
  26. ChainID uint16
  27. }
  28. // ZKInputs represents the inputs that will be used to generate the zkSNARK proof
  29. type ZKInputs struct {
  30. Metadata ZKMetadata
  31. //
  32. // General
  33. //
  34. // inputs for final `hashGlobalInputs`
  35. // OldLastIdx is the last index assigned to an account
  36. OldLastIdx *big.Int `json:"oldLastIdx"` // uint64 (max nLevels bits)
  37. // OldStateRoot is the current state merkle tree root
  38. OldStateRoot *big.Int `json:"oldStateRoot"` // Hash
  39. // GlobalChainID is the blockchain ID (0 for Ethereum mainnet). This value can be get from the smart contract.
  40. GlobalChainID *big.Int `json:"globalChainID"` // uint16
  41. // FeeIdxs is an array of merkle tree indexes where the coordinator will receive the accumulated fees
  42. FeeIdxs []*big.Int `json:"feeIdxs"` // uint64 (max nLevels bits), len: [maxFeeIdxs]
  43. // accumulate fees
  44. // FeePlanTokens contains all the tokenIDs for which the fees are being accumulated
  45. FeePlanTokens []*big.Int `json:"feePlanTokens"` // uint32 (max 32 bits), len: [maxFeeIdxs]
  46. //
  47. // Txs (L1&L2)
  48. //
  49. // transaction L1-L2
  50. // TxCompressedData
  51. TxCompressedData []*big.Int `json:"txCompressedData"` // big.Int (max 251 bits), len: [nTx]
  52. // TxCompressedDataV2, only used in L2Txs, in L1Txs is set to 0
  53. TxCompressedDataV2 []*big.Int `json:"txCompressedDataV2"` // big.Int (max 193 bits), len: [nTx]
  54. // FromIdx
  55. FromIdx []*big.Int `json:"fromIdx"` // uint64 (max nLevels bits), len: [nTx]
  56. // AuxFromIdx is the Idx of the new created account which is consequence of a L1CreateAccountTx
  57. AuxFromIdx []*big.Int `json:"auxFromIdx"` // uint64 (max nLevels bits), len: [nTx]
  58. // ToIdx
  59. ToIdx []*big.Int `json:"toIdx"` // uint64 (max nLevels bits), len: [nTx]
  60. // 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'
  61. AuxToIdx []*big.Int `json:"auxToIdx"` // uint64 (max nLevels bits), len: [nTx]
  62. // ToBJJAy
  63. ToBJJAy []*big.Int `json:"toBjjAy"` // big.Int, len: [nTx]
  64. // ToEthAddr
  65. ToEthAddr []*big.Int `json:"toEthAddr"` // ethCommon.Address, len: [nTx]
  66. // OnChain determines if is L1 (1/true) or L2 (0/false)
  67. OnChain []*big.Int `json:"onChain"` // bool, len: [nTx]
  68. //
  69. // Txs/L1Txs
  70. //
  71. // NewAccount boolean (0/1) flag set 'true' when L1 tx creates a new account (fromIdx==0)
  72. NewAccount []*big.Int `json:"newAccount"` // bool, len: [nTx]
  73. // LoadAmountF encoded as float16
  74. LoadAmountF []*big.Int `json:"loadAmountF"` // uint16, len: [nTx]
  75. // FromEthAddr
  76. FromEthAddr []*big.Int `json:"fromEthAddr"` // ethCommon.Address, len: [nTx]
  77. // FromBJJCompressed boolean encoded where each value is a *big.Int
  78. FromBJJCompressed [][256]*big.Int `json:"fromBjjCompressed"` // bool array, len: [nTx][256]
  79. //
  80. // Txs/L2Txs
  81. //
  82. // RqOffset relative transaction position to be linked. Used to perform atomic transactions.
  83. RqOffset []*big.Int `json:"rqOffset"` // uint8 (max 3 bits), len: [nTx]
  84. // transaction L2 request data
  85. // RqTxCompressedDataV2
  86. RqTxCompressedDataV2 []*big.Int `json:"rqTxCompressedDataV2"` // big.Int (max 251 bits), len: [nTx]
  87. // RqToEthAddr
  88. RqToEthAddr []*big.Int `json:"rqToEthAddr"` // ethCommon.Address, len: [nTx]
  89. // RqToBJJAy
  90. RqToBJJAy []*big.Int `json:"rqToBjjAy"` // big.Int, len: [nTx]
  91. // transaction L2 signature
  92. // S
  93. S []*big.Int `json:"s"` // big.Int, len: [nTx]
  94. // R8x
  95. R8x []*big.Int `json:"r8x"` // big.Int, len: [nTx]
  96. // R8y
  97. R8y []*big.Int `json:"r8y"` // big.Int, len: [nTx]
  98. //
  99. // State MerkleTree Leafs transitions
  100. //
  101. // state 1, value of the sender (from) account leaf
  102. TokenID1 []*big.Int `json:"tokenID1"` // uint32, len: [nTx]
  103. Nonce1 []*big.Int `json:"nonce1"` // uint64 (max 40 bits), len: [nTx]
  104. Sign1 []*big.Int `json:"sign1"` // bool, len: [nTx]
  105. Ay1 []*big.Int `json:"ay1"` // big.Int, len: [nTx]
  106. Balance1 []*big.Int `json:"balance1"` // big.Int (max 192 bits), len: [nTx]
  107. EthAddr1 []*big.Int `json:"ethAddr1"` // ethCommon.Address, len: [nTx]
  108. Siblings1 [][]*big.Int `json:"siblings1"` // big.Int, len: [nTx][nLevels + 1]
  109. // Required for inserts and deletes, values of the CircomProcessorProof (smt insert proof)
  110. IsOld0_1 []*big.Int `json:"isOld0_1"` // bool, len: [nTx]
  111. OldKey1 []*big.Int `json:"oldKey1"` // uint64 (max 40 bits), len: [nTx]
  112. OldValue1 []*big.Int `json:"oldValue1"` // Hash, len: [nTx]
  113. // state 2, value of the receiver (to) account leaf
  114. // if Tx is an Exit, state 2 is used for the Exit Merkle Proof
  115. TokenID2 []*big.Int `json:"tokenID2"` // uint32, len: [nTx]
  116. Nonce2 []*big.Int `json:"nonce2"` // uint64 (max 40 bits), len: [nTx]
  117. Sign2 []*big.Int `json:"sign2"` // bool, len: [nTx]
  118. Ay2 []*big.Int `json:"ay2"` // big.Int, len: [nTx]
  119. Balance2 []*big.Int `json:"balance2"` // big.Int (max 192 bits), len: [nTx]
  120. EthAddr2 []*big.Int `json:"ethAddr2"` // ethCommon.Address, len: [nTx]
  121. Siblings2 [][]*big.Int `json:"siblings2"` // big.Int, len: [nTx][nLevels + 1]
  122. // newExit determines if an exit transaction has to create a new leaf in the exit tree
  123. NewExit []*big.Int `json:"newExit"` // bool, len: [nTx]
  124. // Required for inserts and deletes, values of the CircomProcessorProof (smt insert proof)
  125. IsOld0_2 []*big.Int `json:"isOld0_2"` // bool, len: [nTx]
  126. OldKey2 []*big.Int `json:"oldKey2"` // uint64 (max 40 bits), len: [nTx]
  127. OldValue2 []*big.Int `json:"oldValue2"` // Hash, len: [nTx]
  128. // state 3, value of the account leaf receiver of the Fees
  129. // fee tx
  130. // State fees
  131. TokenID3 []*big.Int `json:"tokenID3"` // uint32, len: [maxFeeIdxs]
  132. Nonce3 []*big.Int `json:"nonce3"` // uint64 (max 40 bits), len: [maxFeeIdxs]
  133. Sign3 []*big.Int `json:"sign3"` // bool, len: [maxFeeIdxs]
  134. Ay3 []*big.Int `json:"ay3"` // big.Int, len: [maxFeeIdxs]
  135. Balance3 []*big.Int `json:"balance3"` // big.Int (max 192 bits), len: [maxFeeIdxs]
  136. EthAddr3 []*big.Int `json:"ethAddr3"` // ethCommon.Address, len: [maxFeeIdxs]
  137. Siblings3 [][]*big.Int `json:"siblings3"` // Hash, len: [maxFeeIdxs][nLevels + 1]
  138. //
  139. // Intermediate States
  140. //
  141. // Intermediate States to parallelize witness computation
  142. // Note: the Intermediate States (IS) of the last transaction does not
  143. // exist. Meaning that transaction 3 (4th) will fill the parameters
  144. // FromIdx[3] and ISOnChain[3], but last transaction (nTx-1) will fill
  145. // FromIdx[nTx-1] but will not fill ISOnChain. That's why IS have
  146. // length of nTx-1, while the other parameters have length of nTx.
  147. // Last transaction does not need intermediate state since its output
  148. // will not be used.
  149. // decode-tx
  150. // ISOnChain indicates if tx is L1 (true) or L2 (false)
  151. ISOnChain []*big.Int `json:"imOnChain"` // bool, len: [nTx - 1]
  152. // ISOutIdx current index account for each Tx
  153. ISOutIdx []*big.Int `json:"imOutIdx"` // uint64 (max nLevels bits), len: [nTx - 1]
  154. // rollup-tx
  155. // ISStateRoot root at the moment of the Tx, the state root value once the Tx is processed into the state tree
  156. ISStateRoot []*big.Int `json:"imStateRoot"` // Hash, len: [nTx - 1]
  157. // ISExitTree root at the moment of the Tx the value once the Tx is processed into the exit tree
  158. ISExitRoot []*big.Int `json:"imExitRoot"` // Hash, len: [nTx - 1]
  159. // ISAccFeeOut accumulated fees once the Tx is processed
  160. ISAccFeeOut [][]*big.Int `json:"imAccFeeOut"` // big.Int, len: [nTx - 1][maxFeeIdxs]
  161. // fee-tx
  162. // ISStateRootFee root at the moment of the Tx, the state root value once the Tx is processed into the state tree
  163. ISStateRootFee []*big.Int `json:"imStateRootFee"` // Hash, len: [maxFeeIdxs - 1]
  164. // ISInitStateRootFee state root once all L1-L2 tx are processed (before computing the fees-tx)
  165. ISInitStateRootFee *big.Int `json:"imInitStateRootFee"` // Hash
  166. // ISFinalAccFee final accumulated fees (before computing the fees-tx)
  167. ISFinalAccFee []*big.Int `json:"imFinalAccFee"` // big.Int, len: [maxFeeIdxs - 1]
  168. }
  169. func bigIntsToStrings(v interface{}) interface{} {
  170. switch c := v.(type) {
  171. case *big.Int:
  172. return c.String()
  173. case []*big.Int:
  174. r := make([]interface{}, len(c))
  175. for i := range c {
  176. r[i] = bigIntsToStrings(c[i])
  177. }
  178. return r
  179. case [256]*big.Int:
  180. r := make([]interface{}, len(c))
  181. for i := range c {
  182. r[i] = bigIntsToStrings(c[i])
  183. }
  184. return r
  185. case [][]*big.Int:
  186. r := make([]interface{}, len(c))
  187. for i := range c {
  188. r[i] = bigIntsToStrings(c[i])
  189. }
  190. return r
  191. case [][256]*big.Int:
  192. r := make([]interface{}, len(c))
  193. for i := range c {
  194. r[i] = bigIntsToStrings(c[i])
  195. }
  196. return r
  197. case map[string]interface{}:
  198. // avoid printing a warning when there is a struct type
  199. default:
  200. log.Warnf("bigIntsToStrings unexpected type: %T\n", v)
  201. }
  202. return nil
  203. }
  204. // MarshalJSON implements the json marshaler for ZKInputs
  205. func (z ZKInputs) MarshalJSON() ([]byte, error) {
  206. var m map[string]interface{}
  207. dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
  208. TagName: "json",
  209. Result: &m,
  210. })
  211. if err != nil {
  212. return nil, err
  213. }
  214. err = dec.Decode(z)
  215. if err != nil {
  216. return nil, err
  217. }
  218. for k, v := range m {
  219. m[k] = bigIntsToStrings(v)
  220. }
  221. return json.Marshal(m)
  222. }
  223. // NewZKInputs returns a pointer to an initialized struct of ZKInputs
  224. func NewZKInputs(nTx, maxFeeIdxs, nLevels int) *ZKInputs {
  225. zki := &ZKInputs{}
  226. zki.Metadata.NTx = uint32(nTx)
  227. zki.Metadata.MaxFeeIdxs = uint32(maxFeeIdxs)
  228. zki.Metadata.NLevels = uint32(nLevels)
  229. // General
  230. zki.OldLastIdx = big.NewInt(0)
  231. zki.OldStateRoot = big.NewInt(0)
  232. zki.GlobalChainID = big.NewInt(0)
  233. zki.FeeIdxs = newSlice(maxFeeIdxs)
  234. zki.FeePlanTokens = newSlice(maxFeeIdxs)
  235. // Txs
  236. zki.TxCompressedData = newSlice(nTx)
  237. zki.TxCompressedDataV2 = newSlice(nTx)
  238. zki.FromIdx = newSlice(nTx)
  239. zki.AuxFromIdx = newSlice(nTx)
  240. zki.ToIdx = newSlice(nTx)
  241. zki.AuxToIdx = newSlice(nTx)
  242. zki.ToBJJAy = newSlice(nTx)
  243. zki.ToEthAddr = newSlice(nTx)
  244. zki.OnChain = newSlice(nTx)
  245. zki.NewAccount = newSlice(nTx)
  246. // L1
  247. zki.LoadAmountF = newSlice(nTx)
  248. zki.FromEthAddr = newSlice(nTx)
  249. zki.FromBJJCompressed = make([][256]*big.Int, nTx)
  250. for i := 0; i < len(zki.FromBJJCompressed); i++ {
  251. // zki.FromBJJCompressed[i] = newSlice(256)
  252. for j := 0; j < 256; j++ {
  253. zki.FromBJJCompressed[i][j] = big.NewInt(0)
  254. }
  255. }
  256. // L2
  257. zki.RqOffset = newSlice(nTx)
  258. zki.RqTxCompressedDataV2 = newSlice(nTx)
  259. zki.RqToEthAddr = newSlice(nTx)
  260. zki.RqToBJJAy = newSlice(nTx)
  261. zki.S = newSlice(nTx)
  262. zki.R8x = newSlice(nTx)
  263. zki.R8y = newSlice(nTx)
  264. // State MerkleTree Leafs transitions
  265. zki.TokenID1 = newSlice(nTx)
  266. zki.Nonce1 = newSlice(nTx)
  267. zki.Sign1 = newSlice(nTx)
  268. zki.Ay1 = newSlice(nTx)
  269. zki.Balance1 = newSlice(nTx)
  270. zki.EthAddr1 = newSlice(nTx)
  271. zki.Siblings1 = make([][]*big.Int, nTx)
  272. for i := 0; i < len(zki.Siblings1); i++ {
  273. zki.Siblings1[i] = newSlice(nLevels + 1)
  274. }
  275. zki.IsOld0_1 = newSlice(nTx)
  276. zki.OldKey1 = newSlice(nTx)
  277. zki.OldValue1 = newSlice(nTx)
  278. zki.TokenID2 = newSlice(nTx)
  279. zki.Nonce2 = newSlice(nTx)
  280. zki.Sign2 = newSlice(nTx)
  281. zki.Ay2 = newSlice(nTx)
  282. zki.Balance2 = newSlice(nTx)
  283. zki.EthAddr2 = newSlice(nTx)
  284. zki.Siblings2 = make([][]*big.Int, nTx)
  285. for i := 0; i < len(zki.Siblings2); i++ {
  286. zki.Siblings2[i] = newSlice(nLevels + 1)
  287. }
  288. zki.NewExit = newSlice(nTx)
  289. zki.IsOld0_2 = newSlice(nTx)
  290. zki.OldKey2 = newSlice(nTx)
  291. zki.OldValue2 = newSlice(nTx)
  292. zki.TokenID3 = newSlice(maxFeeIdxs)
  293. zki.Nonce3 = newSlice(maxFeeIdxs)
  294. zki.Sign3 = newSlice(maxFeeIdxs)
  295. zki.Ay3 = newSlice(maxFeeIdxs)
  296. zki.Balance3 = newSlice(maxFeeIdxs)
  297. zki.EthAddr3 = newSlice(maxFeeIdxs)
  298. zki.Siblings3 = make([][]*big.Int, maxFeeIdxs)
  299. for i := 0; i < len(zki.Siblings3); i++ {
  300. zki.Siblings3[i] = newSlice(nLevels + 1)
  301. }
  302. // Intermediate States
  303. zki.ISOnChain = newSlice(nTx - 1)
  304. zki.ISOutIdx = newSlice(nTx - 1)
  305. zki.ISStateRoot = newSlice(nTx - 1)
  306. zki.ISExitRoot = newSlice(nTx - 1)
  307. zki.ISAccFeeOut = make([][]*big.Int, nTx-1)
  308. for i := 0; i < len(zki.ISAccFeeOut); i++ {
  309. zki.ISAccFeeOut[i] = newSlice(maxFeeIdxs)
  310. }
  311. zki.ISStateRootFee = newSlice(maxFeeIdxs - 1)
  312. zki.ISInitStateRootFee = big.NewInt(0)
  313. zki.ISFinalAccFee = newSlice(maxFeeIdxs - 1)
  314. return zki
  315. }
  316. // newSlice returns a []*big.Int slice of length n with values initialized at
  317. // 0.
  318. // Is used to initialize all *big.Ints of the ZKInputs data structure, so when
  319. // the transactions are processed and the ZKInputs filled, there is no need to
  320. // set all the elements, and if a transaction does not use a parameter, can be
  321. // leaved as it is in the ZKInputs, as will be 0, so later when using the
  322. // ZKInputs to generate the zkSnark proof there is no 'nil'/'null' values.
  323. func newSlice(n int) []*big.Int {
  324. s := make([]*big.Int, n)
  325. for i := 0; i < len(s); i++ {
  326. s[i] = big.NewInt(0)
  327. }
  328. return s
  329. }