num

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

README

num

Package num provides arbitrary-precision arithmetic for cryptographic applications. It is built over the constant-time numct package and is suitable for high-level applications.

Overview

This package provides immutable, strongly-typed number representations that implement standard algebraic interfaces. Unlike numct, operations in num are not constant-time; Use numct directly when timing side-channels must be avoided.

Key Types

  • NatPlus (NPlus()): Positive natural numbers (excludes zero).
  • Nat (N()): Natural numbers (non-negative integers).
  • Int (Z()): Signed integers.
  • Rat (Q()): Rational numbers (fractions).
  • Uint / ZMod: Integers modulo n. Implements algebra.ZModLike.

Architecture

Each numeric type has an associated structure type (e.g., NaturalNumbers, Integers, Rationals, ZMod) accessible via singleton functions (N(), Z(), Q(), NewZMod()). These structures provide constructors, constants, and implement algebraic interfaces from the algebra package.

All types wrap numct primitives internally and provide:

  • Conversion to/from *big.Int and *big.Rat
  • CBOR serialization
  • Hash codes for use in maps and sets
  • Random sampling within ranges

Usage Notes

  • Types are immutable; all operations return new values.
  • Use Canonical() on Rat to reduce fractions to lowest terms.
  • ZMod caches Montgomery context for efficient modular arithmetic.
  • Random sampling uses half-open intervals [low, high).

Documentation

Overview

Package num provides arbitrary-precision arithmetic for cryptographic applications. It is built over the constant-time numct package and is suitable for high-level applications.

See README.md for details.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInexactDivision = errs.New("inexact division")
	ErrUndefined       = errs.New("operation is undefined")
	ErrOutOfRange      = errs.New("value is out of range")
	ErrUnequalModuli   = errs.New("moduli are unequal")
	ErrIsNil           = errs.New("value must not be nil")
	ErrDivisionByZero  = errs.New("division by zero")
)

Functions

This section is empty.

Types

type Int

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

Int represents an integer in the ring of integers Z.

func (*Int) Abs

func (i *Int) Abs() *Nat

Abs returns the absolute value of the integer.

func (*Int) Add

func (i *Int) Add(other *Int) *Int

Add performs addition of two integers.

func (*Int) AnnouncedLen

func (i *Int) AnnouncedLen() int

AnnouncedLen returns the announced length of the integer in bytes.

func (*Int) Big

func (i *Int) Big() *big.Int

Big converts the integer to a big.Int.

func (*Int) Bytes

func (i *Int) Bytes() []byte

Bytes returns the byte representation of the integer.

func (*Int) Cardinal

func (i *Int) Cardinal() cardinal.Cardinal

Cardinal returns the cardinality of the absolute value of the integer.

func (*Int) Clone

func (i *Int) Clone() *Int

Clone creates a copy of the integer.

func (*Int) Compare

func (i *Int) Compare(other *Int) base.Ordering

Compare compares the integer with another integer.

func (*Int) Coprime

func (i *Int) Coprime(other *Int) bool

Coprime checks if two integers are coprime.

func (*Int) Decrement

func (i *Int) Decrement() *Int

Decrement returns the integer decremented by one.

func (*Int) DivRound

func (i *Int) DivRound(other *Int) (*Int, error)

DivRound performs the division of the integer by another integer returning the quotient rounded towards zero.

func (*Int) DivRoundVarTime

func (i *Int) DivRoundVarTime(other *Int) (*Int, error)

DivRoundVarTime performs the division of the integer by another integer returning the quotient rounded towards zero. It is not constant-time due to having to generate montgomery parameters for the divisor (i.e., leaks divisor).

func (*Int) Double

func (i *Int) Double() *Int

Double returns the integer multiplied by 2.

func (*Int) Equal

func (i *Int) Equal(other *Int) bool

Equal checks if the integer is equal to another integer.

func (*Int) EuclideanDiv

func (i *Int) EuclideanDiv(other *Int) (quot, rem *Int, err error)

EuclideanDiv performs Euclidean division of the integer by another integer.

func (*Int) EuclideanDivVarTime

func (i *Int) EuclideanDivVarTime(other *Int) (quot, rem *Int, err error)

EuclideanDivVarTime performs Euclidean division of the integer by another integer. It is not constant-time due to having to generate montgomery parameters for the divisor (i.e., leaks divisor).

func (*Int) EuclideanValuation

func (i *Int) EuclideanValuation() algebra.Cardinal

EuclideanValuation returns the Euclidean valuation of the integer.

func (*Int) HashCode

func (i *Int) HashCode() base.HashCode

HashCode returns the hash code of the integer.

func (*Int) Increment

func (i *Int) Increment() *Int

Increment returns the integer incremented by one.

func (*Int) IsEven

func (i *Int) IsEven() bool

IsEven checks if the integer is even.

func (*Int) IsInRange

func (i *Int) IsInRange(modulus *NatPlus) bool

IsInRange checks if the integer is within the range defined by the modulus i.e., 0 <= x < m.

func (*Int) IsInRangeSymmetric

func (i *Int) IsInRangeSymmetric(modulus *NatPlus) bool

IsInRangeSymmetric checks if the integer is within the symmetric range defined by the modulus i.e., -m/2 <= x < m/2.

func (*Int) IsLessThanOrEqual

func (i *Int) IsLessThanOrEqual(other *Int) bool

IsLessThanOrEqual checks if the integer is less than or equal to another integer.

func (*Int) IsNegative

func (i *Int) IsNegative() bool

IsNegative checks if the integer is negative.

func (*Int) IsOdd

func (i *Int) IsOdd() bool

IsOdd checks if the integer is odd.

func (*Int) IsOne

func (i *Int) IsOne() bool

IsOne checks if the integer is one.

func (*Int) IsOpIdentity

func (i *Int) IsOpIdentity() bool

IsOpIdentity checks if the integer is the additive identity (zero).

func (*Int) IsPositive

func (i *Int) IsPositive() bool

IsPositive checks if the integer is positive.

func (*Int) IsProbablyPrime

func (i *Int) IsProbablyPrime() bool

IsProbablyPrime checks if the integer is probably prime.

func (*Int) IsTorsionFree

func (*Int) IsTorsionFree() bool

IsTorsionFree returns true, indicating that the integers are torsion-free.

func (*Int) IsUnit

func (i *Int) IsUnit(modulus *NatPlus) bool

IsUnit checks if the integer is a unit modulo the given NatPlus modulus.

func (*Int) IsZero

func (i *Int) IsZero() bool

IsZero checks if the integer is zero.

func (*Int) Lift

func (i *Int) Lift() *Int

Lift returns a copy of the integer (identity function for integers).

func (*Int) Lsh

func (i *Int) Lsh(shift uint) *Int

Lsh performs a left shift operation on the integer.

func (*Int) MarshalCBOR

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

func (*Int) Mod

func (i *Int) Mod(modulus *NatPlus) *Uint

Mod returns the integer modulo the given NatPlus modulus.

func (*Int) Mul

func (i *Int) Mul(other *Int) *Int

Mul performs multiplication of two integers.

func (*Int) Neg

func (i *Int) Neg() *Int

Neg computes the negation of the integer.

func (*Int) Op

func (i *Int) Op(other *Int) *Int

Op performs addition of two integers.

func (*Int) OpInv

func (i *Int) OpInv() *Int

OpInv returns the additive inverse of the integer.

func (*Int) OtherOp

func (i *Int) OtherOp(other *Int) *Int

OtherOp performs multiplication of two integers.

func (*Int) Rat

func (i *Int) Rat() *Rat

Rat converts the integer to a rational number.

func (*Int) Rsh

func (i *Int) Rsh(shift uint) *Int

Rsh performs a right shift operation on the integer.

func (*Int) ScalarMul

func (i *Int) ScalarMul(other *Int) *Int

