field

package
v1.2.23 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Copyright Consensys Software Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

SPDX-License-Identifier: Apache-2.0

Copyright Consensys Software Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

SPDX-License-Identifier: Apache-2.0

Copyright Consensys Software Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

SPDX-License-Identifier: Apache-2.0

Copyright Consensys Software Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

SPDX-License-Identifier: Apache-2.0

Index

Constants

This section is empty.

Variables

View Source
var BLS12_377 = Config{"BLS12_377", 252, 160}

BLS12_377 is the defacto default field at this time.

View Source
var FIELD_CONFIGS = []Config{
	GF_251,
	GF_8209,
	KOALABEAR_16,
	BLS12_377,
}

FIELD_CONFIGS determines the set of supported fields.

View Source
var GF_251 = Config{"GF_251", 7, 4}

GF_251 is teany tiny prime field used exclusively for testing.

View Source
var GF_8209 = Config{"GF_8209", 13, 8}

GF_8209 is small prime field used exclusively for testing.

View Source
var KOALABEAR_16 = Config{"KOALABEAR_16", 30, 16}

KOALABEAR_16 corresponds to the KoalaBear field with a 16bit register size.

Functions

func BatchInvert

func BatchInvert[T Element[T]](s array.MutArray[T]) array.MutArray[T]

BatchInvert efficiently inverts the list of elements s, in place.

func BigInt

func BigInt[F Element[F]](val big.Int) F

BigInt constructs a field element from a given big.Int

func BigInts

func BigInts[F Element[F]](values []big.Int) []F

BigInts constructs an array of field elements from a given array of big.Int.

func FromBigEndianBytes

func FromBigEndianBytes[F Element[F]](bytes []byte) F

FromBigEndianBytes constructs a word from an array of bytes given in big endian order.

func One

func One[F Element[F]]() F

One constructs a field element representing 1

func Pow

func Pow[F Element[F]](val F, n uint64) F

Pow takes a given value to the power n.

func SplitWord

func SplitWord[F Element[F]](val word.BigEndian, widths []uint) ([]F, bool)

SplitWord splits a BigEndian word into one or more limbs in a given field F, where each has a given width. If the given value cannot be split into the given widths (i.e. because it overflows their combined width), false is returned to signal a splitting failure.

func TwoPowN

func TwoPowN[F Element[F]](n uint) F

TwoPowN constructs a field element representing 2^n

func Uint1 added in v1.2.21

func Uint1[F Element[F]](val bool) F

Uint1 construct a field element from a boolean value, where false returns 0 and true returns 1.

func Uint64

func Uint64[F Element[F]](val uint64) F

Uint64 construct a field element from a given uint64

func Zero

func Zero[F Element[F]]() F

Zero constructs a field element representing 0

Types

type Config

type Config struct {
	// Name suitable for identifying the config.  This is only really used for
	// improving error reporting, etc.
	Name string
	// Maximum field bandwidth available in the field.
	BandWidth uint
	// Maximum register width to use with this field.
	RegisterWidth uint
}

Config provides a simple mechanism for configuring the field agnosticity pipeline.

func GetConfig

func GetConfig(name string) *Config

GetConfig returns the field configuration corresponding with the given name, or nil no such config exists.

func (Config) Modulus

func (p Config) Modulus() *big.Int

Modulus returns the (prime) modulus used for this field.

type Element

type Element[Operand any] interface {
	fmt.Stringer
	word.Word[Operand]
	// Add x+y
	Add(y Operand) Operand
	// Return the value of this word as a big integer.
	BigInt() *big.Int
	// Check whether this value is zero (or not).
	IsZero() bool
	// Check whether this value is one (or not).
	IsOne() bool
	// Return the modulus for the field in question.
	Modulus() *big.Int
	// Compute x * y
	Mul(y Operand) Operand
	// Compute x⁻¹, or 0 if x = 0.
	Inverse() Operand
	// Compute x - y
	Sub(y Operand) Operand
	// Text returns the numerical value of x in the given base.
	Text(base int) string
}

An Element of a prime-order field.

Directories

Path Synopsis
internal
generator command

Jump to

Keyboard shortcuts

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