common

package
v0.0.0-...-202feaa Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2024 License: BSD-3-Clause Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNoModInverse = errors.New("modular inverse does not exist")
View Source
var SmallPrimes = []uint8{
	3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53,
}

SmallPrimes is a list of small prime numbers that allows us to rapidly exclude some fraction of composite candidates when searching for a random prime. This list is truncated at the point where SmallPrimesProduct exceeds a uint64. It does not include two because we ensure that the candidates are odd by construction.

View Source
var SmallPrimesProduct = new(big.Int).SetUint64(16294579238595022365)

SmallPrimesProduct is the product of the values in SmallPrimes and allows us to reduce a candidate prime by this number and then determine whether it's coprime with all the elements of SmallPrimes without further big.Int operations.

Functions

func Close

func Close(o io.Closer)

Close is a helper function for absorbing errors in the `defer x.Close()` pattern

func Crt

func Crt(a *big.Int, pa *big.Int, b *big.Int, pb *big.Int) *big.Int

Crt finds a number x (mod pa*pb) such that x = a (mod pa) and x = b (mod pb)

func FastRandomBigInt

func FastRandomBigInt(limit *big.Int) *big.Int

FastRandomBigInt derives a random number uniformly chosen below the given limit from a random 256 bit seed generated when the application starts.

func GetHashNumber

func GetHashNumber(a *big.Int, b *big.Int, index int, bitlen uint) *big.Int

GetHashNumber uses a hash to generate random numbers of a given bit-length in the fiat-shamir heuristic

func HashCommit

func HashCommit(values []*big.Int, issig bool) *big.Int

HashCommit computes the sha256 hash over the asn1 representation of a slice of big integers and returns a positive big integer that can be represented with that hash.

func IntHashSha256

func IntHashSha256(input []byte) *big.Int

IntHashSha256 is a utility function which computes the sha256 hash over a byte array and returns this hash as a big.Int.

func LegendreSymbol

func LegendreSymbol(a, p *big.Int) int

LegendreSymbol calculates the Legendre symbol (a/p).

func ModInverse

func ModInverse(a, n *big.Int) (ia *big.Int, ok bool)

ModInverse returns ia, the inverse of a in the multiplicative group of prime order n. It requires that a be a member of the group (i.e. less than n). This function was taken from Go's RSA implementation

func ModPow

func ModPow(x, y, m *big.Int) (*big.Int, error)

ModPow computes x^y mod m. The exponent (y) can be negative, in which case it uses the modular inverse to compute the result (in contrast to Go's Exp function).

func ModSqrt

func ModSqrt(a *big.Int, factors []*big.Int) (*big.Int, bool)

ModSqrt calculates Sqrt modulo a number with given prime factors. Also allows 4 as a factor All factors should be relatively prime to each other!

func PrimeSqrt

func PrimeSqrt(a *big.Int, pa *big.Int) (*big.Int, bool)

PrimeSqrt calculates sqrt modulo a prime

func RandomBigInt

func RandomBigInt(numBits uint) (*big.Int, error)

RandomBigInt returns a random big integer value in the range [0,(2^numBits)-1], inclusive.

func RandomPrimeInRange

func RandomPrimeInRange(rand io.Reader, start, length uint) (p *big.Int, err error)

RandomPrimeInRange returns a random probable prime in the range [2^start, 2^start + 2^length] This code is an adaption of Go's own Prime function in rand/util.go

func RandomQR

func RandomQR(n *big.Int) *big.Int

func RepresentToBases

func RepresentToBases(bases, exps []*big.Int, modulus *big.Int, maxMessageLength uint) *big.Int

RepresentToBases returns a representation of the given exponents in terms of the R bases from the public key. For example given exponents exps[1],...,exps[k] this function returns

R[1]^{exps[1]}*...*R[k]^{exps[k]} (mod N)

with R and N coming from the public key. The exponents are hashed if their length exceeds the maximum message length from the public key.

func SumFourSquares

func SumFourSquares(n *big.Int) (*big.Int, *big.Int, *big.Int, *big.Int)

SumFourSquares expresses a number as sum of four squares algorithm from "Randomized algorithms in number theory" by M. Rabin and J. Shallit

Types

type CPRNG

type CPRNG struct {
	// contains filtered or unexported fields
}

CPRNG is a simple thread-safe cryptographically secure pseudo-random number generator. Implemented with AES in counter mode with the seed as key and an atomic uint64 as counter.

func NewCPRNG

func NewCPRNG(seed *[32]byte) (*CPRNG, error)

func (*CPRNG) Read

func (c *CPRNG) Read(buf []byte) (n int, err error)

type FastMod

type FastMod struct {
	// contains filtered or unexported fields
}

func (*FastMod) Mod

func (m *FastMod) Mod(ret, x *big.Int) *big.Int

func (*FastMod) Set

func (m *FastMod) Set(p *big.Int)

Jump to

Keyboard shortcuts

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