ScalarMul performs scalar multiplication of the integer by another integer.

func (*Int) ScalarOp

func (i *Int) ScalarOp(other *Int) *Int

ScalarOp performs scalar multiplication of the integer by another integer.

func (*Int) Square

func (i *Int) Square() *Int

Square returns the square of the integer.

func (*Int) String

func (i *Int) String() string

String returns the string representation of the integer.

func (*Int) Structure

func (*Int) Structure() algebra.Structure[*Int]

Structure returns the algebraic structure of the integers.

func (*Int) Sub

func (i *Int) Sub(other *Int) *Int

Sub performs subtraction of two integers.

func (*Int) TrueLen

func (i *Int) TrueLen() int

TrueLen returns the true length of the integer in bytes.

func (*Int) TryDiv

func (i *Int) TryDiv(other *Int) (*Int, error)

TryDiv performs the exact division of the integer by another integer.

func (*Int) TryDivVarTime

func (i *Int) TryDivVarTime(other *Int) (*Int, error)

TryDivVarTime performs the exact division of the integer by another integer. It is not constant-time due to having to generate montgomery parameters for the divisor (i.e., leaks divisor).

func (*Int) TryInv

func (i *Int) TryInv() (*Int, error)

TryInv attempts to compute the multiplicative inverse of the integer.

func (*Int) TryNeg

func (i *Int) TryNeg() (*Int, error)

TryNeg attempts to compute the negation of the integer. It never fails.

func (*Int) TryOpInv

func (i *Int) TryOpInv() (*Int, error)

TryOpInv returns the additive inverse of the integer.

func (*Int) TrySub

func (i *Int) TrySub(other *Int) (*Int, error)

TrySub performs subtraction of two integers.

func (*Int) UnmarshalCBOR

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

func (*Int) Value

func (i *Int) Value() *numct.Int

Value returns the underlying numct.Int value of the integer.

type Integers

type Integers struct{}

Integers implements the algebra.Structure interface for the ring of integers Z.

func Z

func Z() *Integers

Z returns the singleton instance of the Integers structure.

func (*Integers) Characteristic

func (*Integers) Characteristic() cardinal.Cardinal

Characteristic returns the characteristic of the integers, which is 0.

func (*Integers) ElementSize

func (*Integers) ElementSize() int

ElementSize returns -1 indicating that the size of integer elements is unbounded.

func (*Integers) FromBig

func (zs *Integers) FromBig(value *big.Int) (*Int, error)

FromBig creates an integer from a big.Int value.

func (*Integers) FromBytes

func (*Integers) FromBytes(input []byte) (*Int, error)

FromBytes creates an integer from its byte representation.

func (*Integers) FromCardinal

func (zs *Integers) FromCardinal(value cardinal.Cardinal) (*Int, error)

FromCardinal creates an integer from a cardinal.Cardinal value.

func (*Integers) FromInt64

func (*Integers) FromInt64(value int64) *Int

FromInt64 creates an integer from an int64 value.

func (*Integers) FromIntCT

func (*Integers) FromIntCT(value *numct.Int) (*Int, error)

FromIntCT creates an integer from a numct.Int value.

func (*Integers) FromNat

func (zs *Integers) FromNat(value *Nat) (*Int, error)

FromNat creates an integer from a Nat value.

func (*Integers) FromNatCT

func (*Integers) FromNatCT(value *numct.Nat) (*Int, error)

FromNatCT creates an integer from a numct.Nat value.

func (*Integers) FromNatPlus

func (zs *Integers) FromNatPlus(value *NatPlus) (*Int, error)

FromNatPlus creates an integer from a NatPlus value.

func (*Integers) FromRat

func (*Integers) FromRat(input *Rat) (*Int, error)

FromRat creates an integer from a Rat value, if the Rat is an integer.

func (*Integers) FromUint

func (zs *Integers) FromUint(input *Uint) (*Int, error)

FromUint creates an integer from a Uint value.

func (*Integers) FromUint64

func (*Integers) FromUint64(value uint64) *Int

FromUint64 creates an integer from a uint64 value.

func (*Integers) FromUintSymmetric

func (*Integers) FromUintSymmetric(input *Uint) (*Int, error)

FromUintSymmetric creates an integer from a Uint value using symmetric representation.

func (*Integers) IsDomain

func (*Integers) IsDomain() bool

IsDomain returns true, indicating that the integers form a domain (no zero divisors).

func (*Integers) Name

func (*Integers) Name() string

func (*Integers) One

func (*Integers) One() *Int

One returns the multiplicative identity element (one) of the integers.

func (*Integers) OpIdentity

func (zs *Integers) OpIdentity() *Int

OpIdentity returns the additive identity element (zero) of the integers.

func (*Integers) Order

func (*Integers) Order() cardinal.Cardinal

Order returns the (infinite) order of the integers.

func (*Integers) Random

func (*Integers) Random(lowInclusive, highExclusive *Int, prng io.Reader) (*Int, error)

Random generates a random integer in the range [lowInclusive, highExclusive).

func (*Integers) ScalarStructure

func (*Integers) ScalarStructure() algebra.Structure[*Int]

ScalarStructure returns the structure of the scalars, which is also the integers.

func (*Integers) Zero

func (*Integers) Zero() *Int

Zero returns the zero element of the integers.

type Nat

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

Nat represents a natural number (non-negative integer).

func (*Nat) Add

func (n *Nat) Add(other *Nat) *Nat

Add performs the addition of two Nat values.

func (*Nat) AnnouncedLen

func (n *Nat) AnnouncedLen() int

AnnouncedLen returns the announced length of the Nat in bytes.

func (*Nat) Big

func (n *Nat) Big() *big.Int

Big returns the big.Int representation of the Nat.

func (*Nat) Bit

func (n *Nat) Bit(i uint) byte

Bit returns the value of the i-th bit of the Nat.

func (*Nat) Byte

func (n *Nat) Byte(i uint) byte

Byte returns the value of the i-th byte of the Nat.

func (*Nat) Bytes

func (n *Nat) Bytes() []byte

Bytes returns the byte slice representation of the Nat.

func (*Nat) BytesBE

func (n *Nat) BytesBE() []byte

BytesBE returns the big-endian byte slice representation of the Nat.

func (*Nat) Cardinal

func (n *Nat) Cardinal() cardinal.Cardinal

Cardinal returns the cardinal representation of the Nat.

func (*Nat) Clone

func (n *Nat) Clone() *Nat

Clone creates a copy of the Nat.

func (*Nat) Compare

func (n *Nat) Compare(other *Nat) base.Ordering

Compare compares the Nat with another Nat, returning an ordering result.

func (*Nat) Coprime

func (n *Nat) Coprime(other *Nat) bool

Coprime checks if the Nat is coprime with another Nat.

func (*Nat) Decrement

func (n *Nat) Decrement() (*Nat, error)

Decrement returns the Nat decremented by 1, returning an error if the result would be negative.

func (*Nat) DivRound

func (n *Nat) DivRound(other *Nat) (*Nat, error)

DivRound divides the Nat by another Nat returning quotient rounded towards zero. It returns an error if the division is not exact.

func (*Nat) DivRoundVarTime

func (n *Nat) DivRoundVarTime(other *Nat) (*Nat, error)

DivRoundVarTime divides the Nat by another Nat returning quotient rounded towards zero. It returns an error if the division is not exact. It is not constant-time due to having to generate montgomery parameters for the divisor (i.e., leaks divisor).

func (*Nat) Double

func (n *Nat) Double() *Nat

Double returns the Nat doubled.

func (*Nat) Equal

func (n *Nat) Equal(other *Nat) bool

Equal checks if the Nat is equal to another Nat.

func (*Nat) EuclideanDiv

func (n *Nat) EuclideanDiv(other *Nat) (quot, rem *Nat, err error)

