vectorext

package
v0.0.0-...-db31170 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2025 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

vector offers a set of utility function relating to slices of field element and that are commonly used as part of the repo.

Index

Constants

View Source
const (
	Bytes = 64 // number of bytes needed to represent a Element
)

Variables

This section is empty.

Functions

func Add

func Add(res, a, b []fext.Element, extras ...[]fext.Element)

Add adds two vectors `a` and `b` and put the result in `res` `res` must be pre-allocated by the caller and res, a and b must all have the same size. res == a or res == b or both is valid assignment.

func AddExt

func AddExt(res, a, b []fext.Element, extras ...[]fext.Element)

func DeepCopy

func DeepCopy(pol []fext.Element) []fext.Element

DeepCopy deep-copies the input vector

func Equal

func Equal(a, b []fext.Element) bool

Equal compares a and b and returns a boolean indicating whether they contain the same value. The function assumes that a and b have the same length. It panics otherwise.

func Fill

func Fill(v []fext.Element, val fext.Element)

Fill a vector `vec` in place with the given value `val`.

func ForTest

func ForTest(xs ...int) []fext.Element

ForTest returns a vector instantiated from a list of integers.

func ForTestFromPairs

func ForTestFromPairs(xs ...int) []fext.Element

ForTestFromPairs groups the input into pairs. Each pair populates the first two coordinates of a field extension, and the function then returns a vector instantiated these field extension elements.

func ForTestFromVect

func ForTestFromVect(xs ...[fext.ExtensionDegree]int) []fext.Element

ForTestFromVect computes a vector of field extensions, where each field extension is populated using one vector of size fext.ExtensionDegree

func Interleave

func Interleave(vecs ...[]fext.Element) []fext.Element

Interleave interleave two vectors:

(a, a, a, a), (b, b, b, b) -> (a, b, a, b, a, b, a, b)

The vecs[i] vectors must all have the same length

func IntoGnarkAssignment

func IntoGnarkAssignment(msgData []fext.Element) []gnarkfext.Variable

IntoGnarkAssignment converts an array of field.Element into an array of frontend.Variable that can be used to assign a vector of frontend.Variable in a circuit or to generate a vector of constant in the circuit definition.

func MulElementWise

func MulElementWise(res, a, b []fext.Element)

MulElementWise multiplies two vectors element wise and write the result in res. res = a is a valid assignment.

func PowerVec

func PowerVec(x fext.Element, n int) []fext.Element

PowerVec allocates and returns a vector of size n consisting of consecutive powers of x, starting from x^0 = 1 and ending on x^{n-1}. The function panics if given x=0 and returns an empty vector if n=0.

func Prettify

func Prettify(a []fext.Element) string

Prettify returns a string representing `a` in a human-readable fashion

func PseudoRand

func PseudoRand(rng *rand.Rand, size int) []fext.Element

PseudoRand generates a vector of field element with a given size using the provided random number generator

func Rand

func Rand(n int) []fext.Element

Rand creates a random vector of size n

func Repeat

func Repeat(x fext.Element, n int) []fext.Element

Repeat returns a vector of size n whose values are all equal to x.

func Reverse

func Reverse(v []fext.Element)

Reverse the elements of a vector inplace

func ScalarMul

func ScalarMul(res, vec []fext.Element, scalar fext.Element)

ScalarMul multiplies a vector by a scalar - in place. The result should be preallocated or it is going to panic. res = vec is a valid parameter assignment.

func ScalarProd

func ScalarProd(a, b []fext.Element) fext.Element

ScalarProd returns the scalar (inner) product of a and b. The function panics if a and b do not have the same size. If they have both empty vectors, the function returns 0.

func Sub

func Sub(res, a, b []fext.Element)

Sub substracts two vectors `a` and `b` and put the result in `res` `res` must be pre-allocated by the caller and res, a and b must all have the same size. res == a or res == b or both is valid assignment.

func ZeroPad

func ZeroPad(v []fext.Element, newLen int) []fext.Element

ZeroPad pads a vector to a given length. If the newLen is smaller than len(v), the function panic. It pads to the right (appending, not prepending) The resulting slice is allocated by the function, so it can be safely modified by the caller after the function returns.

Types

type Vector

type Vector []fext.Element

Vector represents a slice of Element.

It implements the following interfaces:

  • Stringer
  • io.WriterTo
  • io.ReaderFrom
  • encoding.BinaryMarshaler
  • encoding.BinaryUnmarshaler
  • sort.Interface

func (*Vector) Add

func (vector *Vector) Add(a, b Vector)

Add adds two vectors element-wise and stores the result in self. It panics if the vectors don't have the same length.

func (*Vector) AsyncReadFrom

func (vector *Vector) AsyncReadFrom(r io.Reader) (int64, error, chan error)

AsyncReadFrom reads a vector of big endian encoded Element. Length of the vector must be encoded as a uint32 on the first 4 bytes. It consumes the needed bytes from the reader and returns the number of bytes read and an error if any. It also returns a channel that will be closed when the validation is done. The validation consist of checking that the elements are smaller than the modulus, and converting them to montgomery form.

func (*Vector) InnerProduct

func (vector *Vector) InnerProduct(other Vector) (res fext.Element)

InnerProduct computes the inner product of two vectors. It panics if the vectors don't have the same length.

func (Vector) Len

func (vector Vector) Len() int

Len is the number of elements in the collection.

func (Vector) Less

func (vector Vector) Less(i, j int) bool

Less reports whether the element with index i should sort before the element with index j.

func (*Vector) MarshalBinary

func (vector *Vector) MarshalBinary() (data []byte, err error)

MarshalBinary implements encoding.BinaryMarshaler

func (*Vector) Mul

func (vector *Vector) Mul(a, b Vector)

Mul multiplies two vectors element-wise and stores the result in self. It panics if the vectors don't have the same length.

func (*Vector) ReadFrom

func (vector *Vector) ReadFrom(r io.Reader) (int64, error)

ReadFrom implements io.ReaderFrom and reads a vector of big endian encoded Element. Length of the vector must be encoded as a uint32 on the first 4 bytes.

func (*Vector) ScalarMul

func (vector *Vector) ScalarMul(a Vector, b *fext.Element)

ScalarMul multiplies a vector by a scalar element-wise and stores the result in self. It panics if the vectors don't have the same length.

func (Vector) String

func (vector Vector) String() string

String implements fmt.Stringer interface

func (*Vector) Sub

func (vector *Vector) Sub(a, b Vector)

Sub subtracts two vectors element-wise and stores the result in self. It panics if the vectors don't have the same length.

func (*Vector) Sum

func (vector *Vector) Sum() (res fext.Element)

Sum computes the sum of all elements in the vector.

func (Vector) Swap

func (vector Vector) Swap(i, j int)

Swap swaps the elements with indexes i and j.

func (*Vector) UnmarshalBinary

func (vector *Vector) UnmarshalBinary(data []byte) error

UnmarshalBinary implements encoding.BinaryUnmarshaler

func (*Vector) WriteTo

func (vector *Vector) WriteTo(w io.Writer) (int64, error)

WriteTo implements io.WriterTo and writes a vector of big endian encoded Element. Length of the vector is encoded as a uint32 on the first 4 bytes.

Jump to

Keyboard shortcuts

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