felt

package
v0.17.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 3, 2026 License: Apache-2.0 Imports: 14 Imported by: 152

Documentation

Index

Constants

View Source
const (
	Base16 = 16
	Base10 = 10
)
View Source
const (
	Limbs = fp.Limbs // number of 64 bits words needed to represent a Element
	Bits  = fp.Bits  // number of bits needed to represent a Element
	Bytes = fp.Bytes // number of bytes needed to represent a Element

	// Max number of chars to represent a Felt as Hex.
	// 0x at the start + 2 chars per byte.
	MaxFeltAsHexSize = len("0x") + Bytes*2
)

Variables

View Source
var One = Felt(fp.Element(
	[4]uint64{
		18446744073709551585,
		18446744073709551615,
		18446744073709551615,
		576460752303422960,
	},
))
View Source
var Zero = Felt{}

Functions

func Equal added in v0.15.15

func Equal[F FeltLike](a, b *F) bool

Equal reports whether a and b are equal; limb equality is the field element's own definition.

func FromBytes added in v0.14.3

func FromBytes[F FeltLike](value []byte) F

FromBytes crates a new Felt based type given a byte array

func FromString added in v0.15.7

func FromString[F FeltLike](value string) (F, error)

FromString crates a new Felt based type given a any string

func FromUint64 added in v0.14.3

func FromUint64[F FeltLike](num uint64) F

FromUint64 creates a new Felt based type given an uint64

func IsZero added in v0.15.15

func IsZero[F FeltLike](v *F) bool

IsZero reports whether v is zero; limb equality is the field element's own definition.

func NewFromBytes added in v0.15.7

func NewFromBytes[F FeltLike](val []byte) *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 NewFromString added in v0.15.7

func NewFromString[F FeltLike](val string) (*F, error)

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 NewFromUint64 added in v0.15.7

func NewFromUint64[F FeltLike](num uint64) *F

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 NewRandom added in v0.15.7

func NewRandom[F FeltLike]() *F

NewRandom creates a new random Felt based type. It also forces a heap allocation. For efficient code use `Random`

func NewUnsafeFromString added in v0.15.8

func NewUnsafeFromString[F FeltLike](val string) *F

NewUnsafeFromString is similar to `NewFromString` but it panics instead of returning an error.

func Random added in v0.15.8

func Random[F FeltLike]() F

Creates a new random Felt based type

func UnsafeFromString added in v0.15.7

func UnsafeFromString[F FeltLike](value string) F

UnsafeFromString crates a new Felt based type given a any string. It panics if the string is not valid.

Types

type Address added in v0.15.7

type Address = Typed[addressKind]

type CasmClassHash added in v0.15.10

type CasmClassHash = Typed[casmClassHashKind]

type ClassHash added in v0.15.7

type ClassHash = Typed[classHashKind]

type Felt

type Felt = Typed[feltKind]

Felt is the untyped field element.

type FeltLike added in v0.15.7

type FeltLike interface {
	~[4]uint64
}

FeltLike is the constraint for generic code over felts: every Typed[K] satisfies it, as does any type declared over one. It exists because a generic type such as Slice cannot infer K.

type Hash added in v0.15.7

type Hash = Typed[hashKind]

type SierraClassHash added in v0.15.10

type SierraClassHash = Typed[sierraClassHashKind]

type Slice added in v0.16.6

type Slice[F FeltLike] []F

func (Slice[F]) MarshalCBOR added in v0.16.6

func (s Slice[F]) MarshalCBOR() ([]byte, error)

func (Slice[F]) MarshalJSONTo added in v0.16.6

func (s Slice[F]) MarshalJSONTo(enc *jsontext.Encoder) error

func (*Slice[F]) UnmarshalCBOR added in v0.16.6

func (s *Slice[F]) UnmarshalCBOR(data []byte) error

func (*Slice[F]) UnmarshalJSONFrom added in v0.16.6

func (s *Slice[F]) UnmarshalJSONFrom(dec *jsontext.Decoder) error

UnmarshalJSONFrom reads a JSON array of 0x-hex strings into the slice.

type StateRootHash added in v0.15.17

type StateRootHash = Typed[stateRootHashKind]

type TransactionHash added in v0.15.7

type TransactionHash = Typed[transactionHashKind]

type Typed added in v0.17.0

type Typed[K any] fp.Element

Typed is a field element told apart at compile time by a phantom kind. Every Typed[K] shares one implementation, so all felt types behave and encode the same and converting between them is free.

func (*Typed[K]) Add added in v0.17.0

func (z *Typed[K]) Add(x, y *Typed[K]) *Typed[K]

Add forwards the call to underlying field element implementation

func (Typed[K]) AppendText added in v0.17.0

func (z Typed[K]) AppendText(data []byte) ([]byte, error)

AppendText appends the felt's 0x hex representation to data. The quotes are placed at the encoding/json level. It is a json/v2 interface and enforces appending.

func (*Typed[K]) BigInt added in v0.17.0

func (z *Typed[K]) BigInt(res *big.Int) *big.Int

BigInt forwards the call to underlying field element implementation

func (*Typed[K]) Bits added in v0.17.0

func (z *Typed[K]) Bits() [4]uint64

Bits forwards the call to underlying field element implementation. Provides access to z by returning its value as a little-endian [4]uint64 array.

func (*Typed[K]) Bytes added in v0.17.0

func (z *Typed[K]) Bytes() [32]byte

Bytes forwards the call to underlying field element implementation. Returns the value of z as a big-endian byte array

func (*Typed[K]) Clone added in v0.17.0

func (z *Typed[K]) Clone() *Typed[K]