EuclideanDiv performs Euclidean division of the Nat by another Nat, returning the quotient and remainder.

func (*Nat) EuclideanDivVarTime

func (n *Nat) EuclideanDivVarTime(other *Nat) (quot, rem *Nat, err error)

EuclideanDivVarTime performs Euclidean division of the Nat by another Nat, returning the quotient and remainder. It is not constant-time due to having to generate montgomery parameters for the divisor (i.e., leaks divisor).

func (*Nat) EuclideanValuation

func (n *Nat) EuclideanValuation() cardinal.Cardinal

EuclideanValuation computes the Euclidean valuation of the Nat.

func (*Nat) GCD

func (n *Nat) GCD(other *Nat) *Nat

GCD computes the greatest common divisor (GCD) of the Nat and another Nat.

func (*Nat) HashCode

func (n *Nat) HashCode() base.HashCode

HashCode computes a hash code for the Nat.

func (*Nat) Increment

func (n *Nat) Increment() *Nat

Increment returns the Nat incremented by 1.

func (*Nat) IsBottom

func (n *Nat) IsBottom() bool

IsBottom checks if the Nat is the bottom element (0).

func (*Nat) IsEven

func (n *Nat) IsEven() bool

IsEven checks if the Nat is even.

func (*Nat) IsLessThanOrEqual

func (n *Nat) IsLessThanOrEqual(other *Nat) bool

IsLessThanOrEqual checks if the Nat is less than or equal to another Nat.

func (*Nat) IsOdd

func (n *Nat) IsOdd() bool

IsOdd checks if the Nat is odd.

func (*Nat) IsOne

func (n *Nat) IsOne() bool

IsOne checks if the Nat is one.

func (*Nat) IsOpIdentity

func (n *Nat) IsOpIdentity() bool

IsOpIdentity checks if the Nat is the additive identity (0).

func (*Nat) IsPositive

func (n *Nat) IsPositive() bool

IsPositive checks if the Nat is positive (greater than 0).

func (*Nat) IsProbablyPrime

func (n *Nat) IsProbablyPrime() bool

IsProbablyPrime checks if the Nat is probably prime.

func (*Nat) IsTorsionFree

func (*Nat) IsTorsionFree() bool

IsTorsionFree checks if the Nat is torsion-free under addition, which is always true for natural numbers.

func (*Nat) IsUnit

func (n *Nat) IsUnit(modulus *NatPlus) bool

IsUnit checks if the Nat is a unit modulo the given NatPlus modulus.

func (*Nat) IsZero

func (n *Nat) IsZero() bool

IsZero checks if the Nat is zero.

func (*Nat) Lift

func (n *Nat) Lift() *Int

Lift converts the Nat to an Int.

func (*Nat) Lsh

func (n *Nat) Lsh(shift uint) *Nat

Lsh performs a left shift operation on the Nat by the specified number of bits.

func (*Nat) MarshalCBOR

func (n *Nat) MarshalCBOR() ([]byte, error)

func (*Nat) Mod

func (n *Nat) Mod(modulus *NatPlus) *Uint

Mod computes the Nat modulo the given NatPlus modulus.

func (*Nat) Mul

func (n *Nat) Mul(other *Nat) *Nat

Mul performs the multiplication of two Nat values.

func (*Nat) Op

func (n *Nat) Op(other *Nat) *Nat

Op performs the addition operation on two Nat values.

func (*Nat) OtherOp

func (n *Nat) OtherOp(other *Nat) *Nat

OtherOp performs the multiplication operation on two Nat values.

func (*Nat) Rsh

func (n *Nat) Rsh(shift uint) *Nat

Rsh performs a right shift operation on the Nat by the specified number of bits.

func (*Nat) ScalarMul

func (n *Nat) ScalarMul(sc *Nat) *Nat

ScalarMul performs scalar multiplication of the Nat by another Nat.

func (*Nat) ScalarOp

func (n *Nat) ScalarOp(sc *Nat) *Nat

ScalarOp performs scalar multiplication of the Nat by another Nat.

func (*Nat) Sqrt

func (n *Nat) Sqrt() (*Nat, error)

Sqrt computes the square root of the Nat, returning an error if the square root is not defined.

func (*Nat) Square

func (n *Nat) Square() *Nat

Square returns the square of the Nat.

func (*Nat) String

func (n *Nat) String() string

String returns the string representation of the Nat.

func (*Nat) Structure

func (*Nat) Structure() algebra.Structure[*Nat]

Structure returns the NaturalNumbers structure.

func (*Nat) TrueLen

func (n *Nat) TrueLen() int

TrueLen returns the true length of the Nat in bytes.

func (*Nat) TryDiv

func (n *Nat) TryDiv(other *Nat) (*Nat, error)

TryDiv attempts to divide the Nat by another Nat. It returns an error if the division is not exact.

func (*Nat) TryDivVarTime

func (n *Nat) TryDivVarTime(other *Nat) (*Nat, error)

TryDivVarTime attempts to divide the Nat by another Nat. It returns an error if the division is not exact. It is not constant-time due to having to generate montgomery parameters for the divisor (i.e., leaks divisor).

func (*Nat) TryInv

func (n *Nat) TryInv() (*Nat, error)

TryInv attempts to compute the multiplicative inverse of the Nat. It returns an error unless the Nat is 1.

func (*Nat) TryNeg

func (*Nat) TryNeg() (*Nat, error)

TryNeg attempts to compute the negation of the Nat. It will always return an error since natural numbers do not have negation.

func (*Nat) TryOpInv

func (n *Nat) TryOpInv() (*Nat, error)

TryOpInv attempts to compute the additive inverse of the Nat. It will always return an error since natural numbers do not have additive inverses.

func (*Nat) TrySub

func (n *Nat) TrySub(other *Nat) (*Nat, error)

TrySub attempts to subtract another Nat from the current Nat. It returns an error if the result would not be a natural number.

func (*Nat) Uint64

func (n *Nat) Uint64() uint64

Uint64 returns the uint64 representation of the Nat. It wraps around if the Nat is too large.

func (*Nat) UnmarshalCBOR

func (n *Nat) UnmarshalCBOR(data []byte) error

func (*Nat) Value

func (n *Nat) Value() *numct.Nat

Value returns the underlying numct.Nat value of the Nat.

type NatPlus

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

NatPlus represents a positive natural number (N\{0}).

func (*NatPlus) Abs

func (np *NatPlus) Abs() *NatPlus

Abs returns the absolute value of the NatPlus, which is itself.

func (*NatPlus) Add

func (np *NatPlus) Add(other *NatPlus) *NatPlus

Add performs addition of two NatPlus elements.

func (*NatPlus) AnnouncedLen

func (np *NatPlus) AnnouncedLen() int

AnnouncedLen returns the announced length of the NatPlus in bytes.

func (*NatPlus) Big

func (np *NatPlus) Big() *big.Int

Big returns the big.Int representation of the NatPlus.

func (*NatPlus) Bit

func (np *NatPlus) Bit(i uint) byte

Bit returns the value of the i-th bit of the NatPlus.

func (*NatPlus) Byte

func (np *NatPlus) Byte(i uint) byte

Byte returns the value of the i-th byte of the NatPlus.

func (*NatPlus) Bytes

func (np *NatPlus) Bytes() []byte

Bytes returns the big-endian byte representation of the NatPlus.

func (*NatPlus) BytesBE

func (np *NatPlus) BytesBE() []byte

BytesBE returns the big-endian byte representation of the NatPlus.

func (*NatPlus) Cardinal

func (np *NatPlus) Cardinal() cardinal.Cardinal

Cardinal returns the cardinal representation of the NatPlus.

func (*NatPlus) Clone

func (np *NatPlus) Clone() *NatPlus

Clone creates a copy of the NatPlus.

