Repository navigation
Expand file tree
/
Copy pathctors.go
More file actions
80 lines (68 loc) · 2.17 KB
/
Copy pathctors.go
File metadata and controls
80 lines (68 loc) · 2.17 KB
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
package felt
import (
"github.com/consensys/gnark-crypto/ecc/stark-curve/fp"
)
// Interface that represents all types that implement Felt as a sublying type
// E.g: Hash and Address
type FeltLike interface {
~[4]uint64
}
// NewFromUint64 crates a new Felt based type given a uint64. Use this function with
// care since it forces a heap allocation. For efficient code use `FromUint64`
func NewFromUint64[F FeltLike](num uint64) *F {
f := FromUint64[F](num)
return &f
}
// NewFromString crates a new Felt based type given a string. Use this function with
// care since it forces a heap allocation. For efficient code use `FromString`
func NewFromString[F FeltLike](val string) (*F, error) {
f, err := FromString[F](val)
return &f, err
}
// NewUnsafeFromString is similar to `NewFromString` but it panics instead of returning
// an error.
func NewUnsafeFromString[F FeltLike](val string) *F {
f := UnsafeFromString[F](val)
return &f
}
// NewFromBytes crates a new Felt based type given a byte array. Use this function with
// care since it forces a heap allocation. For efficient code use `FromBytes`
func NewFromBytes[F FeltLike](val []byte) *F {
f := FromBytes[F](val)
return &f
}
// NewRandom creates a new random Felt based type. It also forces a heap allocation.
// For efficient code use `Random`
func NewRandom[F FeltLike]() *F {
f := Random[F]()
return &f
}
// FromUint64 creates a new Felt based type given an uint64
func FromUint64[F FeltLike](num uint64) F {
f := fp.NewElement(num)
return F(f)
}
// FromBytes crates a new Felt based type given a byte array
func FromBytes[F FeltLike](value []byte) F {
f := new(Felt).SetBytes(value)
return F(*f)
}
// FromString crates a new Felt based type given a any string
func FromString[F FeltLike](value string) (F, error) {
f, err := new(Felt).SetString(value)
return F(*f), err
}
// UnsafeFromString crates a new Felt based type given a any string. It panics
// if the string is not valid.
func UnsafeFromString[F FeltLike](value string) F {
f, err := new(Felt).SetString(value)
if err != nil {
panic(err)
}
return F(*f)
}
// Creates a new random Felt based type
func Random[F FeltLike]() F {
f := new(Felt).SetRandom()
return F(*f)
}