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.2 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.st/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/ecdsa"
  13. "crypto/rand"
  14. "fmt"
  15. "math/big"
  16. "github.com/ethereum/go-ethereum/crypto"
  17. "github.com/ethereum/go-ethereum/crypto/secp256k1"
  18. )
  19. var (
  20. s256 *secp256k1.BitCurve = secp256k1.S256()
  21. zero *big.Int = big.NewInt(0)
  22. // B (from y^2 = x^3 + B)
  23. B *big.Int = s256.B
  24. // P represents the secp256k1 finite field
  25. P *big.Int = s256.P
  26. // G represents the base point of secp256k1
  27. G *Point = &Point{
  28. X: s256.Gx,
  29. Y: s256.Gy,
  30. }
  31. // N represents the order of G of secp256k1
  32. N *big.Int = s256.N
  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 := 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 := 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 !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. // Compress packs a Point to a byte array of 33 bytes, encoded in little-endian.
  67. func (p *Point) Compress() [33]byte {
  68. xBytes := p.X.Bytes()
  69. odd := byte(0)
  70. if isOdd(p.Y) {
  71. odd = byte(1)
  72. }
  73. var b [33]byte
  74. copy(b[32-len(xBytes):32], xBytes)
  75. b[32] = odd
  76. return b
  77. }
  78. func isOdd(b *big.Int) bool {
  79. return b.Bit(0) != 0
  80. }
  81. // DecompressPoint unpacks a Point from the given byte array of 33 bytes
  82. // https://bitcointalk.org/index.php?topic=162805.msg1712294#msg1712294
  83. func DecompressPoint(b [33]byte) (*Point, error) {
  84. x := new(big.Int).SetBytes(b[:32])
  85. var odd bool
  86. if b[32] == byte(1) {
  87. odd = true
  88. }
  89. // secp256k1: y2 = x3+ ax2 + b (where A==0, B==7)
  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() (*big.Int, error) {
  119. pk, err := ecdsa.GenerateKey(s256, rand.Reader)
  120. if err != nil {
  121. return nil, err
  122. }
  123. return pk.D, nil
  124. }
  125. // PrivateKey represents the signer's private key
  126. type PrivateKey big.Int
  127. // PublicKey represents the signer's public key
  128. type PublicKey Point
  129. // NewPrivateKey returns a new random private key
  130. func NewPrivateKey() (*PrivateKey, error) {
  131. k, err := newRand()
  132. if err != nil {
  133. return nil, err
  134. }
  135. if err := checkBigIntSize(k); err != nil {
  136. return nil, fmt.Errorf("k error: %s", err)
  137. }
  138. sk := PrivateKey(*k)
  139. return &sk, nil
  140. }
  141. // BigInt returns a *big.Int representation of the PrivateKey
  142. func (sk *PrivateKey) BigInt() *big.Int {
  143. return (*big.Int)(sk)
  144. }
  145. // Public returns the PublicKey from the PrivateKey
  146. func (sk *PrivateKey) Public() *PublicKey {
  147. q := G.Mul(sk.BigInt())
  148. pk := PublicKey(*q)
  149. return &pk
  150. }
  151. // Point returns a *Point representation of the PublicKey
  152. func (pk *PublicKey) Point() *Point {
  153. return (*Point)(pk)
  154. }
  155. // NewRequestParameters returns a new random k (secret) & R (public) parameters
  156. func NewRequestParameters() (*big.Int, *Point, error) {
  157. k, err := newRand()
  158. if err != nil {
  159. return nil, nil, err
  160. }
  161. // k, R = kG
  162. return k, G.Mul(k), nil
  163. }
  164. func checkBigIntSize(b *big.Int) error {
  165. // check b.Bytes()==32, as go returns big-endian representation of the
  166. // bigint, so if length is not 32 we have a smaller value than expected
  167. if len(b.Bytes()) != 32 { //nolint:gomnd
  168. return fmt.Errorf("invalid length, need 32 bytes")
  169. }
  170. return nil
  171. }
  172. // BlindSign performs the blind signature on the given mBlinded using the
  173. // PrivateKey and the secret k values.
  174. func (sk *PrivateKey) BlindSign(mBlinded *big.Int, k *big.Int) (*big.Int, error) {
  175. // TODO add pending checks
  176. if mBlinded.Cmp(N) != -1 {
  177. return nil, fmt.Errorf("mBlinded not inside the finite field")
  178. }
  179. if bytes.Equal(mBlinded.Bytes(), big.NewInt(0).Bytes()) {
  180. return nil, fmt.Errorf("mBlinded can not be 0")
  181. }
  182. if err := checkBigIntSize(mBlinded); err != nil {
  183. return nil, fmt.Errorf("mBlinded error: %s", err)
  184. }
  185. if err := checkBigIntSize(k); err != nil {
  186. return nil, fmt.Errorf("k error: %s", err)
  187. }
  188. // s' = dm' + k
  189. sBlind := new(big.Int).Add(
  190. new(big.Int).Mul(sk.BigInt(), mBlinded),
  191. k)
  192. sBlind = new(big.Int).Mod(sBlind, N)
  193. return sBlind, nil
  194. }
  195. // UserSecretData contains the secret values from the User (a, b) and the
  196. // public F
  197. type UserSecretData struct {
  198. A *big.Int
  199. B *big.Int
  200. F *Point // public (in the paper is named R)
  201. }
  202. // Blind performs the blinding operation on m using signerR parameter
  203. func Blind(m *big.Int, signerR *Point) (*big.Int, *UserSecretData, error) {
  204. if err := signerR.isValid(); err != nil {
  205. return nil, nil, fmt.Errorf("signerR %s", err)
  206. }
  207. var err error
  208. u := &UserSecretData{}
  209. u.A, err = newRand()
  210. if err != nil {
  211. return nil, nil, err
  212. }
  213. u.B, err = newRand()
  214. if err != nil {
  215. return nil, nil, err
  216. }
  217. // (R) F = aR' + bG
  218. aR := signerR.Mul(u.A)
  219. bG := G.Mul(u.B)
  220. u.F = aR.Add(bG)
  221. if err := u.F.isValid(); err != nil {
  222. return nil, nil, fmt.Errorf("u.F %s", err)
  223. }
  224. rx := new(big.Int).Mod(u.F.X, N)
  225. // m' = a^-1 rx h(m)
  226. ainv := new(big.Int).ModInverse(u.A, N)
  227. ainvrx := new(big.Int).Mul(ainv, rx)
  228. hBytes := crypto.Keccak256(m.Bytes())
  229. h := new(big.Int).SetBytes(hBytes)
  230. mBlinded := new(big.Int).Mul(ainvrx, h)
  231. mBlinded = new(big.Int).Mod(mBlinded, N)
  232. return mBlinded, u, nil
  233. }
  234. // Signature contains the signature values S & F
  235. type Signature struct {
  236. S *big.Int
  237. F *Point
  238. }
  239. // Compress packs a Signature to a byte array of 65 bytes, encoded in
  240. // little-endian.
  241. func (s *Signature) Compress() [65]byte {
  242. var b [65]byte
  243. sBytes := s.S.Bytes()
  244. fBytes := s.F.Compress()
  245. copy(b[:32], swapEndianness(sBytes))
  246. copy(b[32:], fBytes[:])
  247. return b
  248. }
  249. // DecompressSignature unpacks a Signature from the given byte array of 65 bytes
  250. func DecompressSignature(b [65]byte) (*Signature, error) {
  251. s := new(big.Int).SetBytes(swapEndianness(b[:32]))
  252. var fBytes [33]byte
  253. copy(fBytes[:], b[32:])
  254. f, err := DecompressPoint(fBytes)
  255. if err != nil {
  256. return nil, err
  257. }
  258. sig := &Signature{S: s, F: f}
  259. return sig, nil
  260. }
  261. // Unblind performs the unblinding operation of the blinded signature for the
  262. // given the UserSecretData
  263. func Unblind(sBlind *big.Int, u *UserSecretData) *Signature {
  264. // s = a s' + b
  265. as := new(big.Int).Mul(u.A, sBlind)
  266. s := new(big.Int).Add(as, u.B)
  267. s = new(big.Int).Mod(s, N)
  268. return &Signature{
  269. S: s,
  270. F: u.F,
  271. }
  272. }
  273. // Verify checks the signature of the message m for the given PublicKey
  274. func Verify(m *big.Int, s *Signature, q *PublicKey) bool {
  275. // TODO add pending checks
  276. if err := s.F.isValid(); err != nil {
  277. return false
  278. }
  279. if err := q.Point().isValid(); err != nil {
  280. return false
  281. }
  282. sG := G.Mul(s.S) // sG
  283. hBytes := crypto.Keccak256(m.Bytes())
  284. h := new(big.Int).SetBytes(hBytes)
  285. rx := new(big.Int).Mod(s.F.X, N)
  286. rxh := new(big.Int).Mul(rx, h)
  287. // do mod, as go-ethereum/crypto/secp256k1 can not handle scalars > 256 bits
  288. rxhMod := new(big.Int).Mod(rxh, N)
  289. // rxhG := G.Mul(rxh) // originally the paper uses G
  290. rxhG := q.Point().Mul(rxhMod)
  291. right := s.F.Add(rxhG)
  292. // check sG == R + rx h(m) Q (where R in this code is F)
  293. if bytes.Equal(sG.X.Bytes(), right.X.Bytes()) &&
  294. bytes.Equal(sG.Y.Bytes(), right.Y.Bytes()) {
  295. return true
  296. }
  297. return false
  298. }