big

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2023 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidParse = errors.New("invalid parse")
)

Functions

func Init

func Init() error

Init loads and initializes OpenSSL. It must be called before any other OpenSSL call.

Only the first call to Init is effective, subsequent calls will return the same error result as the one from the first call.

If GO_OPENSSL_VERSION_OVERRIDE environment variable is not empty, its value will be appended to the OpenSSL shared library name as a version suffix when calling dlopen. For example, "GO_OPENSSL_VERSION_OVERRIDE=1.1.1k-fips" makes Init look for the shared library libcrypto.so.1.1.1k-fips. If GO_OPENSSL_VERSION_OVERRIDE environment variable is empty, Init will try to load the OpenSSL shared library using a list if supported and well-known version suffixes, going from higher to lower versions.

func VersionText

func VersionText() string

VersionText returns the version text of the OpenSSL currently loaded.

Types

type Int

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

An Int represents a signed multi-precision integer.

func GeneratePrime

func GeneratePrime(ctx *IntContext, bits int, safe bool) (*Int, error)

GeneratePrime generates a pseudo-random prime number of at least bit length bits using the IntContext provided in ctx. The returned number is probably prime with a negligible error. The maximum error rate is 2^-128. It's 2^-287 for a 512 bit prime, 2^-435 for a 1024 bit prime, 2^-648 for a 2048 bit prime, and lower than 2^-882 for primes larger than 2048 bit. If safe is true, it will be a safe prime (i.e. a prime p so that (p-1)/2 is also prime). ctx is a previously allocated IntContext used for temporary variables.

func NewInt

func NewInt() (*Int, error)

Allocates and initialize an Int struct.

func One

func One() *Int

Returns a constant Int with value 1.

func (*Int) Add

func (z *Int) Add(x, y *Int) error

Add sets z to the sum x+y and places the result in z.

func (*Int) And

func (z *Int) And(x, y *Int) error

And sets z = x & y.

func (*Int) BitLen

func (z *Int) BitLen() int

BitLen returns the length of the absolute value of z in bits. The bit length of 0 is 0.

func (*Int) Bytes

func (z *Int) Bytes() ([]byte, error)

Bytes returns the absolute value of z as a big-endian byte slice. To use a fixed length slice, or a preallocated one, use [FillBytes].

func (*Int) BytesLen

func (z *Int) BytesLen() int

BytesLen returns the size of z in bytes.

func (*Int) Cmp

func (z *Int) Cmp(x *Int) int

Cmp compares z and x and returns:

-1 if z <  x
 0 if z == x
+1 if z >  x

func (*Int) ConstantTimeEq

func (z *Int) ConstantTimeEq(x *Int) (bool, error)

ConstantTimeEq compares z and x and returns true if they are equal, false otherwise. The time taken is a function of the bytes length of the numbers and is independent of the contents.

func (*Int) Div

func (z *Int) Div(ctx *IntContext, x, y *Int) error

Div divides z by y and places the result in z. For division by powers of 2, use [Rsh]. ctx is a previously allocated IntContext used for temporary variables.

func (*Int) Exp

func (z *Int) Exp(ctx *IntContext, x, y *Int) error

Exp raises x to the y-th power and places the result in z (z=x^y). This function is faster than repeated applications of [Mul]. ctx is a previously allocated IntContext used for temporary variables.

func (*Int) FillBytes

func (z *Int) FillBytes(buf []byte) error

Bytes sets buf to the absolute value of z as a big-endian byte slice. If the absolute value of z doesn't fit in buf, FillBytes will panic.

func (*Int) Hex

func (i *Int) Hex() string

String returns the hexadecimal representation of i.

func (*Int) Lsh

func (z *Int) Lsh(x *Int, n uint) error

Lsh shifts x left by n bits and places the result in z (z=x*2^n). Note that n must be nonnegative.

func (*Int) MarshalJSON

func (i *Int) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (*Int) MarshalText

func (i *Int) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface.

func (*Int) Mod

func (z *Int) Mod(ctx *IntContext, x, y *Int) error

Mod sets z to the modulus x%y for y != 0. ctx is a previously allocated IntContext used for temporary variables.

func (*Int) ModExp

func (z *Int) ModExp(ctx *IntContext, x, y, m *Int) error

ModExp computes x to the y-th power modulo m (z=x^y % m). This function uses less time and space than [Exp]. Do not call this function when m is even and any of the parameters have the constant-time flag set. ctx is a previously allocated IntContext used for temporary variables.

func (*Int) ModExpMont

func (z *Int) ModExpMont(mont *MontgomeryContext, ctx *IntContext, x, y, m *Int) error

ModExpMont computes z to the y-th power modulo m (z=x^y % m) using Montgomery multiplication. mont is a Montgomery context and can be nil. In the case mont is nil, it will be initialized within the function, so you can save time on initialization if you provide it in advance. If any of the parameters x, y or m have the constant-time flag set, this function uses fixed windows and the special precomputation memory layout to limit data-dependency to a minimum to protect secret exponents. ctx is a previously allocated IntContext used for temporary variables.

