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.

338 lines
8.0 KiB

3 years ago
3 years ago
3 years ago
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. var (
  19. // G represents the base point of secp256k1
  20. G *Point = &Point{
  21. X: btcec.S256().Gx,
  22. Y: btcec.S256().Gy,
  23. }
  24. // N represents the order of G of secp256k1
  25. N *big.Int = btcec.S256().N
  26. // B (from y^2 = x^3 + B)
  27. B *big.Int = btcec.S256().B
  28. // Q = (P+1)/4
  29. Q = new(big.Int).Div(new(big.Int).Add(btcec.S256().P,
  30. big.NewInt(1)), big.NewInt(4))
  31. )
  32. // Point represents a point on the secp256k1 curve
  33. type Point struct {
  34. X *big.Int
  35. Y *big.Int
  36. }
  37. // Add performs the Point addition
  38. func (p *Point) Add(q *Point) *Point {
  39. x, y := btcec.S256().Add(p.X, p.Y, q.X, q.Y)
  40. return &Point{
  41. X: x,
  42. Y: y,
  43. }
  44. }
  45. // Mul performs the Point scalar multiplication
  46. func (p *Point) Mul(scalar *big.Int) *Point {
  47. x, y := btcec.S256().ScalarMult(p.X, p.Y, scalar.Bytes())
  48. return &Point{
  49. X: x,
  50. Y: y,
  51. }
  52. }
  53. func (p *Point) Compress() [33]byte {
  54. xBytes := p.X.Bytes()
  55. sign := byte(0)
  56. if isOdd(p.Y) {
  57. sign = byte(1)
  58. }
  59. var b [33]byte
  60. copy(b[32-len(xBytes):32], xBytes)
  61. b[32] = sign
  62. return b
  63. }
  64. func isOdd(b *big.Int) bool {
  65. return b.Bit(0) != 0
  66. }
  67. func squareMul(r, x *big.Int, bit bool) *big.Int {
  68. // r = new(big.Int).Mul(r, r) // r^2
  69. r = new(big.Int).Exp(r, big.NewInt(2), N)
  70. if bit {
  71. r = new(big.Int).Mul(r, x)
  72. }
  73. return new(big.Int).Mod(r, N)
  74. }
  75. // https://en.wikipedia.org/wiki/Exponentiation_by_squaring
  76. func sqrtQ(x *big.Int) *big.Int {
  77. // xBytes := x.Bytes()
  78. qBytes := Q.Bytes()
  79. r := big.NewInt(1)
  80. // fmt.Println(hex.EncodeToString(qBytes))
  81. for _, b := range qBytes {
  82. // fmt.Printf("%d, %x %d\n", i, b, r)
  83. // fmt.Printf("%x %s\n", b, r.String())
  84. switch b {
  85. // Most common case, where all 8 bits are set.
  86. case 0xff:
  87. r = squareMul(r, x, true)
  88. r = squareMul(r, x, true)
  89. r = squareMul(r, x, true)
  90. r = squareMul(r, x, true)
  91. r = squareMul(r, x, true)
  92. r = squareMul(r, x, true)
  93. r = squareMul(r, x, true)
  94. r = squareMul(r, x, true)
  95. // fmt.Printf("%x %s\n", b, r.String())
  96. // First byte of Q (0x3f), where all but the top two bits are
  97. // set. Note that this case only applies six operations, since
  98. // the highest bit of Q resides in bit six of the first byte. We
  99. // ignore the first two bits, since squaring for these bits will
  100. // result in an invalid result. We forgo squaring f before the
  101. // first multiply, since 1^2 = 1.
  102. case 0x3f:
  103. r = new(big.Int).Mul(r, x)
  104. r = squareMul(r, x, true)
  105. r = squareMul(r, x, true)
  106. r = squareMul(r, x, true)
  107. r = squareMul(r, x, true)
  108. r = squareMul(r, x, true)
  109. // Byte 28 of Q (0xbf), where only bit 7 is unset.
  110. case 0xbf:
  111. r = squareMul(r, x, true)
  112. r = squareMul(r, x, false)
  113. r = squareMul(r, x, true)
  114. r = squareMul(r, x, true)
  115. r = squareMul(r, x, true)
  116. r = squareMul(r, x, true)
  117. r = squareMul(r, x, true)
  118. r = squareMul(r, x, true)
  119. // Byte 31 of Q (0x0c), where only bits 3 and 4 are set.
  120. default:
  121. r = squareMul(r, x, false)
  122. r = squareMul(r, x, false)
  123. r = squareMul(r, x, false)
  124. r = squareMul(r, x, false)
  125. r = squareMul(r, x, true)
  126. r = squareMul(r, x, true)
  127. r = squareMul(r, x, false)
  128. r = squareMul(r, x, false)
  129. }
  130. }
  131. return r
  132. }
  133. // https://bitcointalk.org/index.php?topic=162805.msg1712294#msg1712294
  134. // func (p *Point) Decompress(b [33]byte) error {
  135. func Decompress(b [33]byte) (*Point, error) {
  136. fmt.Println(b)
  137. x := new(big.Int).SetBytes(b[:32])
  138. fmt.Println(x)
  139. var sign bool
  140. if b[32] == byte(1) {
  141. sign = true
  142. }
  143. // y2 = x3+ ax2 + b (where A==0, B==7)
  144. // compute x^3 + B mod p
  145. x3 := new(big.Int).Mul(x, x)
  146. x3 = new(big.Int).Mul(x3, x)
  147. // x3 := new(big.Int).Exp(x, big.NewInt(3), N)
  148. x3 = new(big.Int).Add(x3, B)
  149. x3 = new(big.Int).Mod(x3, N)
  150. // sqrt mod p of x^3 + B
  151. fmt.Println("x3", x3)
  152. y := new(big.Int).ModSqrt(x3, N)
  153. // y := sqrtQ(x3)
  154. if y == nil {
  155. return nil, fmt.Errorf("not sqrt mod of x^3")
  156. }
  157. fmt.Println("y", y)
  158. fmt.Println("y", new(big.Int).Sub(N, y))
  159. fmt.Println("y", new(big.Int).Mod(new(big.Int).Neg(y), N))
  160. if sign != isOdd(y) {
  161. y = new(big.Int).Sub(N, y)
  162. // TODO check if needed Mod
  163. }
  164. // check that y is a square root of x^3 + B
  165. y2 := new(big.Int).Mul(y, y)
  166. y2 = new(big.Int).Mod(y2, N)
  167. if !bytes.Equal(y2.Bytes(), x3.Bytes()) {
  168. return nil, fmt.Errorf("invalid square root")
  169. }
  170. if sign != isOdd(y) {
  171. return nil, fmt.Errorf("sign does not match oddness")
  172. }
  173. p := &Point{X: x, Y: y}
  174. // p = &Point{}
  175. // p.X = x
  176. // p.Y = y
  177. // fmt.Println("I", p.X, p.Y)
  178. return p, nil
  179. }
  180. // WIP
  181. func newRand() *big.Int {
  182. var b [32]byte
  183. _, err := rand.Read(b[:])
  184. if err != nil {
  185. panic(err)
  186. }
  187. bi := new(big.Int).SetBytes(b[:])
  188. return new(big.Int).Mod(bi, N)
  189. }
  190. // PrivateKey represents the signer's private key
  191. type PrivateKey big.Int
  192. // PublicKey represents the signer's public key
  193. type PublicKey Point
  194. // NewPrivateKey returns a new random private key
  195. func NewPrivateKey() *PrivateKey {
  196. k := newRand()
  197. sk := PrivateKey(*k)
  198. return &sk
  199. }
  200. // BigInt returns a *big.Int representation of the PrivateKey
  201. func (sk *PrivateKey) BigInt() *big.Int {
  202. return (*big.Int)(sk)
  203. }
  204. // Public returns the PublicKey from the PrivateKey
  205. func (sk *PrivateKey) Public() *PublicKey {
  206. Q := G.Mul(sk.BigInt())
  207. pk := PublicKey(*Q)
  208. return &pk
  209. }
  210. // Point returns a *Point representation of the PublicKey
  211. func (pk *PublicKey) Point() *Point {
  212. return (*Point)(pk)
  213. }
  214. // NewRequestParameters returns a new random k (secret) & R (public) parameters
  215. func NewRequestParameters() (*big.Int, *Point) {
  216. k := newRand()
  217. return k, G.Mul(k) // R = kG
  218. }
  219. // BlindSign performs the blind signature on the given mBlinded using the
  220. // PrivateKey and the secret k values
  221. func (sk *PrivateKey) BlindSign(mBlinded *big.Int, k *big.Int) *big.Int {
  222. // TODO add pending checks
  223. // s' = dm' + k
  224. sBlind := new(big.Int).Add(
  225. new(big.Int).Mul(sk.BigInt(), mBlinded),
  226. k)
  227. return sBlind
  228. }
  229. // UserSecretData contains the secret values from the User (a, b, c) and the
  230. // public F
  231. type UserSecretData struct {
  232. A *big.Int
  233. B *big.Int
  234. F *Point // public (in the paper is R)
  235. }
  236. // Blind performs the blinding operation on m using signerR parameter
  237. func Blind(m *big.Int, signerR *Point) (*big.Int, *UserSecretData) {
  238. u := &UserSecretData{}
  239. u.A = newRand()
  240. u.B = newRand()
  241. // (R) F = aR' + bG
  242. aR := signerR.Mul(u.A)
  243. bG := G.Mul(u.B)
  244. u.F = aR.Add(bG)
  245. // TODO check that F != O (point at infinity)
  246. rx := new(big.Int).Mod(u.F.X, N)
  247. // m' = a^-1 rx h(m)
  248. ainv := new(big.Int).ModInverse(u.A, N)
  249. ainvrx := new(big.Int).Mul(ainv, rx)
  250. hBytes := crypto.Keccak256(m.Bytes())
  251. h := new(big.Int).SetBytes(hBytes)
  252. mBlinded := new(big.Int).Mul(ainvrx, h)
  253. return mBlinded, u
  254. }
  255. // Signature contains the signature values S & F
  256. type Signature struct {
  257. S *big.Int
  258. F *Point
  259. }
  260. // Unblind performs the unblinding operation of the blinded signature for the
  261. // given message m and the UserSecretData
  262. func Unblind(sBlind, m *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. return &Signature{
  267. S: s,
  268. F: u.F,
  269. }
  270. }
  271. // Verify checks the signature of the message m for the given PublicKey
  272. func Verify(m *big.Int, s *Signature, q *PublicKey) bool {
  273. // TODO add pending checks
  274. sG := G.Mul(s.S) // sG
  275. hBytes := crypto.Keccak256(m.Bytes())
  276. h := new(big.Int).SetBytes(hBytes)
  277. rx := new(big.Int).Mod(s.F.X, N)
  278. rxh := new(big.Int).Mul(rx, h)
  279. // rxhG := G.Mul(rxh) // originally the paper uses G
  280. rxhG := q.Point().Mul(rxh)
  281. right := s.F.Add(rxhG)
  282. // check sG == R + rx h(m) G (where R in this code is F)
  283. if bytes.Equal(sG.X.Bytes(), right.X.Bytes()) &&
  284. bytes.Equal(sG.Y.Bytes(), right.Y.Bytes()) {
  285. return true
  286. }
  287. return false
  288. }