-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
key.go
116 lines (96 loc) · 2.63 KB
/
key.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package dkgencryptkey
import (
"encoding/hex"
"fmt"
"math/big"
"github.com/pkg/errors"
"go.dedis.ch/kyber/v3"
"go.dedis.ch/kyber/v3/pairing"
"github.com/smartcontractkit/chainlink-vrf/altbn_128"
)
var suite pairing.Suite = &altbn_128.PairingSuite{}
var g1 = suite.G1()
type Raw []byte
func (r Raw) Key() Key {
scalar := g1.Scalar()
err := scalar.UnmarshalBinary(r)
if err != nil {
panic(err) // should never happen6
}
key, err := keyFromScalar(scalar)
if err != nil {
panic(err) // should never happen
}
return key
}
func (r Raw) String() string {
return "<DKGEncrypt Raw Private Key>"
}
func (r Raw) GoString() string {
return r.String()
}
type Key struct {
privateKey kyber.Scalar
publicKeyBytes []byte
PublicKey kyber.Point
}
// New returns a new dkgencryptkey key
func New() (Key, error) {
return keyFromScalar(g1.Scalar().Pick(suite.RandomStream()))
}
// MustNewXXXTestingOnly creates a new DKGEncrypt key from the given secret key.
// NOTE: for testing only.
func MustNewXXXTestingOnly(sk *big.Int) Key {
key, err := keyFromScalar(g1.Scalar().SetInt64(sk.Int64()))
if err != nil {
panic(err)
}
return key
}
var _ fmt.GoStringer = &Key{}
// GoString implements fmt.GoStringer
func (k Key) GoString() string {
return k.String()
}
// String returns the string representation of this key
func (k Key) String() string {
return fmt.Sprintf("DKGEncryptKey{PrivateKey: <redacted>, PublicKey: %s", k.PublicKeyString())
}
// ID returns the ID of this key
func (k Key) ID() string {
return k.PublicKeyString()
}
// PublicKeyString returns the hex representation of this key's public key
func (k Key) PublicKeyString() string {
return hex.EncodeToString(k.publicKeyBytes)
}
// Raw returns the key raw data
func (k Key) Raw() Raw {
raw, err := k.privateKey.MarshalBinary()
if err != nil {
panic(err) // should never happen
}
return Raw(raw)
}
// KyberScalar returns the private key as a kyber.Scalar object
func (k Key) KyberScalar() kyber.Scalar {
return g1.Scalar().Set(k.privateKey)
}
// KyberPoint returns the public key as a kyber.Point object
func (k Key) KyberPoint() kyber.Point {
return g1.Point().Base().Mul(k.privateKey, nil)
}
// keyFromScalar creates a new dkgencryptkey key from the given scalar.
// the given scalar must be a scalar of the g1 group in the altbn_128 pairing.
func keyFromScalar(k kyber.Scalar) (Key, error) {
publicKey := g1.Point().Base().Mul(k, nil)
publicKeyBytes, err := publicKey.MarshalBinary()
if err != nil {
return Key{}, errors.Wrap(err, "kyber point MarshalBinary")
}
return Key{
privateKey: k,
PublicKey: publicKey,
publicKeyBytes: publicKeyBytes,
}, nil
}