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.

388 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. privateKey, err := crypto.HexToECDSA("fad9c8855b740a0b7ed4c221dbad0f33a83a49cad6b3fe8d5817ac83d38b6a19")
  211. require.NoError(t, err)
  212. publicKey := privateKey.Public()
  213. publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
  214. if !ok {
  215. log.Fatal("error casting public key to ECDSA")
  216. }
  217. publicKeyBytes := crypto.FromECDSAPub(publicKeyECDSA)
  218. pubKey, err := crypto.UnmarshalPubkey(publicKeyBytes)
  219. require.NoError(t, err)
  220. fromEthAddr := crypto.PubkeyToAddress(*pubKey)
  221. var pkComp babyjub.PublicKeyComp
  222. pkCompL := []byte("56ca90f80d7c374ae7485e9bcc47d4ac399460948da6aeeb899311097925a72c")
  223. err = pkComp.UnmarshalText(pkCompL)
  224. require.NoError(t, err)
  225. accCreationAuth := AccountCreationAuth{
  226. EthAddr: fromEthAddr,
  227. BJJ: pkComp,
  228. }
  229. h, err := accCreationAuth.HashToSign(uint16(chainID.Uint64()), hermezAddress)
  230. require.NoError(t, err)
  231. signature, err := crypto.Sign(h, privateKey)
  232. require.NoError(t, err)
  233. // Ethereum adds 27 to v
  234. v := int(signature[64])
  235. signature[64] = byte(v + 27)
  236. l1Tx := &L1Tx{
  237. TokenID: 231,
  238. FromBJJ: pkComp,
  239. FromEthAddr: fromEthAddr,
  240. Amount: big.NewInt(0),
  241. DepositAmount: big.NewInt(0),
  242. }
  243. bytesCoordinatorL1, err := l1Tx.BytesCoordinatorTx(signature)
  244. require.NoError(t, err)
  245. l1txDecoded, err := L1CoordinatorTxFromBytes(bytesCoordinatorL1, chainID, hermezAddress)
  246. require.NoError(t, err)
  247. assert.Equal(t, l1Tx, l1txDecoded)
  248. bytesCoordinatorL12, err := l1txDecoded.BytesCoordinatorTx(signature)
  249. require.NoError(t, err)
  250. assert.Equal(t, bytesCoordinatorL1, bytesCoordinatorL12)
  251. // expect error if length!=68
  252. _, err = L1CoordinatorTxFromBytes(bytesCoordinatorL1[:66], chainID, hermezAddress)
  253. require.NotNil(t, err)
  254. _, err = L1CoordinatorTxFromBytes([]byte{}, chainID, hermezAddress)
  255. require.NotNil(t, err)
  256. _, err = L1CoordinatorTxFromBytes(nil, chainID, hermezAddress)
  257. require.NotNil(t, err)
  258. }
  259. func TestL1CoordinatorTxByteParsersCompatibility(t *testing.T) {
  260. // Data from compatibility test
  261. var signature []byte
  262. r, err := hex.DecodeString("da71e5eb097e115405d84d1e7b464009b434b32c014a2df502d1f065ced8bc3b")
  263. require.NoError(t, err)
  264. s, err := hex.DecodeString("186d7122ff7f654cfed3156719774898d573900c86599a885a706dbdffe5ea8c")
  265. require.NoError(t, err)
  266. v, err := hex.DecodeString("1b")
  267. require.NoError(t, err)
  268. signature = append(signature, r[:]...)
  269. signature = append(signature, s[:]...)
  270. signature = append(signature, v[:]...)
  271. var pkComp babyjub.PublicKeyComp
  272. pkCompB, err := hex.DecodeString("a2c2807ee39c3b3378738cff85a46a9465bb8fcf44ea597c33da9719be7c259c")
  273. require.NoError(t, err)
  274. pkCompL := SwapEndianness(pkCompB)
  275. err = pkComp.UnmarshalText([]byte(hex.EncodeToString(pkCompL)))
  276. require.NoError(t, err)
  277. // Data from the compatibility test
  278. require.NoError(t, err)
  279. l1Tx := &L1Tx{
  280. TokenID: 231,
  281. FromBJJ: pkComp,
  282. }
  283. encodeData, err := l1Tx.BytesCoordinatorTx(signature)
  284. require.NoError(t, err)
  285. expected, err := utils.HexDecode("1b186d7122ff7f654cfed3156719774898d573900c86599a885a706dbdffe5ea8cda71e5eb097e115405d84d1e7b464009b434b32c014a2df502d1f065ced8bc3ba2c2807ee39c3b3378738cff85a46a9465bb8fcf44ea597c33da9719be7c259c000000e7")
  286. require.NoError(t, err)
  287. assert.Equal(t, expected, encodeData)
  288. }
  289. func TestL1TxID(t *testing.T) {
  290. // L1UserTx
  291. i64_1 := int64(1)
  292. i64_2 := int64(2)
  293. tx0 := L1Tx{
  294. UserOrigin: true,
  295. ToForgeL1TxsNum: &i64_1,
  296. Position: 1,
  297. }
  298. err := tx0.SetID()
  299. require.NoError(t, err)
  300. assert.Equal(t, TxIDPrefixL1UserTx, tx0.TxID[0])
  301. // differ ToForgeL1TxsNum
  302. tx1 := L1Tx{
  303. UserOrigin: true,
  304. ToForgeL1TxsNum: &i64_2,
  305. Position: 1,
  306. }
  307. err = tx1.SetID()
  308. require.NoError(t, err)
  309. assert.NotEqual(t, tx0.TxID, tx1.TxID)
  310. // differ Position
  311. tx1 = L1Tx{
  312. UserOrigin: true,
  313. ToForgeL1TxsNum: &i64_1,
  314. Position: 2,
  315. }
  316. err = tx1.SetID()
  317. require.NoError(t, err)
  318. assert.NotEqual(t, tx0.TxID, tx1.TxID)
  319. // L1CoordinatorTx
  320. bn1 := BatchNum(1)
  321. bn2 := BatchNum(2)
  322. tx0 = L1Tx{
  323. UserOrigin: false,
  324. BatchNum: &bn1,
  325. Position: 1,
  326. }
  327. err = tx0.SetID()
  328. require.NoError(t, err)
  329. assert.Equal(t, TxIDPrefixL1CoordTx, tx0.TxID[0])
  330. // differ BatchNum
  331. tx1 = L1Tx{
  332. UserOrigin: false,
  333. BatchNum: &bn2,
  334. Position: 1,
  335. }
  336. err = tx1.SetID()
  337. require.NoError(t, err)
  338. assert.NotEqual(t, tx0.TxID, tx1.TxID)
  339. // differ Position
  340. tx1 = L1Tx{
  341. UserOrigin: false,
  342. BatchNum: &bn1,
  343. Position: 2,
  344. }
  345. err = tx1.SetID()
  346. require.NoError(t, err)
  347. assert.NotEqual(t, tx0.TxID, tx1.TxID)
  348. }