field

package
v0.0.0-...-f261603 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2023 License: BSD-3-Clause Imports: 7 Imported by: 0

Documentation

Overview

Package field implements arithmetic modulo p = 2^256 - 2^32 - 977.

Index

Constants

View Source
const (
	// ElementSize is the size of a field element in bytes.
	ElementSize = 32

	// WideElementSize is the size of a wide field element in bytes.
	WideElementSize = 64
)

Variables

This section is empty.

Functions

func BytesAreCanonical

func BytesAreCanonical(src *[ElementSize]byte) bool

BytesAreCanonical returns true iff `src` represents a canonically encoded field element in big-endian byte order.

Types

type Element

type Element struct {
	// contains filtered or unexported fields
}

Element is a field element. All arguments and receivers are allowed to alias. The zero value is a valid zero element.

func NewElement

func NewElement() *Element

NewElement returns a new zero Element.

func NewElementFrom

func NewElementFrom(other *Element) *Element

NewElementFrom creates a new Element from another.

func NewElementFromCanonicalBytes

func NewElementFromCanonicalBytes(src *[ElementSize]byte) (*Element, error)

NewElementFromCanonicalBytes creates a new Element from the canonical big-endian byte representation.

func NewElementFromCanonicalHex

func NewElementFromCanonicalHex(str string) *Element

NewElementFromCanonicalHex creates a new Element from the canonical hex big-endian byte representation. The hex string MUST be less than or equal to `2*ElementSize`.

func NewElementFromUint64

func NewElementFromUint64(l0 uint64) *Element

NewElementFromUint64 creates a new Element from a uint64.

func (*Element) Add

func (fe *Element) Add(a, b *Element) *Element

Add sets `fe = a + b` and returns `fe`.

func (*Element) Bytes

func (fe *Element) Bytes() []byte

Bytes returns the canonical big-endian encoding of `fe`.

func (*Element) ConditionalNegate

func (fe *Element) ConditionalNegate(a *Element, ctrl uint64) *Element

ConditionalNegate sets `fe = a` iff `ctrl == 0`, `fe = -a` otherwise, and returns `fe`.

func (*Element) ConditionalSelect

func (fe *Element) ConditionalSelect(a, b *Element, ctrl uint64) *Element

ConditionalSelect sets `fe = a` iff `ctrl == 0`, `fe = b` otherwise, and returns `fe`.

func (*Element) DebugMustRandomizeNonZero

func (fe *Element) DebugMustRandomizeNonZero() *Element

DebugMustRandomizeNonZero randomizes and returns `fe`, or panics.

func (*Element) Equal

func (fe *Element) Equal(a *Element) uint64

Equal returns 1 iff `fe == a`, 0 otherwise.

func (*Element) Invert

func (z *Element) Invert(x *Element) *Element

Invert sets `z = 1/x` and returns `z`. If `x == 0`, `z` is set to `0`.

func (*Element) IsOdd

func (fe *Element) IsOdd() uint64

IsOdd returns 1 iff `fe % 2 == 1`, 0 otherwise.

func (*Element) IsZero

func (fe *Element) IsZero() uint64

IsZero returns 1 iff `fe == 0`, 0 otherwise.

func (*Element) Multiply

func (fe *Element) Multiply(a, b *Element) *Element

Multiply sets `fe = a * b` and returns `fe`.

func (*Element) MustSetCanonicalBytes

func (fe *Element) MustSetCanonicalBytes(src *[ElementSize]byte) *Element

MustSetCanonicalBytes sets `fe = src`, where `src` MUST be the 32-byte big-endian canonical encoding of `fe`, and returns `fe`. All errors will panic.

func (*Element) Negate

func (fe *Element) Negate(a *Element) *Element

Negate sets `fe = -a` and returns `fe`.

func (*Element) One

func (fe *Element) One() *Element

One sets `fe = 1` and returns `fe`.

func (*Element) Pow2k

func (fe *Element) Pow2k(a *Element, k uint) *Element

Pow2k sets `fe = a ^ (2 * k)` and returns `fe`. k MUST be non-zero.

func (*Element) Set

func (fe *Element) Set(a *Element) *Element

Set sets `fe = a` and returns `fe`.

func (*Element) SetBytes

func (fe *Element) SetBytes(src *[ElementSize]byte) (*Element, uint64)

SetBytes sets `fe = src`, where `src` is a 32-byte big-endian encoding of `fe`, and returns `fe, 0`. If `src` is not a canonical encoding of `fe`, `src` is reduced modulo p, and SetBytes returns `fe, 1`.

func (*Element) SetCanonicalBytes

func (fe *Element) SetCanonicalBytes(src *[ElementSize]byte) (*Element, error)

SetCanonicalBytes sets `fe = src`, where `src` is a 32-byte big-endian encoding of `fe`, and returns `fe`. If `src` is not a canonical encoding of `fe`, SetCanonicalBytes returns nil and an error, and the receiver is unchanged.

func (*Element) SetWideBytes

func (fe *Element) SetWideBytes(src []byte) *Element

SetWideBytes sets `fe = src % p`, where `src` is a big-endian encoding of `fe` with a length in the range `[32,64]`-bytes, and returns `fe`. This routine only exists to implement certain standards that require this. In practice, p is close enough to `2^256-1` such that this is largely unnecessary.

func (*Element) Sqrt

func (fe *Element) Sqrt(a *Element) (*Element, uint64)

Sqrt sets `fe = Sqrt(a)`, and returns 1 iff the square root exists. In all other cases, `fe = 0`, and 0 is returned.

func (*Element) SqrtRatio

func (z *Element) SqrtRatio(u, v *Element) (*Element, uint64)

func (*Element) Square

func (fe *Element) Square(a *Element) *Element

Square sets `fe = a * a` and returns `fe`.

func (*Element) String

func (fe *Element) String() string

String returns the big-endian hex representation of `fe`.

func (*Element) Subtract

func (fe *Element) Subtract(a, b *Element) *Element

Subtract sets `fe = a - b` and returns `fe`.

func (*Element) Zero

func (fe *Element) Zero() *Element

Zero sets `fe = 0` and returns `fe`.

Jump to

Keyboard shortcuts

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