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.

149 lines
5.6 KiB

  1. package common
  2. import (
  3. "encoding/hex"
  4. "testing"
  5. ethCommon "github.com/ethereum/go-ethereum/common"
  6. ethCrypto "github.com/ethereum/go-ethereum/crypto"
  7. "github.com/iden3/go-iden3-crypto/babyjub"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. )
  11. func TestAccountCreationAuthSignVerify(t *testing.T) {
  12. // Ethereum key
  13. ethSk, err :=
  14. ethCrypto.HexToECDSA("fad9c8855b740a0b7ed4c221dbad0f33a83a49cad6b3fe8d5817ac83d38b6a19")
  15. require.NoError(t, err)
  16. ethAddr := ethCrypto.PubkeyToAddress(ethSk.PublicKey)
  17. // BabyJubJub key
  18. var sk babyjub.PrivateKey
  19. _, err = hex.Decode(sk[:],
  20. []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  21. require.NoError(t, err)
  22. chainID := uint16(0)
  23. hermezContractAddr := ethCommon.HexToAddress("0xc344E203a046Da13b0B4467EB7B3629D0C99F6E6")
  24. a := AccountCreationAuth{
  25. EthAddr: ethAddr,
  26. BJJ: sk.Public().Compress(),
  27. }
  28. // Sign using the Sign function (stores signature in a.Signature)
  29. err = a.Sign(func(hash []byte) ([]byte, error) {
  30. return ethCrypto.Sign(hash, ethSk)
  31. }, chainID, hermezContractAddr)
  32. require.NoError(t, err)
  33. // Hash and sign manually and compare the generated signature
  34. hash, err := a.HashToSign(chainID, hermezContractAddr)
  35. require.NoError(t, err)
  36. assert.Equal(t, "9414667457e658dd31949b82996b75c65a055512244c3bbfd22ff56add02ba65",
  37. hex.EncodeToString(hash))
  38. sig, err := ethCrypto.Sign(hash, ethSk)
  39. require.NoError(t, err)
  40. sig[64] += 27
  41. assert.Equal(t, sig, a.Signature)
  42. assert.True(t, a.VerifySignature(chainID, hermezContractAddr))
  43. }
  44. func TestKeccak256JSComp(t *testing.T) {
  45. // check keccak256 compatible with js version
  46. h := ethCrypto.Keccak256([]byte("test"))
  47. assert.Equal(t, "9c22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658",
  48. hex.EncodeToString(h))
  49. }
  50. func TestAccountCreationAuthJSComp(t *testing.T) {
  51. // The values of this test have been tested with the js implementation
  52. type testVector struct {
  53. ethSk string
  54. expectedAddress string
  55. pkCompStr string
  56. chainID uint16
  57. hermezContractAddr string
  58. toHashExpected string
  59. hashExpected string
  60. sigExpected string
  61. }
  62. var tvs []testVector
  63. //nolint:lll
  64. tv0 := testVector{
  65. ethSk: "0000000000000000000000000000000000000000000000000000000000000001",
  66. expectedAddress: "0x7E5F4552091A69125d5DfCb7b8C2659029395Bdf",
  67. pkCompStr: "21b0a1688b37f77b1d1d5539ec3b826db5ac78b2513f574a04c50a7d4f8246d7",
  68. chainID: uint16(4),
  69. hermezContractAddr: "0x7e5f4552091a69125d5dfcb7b8c2659029395bdf",
  70. toHashExpected: "190189658bba487e11c7da602676ee32bc90b77d3f32a305b147e4f3c3b35f19672e5d84ccc38d0ab245c469b719549d837113465c2abf9972c49403ca6fd10ed3dc",
  71. hashExpected: "c56eba41e511df100c804c5c09288f35887efea4f033be956481af335df3bea2",
  72. sigExpected: "dbedcc5ce02db8f48afbdb2feba9a3a31848eaa8fca5f312ce37b01db45d2199208335330d4445bd2f51d1db68dbc0d0bf3585c4a07504b4efbe46a69eaae5a21b",
  73. }
  74. //nolint:lll
  75. tv1 := testVector{
  76. ethSk: "0000000000000000000000000000000000000000000000000000000000000002",
  77. expectedAddress: "0x2B5AD5c4795c026514f8317c7a215E218DcCD6cF",
  78. pkCompStr: "093985b1993d9f743f9d7d943ed56f38601cb8b196db025f79650c4007c3054d",
  79. chainID: uint16(0),
  80. hermezContractAddr: "0x2b5ad5c4795c026514f8317c7a215e218dccd6cf",
  81. toHashExpected: "1901dafbc253dedf90d6421dc6e25d5d9efc6985133cb2a8d363d0a081a0e3eddddc65f603a88de36aaeabd3b4cf586538c7f3fd50c94780530a3707c8c14ad9fd11",
  82. hashExpected: "deb9afa479282cf27b442ce8ba86b19448aa87eacef691521a33db5d0feb9959",
  83. sigExpected: "6a0da90ba2d2b1be679a28ebe54ee03082d44b836087391cd7d2607c1e4dafe04476e6e88dccb8707c68312512f16c947524b35c80f26c642d23953e9bb84c701c",
  84. }
  85. //nolint:lll
  86. tv2 := testVector{
  87. ethSk: "c5e8f61d1ab959b397eecc0a37a6517b8e67a0e7cf1f4bce5591f3ed80199122",
  88. expectedAddress: "0xc783df8a850f42e7F7e57013759C285caa701eB6",
  89. pkCompStr: "22870c1bcc451396202d62f566026eab8e438c6c91decf8ddf63a6c162619b52",
  90. chainID: uint16(31337), // =0x7a69
  91. hermezContractAddr: "0xf4e77E5Da47AC3125140c470c71cBca77B5c638c",
  92. toHashExpected: "190167617949b934d7e01add4009cd3d47415a26727b7d6288e5dce33fb3721d5a1a9ce511b19b694c9aaf8183f4987ed752f24884c54c003d11daa2e98c7547a79e",
  93. hashExpected: "157b570c597e615b8356ce008ac39f43bc9b6d50080bc07d968031b9378acbbb",
  94. sigExpected: "a0766181102428b5672e523dc4b905c10ddf025c10dbd0b3534ef864632a14652737610041c670b302fc7dca28edd5d6eac42b72d69ce58da8ce21287b244e381b",
  95. }
  96. tvs = append(tvs, tv0)
  97. tvs = append(tvs, tv1)
  98. tvs = append(tvs, tv2)
  99. for _, tv := range tvs {
  100. // Ethereum key
  101. ethSk, err := ethCrypto.HexToECDSA(tv.ethSk)
  102. require.NoError(t, err)
  103. ethAddr := ethCrypto.PubkeyToAddress(ethSk.PublicKey)
  104. assert.Equal(t, tv.expectedAddress, ethAddr.Hex())
  105. // BabyJubJub key
  106. pkCompStr := tv.pkCompStr
  107. pkComp, err := BJJFromStringWithChecksum(pkCompStr)
  108. require.NoError(t, err)
  109. chainID := tv.chainID
  110. hermezContractAddr := ethCommon.HexToAddress(tv.hermezContractAddr)
  111. a := AccountCreationAuth{
  112. EthAddr: ethAddr,
  113. BJJ: pkComp,
  114. }
  115. toHash, err := a.toHash(chainID, hermezContractAddr)
  116. require.NoError(t, err)
  117. assert.Equal(t, tv.toHashExpected,
  118. hex.EncodeToString(toHash))
  119. msg, err := a.HashToSign(chainID, hermezContractAddr)
  120. require.NoError(t, err)
  121. assert.Equal(t, tv.hashExpected,
  122. hex.EncodeToString(msg))
  123. // sign AccountCreationAuth with eth key
  124. sig, err := ethCrypto.Sign(msg, ethSk)
  125. require.NoError(t, err)
  126. sig[64] += 27
  127. assert.Equal(t, tv.sigExpected,
  128. hex.EncodeToString(sig))
  129. a.Signature = sig
  130. assert.True(t, a.VerifySignature(chainID, hermezContractAddr))
  131. }
  132. }