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.

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