field

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: May 7, 2025 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package field implements the base field for elements on the curve SECP256k1 building on Fiat-Crypto.

Index

Constants

View Source
const (
	// ElementSize is the size of a field element in bytes.
	ElementSize = 32
)
View Source
const (
	// SecLength is the security length dictating the input length for HashToFieldElement.
	SecLength = 48
)

Variables

This section is empty.

Functions

func Add added in v0.2.0

Add adds two field elements in the Montgomery domain.

Preconditions:

0 ≤ eval arg1 < m
0 ≤ eval arg2 < m

Postconditions:

eval (from_montgomery out1) mod m = (eval (from_montgomery arg1) + eval (from_montgomery arg2)) mod m
0 ≤ eval out1 < m

func FromMontgomery added in v0.2.0

FromMontgomery translates a field element out of the Montgomery domain.

Preconditions:

0 ≤ eval arg1 < m

Postconditions:

eval out1 mod m = (eval arg1 * ((2^64)⁻¹ mod m)^4) mod m
0 ≤ eval out1 < m

func IsEqual added in v0.2.0

func IsEqual(u, v uint64) uint64

IsEqual returns 1 if u == v, and 0 otherwise.

func IsNonZero added in v0.2.0

func IsNonZero(u uint64) uint64

IsNonZero returns 1 if u != 0, and 0 otherwise.

func IsZero added in v0.2.0

func IsZero(u uint64) uint64

IsZero returns 1 if i == 0, and otherwise.

func Mul added in v0.2.0

Mul multiplies two field elements in the Montgomery domain.

Preconditions:

0 ≤ eval arg1 < m
0 ≤ eval arg2 < m

Postconditions:

eval (from_montgomery out1) mod m = (eval (from_montgomery arg1) * eval (from_montgomery arg2)) mod m
0 ≤ eval out1 < m

func Nonzero added in v0.2.0

func Nonzero(out1 *uint64, arg1 *[4]uint64)

Nonzero outputs a single non-zero word if the input is non-zero and zero otherwise.

Preconditions:

0 ≤ eval arg1 < m

Postconditions:

out1 = 0 ↔ eval (from_montgomery arg1) mod m = 0

Input Bounds:

arg1: [[0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff]]

Output Bounds:

out1: [0x0 ~> 0xffffffffffffffff]

func Opp added in v0.2.0

Opp negates a field element in the Montgomery domain.

Preconditions:

0 ≤ eval arg1 < m

Postconditions:

eval (from_montgomery out1) mod m = -eval (from_montgomery arg1) mod m
0 ≤ eval out1 < m

func Reduce added in v0.2.0

Reduce will set x to x mod p, and return 0 if a reduction was necessary and 1 otherwise.

func Selectznz added in v0.2.0

func Selectznz(out1 *[4]uint64, arg1 uint1, arg2 *[4]uint64, arg3 *[4]uint64)

Selectznz is a multi-limb conditional select.

Postconditions:

out1 = (if arg1 = 0 then arg2 else arg3)

Input Bounds:

arg1: [0x0 ~> 0x1]
arg2: [[0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff]]
arg3: [[0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff]]

Output Bounds:

out1: [[0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff]]

func SetOne added in v0.2.0

func SetOne(out1 *MontgomeryDomainFieldElement)

SetOne returns the field element One in the Montgomery domain.

Postconditions:

eval (from_montgomery out1) mod m = 1 mod m
0 ≤ eval out1 < m

func Square added in v0.2.0

Square squares a field element in the Montgomery domain.

Preconditions:

0 ≤ eval arg1 < m

Postconditions:

eval (from_montgomery out1) mod m = (eval (from_montgomery arg1) * eval (from_montgomery arg1)) mod m
0 ≤ eval out1 < m

func Sub added in v0.2.0

Sub subtracts two field elements in the Montgomery domain.

Preconditions:

0 ≤ eval arg1 < m
0 ≤ eval arg2 < m

Postconditions:

eval (from_montgomery out1) mod m = (eval (from_montgomery arg1) - eval (from_montgomery arg2)) mod m
0 ≤ eval out1 < m

func ToMontgomery added in v0.2.0

