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.

63 lines
1.4 KiB

  1. package main
  2. import (
  3. "crypto/ecdsa"
  4. "math/big"
  5. "testing"
  6. "github.com/arnaucube/go-blindsecp256k1"
  7. "github.com/btcsuite/btcd/btcec"
  8. "github.com/stretchr/testify/require"
  9. "gotest.tools/assert"
  10. )
  11. func calculateKeysFromK(t *testing.T, k *big.Int) (*btcec.PublicKey, *blindsecp256k1.PublicKey) {
  12. curve := btcec.S256()
  13. x, y := curve.ScalarBaseMult(k.Bytes())
  14. skEth := (*btcec.PrivateKey)(&ecdsa.PrivateKey{
  15. PublicKey: ecdsa.PublicKey{
  16. Curve: curve,
  17. X: x,
  18. Y: y,
  19. },
  20. D: k,
  21. })
  22. pkEthAux := (*btcec.PublicKey)(&skEth.PublicKey)
  23. pkEth := skEth.PubKey()
  24. assert.Equal(t, pkEth, pkEthAux)
  25. skBlind := blindsecp256k1.PrivateKey(*k)
  26. pkBlind := skBlind.Public()
  27. return pkEth, pkBlind
  28. }
  29. func TestCompatibleKeys(t *testing.T) {
  30. // using incremental k
  31. for i := 0; i < 10000; i++ {
  32. k := big.NewInt(int64(i))
  33. pkEth, pkBlind := calculateKeysFromK(t, k)
  34. assert.Equal(t, pkEth.X.String(), pkBlind.X.String())
  35. assert.Equal(t, pkEth.Y.String(), pkBlind.Y.String())
  36. // fmt.Println("k", k)
  37. // fmt.Println(pkEth)
  38. // fmt.Println(pkBlind)
  39. }
  40. // using random k
  41. for i := 0; i < 10000; i++ {
  42. kaux, err := btcec.NewPrivateKey(btcec.S256())
  43. require.NoError(t, err)
  44. k := kaux.D
  45. pkEth, pkBlind := calculateKeysFromK(t, k)
  46. assert.Equal(t, pkEth.X.String(), pkBlind.X.String())
  47. assert.Equal(t, pkEth.Y.String(), pkBlind.Y.String())
  48. // fmt.Println("k", k)
  49. // fmt.Println(pkEth)
  50. // fmt.Println(pkBlind)
  51. }
  52. }