arnaucube
arnaucube/go-blindsecp256k1Public Self-hosted repository
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	sBlind = new(big.Int).Mod(sBlind, N)
112	return sBlind
113}
114
115// UserSecretData contains the secret values from the User (a, b, c) and the
116// public F
117type UserSecretData struct {
118	A *big.Int
119	B *big.Int
120
121	F *Point // public (in the paper is R)
122}
123
124// Blind performs the blinding operation on m using signerR parameter
125func Blind(m *big.Int, signerR *Point) (*big.Int, *UserSecretData) {
126	u := &UserSecretData{}
127	u.A = newRand()
128	u.B = newRand()
129
130	// (R) F = aR' + bG
131	aR := signerR.Mul(u.A)
132	bG := G.Mul(u.B)
133	u.F = aR.Add(bG)
134
135	// TODO check that F != O (point at infinity)
136
137	rx := new(big.Int).Mod(u.F.X, N)
138
139	// m' = a^-1 rx h(m)
140	ainv := new(big.Int).ModInverse(u.A, N)
141	ainvrx := new(big.Int).Mul(ainv, rx)
142	hBytes := crypto.Keccak256(m.Bytes())
143	h := new(big.Int).SetBytes(hBytes)
144	mBlinded := new(big.Int).Mul(ainvrx, h)
145	mBlinded = new(big.Int).Mod(mBlinded, N)
146
147	return mBlinded, u
148}
149
150// Signature contains the signature values S & F
151type Signature struct {
152	S *big.Int
153	F *Point
154}
155
156// Unblind performs the unblinding operation of the blinded signature for the
157// given the UserSecretData
158func Unblind(sBlind *big.Int, u *UserSecretData) *Signature {
159	// s = a s' + b
160	as := new(big.Int).Mul(u.A, sBlind)
161	s := new(big.Int).Add(as, u.B)
162	s = new(big.Int).Mod(s, N)
163
164	return &Signature{
165		S: s,
166		F: u.F,
167	}
168}
169
170// Verify checks the signature of the message m for the given PublicKey
171func Verify(m *big.Int, s *Signature, q *PublicKey) bool {
172	// TODO add pending checks
173
174	sG := G.Mul(s.S) // sG
175
176	hBytes := crypto.Keccak256(m.Bytes())
177	h := new(big.Int).SetBytes(hBytes)
178
179	rx := new(big.Int).Mod(s.F.X, N)
180	rxh := new(big.Int).Mul(rx, h)
181	// rxhG := G.Mul(rxh) // originally the paper uses G
182	rxhG := q.Point().Mul(rxh)
183
184	right := s.F.Add(rxhG)
185
186	// check sG == R + rx h(m) Q (where R in this code is F)
187	if bytes.Equal(sG.X.Bytes(), right.X.Bytes()) &&
188		bytes.Equal(sG.Y.Bytes(), right.Y.Bytes()) {
189		return true
190	}
191	return false
192}