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.

86 lines
2.6 KiB

  1. package blindsecp256k1
  2. import (
  3. "math/big"
  4. "testing"
  5. "github.com/ethereum/go-ethereum/crypto"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. )
  9. func TestFlow(t *testing.T) {
  10. // signer: create new signer key pair
  11. sk := NewPrivateKey()
  12. signerPubK := sk.Public()
  13. // signer: when user requests new R parameter to blind a new msg,
  14. // create new signerR (public) with its secret k
  15. k, signerR := NewRequestParameters()
  16. // user: blinds the msg using signer's R
  17. msg := new(big.Int).SetBytes([]byte("test"))
  18. msgBlinded, userSecretData, err := Blind(msg, signerR)
  19. require.Nil(t, err)
  20. // signer: signs the blinded message using its private key & secret k
  21. sBlind, err := sk.BlindSign(msgBlinded, k)
  22. require.Nil(t, err)
  23. // user: unblinds the blinded signature
  24. sig := Unblind(sBlind, userSecretData)
  25. sigB := sig.Bytes()
  26. sig2, err := NewSignatureFromBytes(sigB)
  27. assert.Nil(t, err)
  28. assert.Equal(t, sig, sig2)
  29. // signature can be verified with signer PublicKey
  30. verified := Verify(msg, sig, signerPubK)
  31. assert.True(t, verified)
  32. }
  33. func TestHashMOddBytes(t *testing.T) {
  34. // This test is made with same values than
  35. // https://github.com/arnaucube/blindsecp256k1-js to ensure
  36. // compatibility
  37. mStr := "3024162961766929396601888431330224482373544644288322432261208139289299439809"
  38. m, ok := new(big.Int).SetString(mStr, 10)
  39. require.True(t, ok)
  40. mBytes := m.Bytes()
  41. hBytes := crypto.Keccak256(mBytes[3:])
  42. h := new(big.Int).SetBytes(hBytes)
  43. assert.Equal(t,
  44. "57523339312508913023232057765773019244858443678197951618720342803494056599369",
  45. h.String())
  46. hBytes = crypto.Keccak256(append(mBytes, []byte{0x12, 0x34}...))
  47. h = new(big.Int).SetBytes(hBytes)
  48. assert.Equal(t,
  49. "9697834584560956691445940439424778243200861871421750951058436814122640359156",
  50. h.String())
  51. }
  52. // func newBigIntWithBitLen(n int) *big.Int {
  53. // b := make([]byte, n/8)
  54. // for i := 0; i < len(b); i++ {
  55. // b[i] = 255
  56. // }
  57. // bi := new(big.Int).SetBytes(b[:])
  58. // return bi
  59. // }
  60. //
  61. // func TestMinBigIntBytesLen(t *testing.T) {
  62. // k := big.NewInt(1)
  63. // sk := PrivateKey(*k)
  64. //
  65. // mBlinded := newBigIntWithBitLen(MinBigIntBytesLen)
  66. // require.Equal(t, MinBigIntBytesLen, mBlinded.BitLen())
  67. // _, err := sk.BlindSign(mBlinded, k)
  68. // assert.Nil(t, err)
  69. //
  70. // mBlinded = new(big.Int).Div(mBlinded, big.NewInt(2))
  71. // require.Equal(t, MinBigIntBytesLen-1, mBlinded.BitLen())
  72. // _, err = sk.BlindSign(mBlinded, k)
  73. // assert.Equal(t, "mBlinded too small", err.Error())
  74. // }