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.6 KiB

  1. package blindsecp256k1
  2. import (
  3. "math/big"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/stretchr/testify/require"
  7. )
  8. func TestFlow(t *testing.T) {
  9. // signer: create new signer key pair
  10. sk := NewPrivateKey()
  11. signerPubK := sk.Public()
  12. // signer: when user requests new R parameter to blind a new msg,
  13. // create new signerR (public) with its secret k
  14. k, signerR := NewRequestParameters()
  15. // user: blinds the msg using signer's R
  16. msg := new(big.Int).SetBytes([]byte("test"))
  17. msgBlinded, userSecretData, err := Blind(msg, signerR)
  18. require.Nil(t, err)
  19. // signer: signs the blinded message using its private key & secret k
  20. sBlind, err := sk.BlindSign(msgBlinded, k)
  21. require.Nil(t, err)
  22. // user: unblinds the blinded signature
  23. sig := Unblind(sBlind, userSecretData)
  24. sigB := sig.Bytes()
  25. sig2, err := NewSignatureFromBytes(sigB)
  26. assert.Nil(t, err)
  27. assert.Equal(t, sig, sig2)
  28. // signature can be verified with signer PublicKey
  29. verified := Verify(msg, sig, signerPubK)
  30. assert.True(t, verified)
  31. }
  32. func newBigIntWithBitLen(n int) *big.Int {
  33. b := make([]byte, n/8)
  34. for i := 0; i < len(b); i++ {
  35. b[i] = 255
  36. }
  37. bi := new(big.Int).SetBytes(b[:])
  38. return bi
  39. }
  40. func TestMinBigIntBytesLen(t *testing.T) {
  41. k := big.NewInt(1)
  42. sk := PrivateKey(*k)
  43. mBlinded := newBigIntWithBitLen(MinBigIntBytesLen)
  44. require.Equal(t, MinBigIntBytesLen, mBlinded.BitLen())
  45. _, err := sk.BlindSign(mBlinded, k)
  46. assert.Nil(t, err)
  47. mBlinded = new(big.Int).Div(mBlinded, big.NewInt(2))
  48. require.Equal(t, MinBigIntBytesLen-1, mBlinded.BitLen())
  49. _, err = sk.BlindSign(mBlinded, k)
  50. assert.Equal(t, "mBlinded too small", err.Error())
  51. }