genmatrix

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2020 License: GPL-3.0 Imports: 5 Imported by: 1

README

Generic Matrix

This library provides basic matrix operations for matrices with elements from any space.

Space

The library is built around the interface space. It defines the element-wise operations needed for the matrix operations to work. Two examples are implemented in bigint.go and damgard-jurik.go where the operations are defined for *big.Int from the standard library, and the additive homomorphic cryptosystem described by Damgård and Jurik and implemented in tcpaillier.

Usage

The library is in the package genmatrix. Import it by import github.com/ontanj/generic-matrix and use it as genmatrix.NewMatrix(...).

Matrix structure

The matrices are defined as

type Matrix struct {
    values []interface{}
    Rows, Cols int
    Space space
}

where values are stored in row-major order and space stores the evaluation space for the matrix.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bigint

type Bigint struct{}

func (Bigint) Add

func (p Bigint) Add(a, b interface{}) (interface{}, error)

func (Bigint) Multiply

func (p Bigint) Multiply(a, b interface{}) (interface{}, error)

func (Bigint) Scalarspace

func (p Bigint) Scalarspace() bool

func (Bigint) Scale

func (p Bigint) Scale(a interface{}, b interface{}) (interface{}, error)

func (Bigint) Subtract

func (p Bigint) Subtract(a, b interface{}) (interface{}, error)

type DJ_public_key

type DJ_public_key struct {
	*tcpaillier.PubKey
}

func NewDJCryptosystem

func NewDJCryptosystem() (public_key DJ_public_key, secret_keys []*tcpaillier.KeyShare, err error)

func (DJ_public_key) Add

func (pk DJ_public_key) Add(a, b interface{}) (sum interface{}, err error)

func (DJ_public_key) Multiply

func (pk DJ_public_key) Multiply(a, b interface{}) (interface{}, error)

func (DJ_public_key) Scalarspace

func (pk DJ_public_key) Scalarspace() bool

func (DJ_public_key) Scale

func (pk DJ_public_key) Scale(ciphertext, factor interface{}) (product interface{}, err error)

func (DJ_public_key) Subtract

func (pk DJ_public_key) Subtract(a, b interface{}) (diff interface{}, err error)

type Matrix

type Matrix struct {
	Rows, Cols int
	Space      Space
	// contains filtered or unexported fields
}

func NewMatrix

func NewMatrix(rows, cols int, data []interface{}, space Space) (m Matrix, err error)

create a new Matrix with the given size and data acting in space

func NewMatrixFromInt

func NewMatrixFromInt(rows, cols int, data []int) (Matrix, error)

create a new Matrix from int values

func (Matrix) Add

func (a Matrix) Add(b Matrix) (c Matrix, err error)

matrix addition

func (Matrix) Apply

func (a Matrix) Apply(f func(interface{}) (interface{}, error)) (b Matrix, err error)

apply function f to all matrix elements

func (Matrix) At

func (m Matrix) At(row, col int) (interface{}, error)

get value at (row, col), where first row/col is 0.

func (Matrix) Concatenate

func (a Matrix) Concatenate(b Matrix) (Matrix, error)

concatenate matrices as A|B

func (Matrix) CropHorizontally

func (a Matrix) CropHorizontally(k int) Matrix

create a new matrix from last k columns of a

func (Matrix) Multiply

func (a Matrix) Multiply(b Matrix) (c Matrix, err error)

multiply a * b also handles multiplication of scalar * non-scalar matrices and vice versa if a and b are non-scalar in different spaces, the space of a is used

func (Matrix) MultiplyScalar

func (a Matrix) MultiplyScalar(scalar interface{}) (Matrix, error)

multiplication of a by a scalar assumes matrix and factor is in same space, otherwise use Scale

func (Matrix) Scale

func (a Matrix) Scale(factor interface{}) (Matrix, error)

scale a according to scalar to be used if factor is in a scalar space wile a is not

func (Matrix) Set

func (m Matrix) Set(row, col int, value interface{}) error

set value at (row, col), where first row/col is 0.

func (Matrix) Subtract

func (a Matrix) Subtract(b Matrix) (c Matrix, err error)

matrix subtraction

type Space

type Space interface {

	// addition of two elements in the space
	Add(interface{}, interface{}) (sum interface{}, err error)

	// subtraction of two elements in the space
	Subtract(interface{}, interface{}) (diff interface{}, err error)

	// multiplication of two elements in the space
	Multiply(interface{}, interface{}) (product interface{}, err error)

	// scaling of an element by scalar factor
	Scale(spaced interface{}, factor interface{}) (product interface{}, err error)

	// return true if this space (matrix) consist of scalar factors
	// e.i. if Scale are to be used in matrix multiplication
	Scalarspace() bool
}

Jump to

Keyboard shortcuts

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