felt

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2022 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package felt contains field arithmetic operations for modulus = 0x800000...000001.

The API is similar to math/big (big.Int), but the operations are significantly faster (up to 20x for the modular multiplication on amd64, see also https://hackmd.io/@gnark/modular_multiplication)

The modulus is hardcoded in all the operations.

Field elements are represented as an array, and assumed to be in Montgomery form in all methods:

type Felt [4]uint64

Example API signature

// Mul z = x * y mod q
func (z *Element) Mul(x, y *Element) *Element

and can be used like so:

var a, b Element
a.SetUint64(2)
b.SetString("984896738")
a.Mul(a, b)
a.Sub(a, a)
 .Add(a, b)
 .Inv(a)
b.Exp(b, new(big.Int).SetUint64(42))

Modulus

0x800000000000011000000000000000000000000000000000000000000000001 // base 16
3618502788666131213697322783095070105623107215331596699973092056135872020481 // base 10

Index

Constants

View Source
const Bits = 252

Bits number bits needed to represent Felt

View Source
const Bytes = Limbs * 8

Bytes number bytes needed to represent Felt

View Source
const Limbs = 4

Limbs number of 64 bits words needed to represent Felt

Variables

This section is empty.

Functions

func Butterfly

func Butterfly(a, b *Felt)

func Modulus

func Modulus() *big.Int

Modulus returns q as a big.Int q =

3618502788666131213697322783095070105623107215331596699973092056135872020481

func MulBy13

func MulBy13(x *Felt)

func MulBy3

func MulBy3(x *Felt)

func MulBy5

func MulBy5(x *Felt)

Types

type Felt

type Felt [4]uint64

Felt represents a field element stored on 4 words (uint64) Felt are assumed to be in Montgomery form in all methods field modulus q =

3618502788666131213697322783095070105623107215331596699973092056135872020481

func BatchInvert

func BatchInvert(a []Felt) []Felt

BatchInvert returns a new slice with every element inverted. Uses Montgomery batch inversion trick

func NewFelt

func NewFelt(v uint64) Felt

NewFelt returns a new Felt from a uint64 value

it is equivalent to

var v NewFelt
v.SetUint64(...)

func One

func One() Felt

One returns 1 (in montgommery form)

func (*Felt) Add

func (z *Felt) Add(x, y *Felt) *Felt

Add z = x + y mod q

func (*Felt) Bit

func (z *Felt) Bit(i uint64) uint64

Bit returns the i'th bit, with lsb == bit 0. It is the responsibility of the caller to convert from Montgomery to Regular form if needed

func (*Felt) BitLen

func (z *Felt) BitLen() int

BitLen returns the minimum number of bits needed to represent z returns 0 if z == 0

func (*Felt) ByteSlice

func (z *Felt) ByteSlice() []byte

ByteSlice() is exactly the same as Marshal() except that it returns nil when z is nil. This mimics the behavior of big.Int.Bytes.

func (*Felt) Bytes

func (z *Felt) Bytes() (res [Limbs * 8]byte)

Bytes returns the regular (non montgomery) value of z as a big-endian byte array.

func (*Felt) Cmp

func (z *Felt) Cmp(x *Felt) int

Cmp compares (lexicographic order) z and x and returns:

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

func (*Felt) CmpCompat

func (z *Felt) CmpCompat(x *Felt) int

CmpCompat is exactly the same as Cmp except that it returns zero if both z and x are nil.

func (*Felt) Deref

func (z *Felt) Deref() Felt

func (*Felt) Div

func (z *Felt) Div(x, y *Felt) *Felt

Div z = x*y^-1 mod q

func (*Felt) Double

func (z *Felt) Double(x *Felt) *Felt

Double z = x + x mod q, aka Lsh 1

func (*Felt) Equal

func (z *Felt) Equal(x *Felt) bool

Equal returns z == x; constant-time

func (*Felt) Exp

func (z *Felt) Exp(x Felt, exponent *big.Int) *Felt

Exp z = x^exponent mod q

func (*Felt) FitsOnOneWord

func (z *Felt) FitsOnOneWord() bool

FitsOnOneWord reports whether z words (except the least significant word) are 0

func (*Felt) FromMont

func (z *Felt) FromMont() *Felt

FromMont converts z in place (i.e. mutates) from Montgomery to regular representation sets and returns z = z * 1

func (*Felt) Halve

func (z *Felt) Halve()

Halve sets z to z / 2 (mod p)

func (*Felt) Hex

func (z *Felt) Hex() string

Hex returns a hex string without the 0x prefix. This function will automatically convert to regular form.

func (*Felt) Hex0x

func (z *Felt) Hex0x() string

func (*Felt) Inverse

func (z *Felt) Inverse(x *Felt) *Felt

Inverse z = x⁻¹ mod q Implements "Optimized Binary GCD for Modular Inversion" https://github.com/pornin/bingcd/blob/main/doc/bingcd.pdf

func (*Felt) IsOne

func (z *Felt) IsOne() bool

IsOne returns z == 1

func (*Felt) IsUint64

func (z *Felt) IsUint64() bool

IsUint64 reports whether z can be represented as an uint64.

func (*Felt) IsZero

func (z *Felt) IsZero() bool

IsZero returns z == 0

func (*Felt) Legendre

func (z *Felt) Legendre() int

Legendre returns the Legendre symbol of z (either +1, -1, or 0.)

func (*Felt) LexicographicallyLargest

func (z *Felt) LexicographicallyLargest() bool

LexicographicallyLargest returns true if this element is strictly lexicographically larger than its negation, false otherwise

func (*Felt) Marshal

func (z *Felt) Marshal() []byte

Marshal returns the regular (non montgomery) value of z as a big-endian byte slice.

func (*Felt) MarshalJSON

func (z *Felt) MarshalJSON() ([]byte, error)

MarshalJSON returns json encoding of z (z.Text(10)) If z == nil, returns null

func (*Felt) Mul

func (z *Felt) Mul(x, y *Felt) *Felt

Mul z = x * y mod q see https://hackmd.io/@gnark/modular_multiplication

func (*Felt) Neg

func (z *Felt) Neg(x *Felt) *Felt

Neg z = q - x

func (*Felt) NotEqual

func (z *Felt) NotEqual(x *Felt) uint64

NotEqual returns 0 if and only if z == x; constant-time

func (*Felt) Rsh

func (z *Felt) Rsh(x *Felt, n uint) *Felt

Rsh sets z = x >> n and returns z.

func (*Felt) Select

func (z *Felt) Select(c int, x0 *Felt, x1 *Felt) *Felt

Select is a constant-time conditional move. If c=0, z = x0. Else z = x1

func (*Felt) Set

func (z *Felt) Set(x *Felt) *Felt

Set z = x

func (*Felt) SetBigInt

func (z *Felt) SetBigInt(v *big.Int) *Felt

SetBigInt sets z to v (regular form) and returns z in Montgomery form

func (*Felt) SetBit

func (z *Felt) SetBit(i uint64, j uint64) *Felt

SetBit sets bit i to j on z. Undefined behavior if j is not zero or one. It is the responsibility of the caller to convert to/from regular form if necessary.

func (*Felt) SetBytes

func (z *Felt) SetBytes(e []byte) *Felt

SetBytes interprets e as the bytes of a big-endian unsigned integer, sets z to that value (in Montgomery form), and returns z.

func (*Felt) SetHex

func (z *Felt) SetHex(s string) *Felt

SetHex sets z to the value represented by the hex string s mod q. It makes no difference if there is a leading 0x prefix on s. This function assumes s is in regular form and will convert to Montgomery form.

func (*Felt) SetInt64

func (z *Felt) SetInt64(v int64) *Felt

SetInt64 sets z to v and returns z

func (*Felt) SetInterface

func (z *Felt) SetInterface(i1 interface{}) (*Felt, error)

SetInterface converts provided interface into Felt returns an error if provided type is not supported supported types: Felt, *Felt, uint64, int, string (interpreted as base10 integer), *big.Int, big.Int, []byte

func (*Felt) SetOne

func (z *Felt) SetOne() *Felt

SetOne z = 1 (in Montgomery form)

func (*Felt) SetRandom

func (z *Felt) SetRandom() (*Felt, error)

SetRandom sets z to a random element < q

func (*Felt) SetString

func (z *Felt) SetString(number string) *Felt

SetString creates a big.Int with number and calls SetBigInt on z

The number prefix determines the actual base: A prefix of ”0b” or ”0B” selects base 2, ”0”, ”0o” or ”0O” selects base 8, and ”0x” or ”0X” selects base 16. Otherwise, the selected base is 10 and no prefix is accepted.

For base 16, lower and upper case letters are considered the same: The letters 'a' to 'f' and 'A' to 'F' represent digit values 10 to 15.

An underscore character ”_” may appear between a base prefix and an adjacent digit, and between successive digits; such underscores do not change the value of the number. Incorrect placement of underscores is reported as a panic if there are no other errors.

func (*Felt) SetUint64

func (z *Felt) SetUint64(v uint64) *Felt

SetUint64 sets z to v and returns z

func (*Felt) SetZero

func (z *Felt) SetZero() *Felt

SetZero z = 0

func (*Felt) Sqrt

func (z *Felt) Sqrt(x *Felt) *Felt

Sqrt z = √x mod q if the square root doesn't exist (x is not a square mod q) Sqrt leaves z unchanged and returns nil

func (*Felt) Square

func (z *Felt) Square(x *Felt) *Felt

Square z = x * x mod q see https://hackmd.io/@gnark/modular_multiplication

func (*Felt) String

func (z *Felt) String() string

String returns the decimal representation of z as generated by z.Text(10).

func (*Felt) Sub

func (z *Felt) Sub(x, y *Felt) *Felt

Sub z = x - y mod q

func (*Felt) Text

func (z *Felt) Text(base int) string

Text returns the string representation of z in the given base. Base must be between 2 and 36, inclusive. The result uses the lower-case letters 'a' to 'z' for digit values 10 to 35. No prefix (such as "0x") is added to the string. If z is a nil pointer it returns "<nil>". If base == 10 and -z fits in a uint64 prefix "-" is added to the string.

func (*Felt) ToBigInt

func (z *Felt) ToBigInt(res *big.Int) *big.Int

ToBigInt returns z as a big.Int in Montgomery form

func (Felt) ToBigIntRegular

func (z Felt) ToBigIntRegular(res *big.Int) *big.Int

ToBigIntRegular returns z as a big.Int in regular form

func (*Felt) ToMont

func (z *Felt) ToMont() *Felt

ToMont converts z to Montgomery form sets and returns z = z * r²

func (Felt) ToRegular

func (z Felt) ToRegular() Felt

ToRegular returns z in regular form (doesn't mutate z)

func (*Felt) ToggleBit

func (z *Felt) ToggleBit(i uint64) *Felt

ToggleBit flips bit i on z and returns z. Returns z if i is greater than the number of bits than Felt can represent. It is the responsibility of the caller to convert to/from regular form if necessary.

func (*Felt) Uint64

func (z *Felt) Uint64() uint64

Uint64 returns the uint64 representation of x. If x cannot be represented in a uint64, the result is undefined.

func (*Felt) UnmarshalJSON

func (z *Felt) UnmarshalJSON(data []byte) error

UnmarshalJSON accepts numbers and strings as input See Felt.SetString for valid prefixes (0x, 0b, ...)

Directories

Path Synopsis
internal
cmd/generator command

Jump to

Keyboard shortcuts

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