go-blindsecp256k1/blindsecp256k1.go
blindsecp256k1.goRaw
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//
8package blindsecp256k1
9
10// WARNING: WIP code
11
12import (
13 "bytes"
14 "crypto/rand"
15 "math/big"
16
17 "github.com/btcsuite/btcd/btcec"
18 "github.com/ethereum/go-ethereum/crypto"
19)
20
21var (
22 // G represents the base point of secp256k1
23 G *Point = &Point{
24 X: btcec.S256().Gx,
25 Y: btcec.S256().Gy,
26 }
27
28 // N represents the order of G of secp256k1
29 N *big.Int = btcec.S256().N
30)
31
32// Point represents a point on the secp256k1 curve
33type Point struct {
34 X *big.Int
35 Y *big.Int
36}
37
38// Add performs the Point addition
39func (p *Point) Add(q *Point) *Point {
40 x, y := btcec.S256().Add(p.X, p.Y, q.X, q.Y)
41 return &Point{
42 X: x,
43 Y: y,
44 }
45}
46
47// Mul performs the Point scalar multiplication
48func (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
56// WIP
57func newRand() *big.Int {
58 var b [32]byte
59 _, err := rand.Read(b[:])
60 if err != nil {
61 panic(err)
62 }
63 bi := new(big.Int).SetBytes(b[:])
64 return new(big.Int).Mod(bi, N)
65}
66
67// PrivateKey represents the signer's private key
68type PrivateKey big.Int
69
70// PublicKey represents the signer's public key
71type PublicKey Point
72
73// NewPrivateKey returns a new random private key
74func NewPrivateKey() *PrivateKey {
75 k := newRand()
76 sk := PrivateKey(*k)
77 return &sk
78}
79
80// BigInt returns a *big.Int representation of the PrivateKey
81func (sk *PrivateKey) BigInt() *big.Int {
82 return (*big.Int)(sk)
83}
84
85// Public returns the PublicKey from the PrivateKey
86func (sk *PrivateKey) Public() *PublicKey {
87 Q := G.Mul(sk.BigInt())
88 pk := PublicKey(*Q)
89 return &pk
90}
91
92// Point returns a *Point representation of the PublicKey
93func (pk *PublicKey) Point() *Point {
94 return (*Point)(pk)
95}
96
97// NewRequestParameters returns a new random k (secret) & R (public) parameters
98func NewRequestParameters() (*big.Int, *Point) {
99 k := newRand()
100 return k, G.Mul(k) // R = kG
101}
102
103// BlindSign performs the blind signature on the given mBlinded using the
104// PrivateKey and the secret k values
105func (sk *PrivateKey) BlindSign(mBlinded *big.Int, k *big.Int) *big.Int {
106 // TODO add pending checks
107 // s' = dm' + k
108 sBlind := new(big.Int).Add(
109 new(big.Int).Mul(sk.BigInt(), mBlinded),
110 k)
111 return sBlind
112}
113
114// UserSecretData contains the secret values from the User (a, b, c) and the
115// public F
116type UserSecretData struct {
117 A *big.Int
118 B *big.Int
119
120 F *Point // public (in the paper is R)
121}
122
123// Blind performs the blinding operation on m using signerR parameter
124func Blind(m *big.Int, signerR *Point) (*big.Int, *UserSecretData) {
125 u := &UserSecretData{}
126 u.A = newRand()
127 u.B = newRand()
128
129 // (R) F = aR' + bG
130 aR := signerR.Mul(u.A)
131 bG := G.Mul(u.B)
132 u.F = aR.Add(bG)
133
134 // TODO check that F != O (point at infinity)
135
136 rx := new(big.Int).Mod(u.F.X, N)
137
138 // m' = a^-1 rx h(m)
139 ainv := new(big.Int).ModInverse(u.A, N)
140 ainvrx := new(big.Int).Mul(ainv, rx)
141 hBytes := crypto.Keccak256(m.Bytes())
142 h := new(big.Int).SetBytes(hBytes)
143 mBlinded := new(big.Int).Mul(ainvrx, h)
144
145 return mBlinded, u
146}
147
148// Signature contains the signature values S & F
149type Signature struct {
150 S *big.Int
151 F *Point
152}
153
154// Unblind performs the unblinding operation of the blinded signature for the
155// given message m and the UserSecretData
156func Unblind(sBlind, m *big.Int, u *UserSecretData) *Signature {
157 // s = a s' + b
158 as := new(big.Int).Mul(u.A, sBlind)
159 s := new(big.Int).Add(as, u.B)
160 s = new(big.Int).Mod(s, N)
161
162 return &Signature{
163 S: s,
164 F: u.F,
165 }
166}
167
168// Verify checks the signature of the message m for the given PublicKey
169func Verify(m *big.Int, s *Signature, q *PublicKey) bool {
170 // TODO add pending checks
171
172 sG := G.Mul(s.S) // sG
173
174 hBytes := crypto.Keccak256(m.Bytes())
175 h := new(big.Int).SetBytes(hBytes)
176
177 rx := new(big.Int).Mod(s.F.X, N)
178 rxh := new(big.Int).Mul(rx, h)
179 // rxhG := G.Mul(rxh) // originally the paper uses G
180 rxhG := q.Point().Mul(rxh)
181
182 right := s.F.Add(rxhG)
183
184 // check sG == R + rx h(m) G (where R in this code is F)
185 if bytes.Equal(sG.X.Bytes(), right.X.Bytes()) &&
186 bytes.Equal(sG.Y.Bytes(), right.Y.Bytes()) {
187 return true
188 }
189 return false
190}