-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
utils.go
49 lines (39 loc) · 997 Bytes
/
utils.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
package starkkey
import (
"crypto/rand"
"fmt"
"io"
"math/big"
"github.com/smartcontractkit/caigo"
)
// constants
var (
byteLen = 32
)
// reimplements parts of https://github.com/smartcontractkit/caigo/blob/main/utils.go#L85
// generate the PK as a pseudo-random number in the interval [1, CurveOrder - 1]
// using io.Reader, and Key struct
func GenerateKey(material io.Reader) (k Key, err error) {
max := new(big.Int).Sub(caigo.Curve.N, big.NewInt(1))
k.priv, err = rand.Int(material, max)
if err != nil {
return k, err
}
k.pub.X, k.pub.Y, err = caigo.Curve.PrivateToPoint(k.priv)
if err != nil {
return k, err
}
if !caigo.Curve.IsOnCurve(k.pub.X, k.pub.Y) {
return k, fmt.Errorf("key gen is not on stark curve")
}
return k, nil
}
// pad bytes to specific length
func padBytes(a []byte, length int) []byte {
if len(a) < length {
pad := make([]byte, length-len(a))
return append(pad, a...)
}
// return original if length is >= to specified length
return a
}