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.

333 lines
7.8 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. zero *big.Int = big.NewInt(0)
  26. // B (from y^2 = x^3 + B)
  27. B *big.Int = btcec.S256().B
  28. // P represents the secp256k1 finite field
  29. P *big.Int = btcec.S256().P
  30. // Q = (P+1)/4
  31. Q = new(big.Int).Div(new(big.Int).Add(P,
  32. big.NewInt(1)), big.NewInt(4)) // nolint:gomnd
  33. // G represents the base point of secp256k1
  34. G *Point = &Point{
  35. X: btcec.S256().Gx,
  36. Y: btcec.S256().Gy,
  37. }
  38. // N represents the order of G of secp256k1
  39. N *big.Int = btcec.S256().N
  40. )
  41. // Point represents a point on the secp256k1 curve
  42. type Point struct {
  43. X *big.Int
  44. Y *big.Int
  45. }
  46. // Add performs the Point addition
  47. func (p *Point) Add(q *Point) *Point {
  48. x, y := btcec.S256().Add(p.X, p.Y, q.X, q.Y)
  49. return &Point{
  50. X: x,
  51. Y: y,
  52. }
  53. }
  54. // Mul performs the Point scalar multiplication
  55. func (p *Point) Mul(scalar *big.Int) *Point {
  56. x, y := btcec.S256().ScalarMult(p.X, p.Y, scalar.Bytes())
  57. return &Point{
  58. X: x,
  59. Y: y,
  60. }
  61. }
  62. func (p *Point) isValid() error {
  63. if !btcec.S256().IsOnCurve(p.X, p.Y) {
  64. return fmt.Errorf("Point is not on secp256k1")
  65. }
  66. if bytes.Equal(p.X.Bytes(), zero.Bytes()) &&
  67. bytes.Equal(p.Y.Bytes(), zero.Bytes()) {
  68. return fmt.Errorf("Point (%s, %s) can not be (0, 0)",
  69. p.X.String(), p.Y.String())
  70. }
  71. return nil
  72. }
  73. // Compress packs a Point to a byte array of 33 bytes, encoded in little-endian.
  74. func (p *Point) Compress() [33]byte {
  75. xBytes := p.X.Bytes()
  76. odd := byte(0)
  77. if isOdd(p.Y) {
  78. odd = byte(1)
  79. }
  80. var b [33]byte
  81. copy(b[32-len(xBytes):32], xBytes)
  82. b[32] = odd
  83. return b
  84. }
  85. func isOdd(b *big.Int) bool {
  86. return b.Bit(0) != 0
  87. }
  88. // DecompressPoint unpacks a Point from the given byte array of 33 bytes
  89. // https://bitcointalk.org/index.php?topic=162805.msg1712294#msg1712294
  90. func DecompressPoint(b [33]byte) (*Point, error) {
  91. x := new(big.Int).SetBytes(b[:32])
  92. var odd bool
  93. if b[32] == byte(1) {
  94. odd = true
  95. }
  96. // secp256k1: y2 = x3+ ax2 + b (where A==0, B==7)
  97. // compute x^3 + B mod p
  98. x3 := new(big.Int).Mul(x, x)
  99. x3 = new(big.Int).Mul(x3, x)
  100. // x3 := new(big.Int).Exp(x, big.NewInt(3), nil)
  101. x3 = new(big.Int).Add(x3, B)
  102. x3 = new(big.Int).Mod(x3, P)
  103. // sqrt mod p of x^3 + B
  104. y := new(big.Int).ModSqrt(x3, P)
  105. if y == nil {
  106. return nil, fmt.Errorf("not sqrt mod of x^3")
  107. }
  108. if odd != isOdd(y) {
  109. y = new(big.Int).Sub(P, y)
  110. // TODO if needed Mod
  111. }
  112. // check that y is a square root of x^3 + B
  113. y2 := new(big.Int).Mul(y, y)
  114. y2 = new(big.Int).Mod(y2, P)
  115. if !bytes.Equal(y2.Bytes(), x3.Bytes()) {
  116. return nil, fmt.Errorf("invalid square root")
  117. }
  118. if odd != isOdd(y) {
  119. return nil, fmt.Errorf("odd does not match oddness")
  120. }
  121. p := &Point{X: x, Y: y}
  122. return p, nil
  123. }
  124. // WIP
  125. func newRand() *big.Int {
  126. var b [32]byte
  127. _, err := rand.Read(b[:])
  128. if err != nil {
  129. panic(err)
  130. }
  131. bi := new(big.Int).SetBytes(b[:])
  132. return new(big.Int).Mod(bi, N)
  133. }
  134. // PrivateKey represents the signer's private key
  135. type PrivateKey big.Int
  136. // PublicKey represents the signer's public key
  137. type PublicKey Point
  138. // NewPrivateKey returns a new random private key
  139. func NewPrivateKey() *PrivateKey {
  140. k := newRand()
  141. sk := PrivateKey(*k)
  142. return &sk
  143. }
  144. // BigInt returns a *big.Int representation of the PrivateKey
  145. func (sk *PrivateKey) BigInt() *big.Int {
  146. return (*big.Int)(sk)
  147. }
  148. // Public returns the PublicKey from the PrivateKey
  149. func (sk *PrivateKey) Public() *PublicKey {
  150. Q := G.Mul(sk.BigInt())
  151. pk := PublicKey(*Q)
  152. return &pk
  153. }
  154. // Point returns a *Point representation of the PublicKey
  155. func (pk *PublicKey) Point() *Point {
  156. return (*Point)(pk)
  157. }
  158. // NewRequestParameters returns a new random k (secret) & R (public) parameters
  159. func NewRequestParameters() (*big.Int, *Point) {
  160. k := newRand()
  161. return k, G.Mul(k) // R = kG
  162. }
  163. // BlindSign performs the blind signature on the given mBlinded using the
  164. // PrivateKey and the secret k values
  165. func (sk *PrivateKey) BlindSign(mBlinded *big.Int, k *big.Int) (*big.Int, error) {
  166. // TODO add pending checks
  167. if mBlinded.Cmp(N) != -1 {
  168. return nil, fmt.Errorf("mBlinded not inside the finite field")
  169. }
  170. if bytes.Equal(mBlinded.Bytes(), big.NewInt(0).Bytes()) {
  171. return nil, fmt.Errorf("mBlinded can not be 0")
  172. }
  173. // TMP
  174. // if mBlinded.BitLen() < MinBigIntBytesLen {
  175. // return nil, fmt.Errorf("mBlinded too small")
  176. // }
  177. // s' = dm' + k
  178. sBlind := new(big.Int).Add(
  179. new(big.Int).Mul(sk.BigInt(), mBlinded),
  180. k)
  181. sBlind = new(big.Int).Mod(sBlind, N)
  182. return sBlind, nil
  183. }
  184. // UserSecretData contains the secret values from the User (a, b) and the
  185. // public F
  186. type UserSecretData struct {
  187. A *big.Int
  188. B *big.Int
  189. F *Point // public (in the paper is named R)
  190. }
  191. // Blind performs the blinding operation on m using signerR parameter
  192. func Blind(m *big.Int, signerR *Point) (*big.Int, *UserSecretData, error) {
  193. if err := signerR.isValid(); err != nil {
  194. return nil, nil, fmt.Errorf("signerR %s", err)
  195. }
  196. u := &UserSecretData{}
  197. u.A = newRand()
  198. u.B = newRand()
  199. // (R) F = aR' + bG
  200. aR := signerR.Mul(u.A)
  201. bG := G.Mul(u.B)
  202. u.F = aR.Add(bG)
  203. // TODO check that F != O (point at infinity)
  204. if err := u.F.isValid(); err != nil {
  205. return nil, nil, fmt.Errorf("u.F %s", err)
  206. }
  207. rx := new(big.Int).Mod(u.F.X, N)
  208. // m' = a^-1 rx h(m)
  209. ainv := new(big.Int).ModInverse(u.A, N)
  210. ainvrx := new(big.Int).Mul(ainv, rx)
  211. hBytes := crypto.Keccak256(m.Bytes())
  212. h := new(big.Int).SetBytes(hBytes)
  213. mBlinded := new(big.Int).Mul(ainvrx, h)
  214. mBlinded = new(big.Int).Mod(mBlinded, N)
  215. return mBlinded, u, nil
  216. }
  217. // Signature contains the signature values S & F
  218. type Signature struct {
  219. S *big.Int
  220. F *Point
  221. }
  222. // Compress packs a Signature to a byte array of 65 bytes, encoded in
  223. // little-endian.
  224. func (s *Signature) Compress() [65]byte {
  225. var b [65]byte
  226. sBytes := s.S.Bytes()
  227. fBytes := s.F.Compress()
  228. copy(b[:32], swapEndianness(sBytes))
  229. copy(b[32:], fBytes[:])
  230. return b
  231. }
  232. // DecompressSignature unpacks a Signature from the given byte array of 65 bytes
  233. func DecompressSignature(b [65]byte) (*Signature, error) {
  234. s := new(big.Int).SetBytes(swapEndianness(b[:32]))
  235. var fBytes [33]byte
  236. copy(fBytes[:], b[32:])
  237. f, err := DecompressPoint(fBytes)
  238. if err != nil {
  239. return nil, err
  240. }
  241. sig := &Signature{S: s, F: f}
  242. return sig, nil
  243. }
  244. // Unblind performs the unblinding operation of the blinded signature for the
  245. // given the UserSecretData
  246. func Unblind(sBlind *big.Int, u *UserSecretData) *Signature {
  247. // s = a s' + b
  248. as := new(big.Int).Mul(u.A, sBlind)
  249. s := new(big.Int).Add(as, u.B)
  250. s = new(big.Int).Mod(s, N)
  251. return &Signature{
  252. S: s,
  253. F: u.F,
  254. }
  255. }
  256. // Verify checks the signature of the message m for the given PublicKey
  257. func Verify(m *big.Int, s *Signature, q *PublicKey) bool {
  258. // TODO add pending checks
  259. if err := s.F.isValid(); err != nil {
  260. return false
  261. }
  262. if err := q.Point().isValid(); err != nil {
  263. return false
  264. }
  265. sG := G.Mul(s.S) // sG
  266. hBytes := crypto.Keccak256(m.Bytes())
  267. h := new(big.Int).SetBytes(hBytes)
  268. rx := new(big.Int).Mod(s.F.X, N)
  269. rxh := new(big.Int).Mul(rx, h)
  270. // rxhG := G.Mul(rxh) // originally the paper uses G
  271. rxhG := q.Point().Mul(rxh)
  272. right := s.F.Add(rxhG)
  273. // check sG == R + rx h(m) Q (where R in this code is F)
  274. if bytes.Equal(sG.X.Bytes(), right.X.Bytes()) &&
  275. bytes.Equal(sG.Y.Bytes(), right.Y.Bytes()) {
  276. return true
  277. }
  278. return false
  279. }