data

package
v0.0.0-...-a536860 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package data defines basic mathematical structures used throughout the library.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Matrix

type Matrix []Vector

Matrix wraps a slice of Vector elements. It represents a row-major. order matrix.

The j-th element from the i-th vector of the matrix can be obtained as m[i][j].

func Identity

func Identity(rows, cols int) Matrix

Identity returns the identity matrix with given dimensions.

func NewConstantMatrix

func NewConstantMatrix(rows, cols int, c *big.Int) Matrix

NewConstantMatrix returns a new Matrix instance with all elements set to constant c.

func NewMatrix

func NewMatrix(vectors []Vector) (Matrix, error)

NewMatrix accepts a slice of Vector elements and returns a new Matrix instance. It returns error if not all the vectors have the same number of elements.

func NewRandomDetMatrix

func NewRandomDetMatrix(rows, cols int, max *big.Int, key *[32]byte) (Matrix, error)

NewRandomDetMatrix returns a new Matrix instance with random elements sampled by a pseudo-random number generator. Elements are sampled from [0, max) and key determines the pseudo-random generator.

func NewRandomMatrix

func NewRandomMatrix(rows, cols int, sampler sample.Sampler) (Matrix, error)

NewRandomMatrix returns a new Matrix instance with random elements sampled by the provided sample.Sampler. Returns an error in case of sampling failure.

func (Matrix) Add

func (m Matrix) Add(other Matrix) (Matrix, error)

Add adds matrices m and other. The result is returned in a new Matrix. Error is returned if m and other have different dimensions.

func (Matrix) Apply

func (m Matrix) Apply(f func(*big.Int) *big.Int) Matrix

Apply applies an element-wise function f to matrix m. The result is returned in a new Matrix.

func (Matrix) CheckBound

func (m Matrix) CheckBound(bound *big.Int) error

CheckBound checks whether all matrix elements are strictly smaller than the provided bound. It returns error if at least one element is >= bound.

func (Matrix) CheckDims

func (m Matrix) CheckDims(rows, cols int) bool

CheckDims checks whether dimensions of matrix m match the provided rows and cols arguments.

func (Matrix) Cols

func (m Matrix) Cols() int

Cols returns the number of columns of matrix m.

func (Matrix) Copy

func (m Matrix) Copy() Matrix

Copy creates a new Matrix with the same values.

func (Matrix) Determinant

func (m Matrix) Determinant() (*big.Int, error)

Determinant returns the determinant of matrix m. It returns an error if the determinant does not exist.

func (Matrix) DeterminantGauss

func (m Matrix) DeterminantGauss(p *big.Int) (*big.Int, error)

DeterminantGauss returns the determinant of matrix m using Gaussian elimination. It returns an error if the determinant does not exist.

func (Matrix) DimsMatch

func (m Matrix) DimsMatch(other Matrix) bool

DimsMatch returns a bool indicating whether matrices m and other have the same dimensions.

func (Matrix) Dot

func (m Matrix) Dot(other Matrix) (*big.Int, error)

Dot calculates the dot product (inner product) of matrices m and other, which we define as the sum of the dot product of rows of both matrices. It returns an error if m and other have different dimensions.

func (Matrix) GaussianElimination

func (m Matrix) GaussianElimination(p *big.Int) (Matrix, error)

GaussianElimination uses Gaussian elimination to transform a matrix into an equivalent upper triangular form

func (Matrix) GetCol

func (m Matrix) GetCol(i int) (Vector, error)

GetCol returns i-th column of matrix m as a vector. It returns error if i >= the number of m's columns.

func (Matrix) InverseMod

func (m Matrix) InverseMod(p *big.Int) (Matrix, error)

InverseMod returns the inverse matrix of m in the group Z_p. Note that as we consider only matrix with integers, the inverse exists only in Z_p.

It returns an error in case matrix is not invertible.

func (Matrix) InverseModGauss

func (m Matrix) InverseModGauss(p *big.Int) (Matrix, *big.Int, error)

InverseModGauss returns the inverse matrix of m in the group Z_p. The algorithm uses Gaussian elimination. It returns the determinant as well. In case the matrix is not invertible it returns an error.

