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.

196 lines
5.2 KiB

  1. package blindsecp256k1
  2. import (
  3. "encoding/hex"
  4. "math/big"
  5. "testing"
  6. "github.com/ethereum/go-ethereum/crypto"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/require"
  9. )
  10. func TestFlow(t *testing.T) {
  11. // signer: create new signer key pair
  12. sk, err := NewPrivateKey()
  13. require.Nil(t, err)
  14. signerPubK := sk.Public()
  15. // signer: when user requests new R parameter to blind a new msg,
  16. // create new signerR (public) with its secret k
  17. k, signerR, err := NewRequestParameters()
  18. require.Nil(t, err)
  19. // user: blinds the msg using signer's R
  20. // msg := new(big.Int).SetBytes([]byte("test"))
  21. msg := new(big.Int).SetBytes(crypto.Keccak256([]byte("test")))
  22. msgBlinded, userSecretData, err := Blind(msg, signerR)
  23. require.Nil(t, err)
  24. // signer: signs the blinded message using its private key & secret k
  25. sBlind, err := sk.BlindSign(msgBlinded, k)
  26. require.Nil(t, err)
  27. // user: unblinds the blinded signature
  28. sig := Unblind(sBlind, userSecretData)
  29. sigB := sig.Bytes()
  30. sig2, err := NewSignatureFromBytes(sigB)
  31. assert.Nil(t, err)
  32. assert.Equal(t, sig, sig2)
  33. // signature can be verified with signer PublicKey
  34. verified := Verify(msg, sig, signerPubK)
  35. assert.True(t, verified)
  36. }
  37. // func TestSmallBlindedMsg(t *testing.T) {
  38. // sk, err := NewPrivateKey()
  39. // require.Nil(t, err)
  40. // k := big.NewInt(1)
  41. // smallMsgBlinded := big.NewInt(1)
  42. //
  43. // // try to BlindSign a small value
  44. // _, err = sk.BlindSign(smallMsgBlinded, k)
  45. // require.NotNil(t, err)
  46. // require.Equal(t, "mBlinded error: invalid length, need 32 bytes", err.Error())
  47. // }
  48. func TestHashMOddBytes(t *testing.T) {
  49. // This test is made with same values than
  50. // https://github.com/arnaucube/blindsecp256k1-js to ensure
  51. // compatibility
  52. mStr := "3024162961766929396601888431330224482373544644288322432261208139289299439809"
  53. m, ok := new(big.Int).SetString(mStr, 10)
  54. require.True(t, ok)
  55. mBytes := m.Bytes()
  56. hBytes := crypto.Keccak256(mBytes[3:])
  57. h := new(big.Int).SetBytes(hBytes)
  58. assert.Equal(t,
  59. "57523339312508913023232057765773019244858443678197951618720342803494056599369",
  60. h.String())
  61. hBytes = crypto.Keccak256(append(mBytes, []byte{0x12, 0x34}...))
  62. h = new(big.Int).SetBytes(hBytes)
  63. assert.Equal(t,
  64. "9697834584560956691445940439424778243200861871421750951058436814122640359156",
  65. h.String())
  66. }
  67. // func newBigIntWithBitLen(n int) *big.Int {
  68. // b := make([]byte, n/8)
  69. // for i := 0; i < len(b); i++ {
  70. // b[i] = 255
  71. // }
  72. // bi := new(big.Int).SetBytes(b[:])
  73. // return bi
  74. // }
  75. //
  76. // func TestMinBigIntBytesLen(t *testing.T) {
  77. // k := big.NewInt(1)
  78. // sk := PrivateKey(*k)
  79. //
  80. // mBlinded := newBigIntWithBitLen(MinBigIntBytesLen)
  81. // require.Equal(t, MinBigIntBytesLen, mBlinded.BitLen())
  82. // _, err := sk.BlindSign(mBlinded, k)
  83. // assert.Nil(t, err)
  84. //
  85. // mBlinded = new(big.Int).Div(mBlinded, big.NewInt(2))
  86. // require.Equal(t, MinBigIntBytesLen-1, mBlinded.BitLen())
  87. // _, err = sk.BlindSign(mBlinded, k)
  88. // assert.Equal(t, "mBlinded too small", err.Error())
  89. // }
  90. func TestPointCompressDecompress(t *testing.T) {
  91. p := G
  92. b := p.Compress()
  93. assert.Equal(t,
  94. "79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f8179800",
  95. hex.EncodeToString(b[:]))
  96. p2, err := DecompressPoint(b)
  97. require.Nil(t, err)
  98. assert.Equal(t, p, p2)
  99. for i := 2; i < 1000; i++ {
  100. p := G.Mul(big.NewInt(int64(i)))
  101. b := p.Compress()
  102. assert.Equal(t, 33, len(b))
  103. p2, err := DecompressPoint(b)
  104. require.Nil(t, err)
  105. assert.Equal(t, p, p2)
  106. }
  107. }
  108. func TestSignatureCompressDecompress(t *testing.T) {
  109. f := G
  110. sig := &Signature{
  111. S: big.NewInt(1),
  112. F: f,
  113. }
  114. b := sig.Compress()
  115. assert.Equal(t,
  116. "01000000000000000000000000000000000000000000000000000000000000007"+
  117. "9be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f8179800",
  118. hex.EncodeToString(b[:]))
  119. sig2, err := DecompressSignature(b)
  120. require.Nil(t, err)
  121. assert.Equal(t, sig, sig2)
  122. // Q = (P+1)/4
  123. Q := new(big.Int).Div(new(big.Int).Add(P,
  124. big.NewInt(1)), big.NewInt(4)) // nolint:gomnd
  125. f = G
  126. sig = &Signature{
  127. S: Q,
  128. F: f,
  129. }
  130. b = sig.Compress()
  131. assert.Equal(t,
  132. "0cffffbfffffffffffffffffffffffffffffffffffffffffffffffffffffff3f7"+
  133. "9be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f8179800",
  134. hex.EncodeToString(b[:]))
  135. sig2, err = DecompressSignature(b)
  136. require.Nil(t, err)
  137. require.Equal(t, sig, sig2)
  138. for i := 2; i < 10; i++ {
  139. s := new(big.Int).Mod(new(big.Int).Mul(Q, big.NewInt(int64(i))), P)
  140. f := G.Mul(big.NewInt(int64(i)))
  141. sig := &Signature{
  142. S: s,
  143. F: f,
  144. }
  145. b := sig.Compress()
  146. assert.Equal(t, 65, len(b))
  147. sig2, err := DecompressSignature(b)
  148. require.Nil(t, err)
  149. assert.Equal(t, sig, sig2)
  150. }
  151. }
  152. func BenchmarkCompressDecompress(b *testing.B) {
  153. const n = 256
  154. var points [n]*Point
  155. var compPoints [n][33]byte
  156. for i := 0; i < n; i++ {
  157. points[i] = G.Mul(big.NewInt(int64(i)))
  158. }
  159. for i := 0; i < n; i++ {
  160. compPoints[i] = points[i].Compress()
  161. }
  162. b.Run("Compress", func(b *testing.B) {
  163. for i := 0; i < b.N; i++ {
  164. _ = points[i%n].Compress()
  165. }
  166. })
  167. b.Run("DecompressPoint", func(b *testing.B) {
  168. for i := 0; i < b.N; i++ {
  169. _, _ = DecompressPoint(compPoints[i%n])
  170. }
  171. })
  172. }