func (*NatPlus) Compare

func (np *NatPlus) Compare(other *NatPlus) base.Ordering

Compare compares the NatPlus with another NatPlus, returning the ordering result.

func (*NatPlus) Decrement

func (np *NatPlus) Decrement() (*NatPlus, error)

Decrement returns the NatPlus decremented by 1, returning an error if the result would be less than 1.

func (*NatPlus) Double

func (np *NatPlus) Double() *NatPlus

Double returns the result of multiplying the NatPlus by 2.

func (*NatPlus) Equal

func (np *NatPlus) Equal(other *NatPlus) bool

Equal checks if the NatPlus is equal to another NatPlus.

func (*NatPlus) HashCode

func (np *NatPlus) HashCode() base.HashCode

HashCode computes the hash code of the NatPlus.

func (*NatPlus) Increment

func (np *NatPlus) Increment() *NatPlus

Increment returns the NatPlus incremented by 1.

func (*NatPlus) IsBottom

func (np *NatPlus) IsBottom() bool

IsBottom checks if the NatPlus is the smallest element (1).

func (*NatPlus) IsEven

func (np *NatPlus) IsEven() bool

IsEven checks if the NatPlus is even.

func (*NatPlus) IsLessThanOrEqual

func (np *NatPlus) IsLessThanOrEqual(other *NatPlus) bool

IsLessThanOrEqual checks if the NatPlus is less than or equal to another NatPlus.

func (*NatPlus) IsOdd

func (np *NatPlus) IsOdd() bool

IsOdd checks if the NatPlus is odd.

func (*NatPlus) IsOne

func (np *NatPlus) IsOne() bool

IsOne checks if the NatPlus is equal to 1.

func (*NatPlus) IsOpIdentity

func (np *NatPlus) IsOpIdentity() bool

IsOpIdentity checks if the NatPlus is the multiplicative identity (1).

func (*NatPlus) IsProbablyPrime

func (np *NatPlus) IsProbablyPrime() bool

IsProbablyPrime checks if the NatPlus is probably prime.

func (*NatPlus) IsUnit

func (np *NatPlus) IsUnit(modulus *NatPlus) bool

IsUnit checks if the NatPlus is a unit with respect to the given modulus.

func (*NatPlus) Lift

func (np *NatPlus) Lift() *Int

Lift converts the NatPlus to an Int.

func (*NatPlus) Lsh

func (np *NatPlus) Lsh(shift uint) *NatPlus

Lsh performs a left shift operation on the NatPlus.

func (*NatPlus) MarshalCBOR

func (np *NatPlus) MarshalCBOR() ([]byte, error)

func (*NatPlus) Mod

func (np *NatPlus) Mod(modulus *NatPlus) *Uint

Mod computes the modulus of the NatPlus with respect to another NatPlus.

func (*NatPlus) ModulusCT

func (np *NatPlus) ModulusCT() *numct.Modulus

ModulusCT returns the cached modulus or computes it if not cached.

func (*NatPlus) Mul

func (np *NatPlus) Mul(other *NatPlus) *NatPlus

Mul performs multiplication of two NatPlus elements.

func (*NatPlus) Nat

func (np *NatPlus) Nat() *Nat

Nat returns the Nat representation of the NatPlus.

func (*NatPlus) Op

func (np *NatPlus) Op(other *NatPlus) *NatPlus

Op performs multiplication of two NatPlus elements.

func (*NatPlus) OtherOp

func (np *NatPlus) OtherOp(other *NatPlus) *NatPlus

OtherOp performs addition of two NatPlus elements.

func (*NatPlus) Rsh

func (np *NatPlus) Rsh(shift uint) *NatPlus

Rsh performs a right shift operation on the NatPlus. Panics if the result would be zero.

func (*NatPlus) Square

func (np *NatPlus) Square() *NatPlus

Square returns the result of squaring the NatPlus.

func (*NatPlus) String

func (np *NatPlus) String() string

String returns the string representation of the NatPlus.

func (*NatPlus) Structure

func (*NatPlus) Structure() algebra.Structure[*NatPlus]

Structure returns the algebraic structure of NatPlus, which is PositiveNaturalNumbers.

func (*NatPlus) TrueLen

func (np *NatPlus) TrueLen() int

TrueLen returns the true length of the NatPlus in bytes.

func (*NatPlus) TryDiv

func (np *NatPlus) TryDiv(other *NatPlus) (*NatPlus, error)

TryDiv attempts to divide the NatPlus by another NatPlus, returning an error if the division is not exact.

func (*NatPlus) TryInv

func (np *NatPlus) TryInv() (*NatPlus, error)

TryInv attempts to compute the multiplicative inverse of the NatPlus, returning an error since it does not exist.

func (*NatPlus) TryOpInv

func (np *NatPlus) TryOpInv() (*NatPlus, error)

TryOpInv attempts to compute the multiplicative inverse of the NatPlus, returning an error since it does not exist.

func (*NatPlus) TryRsh

func (np *NatPlus) TryRsh(shift uint) (*NatPlus, error)

TryRsh attempts to right shift the NatPlus, returning an error if the result would be zero.

func (*NatPlus) TrySub

func (np *NatPlus) TrySub(other *NatPlus) (*NatPlus, error)

TrySub attempts to subtract another NatPlus from the NatPlus, returning an error if the result is not a positive natural number.

func (*NatPlus) Uint64

func (np *NatPlus) Uint64() uint64

func (*NatPlus) UnmarshalCBOR

func (np *NatPlus) UnmarshalCBOR(data []byte) error

func (*NatPlus) Value

func (np *NatPlus) Value() *numct.Nat

Value returns the underlying numct.Nat value of the NatPlus.

type NaturalNumbers

type NaturalNumbers struct{}

NaturalNumbers represents the set of natural numbers (non-negative integers).

func N

func N() *NaturalNumbers

N returns the singleton instance of the NaturalNumbers structure.

func (*NaturalNumbers) Bottom

func (ns *NaturalNumbers) Bottom() *Nat

Bottom returns the smallest element in the NaturalNumbers structure, which is 0.

func (*NaturalNumbers) Characteristic

func (*NaturalNumbers) Characteristic() cardinal.Cardinal

Characteristic returns the characteristic of the NaturalNumbers structure, which is 0.

func (*NaturalNumbers) ElementSize

func (*NaturalNumbers) ElementSize() int

ElementSize returns -1 indicating that elements of NaturalNumbers do not have a fixed size.

func (*NaturalNumbers) FromBig

func (ns *NaturalNumbers) FromBig(value *big.Int) (*Nat, error)

FromBig creates a Nat from a big.Int value, returning an error if the input is nil or negative.

func (*NaturalNumbers) FromBytes

func (*NaturalNumbers) FromBytes(input []byte) (*Nat, error)

FromBytes creates a Nat from a byte slice, returning an error if the input is nil.

func (*NaturalNumbers) FromBytesBE

func (ns *NaturalNumbers) FromBytesBE(input []byte) (*Nat, error)

FromBytesBE creates a Nat from a big-endian byte slice, returning an error if the input is nil.

func (*NaturalNumbers) FromCardinal

func (ns *NaturalNumbers) FromCardinal(value cardinal.Cardinal) (*Nat, error)

FromCardinal creates a Nat from a cardinal.Cardinal value, returning an error if the input is nil or infinite.

func (*NaturalNumbers) FromInt

func (ns *NaturalNumbers) FromInt(value *Int) (*Nat, error)

FromInt creates a Nat from an Int value, returning an error if the input is nil or negative.

func (*NaturalNumbers) FromNatCT

func (*NaturalNumbers) FromNatCT(value *numct.Nat) (*Nat, error)

FromNatCT creates a Nat from a numct.Nat value, returning an error if the input is nil.

func (*NaturalNumbers) FromNatPlus

