Documentation ¶
Overview ¶
Package field implements arithmetic modulo p = 2^256 - 2^32 - 977.
Index ¶
- Constants
- func BytesAreCanonical(src *[ElementSize]byte) bool
- type Element
- func (fe *Element) Add(a, b *Element) *Element
- func (fe *Element) Bytes() []byte
- func (fe *Element) ConditionalNegate(a *Element, ctrl uint64) *Element
- func (fe *Element) ConditionalSelect(a, b *Element, ctrl uint64) *Element
- func (fe *Element) DebugMustRandomizeNonZero() *Element
- func (fe *Element) Equal(a *Element) uint64
- func (z *Element) Invert(x *Element) *Element
- func (fe *Element) IsOdd() uint64
- func (fe *Element) IsZero() uint64
- func (fe *Element) Multiply(a, b *Element) *Element
- func (fe *Element) MustSetCanonicalBytes(src *[ElementSize]byte) *Element
- func (fe *Element) Negate(a *Element) *Element
- func (fe *Element) One() *Element
- func (fe *Element) Pow2k(a *Element, k uint) *Element
- func (fe *Element) Set(a *Element) *Element
- func (fe *Element) SetBytes(src *[ElementSize]byte) (*Element, uint64)
- func (fe *Element) SetCanonicalBytes(src *[ElementSize]byte) (*Element, error)
- func (fe *Element) SetWideBytes(src []byte) *Element
- func (fe *Element) Sqrt(a *Element) (*Element, uint64)
- func (z *Element) SqrtRatio(u, v *Element) (*Element, uint64)
- func (fe *Element) Square(a *Element) *Element
- func (fe *Element) String() string
- func (fe *Element) Subtract(a, b *Element) *Element
- func (fe *Element) Zero() *Element
Constants ¶
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 NewElementFrom ¶
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 ¶
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 ¶
NewElementFromUint64 creates a new Element from a uint64.
func (*Element) ConditionalNegate ¶
ConditionalNegate sets `fe = a` iff `ctrl == 0`, `fe = -a` otherwise, and returns `fe`.
func (*Element) ConditionalSelect ¶
ConditionalSelect sets `fe = a` iff `ctrl == 0`, `fe = b` otherwise, and returns `fe`.
func (*Element) DebugMustRandomizeNonZero ¶
DebugMustRandomizeNonZero randomizes and returns `fe`, or panics.
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) 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 ¶
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 ¶
Sqrt sets `fe = Sqrt(a)`, and returns 1 iff the square root exists. In all other cases, `fe = 0`, and 0 is returned.