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.

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