-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvoter.go
75 lines (64 loc) · 1.67 KB
/
voter.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package voter
import (
"crypto/ecdsa"
"crypto/rand"
"crypto/sha256"
"encoding/json"
"github.com/securepollingsystem/registrar/blind"
"math/big"
)
type Voter struct {
privateKey *ecdsa.PrivateKey
PublicKey *ecdsa.PublicKey
pubhash *big.Int // hash of the voters public key, this gets signed by registrar
// these will eventually be per registrar
sig *blind.BlindSignature
pollee *blind.BlindRequest
//registrar registrar's publickey
//registration session info from blinding
}
type VoterSignature struct {
R *big.Int
S *big.Int
}
func NewVoter() *Voter {
keys, _ := blind.GenerateKey()
return &Voter{privateKey: keys,
PublicKey: &keys.PublicKey}
}
// get response from registrar.BlindSession, return blinded v.PublicKey
func (v *Voter) RequestRegistration(pub, session *ecdsa.PublicKey) (blinded *big.Int, err error) {
key, err := json.Marshal(v.PublicKey)
if err != nil {
return nil, err
}
hasher := sha256.New()
hasher.Write(key)
hashed := hasher.Sum(nil)
v.pubhash = new(big.Int).SetBytes(hashed)
v.pollee, err = blind.NewPollee(pub, session, v.pubhash)
if err != nil {
return nil, err
}
return v.pollee.Mhat, nil
}
// get sig from registrar, unblind and store it
func (v *Voter) Register(blindsig *big.Int) {
v.sig = v.pollee.BlindExtract(blindsig)
v.sig.M = v.pubhash
}
// sign a thing
func (v *Voter) Sign(msg []byte) (*VoterSignature, error) {
hasher := sha256.New()
hasher.Write(msg)
hashed := hasher.Sum(nil)
r, s, err := ecdsa.Sign(rand.Reader, v.privateKey, hashed)
if err != nil {
return nil, err
}
return &VoterSignature{R: r, S: s}, nil
}
func (v *Voter) String() (string, error) {
b, err := json.Marshal(v)
return string(b), err
}