func (*NaturalNumbers) FromNatPlus(value *NatPlus) (*Nat, error)

FromNatPlus creates a Nat from a NatPlus value, returning an error if the input is nil.

func (*NaturalNumbers) FromRat

func (ns *NaturalNumbers) FromRat(value *Rat) (*Nat, error)

FromRat creates a Nat from a Rat value, returning an error if Rat is not a non-negative integer.

func (*NaturalNumbers) FromUint64

func (*NaturalNumbers) FromUint64(value uint64) *Nat

FromUint64 creates a Nat from a uint64 value.

func (*NaturalNumbers) Name

func (*NaturalNumbers) Name() string

Name returns the name of the structure: "N".

func (*NaturalNumbers) One

func (*NaturalNumbers) One() *Nat

One returns the multiplicative identity element of the NaturalNumbers structure.

func (*NaturalNumbers) OpIdentity

func (ns *NaturalNumbers) OpIdentity() *Nat

OpIdentity returns the identity element for the addition operation in the NaturalNumbers structure.

func (*NaturalNumbers) Order

func (*NaturalNumbers) Order() cardinal.Cardinal

Order returns the order of the NaturalNumbers structure, which is infinite.

func (*NaturalNumbers) Random

func (ns *NaturalNumbers) Random(lowInclusive, highExclusive *Nat, prng io.Reader) (*Nat, error)

Random generates a random Nat in the range [lowInclusive, highExclusive), returning an error if highExclusive is nil.

func (*NaturalNumbers) ScalarStructure

func (*NaturalNumbers) ScalarStructure() algebra.Structure[*Nat]

ScalarStructure returns the regular semi-module structure of NaturalNumbers.

func (*NaturalNumbers) Zero

func (*NaturalNumbers) Zero() *Nat

Zero returns the additive identity element of the NaturalNumbers structure.

type PositiveNaturalNumbers

type PositiveNaturalNumbers struct{}

PositiveNaturalNumbers represents the set of positive natural numbers (N\{0}).

func NPlus

func NPlus() *PositiveNaturalNumbers

NPlus returns the singleton instance of PositiveNaturalNumbers.

func (*PositiveNaturalNumbers) Bottom

func (nps *PositiveNaturalNumbers) Bottom() *NatPlus

Bottom returns the smallest element of PositiveNaturalNumbers, which is 1.

func (*PositiveNaturalNumbers) Characteristic

func (*PositiveNaturalNumbers) Characteristic() cardinal.Cardinal

Characteristic returns the characteristic of PositiveNaturalNumbers, which is 0.

func (*PositiveNaturalNumbers) ElementSize

func (*PositiveNaturalNumbers) ElementSize() int

ElementSize returns -1 indicating that NatPlus does not have a fixed element size.

func (*PositiveNaturalNumbers) FromBig

func (nps *PositiveNaturalNumbers) FromBig(b *big.Int) (*NatPlus, error)

FromBig creates a NatPlus from the given big.Int, returning an error if the integer is nil or not positive.

func (*PositiveNaturalNumbers) FromBytes

func (*PositiveNaturalNumbers) FromBytes(input []byte) (*NatPlus, error)

FromBytes creates a NatPlus from the given big-endian byte slice, returning an error if the input is empty or represents zero.

func (*PositiveNaturalNumbers) FromBytesBE

func (nps *PositiveNaturalNumbers) FromBytesBE(input []byte) (*NatPlus, error)

FromBytesBE creates a NatPlus from the given big-endian byte slice, returning an error if the input is empty or represents zero.

func (*PositiveNaturalNumbers) FromCardinal

func (*PositiveNaturalNumbers) FromCardinal(c algebra.Cardinal) (*NatPlus, error)

FromCardinal creates a NatPlus from the given cardinal, returning an error if the cardinal is zero.

func (*PositiveNaturalNumbers) FromInt

func (*PositiveNaturalNumbers) FromInt(value *Int) (*NatPlus, error)

FromInt creates a NatPlus from the given Int, returning an error if the Int is nil, zero, or negative.

func (*PositiveNaturalNumbers) FromModulusCT

func (*PositiveNaturalNumbers) FromModulusCT(m *numct.Modulus) *NatPlus

FromModulusCT creates a NatPlus from the given numct.Modulus.

func (*PositiveNaturalNumbers) FromNat

func (*PositiveNaturalNumbers) FromNat(value *Nat) (*NatPlus, error)

FromNat creates a NatPlus from the given Nat, returning an error if the Nat is nil or zero.

func (*PositiveNaturalNumbers) FromNatCT

func (*PositiveNaturalNumbers) FromNatCT(value *numct.Nat) (*NatPlus, error)

FromNatCT creates a NatPlus from the given numct.Nat, returning an error if the value is nil or zero.

func (*PositiveNaturalNumbers) FromRat

func (nps *PositiveNaturalNumbers) FromRat(v *Rat) (*NatPlus, error)

FromRat creates a NatPlus from the given Rat, returning an error if the Rat is not a positive integer.

func (*PositiveNaturalNumbers) FromUint64

func (*PositiveNaturalNumbers) FromUint64(value uint64) (*NatPlus, error)

FromUint64 creates a NatPlus from the given uint64, returning an error if the value is zero.

func (*PositiveNaturalNumbers) Name

Name returns the name of the structure: "N\{0}".

func (*PositiveNaturalNumbers) One

One returns the multiplicative identity element of PositiveNaturalNumbers, which is 1.

func (*PositiveNaturalNumbers) OpIdentity

func (nps *PositiveNaturalNumbers) OpIdentity() *NatPlus

OpIdentity returns the multiplicative identity element of PositiveNaturalNumbers, which is 1. Note that this OpIdentity isn't standard, as it considers (N\{0}, *, +) to be a hemi ring, NOT the usual (N\{0}, +, *).

func (*PositiveNaturalNumbers) Order

Order returns the order of PositiveNaturalNumbers, which is infinite.

func (*PositiveNaturalNumbers) Random

func (nps *PositiveNaturalNumbers) Random(lowInclusive, highExclusive *NatPlus, prng io.Reader) (*NatPlus, error)

Random generates a random NatPlus in the range [lowInclusive, highExclusive), returning an error if highExclusive is nil.

type Rat

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

Rat represents an element of the field of rational numbers Q.

func (*Rat) Add

func (r *Rat) Add(rhs *Rat) *Rat

Add performs addition of two Rat elements.

func (*Rat) Big

func (r *Rat) Big() *big.Rat

func (*Rat) Bytes

func (r *Rat) Bytes() []byte

Bytes serialises the Rat element to a byte slice.

func (*Rat) Canonical

func (r *Rat) Canonical() *Rat

Canonical returns the canonical form of the Rat element.

func (*Rat) Ceil

func (r *Rat) Ceil() (*Int, error)

Ceil returns the smallest integer greater than or equal to the Rat element.

func (*Rat) Clone

func (r *Rat) Clone() *Rat

Clone creates a deep copy of the Rat element.

func (*Rat) Denominator

func (r *Rat) Denominator() *NatPlus

Denominator returns the denominator of the Rat element.

func (*Rat) Double

func (r *Rat) Double() *Rat

Double returns the result of adding the Rat element to itself.

func (*Rat) Equal

func (r *Rat) Equal(rhs *Rat) bool

Equal checks if the Rat element is equal to another Rat element.

func (*Rat) EuclideanDiv

func (r *Rat) EuclideanDiv(rhs *Rat) (quo, rem *Rat, err error)

EuclideanDiv performs Euclidean division of two Rat elements.

func (*Rat) EuclideanValuation

func (r *Rat) EuclideanValuation() cardinal.Cardinal

EuclideanValuation returns the Euclidean valuation of the Rat element.

func (*Rat) Floor

func (r *Rat) Floor() (*Int, error)

