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.

423 lines
13 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. package common
  2. import (
  3. "crypto/ecdsa"
  4. "encoding/hex"
  5. "log"
  6. "math/big"
  7. "testing"
  8. ethCommon "github.com/ethereum/go-ethereum/common"
  9. "github.com/ethereum/go-ethereum/crypto"
  10. "github.com/iden3/go-iden3-crypto/babyjub"
  11. "github.com/iden3/go-iden3-crypto/utils"
  12. "github.com/stretchr/testify/assert"
  13. "github.com/stretchr/testify/require"
  14. )
  15. func TestNewL1UserTxID(t *testing.T) {
  16. toForge := int64(123456)
  17. l1Tx := &L1Tx{
  18. ToForgeL1TxsNum: &toForge,
  19. Position: 71,
  20. UserOrigin: true,
  21. ToIdx: 301,
  22. TokenID: 5,
  23. Amount: big.NewInt(1),
  24. DepositAmount: big.NewInt(2),
  25. FromIdx: Idx(300),
  26. }
  27. l1Tx, err := NewL1Tx(l1Tx)
  28. assert.NoError(t, err)
  29. assert.Equal(t, "0x00a6cbae3b8661fb75b0919ca6605a02cfb04d9c6dd16870fa0fcdf01befa32768", l1Tx.TxID.String())
  30. maxInt64 := 0xFFFF_FFFF_FFFF_FFFF >> 1
  31. toForge = int64(maxInt64)
  32. l1Tx = &L1Tx{
  33. ToForgeL1TxsNum: &toForge,
  34. Position: maxInt64,
  35. UserOrigin: true,
  36. }
  37. l1Tx, err = NewL1Tx(l1Tx)
  38. assert.NoError(t, err)
  39. assert.Equal(t, "0x001ff31eb325f324652bfe6b607a19e04789e082ee3b779eefe4a466062ea331d9", l1Tx.TxID.String())
  40. toForge = int64(maxInt64 - 1)
  41. l1Tx = &L1Tx{
  42. ToForgeL1TxsNum: &toForge,
  43. Position: maxInt64 - 1,
  44. UserOrigin: true,
  45. }
  46. l1Tx, err = NewL1Tx(l1Tx)
  47. assert.NoError(t, err)
  48. assert.Equal(t, "0x0003434eca58d35fd85795e3a6cce67c8801deb805ea1f7429cc270aa9f35ea403", l1Tx.TxID.String())
  49. toForge = int64(0)
  50. l1Tx = &L1Tx{
  51. ToForgeL1TxsNum: &toForge,
  52. Position: 0,
  53. UserOrigin: true,
  54. }
  55. l1Tx, err = NewL1Tx(l1Tx)
  56. assert.NoError(t, err)
  57. assert.Equal(t, "0x006bd2dd6bd408cbee33429358bf24fdc64612fbf8b1b4db604518f40ffd34b607", l1Tx.TxID.String())
  58. }
  59. func TestNewL1CoordinatorTx(t *testing.T) {
  60. batchNum := BatchNum(51966)
  61. l1Tx := &L1Tx{
  62. Position: 88,
  63. UserOrigin: false,
  64. ToIdx: 301,
  65. TokenID: 5,
  66. Amount: big.NewInt(1),
  67. DepositAmount: big.NewInt(2),
  68. FromIdx: Idx(300),
  69. BatchNum: &batchNum,
  70. }
  71. l1Tx, err := NewL1Tx(l1Tx)
  72. assert.NoError(t, err)
  73. assert.Equal(t, "0x01274482d73df4dab34a1b6740adfca347a462513aa14e82f27b12f818d1b68c84", l1Tx.TxID.String())
  74. }
  75. func TestL1TxCompressedData(t *testing.T) {
  76. // test vectors values generated from javascript implementation (using
  77. // PoolL2Tx values)
  78. amount, ok := new(big.Int).SetString("343597383670000000000000000000000000000000", 10)
  79. require.True(t, ok)
  80. tx := L1Tx{
  81. FromIdx: (1 << 48) - 1,
  82. ToIdx: (1 << 48) - 1,
  83. Amount: amount,
  84. TokenID: (1 << 32) - 1,
  85. }
  86. txCompressedData, err := tx.TxCompressedData(uint16((1 << 16) - 1))
  87. assert.NoError(t, err)
  88. expectedStr := "ffffffffffffffffffffffffffffffffffffc60be60f"
  89. assert.Equal(t, expectedStr, hex.EncodeToString(txCompressedData.Bytes()))
  90. tx = L1Tx{
  91. FromIdx: 0,
  92. ToIdx: 0,
  93. Amount: big.NewInt(0),
  94. TokenID: 0,
  95. }
  96. txCompressedData, err = tx.TxCompressedData(uint16(0))
  97. assert.NoError(t, err)
  98. expectedStr = "c60be60f"
  99. assert.Equal(t, expectedStr, hex.EncodeToString(txCompressedData.Bytes()))
  100. amount, ok = new(big.Int).SetString("63000000000000000", 10)
  101. require.True(t, ok)
  102. tx = L1Tx{
  103. FromIdx: 324,
  104. ToIdx: 256,
  105. Amount: amount,
  106. TokenID: 123,
  107. }
  108. txCompressedData, err = tx.TxCompressedData(uint16(1))
  109. assert.NoError(t, err)
  110. expectedStr = "7b0000000001000000000001440001c60be60f"
  111. assert.Equal(t, expectedStr, hex.EncodeToString(txCompressedData.Bytes()))
  112. tx = L1Tx{
  113. FromIdx: 1,
  114. ToIdx: 2,
  115. TokenID: 3,
  116. }
  117. txCompressedData, err = tx.TxCompressedData(uint16(0))
  118. assert.NoError(t, err)
  119. expectedStr = "030000000000020000000000010000c60be60f"
  120. assert.Equal(t, expectedStr, hex.EncodeToString(txCompressedData.Bytes()))
  121. }
  122. func TestBytesDataAvailability(t *testing.T) {
  123. // test vectors values generated from javascript implementation
  124. amount, ok := new(big.Int).SetString("343597383670000000000000000000000000000000", 10)
  125. require.True(t, ok)
  126. tx := L1Tx{
  127. ToIdx: (1 << 16) - 1,
  128. FromIdx: (1 << 16) - 1,
  129. EffectiveAmount: amount,
  130. }
  131. txCompressedData, err := tx.BytesDataAvailability(16)
  132. assert.NoError(t, err)
  133. assert.Equal(t, "ffffffffffffffffff00", hex.EncodeToString(txCompressedData))
  134. l1tx, err := L1TxFromDataAvailability(txCompressedData, 16)
  135. require.NoError(t, err)
  136. assert.Equal(t, tx.FromIdx, l1tx.FromIdx)
  137. assert.Equal(t, tx.ToIdx, l1tx.ToIdx)
  138. assert.Equal(t, tx.EffectiveAmount, l1tx.EffectiveAmount)
  139. tx = L1Tx{
  140. ToIdx: (1 << 32) - 1,
  141. FromIdx: (1 << 32) - 1,
  142. EffectiveAmount: amount,
  143. }
  144. txCompressedData, err = tx.BytesDataAvailability(32)
  145. assert.NoError(t, err)
  146. assert.Equal(t, "ffffffffffffffffffffffffff00", hex.EncodeToString(txCompressedData))
  147. l1tx, err = L1TxFromDataAvailability(txCompressedData, 32)
  148. require.NoError(t, err)
  149. assert.Equal(t, tx.FromIdx, l1tx.FromIdx)
  150. assert.Equal(t, tx.ToIdx, l1tx.ToIdx)
  151. assert.Equal(t, tx.EffectiveAmount, l1tx.EffectiveAmount)
  152. tx = L1Tx{
  153. ToIdx: 0,
  154. FromIdx: 0,
  155. EffectiveAmount: big.NewInt(0),
  156. }
  157. txCompressedData, err = tx.BytesDataAvailability(32)
  158. assert.NoError(t, err)
  159. assert.Equal(t, "0000000000000000000000000000", hex.EncodeToString(txCompressedData))
  160. l1tx, err = L1TxFromDataAvailability(txCompressedData, 32)
  161. require.NoError(t, err)
  162. assert.Equal(t, tx.FromIdx, l1tx.FromIdx)
  163. assert.Equal(t, tx.ToIdx, l1tx.ToIdx)
  164. assert.Equal(t, tx.EffectiveAmount, l1tx.EffectiveAmount)
  165. tx = L1Tx{
  166. ToIdx: 635,
  167. FromIdx: 296,
  168. EffectiveAmount: big.NewInt(1000000000000000000),
  169. }
  170. txCompressedData, err = tx.BytesDataAvailability(32)
  171. assert.NoError(t, err)
  172. assert.Equal(t, "000001280000027b42540be40000", hex.EncodeToString(txCompressedData))
  173. l1tx, err = L1TxFromDataAvailability(txCompressedData, 32)
  174. require.NoError(t, err)
  175. assert.Equal(t, tx.FromIdx, l1tx.FromIdx)
  176. assert.Equal(t, tx.ToIdx, l1tx.ToIdx)
  177. assert.Equal(t, tx.EffectiveAmount, l1tx.EffectiveAmount)
  178. }
  179. func TestL1userTxByteParsers(t *testing.T) {
  180. var pkComp babyjub.PublicKeyComp
  181. pkCompL := []byte("0x56ca90f80d7c374ae7485e9bcc47d4ac399460948da6aeeb899311097925a72c")
  182. err := pkComp.UnmarshalText(pkCompL)
  183. require.NoError(t, err)
  184. l1Tx := &L1Tx{
  185. UserOrigin: true,
  186. ToIdx: 3,
  187. TokenID: 5,
  188. Amount: big.NewInt(1),
  189. DepositAmount: big.NewInt(2),
  190. FromIdx: 2,
  191. FromBJJ: pkComp,
  192. FromEthAddr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"),
  193. }
  194. encodedData, err := l1Tx.BytesUser()
  195. require.NoError(t, err)
  196. decodedData, err := L1UserTxFromBytes(encodedData)
  197. require.NoError(t, err)
  198. assert.Equal(t, l1Tx, decodedData)
  199. encodedData2, err := decodedData.BytesUser()
  200. require.NoError(t, err)
  201. assert.Equal(t, encodedData, encodedData2)
  202. // expect error if length!=68
  203. _, err = L1UserTxFromBytes(encodedData[:66])
  204. require.NotNil(t, err)
  205. _, err = L1UserTxFromBytes([]byte{})
  206. require.NotNil(t, err)
  207. _, err = L1UserTxFromBytes(nil)
  208. require.NotNil(t, err)
  209. }
  210. func TestL1TxByteParsersCompatibility(t *testing.T) {
  211. // Data from compatibility test
  212. var pkComp babyjub.PublicKeyComp
  213. pkCompB, err := hex.DecodeString("0dd02deb2c81068e7a0f7e327df80b4ab79ee1f41a7def613e73a20c32eece5a")
  214. require.NoError(t, err)
  215. pkCompL := SwapEndianness(pkCompB)
  216. err = pkComp.UnmarshalText([]byte(hex.EncodeToString(pkCompL)))
  217. require.NoError(t, err)
  218. depositAmount := new(big.Int)
  219. depositAmount.SetString("100000000000000000000", 10)
  220. l1Tx := &L1Tx{
  221. ToIdx: 87865485,
  222. TokenID: 2098076,
  223. Amount: big.NewInt(2400000000000000000),
  224. DepositAmount: depositAmount,
  225. FromIdx: Idx(29767899),
  226. FromBJJ: pkComp,
  227. FromEthAddr: ethCommon.HexToAddress("0x85dab5b9e2e361d0c208d77be90efcc0439b0a53"),
  228. UserOrigin: true,
  229. }
  230. encodedData, err := l1Tx.BytesUser()
  231. require.NoError(t, err)
  232. expected := "85dab5b9e2e361d0c208d77be90efcc0439b0a530dd02deb2c81068e7a0f7e327df80b4ab79ee1f41a7def613e73a20c32eece5a000001c638db52540be400459682f0000020039c0000053cb88d"
  233. assert.Equal(t, expected, hex.EncodeToString(encodedData))
  234. }
  235. func TestL1CoordinatorTxByteParsers(t *testing.T) {
  236. hermezAddress := ethCommon.HexToAddress("0xD6C850aeBFDC46D7F4c207e445cC0d6B0919BDBe")
  237. chainID := big.NewInt(1337)
  238. chainIDBytes := ethCommon.LeftPadBytes(chainID.Bytes(), 2)
  239. privateKey, err := crypto.HexToECDSA("fad9c8855b740a0b7ed4c221dbad0f33a83a49cad6b3fe8d5817ac83d38b6a19")
  240. require.NoError(t, err)
  241. publicKey := privateKey.Public()
  242. publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
  243. if !ok {
  244. log.Fatal("error casting public key to ECDSA")
  245. }
  246. publicKeyBytes := crypto.FromECDSAPub(publicKeyECDSA)
  247. pubKey, err := crypto.UnmarshalPubkey(publicKeyBytes)
  248. require.NoError(t, err)
  249. fromEthAddr := crypto.PubkeyToAddress(*pubKey)
  250. var pkComp babyjub.PublicKeyComp
  251. pkCompL := []byte("56ca90f80d7c374ae7485e9bcc47d4ac399460948da6aeeb899311097925a72c")
  252. err = pkComp.UnmarshalText(pkCompL)
  253. require.NoError(t, err)
  254. bytesMessage1 := []byte("\x19Ethereum Signed Message:\n120")
  255. bytesMessage2 := []byte("I authorize this babyjubjub key for hermez rollup account creation")
  256. babyjubB := SwapEndianness(pkComp[:])
  257. var data []byte
  258. data = append(data, bytesMessage1...)
  259. data = append(data, bytesMessage2...)
  260. data = append(data, babyjubB[:]...)
  261. data = append(data, chainIDBytes...)
  262. data = append(data, hermezAddress.Bytes()...)
  263. hash := crypto.Keccak256Hash(data)
  264. signature, err := crypto.Sign(hash.Bytes(), privateKey)
  265. require.NoError(t, err)
  266. // Ethereum adds 27 to v
  267. v := int(signature[64])
  268. signature[64] = byte(v + 27)
  269. l1Tx := &L1Tx{
  270. TokenID: 231,
  271. FromBJJ: pkComp,
  272. FromEthAddr: fromEthAddr,
  273. Amount: big.NewInt(0),
  274. DepositAmount: big.NewInt(0),
  275. }
  276. bytesCoordinatorL1, err := l1Tx.BytesCoordinatorTx(signature)
  277. require.NoError(t, err)
  278. l1txDecoded, err := L1CoordinatorTxFromBytes(bytesCoordinatorL1, chainID, hermezAddress)
  279. require.NoError(t, err)
  280. assert.Equal(t, l1Tx, l1txDecoded)
  281. bytesCoordinatorL12, err := l1txDecoded.BytesCoordinatorTx(signature)
  282. require.NoError(t, err)
  283. assert.Equal(t, bytesCoordinatorL1, bytesCoordinatorL12)
  284. // expect error if length!=68
  285. _, err = L1CoordinatorTxFromBytes(bytesCoordinatorL1[:66], chainID, hermezAddress)
  286. require.NotNil(t, err)
  287. _, err = L1CoordinatorTxFromBytes([]byte{}, chainID, hermezAddress)
  288. require.NotNil(t, err)
  289. _, err = L1CoordinatorTxFromBytes(nil, chainID, hermezAddress)
  290. require.NotNil(t, err)
  291. }
  292. func TestL1CoordinatorTxByteParsersCompatibility(t *testing.T) {
  293. // Data from compatibility test
  294. var signature []byte
  295. r, err := hex.DecodeString("da71e5eb097e115405d84d1e7b464009b434b32c014a2df502d1f065ced8bc3b")
  296. require.NoError(t, err)
  297. s, err := hex.DecodeString("186d7122ff7f654cfed3156719774898d573900c86599a885a706dbdffe5ea8c")
  298. require.NoError(t, err)
  299. v, err := hex.DecodeString("1b")
  300. require.NoError(t, err)
  301. signature = append(signature, r[:]...)
  302. signature = append(signature, s[:]...)
  303. signature = append(signature, v[:]...)
  304. var pkComp babyjub.PublicKeyComp
  305. pkCompB, err := hex.DecodeString("a2c2807ee39c3b3378738cff85a46a9465bb8fcf44ea597c33da9719be7c259c")
  306. require.NoError(t, err)
  307. pkCompL := SwapEndianness(pkCompB)
  308. err = pkComp.UnmarshalText([]byte(hex.EncodeToString(pkCompL)))
  309. require.NoError(t, err)
  310. // Data from the compatibility test
  311. require.NoError(t, err)
  312. l1Tx := &L1Tx{
  313. TokenID: 231,
  314. FromBJJ: pkComp,
  315. }
  316. encodeData, err := l1Tx.BytesCoordinatorTx(signature)
  317. require.NoError(t, err)
  318. expected, err := utils.HexDecode("1b186d7122ff7f654cfed3156719774898d573900c86599a885a706dbdffe5ea8cda71e5eb097e115405d84d1e7b464009b434b32c014a2df502d1f065ced8bc3ba2c2807ee39c3b3378738cff85a46a9465bb8fcf44ea597c33da9719be7c259c000000e7")
  319. require.NoError(t, err)
  320. assert.Equal(t, expected, encodeData)
  321. }
  322. func TestL1TxID(t *testing.T) {
  323. // L1UserTx
  324. i64_1 := int64(1)
  325. i64_2 := int64(2)
  326. tx0 := L1Tx{
  327. UserOrigin: true,
  328. ToForgeL1TxsNum: &i64_1,
  329. Position: 1,
  330. }
  331. err := tx0.SetID()
  332. require.NoError(t, err)
  333. assert.Equal(t, TxIDPrefixL1UserTx, tx0.TxID[0])
  334. // differ ToForgeL1TxsNum
  335. tx1 := L1Tx{
  336. UserOrigin: true,
  337. ToForgeL1TxsNum: &i64_2,
  338. Position: 1,
  339. }
  340. err = tx1.SetID()
  341. require.NoError(t, err)
  342. assert.NotEqual(t, tx0.TxID, tx1.TxID)
  343. // differ Position
  344. tx1 = L1Tx{
  345. UserOrigin: true,
  346. ToForgeL1TxsNum: &i64_1,
  347. Position: 2,
  348. }
  349. err = tx1.SetID()
  350. require.NoError(t, err)
  351. assert.NotEqual(t, tx0.TxID, tx1.TxID)
  352. // L1CoordinatorTx
  353. bn1 := BatchNum(1)
  354. bn2 := BatchNum(2)
  355. tx0 = L1Tx{
  356. UserOrigin: false,
  357. BatchNum: &bn1,
  358. Position: 1,
  359. }
  360. err = tx0.SetID()
  361. require.NoError(t, err)
  362. assert.Equal(t, TxIDPrefixL1CoordTx, tx0.TxID[0])
  363. // differ BatchNum
  364. tx1 = L1Tx{
  365. UserOrigin: false,
  366. BatchNum: &bn2,
  367. Position: 1,
  368. }
  369. err = tx1.SetID()
  370. require.NoError(t, err)
  371. assert.NotEqual(t, tx0.TxID, tx1.TxID)
  372. // differ Position
  373. tx1 = L1Tx{
  374. UserOrigin: false,
  375. BatchNum: &bn1,
  376. Position: 2,
  377. }
  378. err = tx1.SetID()
  379. require.NoError(t, err)
  380. assert.NotEqual(t, tx0.TxID, tx1.TxID)
  381. }