ToMontgomery translates a field element into the Montgomery domain.

Preconditions:

0 ≤ eval arg1 < m

Postconditions:

eval (from_montgomery out1) mod m = eval arg1 mod m
0 ≤ eval out1 < m

Types

type Element added in v0.2.0

type Element struct {
	E MontgomeryDomainFieldElement
}

An Element on the field.

func New added in v0.2.0

func New() *Element

New returns a new, unset Element.

func (*Element) Add added in v0.2.0

func (e *Element) Add(u, v *Element) *Element

Add sets e = u + v.

func (*Element) Bytes added in v0.2.0

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

Bytes returns the byte representation of e.

func (*Element) CMove added in v0.2.0

func (e *Element) CMove(c uint64, u, v *Element) *Element

CMove sets e to u if c == 0, and to v otherwise.

func (*Element) Equals added in v0.2.0

func (e *Element) Equals(u *Element) uint64

Equals returns 1 if e == u, and 0 otherwise. e and u are considered to be reduced.

func (*Element) FromBytesNoReduce added in v0.2.0

func (e *Element) FromBytesNoReduce(input []byte) *Element

FromBytesNoReduce set e to input.

func (*Element) FromBytesWithReduce added in v0.2.0

func (e *Element) FromBytesWithReduce(input [ElementSize]byte) (*Element, uint64)

FromBytesWithReduce sets e to a reduction of u (if necessary), or u otherwise, and return e and whether a reduction was necessary (in which case 0, and 1 otherwise).

func (*Element) HashToFieldElement added in v0.2.0

func (e *Element) HashToFieldElement(input [SecLength]byte) *Element

HashToFieldElement sets e to a field element from a 48-byte string obtained by ExpandXMD. We use Frank Denis' trick, c.f. blog from Filippo: https://words.filippo.io/dispatches/wide-reduction i.e. represent the value as a+b*2^192+c*2^384.

func (*Element) Invert added in v0.2.0

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

Invert computes z = 1/x (mod p) and returns it, by computing z = x^(p-2) mod p. For some reason, addchain outputs something different than from the examples, using the receiver in the computation, which doesn't work if the receiver and the argument point to the same element. Dereferencing and thus using a local copy of the original does the trick.

func (*Element) IsZero added in v0.2.0

func (e *Element) IsZero() uint64

IsZero returns 1 if e == 0, and 0 otherwise.

func (*Element) Multiply added in v0.2.0

func (e *Element) Multiply(u, v *Element) *Element

Multiply sets e = u * v.

func (*Element) Negate added in v0.2.0

func (e *Element) Negate(u *Element) *Element

Negate sets e = -u.

func (*Element) One added in v0.2.0

func (e *Element) One() *Element

One sets the receiver to 1.

func (*Element) Set added in v0.2.0

func (e *Element) Set(u *Element) *Element

Set sets e to u.

func (*Element) Sgn0 added in v0.2.0

func (e *Element) Sgn0() uint64

Sgn0 returns the parity of e, 0 if e is even, and 1 otherwise.

func (*Element) SqrtRatio added in v0.2.0

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

SqrtRatio sets e to the square root of (u/v) if a quadratic residue (the square root) exists, in which case it also returns 1. In all other cases and returns 1 if there's a quadratic residue (i.e. if a square root actually exists), and 0 otherwise. This uses an optimized implementation sqrt_ratio_3mod4 RFC 9380 section F.2.1.2.

func (*Element) Square added in v0.2.0

func (e *Element) Square(u *Element) *Element

Square sets e = u^2.

func (*Element) Subtract added in v0.2.0

func (e *Element) Subtract(u, v *Element) *Element

Subtract sets e = u - v.

type MontgomeryDomainFieldElement added in v0.2.0

type MontgomeryDomainFieldElement [4]uint64

MontgomeryDomainFieldElement is a field element in the Montgomery domain.

Bounds:

[[0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff]]

type NonMontgomeryDomainFieldElement added in v0.2.0

type NonMontgomeryDomainFieldElement [4]uint64

NonMontgomeryDomainFieldElement is a field element NOT in the Montgomery domain.

Bounds:

[[0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff]]

Jump to

Keyboard shortcuts

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