Floor returns the largest integer less than or equal to the Rat element.

func (*Rat) HashCode

func (r *Rat) HashCode() base.HashCode

HashCode computes the hash code of the Rat element.

func (*Rat) IsInt

func (r *Rat) IsInt() bool

IsInt checks if the Rat element is an integer.

func (*Rat) IsLessThanOrEqual

func (r *Rat) IsLessThanOrEqual(rhs *Rat) bool

IsLessThanOrEqual checks if the Rat element is less than another Rat element.

func (*Rat) IsNegative

func (r *Rat) IsNegative() bool

IsNegative checks if the Rat element is negative.

func (*Rat) IsOne

func (r *Rat) IsOne() bool

IsOne checks if the Rat element is one.

func (*Rat) IsOpIdentity

func (r *Rat) IsOpIdentity() bool

IsOpIdentity checks if the Rat element is the additive identity (zero).

func (*Rat) IsPositive

func (r *Rat) IsPositive() bool

IsPositive checks if the Rat element is positive.

func (*Rat) IsProbablyPrime

func (r *Rat) IsProbablyPrime() bool

IsProbablyPrime checks if the Rat element is probably prime.

func (*Rat) IsZero

func (r *Rat) IsZero() bool

IsZero checks if the Rat element is zero.

func (*Rat) MarshalCBOR

func (r *Rat) MarshalCBOR() ([]byte, error)

func (*Rat) Mul

func (r *Rat) Mul(rhs *Rat) *Rat

Mul performs multiplication of two Rat elements.

func (*Rat) Neg

func (r *Rat) Neg() *Rat

Neg returns the additive inverse of the Rat element.

func (*Rat) Numerator

func (r *Rat) Numerator() *Int

Numerator returns the numerator of the Rat element.

func (*Rat) Op

func (r *Rat) Op(rhs *Rat) *Rat

Op performs addition of two Rat elements.

func (*Rat) OpInv

func (r *Rat) OpInv() *Rat

OpInv returns the additive inverse of the Rat element.

func (*Rat) OtherOp

func (r *Rat) OtherOp(rhs *Rat) *Rat

OtherOp performs multiplication of two Rat elements.

func (*Rat) Square

func (r *Rat) Square() *Rat

Square returns the square of the Rat element.

func (*Rat) String

func (r *Rat) String() string

String returns the string representation of the Rat element.

func (*Rat) Structure

func (*Rat) Structure() algebra.Structure[*Rat]

Structure returns the algebraic structure to which the Rat element belongs.

func (*Rat) Sub

func (r *Rat) Sub(rhs *Rat) *Rat

Sub performs subtraction of two Rat elements.

func (*Rat) TryDiv

func (r *Rat) TryDiv(rhs *Rat) (*Rat, error)

TryDiv performs division of two Rat elements.

func (*Rat) TryInv

func (r *Rat) TryInv() (*Rat, error)

TryInv returns the multiplicative inverse of the Rat element.

func (*Rat) TryNeg

func (r *Rat) TryNeg() (*Rat, error)

TryNeg returns the additive inverse of the Rat element.

func (*Rat) TryOpInv

func (r *Rat) TryOpInv() (*Rat, error)

TryOpInv returns the additive inverse of the Rat element.

func (*Rat) TrySub

func (r *Rat) TrySub(rhs *Rat) (*Rat, error)

TrySub performs subtraction of two Rat elements.

func (*Rat) UnmarshalCBOR

func (r *Rat) UnmarshalCBOR(data []byte) error

type Rationals

type Rationals struct{}

Rationals represents the field of rational numbers Q.

func Q

func Q() *Rationals

Q returns the singleton instance of the Rationals structure.

func (*Rationals) Characteristic

func (*Rationals) Characteristic() algebra.Cardinal

Characteristic returns the characteristic of the field Q, which is 0.

func (*Rationals) ElementSize

func (*Rationals) ElementSize() int

ElementSize returns -1 to indicate that elements of Q do not have a fixed size.

func (*Rationals) ExtensionDegree

func (*Rationals) ExtensionDegree() uint

ExtensionDegree returns the extension degree of Q over itself, which is 1.

func (*Rationals) FromBig

func (*Rationals) FromBig(n *big.Int) (*Rat, error)

FromBig creates a *Rat element from a *big.Int value.

func (*Rationals) FromBigRat

func (*Rationals) FromBigRat(n *big.Rat) (*Rat, error)

FromBigRat creates a *Rat element from a *big.Rat value.

func (*Rationals) FromBytes

func (*Rationals) FromBytes(data []byte) (*Rat, error)

FromBytes deserializes a Rat element from the given byte slice.

func (*Rationals) FromInt

func (*Rationals) FromInt(n *Int) (*Rat, error)

FromInt creates a Rat element from an Int value.

func (*Rationals) FromInt64

func (*Rationals) FromInt64(n int64) *Rat

FromInt64 creates a Rat element from an int64 value.

func (*Rationals) FromNat

func (*Rationals) FromNat(n *Nat) (*Rat, error)

FromNat creates a Rat element from a Nat value.

func (*Rationals) FromNatPlus

func (*Rationals) FromNatPlus(n *NatPlus) (*Rat, error)

FromNatPlus creates a Rat element from a NatPlus value.

func (*Rationals) FromUint

func (*Rationals) FromUint(n *Uint) (*Rat, error)

FromUint creates a Rat element from a Uint value.

func (*Rationals) FromUint64

func (*Rationals) FromUint64(n uint64) *Rat

FromUint64 creates a Rat element from a uint64 value.

func (*Rationals) IsDomain

func (*Rationals) IsDomain() bool

IsDomain indicates that Q is a domain.

func (*Rationals) Name

func (*Rationals) Name() string

Name returns the name of the structure.

func (*Rationals) New

func (*Rationals) New(a *Int, b *NatPlus) (*Rat, error)

New creates a new Rat element with the given numerator and denominator.

func (*Rationals) One

func (*Rationals) One() *Rat

One returns the multiplicative identity element of Q.

func (*Rationals) OpIdentity

func (q *Rationals) OpIdentity() *Rat

OpIdentity returns the additive identity element of Q.

func (*Rationals) Order

func (*Rationals) Order() algebra.Cardinal

Order returns the order of the field Q, which is infinite.

func (*Rationals) Random

func (*Rationals) Random(lowInclusive, highExclusive *Rat, prng io.Reader) (*Rat, error)

Random samples a random *Rat element in the interval [lowInclusive, highExclusive).

func (*Rationals) RandomInt

func (*Rationals) RandomInt(lowInclusive, highExclusive *Rat, prng io.Reader) (*Int, error)

RandomInt samples a random integer *Int element in the interval [lowInclusive, highExclusive).

The valid integers are those n satisfying lowInclusive <= n < highExclusive, which is equivalent to the half-open integer interval [ceil(lowInclusive), ceil(highExclusive)).

Returns ErrOutOfRange if the interval contains no integers.

func (*Rationals) Zero

func (*Rationals) Zero() *Rat

Zero returns the zero element of Q.

type Uint

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

Uint represents an integer modulo n.

func NewUintGivenModulus

func NewUintGivenModulus(value *numct.Nat, m *numct.Modulus) (*Uint, error)

NewUintGivenModulus creates a new Uint element given a value Nat and a modulus Modulus.

func (*Uint) Abs

func (u *Uint) Abs() *Nat

Abs returns the absolute value of the Uint element as a Nat.

func (*Uint) Add

func (u *Uint) Add(other *Uint) *Uint

Add performs addition of two Uint elements.

func (*Uint) AnnouncedLen

func (u *Uint) AnnouncedLen() int

AnnouncedLen returns the announced length in bytes of the Uint element.

func (*Uint) Big

func (u *Uint) Big() *big.Int