func (*Int) ModInverse

func (z *Int) ModInverse(ctx *IntContext, g, n *Int) error

ModInverse sets z to the multiplicative inverse of g in the ring ℤ/nℤ. ctx is a previously allocated IntContext used for temporary variables.

func (*Int) ModMul

func (z *Int) ModMul(ctx *IntContext, x, y, m *Int) error

ModMul multiplies x by y and finds the nonnegative remainder respective to modulus m (z=(x*y) mod m). For more efficient algorithms for repeated computations using the same modulus, see [ModMulMontgomery]. ctx is a previously allocated IntContext used for temporary variables.

func (*Int) ModMulMontgomery

func (z *Int) ModMulMontgomery(mont *MontgomeryContext, ctx *IntContext, x, y *Int) error

ModMulMontgomery implement Montgomery multiplication. It computes Mont(x,y):=x*y*R^-1 and places the result in z. ctx is a previously allocated IntContext used for temporary variables.

func (*Int) Mul

func (z *Int) Mul(ctx *IntContext, x, y *Int) error

Mul multiplies x and y and places the result in z. For multiplication by powers of 2, use [Lsh]. ctx is a previously allocated IntContext used for temporary variables.

func (*Int) Or

func (z *Int) Or(x, y *Int) error

Or sets z = x | y.

func (*Int) ProbablyPrime

func (z *Int) ProbablyPrime(ctx *IntContext) (bool, error)

ProbablyPrime tests if the number z is prime. The functions tests until one of the tests shows that z is composite, or all the tests passed. If z passes all these tests, it is considered a probable prime. The test performed on z are trial division by a number of small primes and rounds of the of the Miller-Rabin probabilistic primality test. The functions do at least 64 rounds of the Miller-Rabin test giving a maximum false positive rate of 2^-128. If the size of z is more than 2048 bits, they do at least 128 rounds giving a maximum false positive rate of 2^-256. ctx is a previously allocated IntContext used for temporary variables.

func (*Int) RandRange

func (z *Int) RandRange(max *Int) error

RandRange generates a cryptographically strong pseudo-random number z in the range 0 <= z < max.

func (*Int) Rsh

func (z *Int) Rsh(x *Int, n uint) error

Rsh shifts x right by n bits and places the result in z (z=x/2^n). Note that n must be nonnegative.

func (*Int) Set

func (z *Int) Set(x *Int) error

Set sets z to x.

func (*Int) SetBytes

func (z *Int) SetBytes(buf []byte) *Int

SetBytes interprets buf as the bytes of a big-endian unsigned integer, sets z to that value, and returns z.

func (*Int) SetConstantTime

func (i *Int) SetConstantTime() *Int

func (*Int) SetDecString

func (z *Int) SetDecString(s string) error

SetDecString sets z to the value of s interpreted in the decimal base.

func (*Int) SetHexString

func (z *Int) SetHexString(s string) error

SetHexString sets z to the value of s interpreted in the hexadecimal base.

func (*Int) SetUInt64

func (z *Int) SetUInt64(x uint64) error

SetUInt64 sets z to the value of x.

func (*Int) String

func (i *Int) String() string

String returns the decimal representation of i.

func (*Int) Sub

func (z *Int) Sub(x, y *Int) error

Sub sets z to the difference x-y and places the result in z.

func (*Int) Uint64

func (z *Int) Uint64() uint64

Uint64 returns the uint64 representation of z. If z cannot be represented in a uint64, the function returns math.MaxUint64.

func (*Int) UnmarshalJSON

func (i *Int) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface..

func (*Int) UnmarshalText

func (i *Int) UnmarshalText(data []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

type IntContext

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

A IntContext is a structure that holds Int temporary variables used by library functions. Since dynamic memory allocation to create [Int]s is rather expensive when used in conjunction with repeated subroutine calls, the IntContext structure is used. A given IntContext must only be used by a single thread of execution. No locking is performed, and the internal pool allocator will not properly handle multiple threads of execution.

func NewIntContext

func NewIntContext() (*IntContext, error)

NewIntContext allocates and initializes a IntContext structure

func (*IntContext) Attach

func (c *IntContext) Attach()

func (*IntContext) Destroy

func (c *IntContext) Destroy()

Destroy frees the components of the IntContext and the structure itself.

func (*IntContext) Detach

func (c *IntContext) Detach()

func (*IntContext) GetInt

func (c *IntContext) GetInt() (*Int, error)

type MontgomeryContext

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

func NewMontgomeryContext

func NewMontgomeryContext() (*MontgomeryContext, error)

func (*MontgomeryContext) Destroy

func (c *MontgomeryContext) Destroy()

func (*MontgomeryContext) Set

func (c *MontgomeryContext) Set(m *Int, ctx *IntContext) error

Jump to

Keyboard shortcuts

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