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.

139 lines
5.9 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 TestAccountCreationAuthVerification(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. assert.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. msg, err := a.HashToSign(chainID, hermezContractAddr)
  28. assert.NoError(t, err)
  29. assert.Equal(t, "4f8df75e96fdce1ac90bb2f8d81c42047600f85bfcef80ce3b91c2a2afc58c1e",
  30. hex.EncodeToString(msg))
  31. // sign AccountCreationAuth with eth key
  32. sig, err := ethCrypto.Sign(msg, ethSk)
  33. assert.NoError(t, err)
  34. sig[64] += 27
  35. a.Signature = sig
  36. assert.True(t, a.VerifySignature(chainID, hermezContractAddr))
  37. }
  38. func TestKeccak256JSComp(t *testing.T) {
  39. // check keccak256 compatible with js version
  40. h := ethCrypto.Keccak256Hash([]byte("test")).Bytes()
  41. assert.Equal(t, "9c22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658",
  42. hex.EncodeToString(h))
  43. }
  44. func TestAccountCreationAuthJSComp(t *testing.T) {
  45. // The values of this test have been tested with the js implementation
  46. type testVector struct {
  47. ethSk string
  48. expectedAddress string
  49. pkCompStr string
  50. chainID uint16
  51. hermezContractAddr string
  52. toHashExpected string
  53. hashExpected string
  54. sigExpected string
  55. }
  56. var tvs []testVector
  57. tv0 := testVector{
  58. ethSk: "0000000000000000000000000000000000000000000000000000000000000001",
  59. expectedAddress: "0x7E5F4552091A69125d5DfCb7b8C2659029395Bdf",
  60. pkCompStr: "21b0a1688b37f77b1d1d5539ec3b826db5ac78b2513f574a04c50a7d4f8246d7",
  61. chainID: uint16(4),
  62. hermezContractAddr: "0x7e5f4552091a69125d5dfcb7b8c2659029395bdf",
  63. toHashExpected: "19457468657265756d205369676e6564204d6573736167653a0a3132304920617574686f72697a65207468697320626162796a75626a7562206b657920666f72206865726d657a20726f6c6c7570206163636f756e74206372656174696f6e21b0a1688b37f77b1d1d5539ec3b826db5ac78b2513f574a04c50a7d4f8246d700047e5f4552091a69125d5dfcb7b8c2659029395bdf",
  64. hashExpected: "39afea52d843a4de905b6b5ebb0ee8c678141f711d96d9b429c4aec10ef9911f",
  65. sigExpected: "73d10d6ecf06ee8a5f60ac90f06b78bef9c650f414ba3ac73e176dc32e896159147457e9c86f0b4bd60fdaf2c0b2aec890a7df993d69a4805e242a6b845ebf231c",
  66. }
  67. tv1 := testVector{
  68. ethSk: "0000000000000000000000000000000000000000000000000000000000000002",
  69. expectedAddress: "0x2B5AD5c4795c026514f8317c7a215E218DcCD6cF",
  70. pkCompStr: "093985b1993d9f743f9d7d943ed56f38601cb8b196db025f79650c4007c3054d",
  71. chainID: uint16(0),
  72. hermezContractAddr: "0x2b5ad5c4795c026514f8317c7a215e218dccd6cf",
  73. toHashExpected: "19457468657265756d205369676e6564204d6573736167653a0a3132304920617574686f72697a65207468697320626162796a75626a7562206b657920666f72206865726d657a20726f6c6c7570206163636f756e74206372656174696f6e093985b1993d9f743f9d7d943ed56f38601cb8b196db025f79650c4007c3054d00002b5ad5c4795c026514f8317c7a215e218dccd6cf",
  74. hashExpected: "89a3895993a4736232212e59566294feb3da227af44375daf3307dcad5451d5d",
  75. sigExpected: "bb4156156c705494ad5f99030342c64657e51e2994750f92125717c40bf56ad632044aa6bd00979feea92c417b552401e65fe5f531f15010d9d1c278da8be1df1b",
  76. }
  77. tv2 := testVector{
  78. ethSk: "c5e8f61d1ab959b397eecc0a37a6517b8e67a0e7cf1f4bce5591f3ed80199122",
  79. expectedAddress: "0xc783df8a850f42e7F7e57013759C285caa701eB6",
  80. pkCompStr: "22870c1bcc451396202d62f566026eab8e438c6c91decf8ddf63a6c162619b52",
  81. chainID: uint16(31337), // =0x7a69
  82. hermezContractAddr: "0xf4e77E5Da47AC3125140c470c71cBca77B5c638c",
  83. toHashExpected: "19457468657265756d205369676e6564204d6573736167653a0a3132304920617574686f72697a65207468697320626162796a75626a7562206b657920666f72206865726d657a20726f6c6c7570206163636f756e74206372656174696f6e22870c1bcc451396202d62f566026eab8e438c6c91decf8ddf63a6c162619b527a69f4e77e5da47ac3125140c470c71cbca77b5c638c",
  84. hashExpected: "4f6ead01278ba4597d4720e37482f585a713497cea994a95209f4c57a963b4a7",
  85. sigExpected: "43b5818802a137a72a190c1d8d767ca507f7a4804b1b69b5e055abf31f4f2b476c80bb1ba63260d95610f6f831420d32130e7f22fec5d76e16644ddfcedd0d441c",
  86. }
  87. tvs = append(tvs, tv0)
  88. tvs = append(tvs, tv1)
  89. tvs = append(tvs, tv2)
  90. for _, tv := range tvs {
  91. // Ethereum key
  92. ethSk, err := ethCrypto.HexToECDSA(tv.ethSk)
  93. require.NoError(t, err)
  94. ethAddr := ethCrypto.PubkeyToAddress(ethSk.PublicKey)
  95. assert.Equal(t, tv.expectedAddress, ethAddr.Hex())
  96. // BabyJubJub key
  97. pkCompStr := tv.pkCompStr
  98. pkComp, err := BJJFromStringWithChecksum(pkCompStr)
  99. assert.NoError(t, err)
  100. chainID := tv.chainID
  101. hermezContractAddr := ethCommon.HexToAddress(tv.hermezContractAddr)
  102. a := AccountCreationAuth{
  103. EthAddr: ethAddr,
  104. BJJ: pkComp,
  105. }
  106. toHash := a.toHash(chainID, hermezContractAddr)
  107. assert.Equal(t, tv.toHashExpected,
  108. hex.EncodeToString(toHash))
  109. assert.Equal(t, 120+len(EthMsgPrefix)+len([]byte("120")), len(toHash))
  110. msg, err := a.HashToSign(chainID, hermezContractAddr)
  111. assert.NoError(t, err)
  112. assert.Equal(t, tv.hashExpected,
  113. hex.EncodeToString(msg))
  114. // sign AccountCreationAuth with eth key
  115. sig, err := ethCrypto.Sign(msg, ethSk)
  116. assert.NoError(t, err)
  117. sig[64] += 27
  118. assert.Equal(t, tv.sigExpected,
  119. hex.EncodeToString(sig))
  120. a.Signature = sig
  121. assert.True(t, a.VerifySignature(chainID, hermezContractAddr))
  122. }
  123. }