forked from mndrix/btcutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblind_signer.go
54 lines (43 loc) · 1.21 KB
/
blind_signer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package btcutil
import "crypto/ecdsa"
import "crypto/rand"
import "fmt"
import "math/big"
type BlindSignerState struct {
// secret stuff
d, k *big.Int
// shareable stuff
Q *ecdsa.PublicKey
}
// Request that the signer start a blind signature protocol. Returns
// the signer's public key and an EC point named R.
func BlindSession(sState *BlindSignerState) (*ecdsa.PublicKey, *ecdsa.PublicKey) {
// generate signer's private & public key pair
if sState.Q == nil {
keys, err := GenerateKey(rand.Reader)
maybePanic(err)
sState.d = keys.D
sState.Q = &keys.PublicKey
fmt.Printf("Signer:\t%x\n\t%x\n", sState.d, sState.Q.X)
}
// generate k and R for each user request (§4.2)
request, err := GenerateKey(rand.Reader)
maybePanic(err)
sState.k = request.D
R := &request.PublicKey
return sState.Q, R
}
// Signs a blinded message
func BlindSign(sState *BlindSignerState, R *ecdsa.PublicKey, mHat *big.Int) *big.Int {
crv := Secp256k1().Params()
// verify that R matches our secret k
R_ := ScalarBaseMult(sState.k)
if !KeysEqual(R, R_) {
panic("unknown R")
}
// signer generates signature (§4.3)
sHat := new(big.Int).Mul(sState.d, mHat)
sHat.Add(sHat, sState.k)
sHat.Mod(sHat, crv.N)
return sHat
}