func (Matrix) JoinCols

func (m Matrix) JoinCols(other Matrix) (Matrix, error)

JoinCols joins matrices m and other in a new Matrix with columns from both matrices.

func (Matrix) JoinRows

func (m Matrix) JoinRows(other Matrix) (Matrix, error)

JoinRows joins matrices m and other in a new Matrix with rows from both matrices.

func (Matrix) MatMulMatG1

func (m Matrix) MatMulMatG1(other MatrixG1) (MatrixG1, error)

MatMulMatG1 multiplies m and other in the sense that if other is t * bn256.G1 for some matrix t, then the function returns m * t * bn256.G1 where m * t is a matrix multiplication.

func (Matrix) MatMulMatG2

func (m Matrix) MatMulMatG2(other MatrixG2) (MatrixG2, error)

MatMulMatG2 multiplies m and other in the sense that if other is t * bn256.G2 for some matrix t, then the function returns m * t * bn256.G2 where m * t is a matrix multiplication.

func (Matrix) MatMulVecG2

func (m Matrix) MatMulVecG2(other VectorG2) (VectorG2, error)

MatMulVecG2 multiplies m and other in the sense that if other is t * bn256.G2 for some vector t, then the function returns m * t * bn256.G2 where m * t is a matrix-vector multiplication.

func (Matrix) Minor

func (m Matrix) Minor(i int, j int) (Matrix, error)

Minor returns a matrix obtained from m by removing row i and column j. It returns an error if i >= number of rows of m, or if j >= number of columns of m.

func (Matrix) Mod

func (m Matrix) Mod(modulo *big.Int) Matrix

Mod applies the element-wise modulo operation on matrix m. The result is returned in a new Matrix.

func (Matrix) Mul

func (m Matrix) Mul(other Matrix) (Matrix, error)

Mul multiplies matrices m and other. The result is returned in a new Matrix. Error is returned if m and other have different dimensions.

func (Matrix) MulG1

func (m Matrix) MulG1() MatrixG1

MulG1 calculates m * bn256.G1 and returns the result in a new MatrixG1 instance.

func (Matrix) MulG2

func (m Matrix) MulG2() MatrixG2

MulG2 calculates m * bn256.G1 and returns the result in a new MatrixG2 instance.

func (Matrix) MulScalar

func (m Matrix) MulScalar(x *big.Int) Matrix

MulScalar multiplies elements of matrix m by a scalar x. The result is returned in a new Matrix.

func (Matrix) MulVec

func (m Matrix) MulVec(v Vector) (Vector, error)

MulVec multiplies matrix m and vector v. It returns the resulting vector. Error is returned if the number of columns of m differs from the number of elements of v.

func (Matrix) MulXMatY

func (m Matrix) MulXMatY(x, y Vector) (*big.Int, error)

MulXMatY calculates the function x^T * m * y, where x and y are vectors.

func (Matrix) Rows

func (m Matrix) Rows() int

Rows returns the number of rows of matrix m.

func (Matrix) Sub

func (m Matrix) Sub(other Matrix) (Matrix, error)

Sub adds matrices m and other. The result is returned in a new Matrix. Error is returned if m and other have different dimensions.

func (Matrix) Tensor

func (m Matrix) Tensor(other Matrix) Matrix

Tensor creates a tensor product of matrices m and other. The result is returned in a new Matrix.

func (Matrix) ToVec

func (m Matrix) ToVec() Vector

ToVec creates a vector whose entries are entries of m ordered as m_11, m_12,..., m_21, m_22,..., m_kl.

func (Matrix) Transpose

func (m Matrix) Transpose() Matrix

Transpose transposes matrix m and returns the result in a new Matrix.

type MatrixG1

type MatrixG1 []VectorG1

MatrixG1 wraps a slice of VectorG1 elements. It represents a row-major. order matrix.

The j-th element from the i-th vector of the matrix can be obtained as m[i][j].

func (MatrixG1) Add

func (m MatrixG1) Add(other MatrixG1) MatrixG1

