forked from rot256/pblind
-
Notifications
You must be signed in to change notification settings - Fork 1
/
keys.go
70 lines (58 loc) · 1.35 KB
/
keys.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
package pblind
import (
"crypto/elliptic"
"crypto/rand"
"fmt"
"math/big"
)
type PublicKey struct {
curve elliptic.Curve
x, y *big.Int
}
type SecretKey struct {
curve elliptic.Curve
scalar *big.Int
}
func (pk PublicKey) String() string {
return fmt.Sprintf("%s-pk: (x = %s, y = %s)", pk.curve.Params().Name, pk.x, pk.y)
}
func (sk SecretKey) String() string {
return fmt.Sprintf("%s-sk: (s = %s)", sk.curve.Params().Name, sk.scalar)
}
func NewSecretKey(curve elliptic.Curve) (SecretKey, error) {
var err error
var sk SecretKey
sk.curve = curve
sk.scalar, err = rand.Int(rand.Reader, curve.Params().N)
return sk, err
}
func SecretKeyFromBytes(curve elliptic.Curve, val []byte) SecretKey {
var sk SecretKey
sk.scalar = big.NewInt(0)
sk.scalar.SetBytes(val)
sk.curve = curve
return sk
}
func (sk SecretKey) Bytes() []byte {
return sk.scalar.Bytes()
}
func (sk SecretKey) GetPublicKey() PublicKey {
var pk PublicKey
pk.x, pk.y = sk.curve.ScalarBaseMult(sk.Bytes())
pk.curve = sk.curve
return pk
}
func PublicKeyFromBytes(curve elliptic.Curve, val []byte) (PublicKey, error) {
x, y := elliptic.UnmarshalCompressed(curve, val)
if x == nil {
return PublicKey{}, ErrorInvalidPublicKey
}
return PublicKey{
curve: curve,
x: x,
y: y,
}, nil
}
func (pk PublicKey) Bytes() []byte {
return elliptic.MarshalCompressed(pk.curve, pk.x, pk.y)
}