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
6.1 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, "4f8df75e96fdce1ac90bb2f8d81c42047600f85bfcef80ce3b91c2a2afc58c1e",
  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.Keccak256Hash([]byte("test")).Bytes()
  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: "19457468657265756d205369676e6564204d6573736167653a0a3132304920617574686f72697a65207468697320626162796a75626a7562206b657920666f72206865726d657a20726f6c6c7570206163636f756e74206372656174696f6e21b0a1688b37f77b1d1d5539ec3b826db5ac78b2513f574a04c50a7d4f8246d700047e5f4552091a69125d5dfcb7b8c2659029395bdf",
  69. hashExpected: "39afea52d843a4de905b6b5ebb0ee8c678141f711d96d9b429c4aec10ef9911f",
  70. sigExpected: "73d10d6ecf06ee8a5f60ac90f06b78bef9c650f414ba3ac73e176dc32e896159147457e9c86f0b4bd60fdaf2c0b2aec890a7df993d69a4805e242a6b845ebf231c",
  71. }
  72. tv1 := testVector{
  73. ethSk: "0000000000000000000000000000000000000000000000000000000000000002",
  74. expectedAddress: "0x2B5AD5c4795c026514f8317c7a215E218DcCD6cF",
  75. pkCompStr: "093985b1993d9f743f9d7d943ed56f38601cb8b196db025f79650c4007c3054d",
  76. chainID: uint16(0),
  77. hermezContractAddr: "0x2b5ad5c4795c026514f8317c7a215e218dccd6cf",
  78. toHashExpected: "19457468657265756d205369676e6564204d6573736167653a0a3132304920617574686f72697a65207468697320626162796a75626a7562206b657920666f72206865726d657a20726f6c6c7570206163636f756e74206372656174696f6e093985b1993d9f743f9d7d943ed56f38601cb8b196db025f79650c4007c3054d00002b5ad5c4795c026514f8317c7a215e218dccd6cf",
  79. hashExpected: "89a3895993a4736232212e59566294feb3da227af44375daf3307dcad5451d5d",
  80. sigExpected: "bb4156156c705494ad5f99030342c64657e51e2994750f92125717c40bf56ad632044aa6bd00979feea92c417b552401e65fe5f531f15010d9d1c278da8be1df1b",
  81. }
  82. tv2 := testVector{
  83. ethSk: "c5e8f61d1ab959b397eecc0a37a6517b8e67a0e7cf1f4bce5591f3ed80199122",
  84. expectedAddress: "0xc783df8a850f42e7F7e57013759C285caa701eB6",
  85. pkCompStr: "22870c1bcc451396202d62f566026eab8e438c6c91decf8ddf63a6c162619b52",
  86. chainID: uint16(31337), // =0x7a69
  87. hermezContractAddr: "0xf4e77E5Da47AC3125140c470c71cBca77B5c638c",
  88. toHashExpected: "19457468657265756d205369676e6564204d6573736167653a0a3132304920617574686f72697a65207468697320626162796a75626a7562206b657920666f72206865726d657a20726f6c6c7570206163636f756e74206372656174696f6e22870c1bcc451396202d62f566026eab8e438c6c91decf8ddf63a6c162619b527a69f4e77e5da47ac3125140c470c71cbca77b5c638c",
  89. hashExpected: "4f6ead01278ba4597d4720e37482f585a713497cea994a95209f4c57a963b4a7",
  90. sigExpected: "43b5818802a137a72a190c1d8d767ca507f7a4804b1b69b5e055abf31f4f2b476c80bb1ba63260d95610f6f831420d32130e7f22fec5d76e16644ddfcedd0d441c",
  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 := a.toHash(chainID, hermezContractAddr)
  112. assert.Equal(t, tv.toHashExpected,
  113. hex.EncodeToString(toHash))
  114. assert.Equal(t, 120+len(EthMsgPrefix)+len([]byte("120")), len(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. }