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.

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