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.

239 lines
5.5 KiB

3 years ago
  1. // Package blindsecp256k1 implements the Blind signature scheme explained at
  2. // "New Blind Signature Schemes Based on the (Elliptic Curve) Discrete
  3. // Logarithm Problem", by Hamid Mala & Nafiseh Nezhadansari
  4. // https://sci-hub.do/10.1109/ICCKE.2013.6682844
  5. //
  6. // LICENSE can be found at https://github.com/arnaucube/go-blindsecp256k1/blob/master/LICENSE
  7. //
  8. package blindsecp256k1
  9. // WARNING: WIP code
  10. import (
  11. "bytes"
  12. "crypto/rand"
  13. "fmt"
  14. "math/big"
  15. "github.com/btcsuite/btcd/btcec"
  16. "github.com/ethereum/go-ethereum/crypto"
  17. )
  18. // TMP
  19. // const (
  20. // // MinBigIntBytesLen defines the minimum bytes length of the minimum
  21. // // accepted value for the checked *big.Int
  22. // MinBigIntBytesLen = 20 * 8
  23. // )
  24. var (
  25. // G represents the base point of secp256k1
  26. G *Point = &Point{
  27. X: btcec.S256().Gx,
  28. Y: btcec.S256().Gy,
  29. }
  30. // N represents the order of G of secp256k1
  31. N *big.Int = btcec.S256().N
  32. zero *big.Int = big.NewInt(0)
  33. )
  34. // Point represents a point on the secp256k1 curve
  35. type Point struct {
  36. X *big.Int
  37. Y *big.Int
  38. }
  39. // Add performs the Point addition
  40. func (p *Point) Add(q *Point) *Point {
  41. x, y := btcec.S256().Add(p.X, p.Y, q.X, q.Y)
  42. return &Point{
  43. X: x,
  44. Y: y,
  45. }
  46. }
  47. // Mul performs the Point scalar multiplication
  48. func (p *Point) Mul(scalar *big.Int) *Point {
  49. x, y := btcec.S256().ScalarMult(p.X, p.Y, scalar.Bytes())
  50. return &Point{
  51. X: x,
  52. Y: y,
  53. }
  54. }
  55. func (p *Point) isValid() error {
  56. if !btcec.S256().IsOnCurve(p.X, p.Y) {
  57. return fmt.Errorf("Point is not on secp256k1")
  58. }
  59. if bytes.Equal(p.X.Bytes(), zero.Bytes()) &&
  60. bytes.Equal(p.Y.Bytes(), zero.Bytes()) {
  61. return fmt.Errorf("Point (%s, %s) can not be (0, 0)",
  62. p.X.String(), p.Y.String())
  63. }
  64. return nil
  65. }
  66. // WIP
  67. func newRand() *big.Int {
  68. var b [32]byte
  69. _, err := rand.Read(b[:])
  70. if err != nil {
  71. panic(err)
  72. }
  73. bi := new(big.Int).SetBytes(b[:])
  74. return new(big.Int).Mod(bi, N)
  75. }
  76. // PrivateKey represents the signer's private key
  77. type PrivateKey big.Int
  78. // PublicKey represents the signer's public key
  79. type PublicKey Point
  80. // NewPrivateKey returns a new random private key
  81. func NewPrivateKey() *PrivateKey {
  82. k := newRand()
  83. sk := PrivateKey(*k)
  84. return &sk
  85. }
  86. // BigInt returns a *big.Int representation of the PrivateKey
  87. func (sk *PrivateKey) BigInt() *big.Int {
  88. return (*big.Int)(sk)
  89. }
  90. // Public returns the PublicKey from the PrivateKey
  91. func (sk *PrivateKey) Public() *PublicKey {
  92. Q := G.Mul(sk.BigInt())
  93. pk := PublicKey(*Q)
  94. return &pk
  95. }
  96. // Point returns a *Point representation of the PublicKey
  97. func (pk *PublicKey) Point() *Point {
  98. return (*Point)(pk)
  99. }
  100. // NewRequestParameters returns a new random k (secret) & R (public) parameters
  101. func NewRequestParameters() (*big.Int, *Point) {
  102. k := newRand()
  103. return k, G.Mul(k) // R = kG
  104. }
  105. // BlindSign performs the blind signature on the given mBlinded using the
  106. // PrivateKey and the secret k values
  107. func (sk *PrivateKey) BlindSign(mBlinded *big.Int, k *big.Int) (*big.Int, error) {
  108. // TODO add pending checks
  109. if mBlinded.Cmp(N) != -1 {
  110. return nil, fmt.Errorf("mBlinded not inside the finite field")
  111. }
  112. if bytes.Equal(mBlinded.Bytes(), big.NewInt(0).Bytes()) {
  113. return nil, fmt.Errorf("mBlinded can not be 0")
  114. }
  115. // TMP
  116. // if mBlinded.BitLen() < MinBigIntBytesLen {
  117. // return nil, fmt.Errorf("mBlinded too small")
  118. // }
  119. // s' = dm' + k
  120. sBlind := new(big.Int).Add(
  121. new(big.Int).Mul(sk.BigInt(), mBlinded),
  122. k)
  123. sBlind = new(big.Int).Mod(sBlind, N)
  124. return sBlind, nil
  125. }
  126. // UserSecretData contains the secret values from the User (a, b) and the
  127. // public F
  128. type UserSecretData struct {
  129. A *big.Int
  130. B *big.Int
  131. F *Point // public (in the paper is named R)
  132. }
  133. // Blind performs the blinding operation on m using signerR parameter
  134. func Blind(m *big.Int, signerR *Point) (*big.Int, *UserSecretData, error) {
  135. if !btcec.S256().IsOnCurve(signerR.X, signerR.Y) {
  136. return nil, nil, fmt.Errorf("signerR point is not on secp256k1")
  137. }
  138. u := &UserSecretData{}
  139. u.A = newRand()
  140. u.B = newRand()
  141. // (R) F = aR' + bG
  142. aR := signerR.Mul(u.A)
  143. bG := G.Mul(u.B)
  144. u.F = aR.Add(bG)
  145. // TODO check that F != O (point at infinity)
  146. if err := u.F.isValid(); err != nil {
  147. return nil, nil, err
  148. }
  149. rx := new(big.Int).Mod(u.F.X, N)
  150. // m' = a^-1 rx h(m)
  151. ainv := new(big.Int).ModInverse(u.A, N)
  152. ainvrx := new(big.Int).Mul(ainv, rx)
  153. hBytes := crypto.Keccak256(m.Bytes())
  154. h := new(big.Int).SetBytes(hBytes)
  155. mBlinded := new(big.Int).Mul(ainvrx, h)
  156. mBlinded = new(big.Int).Mod(mBlinded, N)
  157. return mBlinded, u, nil
  158. }
  159. // Signature contains the signature values S & F
  160. type Signature struct {
  161. S *big.Int
  162. F *Point
  163. }
  164. // Unblind performs the unblinding operation of the blinded signature for the
  165. // given the UserSecretData
  166. func Unblind(sBlind *big.Int, u *UserSecretData) *Signature {
  167. // s = a s' + b
  168. as := new(big.Int).Mul(u.A, sBlind)
  169. s := new(big.Int).Add(as, u.B)
  170. s = new(big.Int).Mod(s, N)
  171. return &Signature{
  172. S: s,
  173. F: u.F,
  174. }
  175. }
  176. // Verify checks the signature of the message m for the given PublicKey
  177. func Verify(m *big.Int, s *Signature, q *PublicKey) bool {
  178. // TODO add pending checks
  179. if err := s.F.isValid(); err != nil {
  180. return false
  181. }
  182. if err := q.Point().isValid(); err != nil {
  183. return false
  184. }
  185. sG := G.Mul(s.S) // sG
  186. hBytes := crypto.Keccak256(m.Bytes())
  187. h := new(big.Int).SetBytes(hBytes)
  188. rx := new(big.Int).Mod(s.F.X, N)
  189. rxh := new(big.Int).Mul(rx, h)
  190. // rxhG := G.Mul(rxh) // originally the paper uses G
  191. rxhG := q.Point().Mul(rxh)
  192. right := s.F.Add(rxhG)
  193. // check sG == R + rx h(m) Q (where R in this code is F)
  194. if bytes.Equal(sG.X.Bytes(), right.X.Bytes()) &&
  195. bytes.Equal(sG.Y.Bytes(), right.Y.Bytes()) {
  196. return true
  197. }
  198. return false
  199. }