Add sums matrices m and other componentwise. It returns the result in a new MatrixG1 instance.

func (MatrixG1) Cols

func (m MatrixG1) Cols() int

Cols returns the number of columns of matrixG1 m.

func (MatrixG1) MulScalar

func (m MatrixG1) MulScalar(s *big.Int) MatrixG1

MulScalar multiplies matrix m by a scalar s. It returns the result in a new MatrixG1 instance.

func (MatrixG1) MulVector

func (m MatrixG1) MulVector(v Vector) VectorG1

MulVector multiplies matrix m by a vector v, i.e if m is t * bn256.G1 for some matrix t, then the result is (t * v) bn256.G1

func (MatrixG1) Rows

func (m MatrixG1) Rows() int

Rows returns the number of rows of matrixG1 m.

type MatrixG2

type MatrixG2 []VectorG2

MatrixG2 wraps a slice of VectorG2 elements. It represents a row-major. order matrix.

The j-th element from the i-th vector of the matrix can be obtained as m[i][j].

func (MatrixG2) Cols

func (m MatrixG2) Cols() int

Cols returns the number of columns of matrixG2 m.

func (MatrixG2) MulScalar

func (m MatrixG2) MulScalar(s *big.Int) MatrixG2

MulScalar multiplies matrix m by a scalar s

func (MatrixG2) MulVector

func (m MatrixG2) MulVector(v Vector) VectorG2

MulVector multiplies matrix m by a vector v, i.e if m is t * bn256.G2 for some matrix t, then the result is (t * v) bn256.G2

func (MatrixG2) Rows

func (m MatrixG2) Rows() int

Rows returns the number of rows of matrixG2 m.

type Vector

type Vector []*big.Int

Vector wraps a slice of *big.Int elements.

func GaussianEliminationSolver

func GaussianEliminationSolver(mat Matrix, v Vector, p *big.Int) (Vector, error)

GaussianEliminationSolver solves a vector equation mat * x = v and finds vector x, using Gaussian elimination. Arithmetic operations are considered to be over Z_p, where p should be a prime number. If such x does not exist, then the function returns an error.

func NewConstantVector

func NewConstantVector(len int, c *big.Int) Vector

NewConstantVector returns a new Vector instance with all elements set to constant c.

func NewRandomDetVector

func NewRandomDetVector(len int, max *big.Int, key *[32]byte) (Vector, error)

NewRandomDetVector returns a new Vector instance with (deterministic) random elements sampled by a pseudo-random number generator. Elements are sampled from [0, max) and key determines the pseudo-random generator.

func NewRandomVector

func NewRandomVector(len int, sampler sample.Sampler) (Vector, error)

NewRandomVector returns a new Vector instance with random elements sampled by the provided sample.Sampler. Returns an error in case of sampling failure.

func NewVector

func NewVector(coordinates []*big.Int) Vector

NewVector returns a new Vector instance.

func (Vector) Add

func (v Vector) Add(other Vector) Vector

Add adds vectors v and other. The result is returned in a new Vector.

func (Vector) Apply

func (v Vector) Apply(f func(*big.Int) *big.Int) Vector

Apply applies an element-wise function f to vector v. The result is returned in a new Vector.

func (Vector) CheckBound

func (v Vector) CheckBound(bound *big.Int) error

CheckBound checks whether the absolute values of all vector elements are strictly smaller than the provided bound. It returns error if at least one element's absolute value is >= bound.

func (Vector) Copy

func (v Vector) Copy() Vector

Copy creates a new vector with the same values of the entries.

func (Vector) Dot

func (v Vector) Dot(other Vector) (*big.Int, error)

Dot calculates the dot product (inner product) of vectors v and other. It returns an error if vectors have different numbers of elements.

func (Vector) Mod

func (v Vector) Mod(modulo *big.Int) Vector

Mod performs modulo operation on vector's elements. The result is returned in a new Vector.

func (Vector) MulAsPolyInRing

func (v Vector) MulAsPolyInRing(other Vector) (Vector, error)

