forked from NethermindEth/starknet.go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
38 lines (31 loc) · 887 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
package caigo
import (
"crypto/rand"
"fmt"
"math/big"
)
// obtain random primary key on stark curve
// NOTE: to be used for testing purposes
func (sc StarkCurve) GetRandomPrivateKey() (priv *big.Int, err error) {
max := new(big.Int).Sub(sc.Max, big.NewInt(1))
priv, err = rand.Int(rand.Reader, max)
if err != nil {
return priv, err
}
x, y, err := sc.PrivateToPoint(priv)
if err != nil {
return priv, err
}
if !sc.IsOnCurve(x, y) {
return priv, fmt.Errorf("key gen is not on stark cruve")
}
return priv, nil
}
// obtain public key coordinates from stark curve given the private key
func (sc StarkCurve) PrivateToPoint(privKey *big.Int) (x, y *big.Int, err error) {
if privKey.Cmp(big.NewInt(0)) != 1 || privKey.Cmp(sc.N) != -1 {
return x, y, fmt.Errorf("private key not in curve range")
}
x, y = sc.EcMult(privKey, sc.EcGenX, sc.EcGenY)
return x, y, nil
}