Big returns the big.Int representation of the Uint element.

func (*Uint) Bit

func (u *Uint) Bit(i uint) byte

Bit returns the i-th bit of the Uint element.

func (*Uint) Bytes

func (u *Uint) Bytes() []byte

Bytes returns the byte slice representation of the Uint element.

func (*Uint) BytesBE

func (u *Uint) BytesBE() []byte

BytesBE returns the big-endian byte slice representation of the Uint element.

func (*Uint) Cardinal

func (u *Uint) Cardinal() cardinal.Cardinal

Cardinal returns the cardinality of the Uint element.

func (*Uint) Clone

func (u *Uint) Clone() *Uint

Clone creates a copy of the Uint element.

func (*Uint) Compare

func (u *Uint) Compare(other *Uint) base.Ordering

Compare performs a total comparison between two Uint elements.

func (*Uint) CondAssign

func (u *Uint) CondAssign(choice ct.Choice, x *Uint)

CondAssign conditionally assigns the value of x to the Uint element if choice is true.

func (*Uint) Coprime

func (u *Uint) Coprime(other *Uint) bool

Coprime checks if the Uint element is coprime to another Uint element.

func (*Uint) Decrement

func (u *Uint) Decrement() *Uint

Decrement decrements the Uint element by one.

func (*Uint) Double

func (u *Uint) Double() *Uint

Double returns the result of adding the Uint element to itself.

func (*Uint) Equal

func (u *Uint) Equal(other *Uint) bool

Equal checks if two Uint elements are equal.

func (*Uint) EqualModulus

func (u *Uint) EqualModulus(other *Uint) bool

EqualModulus checks if two Uint elements have the same modulus.

func (*Uint) EuclideanDiv

func (u *Uint) EuclideanDiv(other *Uint) (quot, rem *Uint, err error)

EuclideanDiv performs Euclidean division of the Uint element by another Uint element.

func (*Uint) EuclideanValuation

func (u *Uint) EuclideanValuation() algebra.Cardinal

EuclideanValuation returns the Euclidean valuation of the Uint element.

func (*Uint) Exp

func (u *Uint) Exp(exponent *Nat) *Uint

Exp performs exponentiation of the Uint element by a Nat exponent.

func (*Uint) ExpBounded

func (u *Uint) ExpBounded(exponent *Nat, bits uint) *Uint

ExpBounded performs exponentiation of the Uint element by a Nat exponent, using only the lower 'bits' bits of the exponent.

func (*Uint) ExpI

func (u *Uint) ExpI(exponent *Int) *Uint

ExpI performs exponentiation of the Uint element by an Int exponent.

func (*Uint) ExpIBounded

func (u *Uint) ExpIBounded(exponent *Int, bits uint) *Uint

ExpIBounded performs exponentiation of the Uint element by an Int exponent, using only the lower 'bits' bits of the exponent.

func (*Uint) Group

func (u *Uint) Group() *ZMod

Group returns the ZMod structure that this Uint belongs to.

func (*Uint) HashCode

func (u *Uint) HashCode() base.HashCode

HashCode returns a hash code for the Uint element.

func (*Uint) Increment

func (u *Uint) Increment() *Uint

Increment increments the Uint element by one.

func (*Uint) IsBottom

func (u *Uint) IsBottom() bool

IsBottom checks if the Uint element is the bottom element.

func (*Uint) IsEven

func (u *Uint) IsEven() bool

IsEven checks if the Uint element is even.

func (*Uint) IsLessThanOrEqual

func (u *Uint) IsLessThanOrEqual(other *Uint) bool

IsLessThanOrEqual checks if the Uint element is less than or equal to another Uint element.

func (*Uint) IsNegative

func (u *Uint) IsNegative() bool

IsNegative checks the Uint would have been wrapped around if interpreted as an element of in [-n/2, n/2).

func (*Uint) IsOdd

func (u *Uint) IsOdd() bool

IsOdd checks if the Uint element is odd.

func (*Uint) IsOne

func (u *Uint) IsOne() bool

IsOne checks if the Uint element is one.

func (*Uint) IsOpIdentity

func (u *Uint) IsOpIdentity() bool

IsOpIdentity checks if the Uint element is the additive identity.

func (*Uint) IsPositive

func (u *Uint) IsPositive() bool

IsPositive checks if the Uint is non-zero.

func (*Uint) IsProbablyPrime

func (u *Uint) IsProbablyPrime() bool

IsProbablyPrime checks if the Uint element is probably prime.

func (*Uint) IsQuadraticResidue

func (u *Uint) IsQuadraticResidue() bool

IsQuadraticResidue checks if the Uint element is a quadratic residue modulo the modulus.

func (*Uint) IsTop

func (u *Uint) IsTop() bool

IsTop checks if the Uint element is the top element.

func (*Uint) IsTorsionFree

func (*Uint) IsTorsionFree() bool

IsTorsionFree checks if the Uint element is torsion-free.

func (*Uint) IsUnit

func (u *Uint) IsUnit() bool

IsUnit checks if the Uint element is a unit (i.e., has a multiplicative inverse).

func (*Uint) IsZero

func (u *Uint) IsZero() bool

IsZero checks if the Uint element is zero.

func (*Uint) Lift

func (u *Uint) Lift() *Int

Lift lifts the Uint element to an Int element.

func (*Uint) Lsh

func (u *Uint) Lsh(shift uint) *Uint

Lsh performs left shift on the Uint element. Lsh is equivalent to multiplying by 2^shift mod modulus.

func (*Uint) MarshalCBOR

func (u *Uint) MarshalCBOR() ([]byte, error)

func (*Uint) Modulus

func (u *Uint) Modulus() *NatPlus

Modulus returns the modulus NatPlus of the Uint element.

func (*Uint) ModulusCT

func (u *Uint) ModulusCT() *numct.Modulus

ModulusCT returns the modulus Modulus of the Uint element.

func (*Uint) Mul

func (u *Uint) Mul(other *Uint) *Uint

Mul performs multiplication of two Uint elements.

func (*Uint) Nat

func (u *Uint) Nat() *Nat

Nat returns the Nat representation of the Uint element.

func (*Uint) Neg

func (u *Uint) Neg() *Uint

Neg returns the additive inverse of the Uint element.

func (*Uint) Op

func (u *Uint) Op(other *Uint) *Uint

Op performs the group operation (addition) on two Uint elements.

func (*Uint) OpInv

func (u *Uint) OpInv() *Uint

OpInv returns the additive inverse of the Uint element.

func (*Uint) OtherOp

func (u *Uint) OtherOp(other *Uint) *Uint

OtherOp performs the other group operation (multiplication) on two Uint elements.

func (*Uint) PartialCompare

func (u *Uint) PartialCompare(other *Uint) base.PartialOrdering

PartialCompare performs a partial comparison between two Uint elements.

func (*Uint) Rsh

func (u *Uint) Rsh(shift uint) *Uint

Rsh performs right shift on the Uint element. Rsh is equivalent to floor division by 2^shift, then mod modulus.

func (*Uint) ScalarExp

func (u *Uint) ScalarExp(other *Nat) *Uint

ScalarExp performs exponentiation of the Uint element by a Nat scalar.

func (*Uint) ScalarMul

func (u *Uint) ScalarMul(other *Nat) *Uint

ScalarMul performs scalar multiplication of the Uint element by a Nat scalar.

func (*Uint) ScalarOp

func (u *Uint) ScalarOp(other *Nat) *Uint

ScalarOp performs scalar multiplication of the Uint element by a Nat scalar.

func (*Uint) Select

func (u *Uint) Select(choice ct.Choice, x0, x1 *Uint)

Select sets the Uint element to x0 if choice is true, and to x1 if choice is false.

func (*Uint) Sqrt

func (u *Uint) Sqrt() (*Uint, error)

Sqrt computes the square root of the Uint element if it exists.

func (*Uint) Square

func (u *Uint) Square() *Uint

Square returns the result of multiplying the Uint element by itself.

func (*Uint) String

func (u *Uint) String() string

String returns the string representation of the Uint element.

func (*Uint) Structure

func (u *Uint) Structure() algebra.Structure[*Uint]

Structure returns the algebraic structure of the Uint.

func (*Uint) Sub

func (u *Uint) Sub(other *Uint) *Uint

Sub performs subtraction of two Uint elements.

func (*Uint) TrueLen

func (u *Uint) TrueLen() int

TrueLen returns the true length in bytes of the Uint element.

func (*Uint) TryDiv

func (u *Uint) TryDiv(other *Uint) (*Uint, error)

TryDiv performs division of the Uint element by another Uint element.

func (*Uint) TryInv

func (u *Uint) TryInv() (*Uint, error)

TryInv returns the multiplicative inverse of the Uint element.

func (*Uint) TryNeg

func (u *Uint) TryNeg() (*Uint, error)

TryNeg returns the additive inverse of the Uint element.

func (*Uint) TryOpInv

func (u *Uint) TryOpInv() (*Uint, error)

TryOpInv returns the additive inverse of the Uint element.

func (*Uint) TrySub

func (u *Uint) TrySub(other *Uint) (*Uint, error)

TrySub performs subtraction of two Uint elements.

func (*Uint) UnmarshalCBOR

func (u *Uint) UnmarshalCBOR(data []byte) error

func (*Uint) Value

func (u *Uint) Value() *numct.Nat

Value returns the underlying numct.Nat value of the Uint.

type ZMod

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

ZMod represents the integers modulo n.

func NewZMod

func NewZMod(modulus *NatPlus) (*ZMod, error)

NewZMod creates a new ZMod structure given a modulus NatPlus.

func NewZModFromCardinal

func NewZModFromCardinal(n cardinal.Cardinal) (*ZMod, error)

NewZModFromCardinal creates a new ZMod structure given a cardinal.

func NewZModFromModulus

func NewZModFromModulus(m *numct.Modulus) (*ZMod, error)

NewZModFromModulus creates a new ZMod structure given a modulus Modulus.

func (*ZMod) AmbientStructure

func (*ZMod) AmbientStructure() algebra.Structure[*Int]

AmbientStructure returns the ambient structure of quotient group ie. Z.

func (*ZMod) Bottom

func (zn *ZMod) Bottom() *Uint

Bottom returns the bottom element of the group.

func (*ZMod) Characteristic

func (zn *ZMod) Characteristic() cardinal.Cardinal

Characteristic returns the characteristic of the group.

func (*ZMod) ElementSize

func (zn *ZMod) ElementSize() int

ElementSize returns the size in bytes of an element.

func (*ZMod) FromBig

func (zn *ZMod) FromBig(v *big.Int) (*Uint, error)

FromBig creates a Uint element from a big.Int value. It will reduce the value modulo the modulus of the ZMod.

func (*ZMod) FromBytes

func (zn *ZMod) FromBytes(input []byte) (*Uint, error)

FromBytes creates a Uint element from a byte slice. It will NOT reduce the value modulo the modulus, and will return an error if the value is out of range.

func (*ZMod) FromBytesBE

func (zn *ZMod) FromBytesBE(input []byte) (*Uint, error)

FromBytesBE creates a Uint element from a big-endian byte slice. It will NOT reduce the value modulo the modulus, and will return an error if the value is out of range.

func (*ZMod) FromBytesBEReduce

func (zn *ZMod) FromBytesBEReduce(input []byte) (*Uint, error)

FromBytesBEReduce creates a Uint element from a big-endian byte slice, reducing it modulo the modulus. It will reduce the value modulo the modulus of the ZMod.

func (*ZMod) FromCardinal

func (zn *ZMod) FromCardinal(v cardinal.Cardinal) (*Uint, error)

FromCardinal creates a Uint element from a cardinal. It will NOT reduce the value modulo the modulus, and will return an error if the value is out of range.

func (*ZMod) FromInt

func (zn *ZMod) FromInt(v *Int) (*Uint, error)

FromInt creates a Uint element from an Int value. It will reduce the Int modulo the modulus of the ZMod.

func (*ZMod) FromInt64

func (zn *ZMod) FromInt64(value int64) (*Uint, error)

FromInt64 creates a Uint element from an int64 value.

func (*ZMod) FromNat

func (zn *ZMod) FromNat(v *Nat) (*Uint, error)

FromNat creates a Uint element from a Nat value. It will reduce the value modulo the modulus of the ZMod.

func (*ZMod) FromNatCT

func (zn *ZMod) FromNatCT(v *numct.Nat) (*Uint, error)

FromNatCT creates a Uint element from a numct.Nat value. It will reduce the value modulo the modulus.

func (*ZMod) FromNatCTReduced

func (zn *ZMod) FromNatCTReduced(reducedV *numct.Nat) (*Uint, error)

FromNatCTReduced creates a Uint element from a reduced numct.Nat value. It will NOT reduce the value modulo the modulus, and will return an error if the value is out of range.

func (*ZMod) FromNatPlus

func (zn *ZMod) FromNatPlus(v *NatPlus) (*Uint, error)

FromNatPlus creates a Uint element from a NatPlus value. It will reduce the value modulo the modulus of the ZMod.

func (*ZMod) FromRat

func (zn *ZMod) FromRat(v *Rat) (*Uint, error)

FromRat creates a Uint element from a Rat value. It will reduce the Rat modulo the modulus of the ZMod.

func (*ZMod) FromUint64

func (zn *ZMod) FromUint64(value uint64) *Uint

FromUint64 creates a Uint element from a uint64 value.

func (*ZMod) Hash

func (zn *ZMod) Hash(input []byte) (*Uint, error)

Hash hashes the input byte slice to an element of the group.

func (*ZMod) IsDomain

func (zn *ZMod) IsDomain() bool

IsDomain checks if the group is a domain (i.e., if the modulus is probably prime).

func (*ZMod) IsInRange

func (zn *ZMod) IsInRange(v *Nat) bool

IsInRange checks if a Nat value is in the range of the group.

func (*ZMod) MarshalCBOR

func (z *ZMod) MarshalCBOR() ([]byte, error)

func (*ZMod) Modulus

func (zn *ZMod) Modulus() *NatPlus

Modulus returns the modulus NatPlus of the group.

func (*ZMod) ModulusCT

func (zn *ZMod) ModulusCT() *numct.Modulus

func (*ZMod) Name

func (zn *ZMod) Name() string

Name returns the name of the structure.

func (*ZMod) One

func (zn *ZMod) One() *Uint

One returns the one element of the group.

func (*ZMod) OpIdentity

func (zn *ZMod) OpIdentity() *Uint

OpIdentity returns the additive identity element of the group.

func (*ZMod) Order

func (zn *ZMod) Order() cardinal.Cardinal

Order returns the order of the group.

func (*ZMod) Random

func (zn *ZMod) Random(prng io.Reader) (*Uint, error)

Random samples a random element from the group using the provided PRNG.

func (*ZMod) ScalarStructure

func (*ZMod) ScalarStructure() algebra.Structure[*Nat]

ScalarStructure returns the scalar structure of the group.

func (*ZMod) Top

func (zn *ZMod) Top() *Uint

Top returns the top element of the group.

func (*ZMod) UnmarshalCBOR

func (z *ZMod) UnmarshalCBOR(data []byte) error

func (*ZMod) WideElementSize

func (zn *ZMod) WideElementSize() int

WideElementSize returns the size in bytes of a wide element.

func (*ZMod) Zero

func (zn *ZMod) Zero() *Uint

Zero returns the zero element of the group.

Jump to

Keyboard shortcuts

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