MulAsPolyInRing multiplies vectors v and other as polynomials in the ring of polynomials R = Z[x]/((x^n)+1), where n is length of the vectors. Note that the input vector [1, 2, 3] represents a polynomial Z[x] = x²+2x+3. It returns a new polynomial with degree <= n-1.

If vectors differ in size, error is returned.

func (Vector) MulG1

func (v Vector) MulG1() VectorG1

MulG1 calculates bn256.G1 * v (also g1^v in multiplicative notation) and returns the result (v[0] * bn256.G1, ... , v[n-1] * bn256.G1) in a VectorG1 instance.

func (Vector) MulG2

func (v Vector) MulG2() VectorG2

MulG2 calculates bn256.G2 * v (also g2^v in multiplicative notation) and returns the result (v[0] * bn256.G2, ... , v[n-1] * bn256.G2) in a VectorG2 instance.

func (Vector) MulScalar

func (v Vector) MulScalar(x *big.Int) Vector

MulScalar multiplies vector v by a given scalar x. The result is returned in a new Vector.

func (Vector) MulVecG1

func (v Vector) MulVecG1(g1 VectorG1) VectorG1

MulVecG1 calculates g1 * v (also g1^v in multiplicative notation) and returns the result (v[0] * g1[0], ... , v[n-1] * g1[n-1]) in a VectorG1 instance.

func (Vector) MulVecG2

func (v Vector) MulVecG2(g2 VectorG2) VectorG2

MulVecG2 calculates g2 * v (also g2^v in multiplicative notation) and returns the result (v[0] * g2[0], ... , v[n-1] * g2[n-1]) in a VectorG2 instance.

func (Vector) Neg

func (v Vector) Neg() Vector

Neg returns -v for given vector v. The result is returned in a new Vector.

func (Vector) String

func (v Vector) String() string

String produces a string representation of a vector.

func (Vector) Sub

func (v Vector) Sub(other Vector) Vector

Sub subtracts vectors v and other. The result is returned in a new Vector.

func (Vector) Tensor

func (v Vector) Tensor(other Vector) Vector

Tensor creates a tensor product of vectors v and other. The result is returned in a new Vector.

type VectorG1

type VectorG1 []*bn256.G1

VectorG1 wraps a slice of elements from elliptic curve BN256.G1 group.

func (VectorG1) Add

func (v VectorG1) Add(other VectorG1) VectorG1

Add sums vectors v1 and v2 (also v1 * v2 in multiplicative notation). It returns the result in a new VectorG1 instance.

func (VectorG1) Copy

func (v VectorG1) Copy() VectorG1

Copy produces a new copy of vector v.

func (VectorG1) MulScalar

func (v VectorG1) MulScalar(s *big.Int) VectorG1

MulScalar multiplies s * v (in additive notation).

func (VectorG1) Neg

func (v VectorG1) Neg() VectorG1

Neg returns a new VectorG1 instance with values -v in the additive notation.

type VectorG2

type VectorG2 []*bn256.G2

VectorG2 wraps a slice of elements from elliptic curve BN256.G2 group.

func (VectorG2) Add

func (v VectorG2) Add(other VectorG2) VectorG2

Add sums vectors v1 and v2 (also v1 * v2 in multiplicative notation). It returns the result in a new VectorG2 instance.

func (VectorG2) Copy

func (v VectorG2) Copy() VectorG2

Copy produces a new copy of vector v.

func (VectorG2) MulScalar

func (v VectorG2) MulScalar(s *big.Int) VectorG2

MulScalar multiplies s * v (in additive notation).

func (VectorG2) Neg

func (v VectorG2) Neg() VectorG2

Neg returns a new VectorG1 instance with values -v in the additive notation.

type VectorGT

type VectorGT []*bn256.GT

VectorGT wraps a slice of elements from pairing BN256.GT group.

func (VectorGT) Dot

func (v VectorGT) Dot(other Vector) *bn256.GT

Dot multiplies v = (v_1,...,v_n) and other = (o_1,...,o_n) to return v1 * o_1 + ... + v_n *o_n (in additive notation)

Jump to

Keyboard shortcuts

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