TODO: look where this is used, the clone shouldn't return a pointer

func (*Typed[K]) Cmp added in v0.17.0

func (z *Typed[K]) Cmp(x *Typed[K]) int

Cmp forwards the call to underlying field element implementation. Returns:

-1 if z <  x
 0 if z == x
+1 if z >  x

func (*Typed[K]) Div added in v0.17.0

func (z *Typed[K]) Div(x, y *Typed[K]) *Typed[K]

Div forwards the call to underlying field element implementation

func (*Typed[K]) Double added in v0.17.0

func (z *Typed[K]) Double(x *Typed[K]) *Typed[K]

Double forwards the call to underlying field element implementation

func (*Typed[K]) Equal added in v0.17.0

func (z *Typed[K]) Equal(x *Typed[K]) bool

Equal forwards the call to underlying field element implementation

func (*Typed[K]) Exp added in v0.17.0

func (z *Typed[K]) Exp(x *Typed[K], y *big.Int) *Typed[K]

Exp forwards the call to underlying field element implementation

func (*Typed[K]) Halve added in v0.17.0

func (z *Typed[K]) Halve()

Halve forwards the call to underlying field element implementation

func (*Typed[K]) Impl added in v0.17.0

func (z *Typed[K]) Impl() *fp.Element

Impl returns the underlying field element type

func (*Typed[K]) IsOne added in v0.17.0

func (z *Typed[K]) IsOne() bool

IsOne forwards the call to underlying field element implementation

func (*Typed[K]) IsZero added in v0.17.0

func (z *Typed[K]) IsZero() bool

IsZero forwards the call to underlying field element implementation

func (*Typed[K]) Marshal added in v0.17.0

func (z *Typed[K]) Marshal() []byte

Marshal forwards the call to underlying field element implementation

func (*Typed[K]) MarshalCBOR added in v0.17.0

func (z *Typed[K]) MarshalCBOR() ([]byte, error)

Fast, felt-specialized CBOR marshaling.

func (Typed[K]) MarshalText added in v0.17.0

func (z Typed[K]) MarshalText() ([]byte, error)

MarshalText returns the felt as an unquoted 0x hex string with no unnecessary leading zeros. The quotes are placed at the encoding/json level. Uses a value receiver so encoding/json can call it on non-addressable values (struct fields in `any`).

func (*Typed[K]) Mul added in v0.17.0

func (z *Typed[K]) Mul(x, y *Typed[K]) *Typed[K]

Mul forwards the call to underlying field element implementation

func (*Typed[K]) Neg added in v0.17.0

func (z *Typed[K]) Neg(x *Typed[K]) *Typed[K]

func (*Typed[K]) Set added in v0.17.0

func (z *Typed[K]) Set(x *Typed[K]) *Typed[K]

Set forwards the call to underlying field element implementation

func (*Typed[K]) SetBigInt added in v0.17.0

func (z *Typed[K]) SetBigInt(v *big.Int) *Typed[K]

SetBigInt forwards the call to underlying field element implementation

func (*Typed[K]) SetBytes added in v0.17.0

func (z *Typed[K]) SetBytes(e []byte) *Typed[K]

SetBytes forwards the call to underlying field element implementation

func (*Typed[K]) SetBytesCanonical added in v0.17.0

func (z *Typed[K]) SetBytesCanonical(e []byte) error

SetBytesCanonical forwards the call to underlying field element implementation

func (*Typed[K]) SetRandom added in v0.17.0

func (z *Typed[K]) SetRandom() *Typed[K]

SetRandom forwards the call to underlying field element implementation

func (*Typed[K]) SetString added in v0.17.0

func (z *Typed[K]) SetString(number string) (*Typed[K], error)

SetString forwards the call to underlying field element implementation

func (*Typed[K]) SetUint64 added in v0.17.0

func (z *Typed[K]) SetUint64(v uint64) *Typed[K]

SetUint64 forwards the call to underlying field element implementation

func (*Typed[K]) ShortString added in v0.17.0

func (z *Typed[K]) ShortString() string

ShortString prints the felt to a string in a shortened format

func (*Typed[K]) String added in v0.17.0

func (z *Typed[K]) String() string

String forwards the call to underlying field element implementation

func (*Typed[K]) Sub added in v0.17.0

func (z *Typed[K]) Sub(x, y *Typed[K]) *Typed[K]

Sub forwards the call to underlying field element implementation

func (*Typed[K]) Text added in v0.17.0

func (z *Typed[K]) Text(base int) string

Text forwards the call to underlying field element implementation

func (*Typed[K]) Uint64 added in v0.17.0

func (z *Typed[K]) Uint64() uint64

Uint64 forwards the call to underlying field element implementation

func (*Typed[K]) Unmarshal added in v0.17.0

func (z *Typed[K]) Unmarshal(e []byte)

Unmarshal forwards the call to underlying field element implementation

func (*Typed[K]) UnmarshalCBOR added in v0.17.0

func (z *Typed[K]) UnmarshalCBOR(data []byte) error

Fast, felt-specialized CBOR unmarshaling. Falls back to the generic decoder on shape mismatch

func (*Typed[K]) UnmarshalJSONFrom added in v0.17.0

func (z *Typed[K]) UnmarshalJSONFrom(dec *jsontext.Decoder) error

UnmarshalJSONFrom sets z from a quoted 0x-prefixed hex JSON string; any other JSON value fails.

func (*Typed[K]) UnmarshalText added in v0.17.0

func (z *Typed[K]) UnmarshalText(text []byte) error

UnmarshalText sets z from an unquoted 0x-prefixed hex string, as map keys and text decoders pass it.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL