Documentation
¶
Index ¶
- Variables
- func Init() error
- func VersionText() string
- type Int
- func (z *Int) Add(x, y *Int) error
- func (z *Int) And(x, y *Int) error
- func (z *Int) BitLen() int
- func (z *Int) Bytes() ([]byte, error)
- func (z *Int) BytesLen() int
- func (z *Int) Cmp(x *Int) int
- func (z *Int) ConstantTimeEq(x *Int) (bool, error)
- func (z *Int) Div(ctx *IntContext, x, y *Int) error
- func (z *Int) Exp(ctx *IntContext, x, y *Int) error
- func (z *Int) FillBytes(buf []byte) error
- func (i *Int) Hex() string
- func (z *Int) Lsh(x *Int, n uint) error
- func (i *Int) MarshalJSON() ([]byte, error)
- func (i *Int) MarshalText() ([]byte, error)
- func (z *Int) Mod(ctx *IntContext, x, y *Int) error
- func (z *Int) ModExp(ctx *IntContext, x, y, m *Int) error
- func (z *Int) ModExpMont(mont *MontgomeryContext, ctx *IntContext, x, y, m *Int) error
- func (z *Int) ModInverse(ctx *IntContext, g, n *Int) error
- func (z *Int) ModMul(ctx *IntContext, x, y, m *Int) error
- func (z *Int) ModMulMontgomery(mont *MontgomeryContext, ctx *IntContext, x, y *Int) error
- func (z *Int) Mul(ctx *IntContext, x, y *Int) error
- func (z *Int) Or(x, y *Int) error
- func (z *Int) ProbablyPrime(ctx *IntContext) (bool, error)
- func (z *Int) RandRange(max *Int) error
- func (z *Int) Rsh(x *Int, n uint) error
- func (z *Int) Set(x *Int) error
- func (z *Int) SetBytes(buf []byte) *Int
- func (i *Int) SetConstantTime() *Int
- func (z *Int) SetDecString(s string) error
- func (z *Int) SetHexString(s string) error
- func (z *Int) SetUInt64(x uint64) error
- func (i *Int) String() string
- func (z *Int) Sub(x, y *Int) error
- func (z *Int) Uint64() uint64
- func (i *Int) UnmarshalJSON(data []byte) error
- func (i *Int) UnmarshalText(data []byte) error
- type IntContext
- type MontgomeryContext
Constants ¶
This section is empty.
Variables ¶
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 (*Int) BitLen ¶
BitLen returns the length of the absolute value of z in bits. The bit length of 0 is 0.
func (*Int) Bytes ¶
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) ConstantTimeEq ¶
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 ¶
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) Lsh ¶
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 ¶
MarshalJSON implements the json.Marshaler interface.
func (*Int) MarshalText ¶
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) 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 ¶
RandRange generates a cryptographically strong pseudo-random number z in the range 0 <= z < max.
func (*Int) Rsh ¶
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) SetBytes ¶
SetBytes interprets buf as the bytes of a big-endian unsigned integer, sets z to that value, and returns z.
func (*Int) SetConstantTime ¶
func (*Int) SetDecString ¶
SetDecString sets z to the value of s interpreted in the decimal base.
func (*Int) SetHexString ¶
SetHexString sets z to the value of s interpreted in the hexadecimal base.
func (*Int) Uint64 ¶
Uint64 returns the uint64 representation of z. If z cannot be represented in a uint64, the function returns math.MaxUint64.
func (*Int) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface..
func (*Int) UnmarshalText ¶
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