C# Class MpcLib.Common.FiniteField.NumTheoryUtils

Implements some number-theoretic utility functions.
Show file Open project: mahdiz/mpclib Class Usage Examples

Public Properties

Property Type Description
DHPrime1536 System.Numerics.BigInteger
DHPrime2048 System.Numerics.BigInteger
DHPrime768 System.Numerics.BigInteger

Public Methods

Method Description
CalcInverse ( System.Numerics.BigInteger n, System.Numerics.BigInteger prime ) : System.Numerics.BigInteger

Returns the inverse of n modulo p.

DLEncrypt ( BigZp x, System.Numerics.BigInteger p ) : System.Numerics.BigInteger

Discrete logarithm encryption.

ExtendedEuclidean ( System.Numerics.BigInteger a, System.Numerics.BigInteger b, System.Numerics.BigInteger &x, System.Numerics.BigInteger &y ) : System.Numerics.BigInteger

Performs the Extended Euclidean algorithm and returns gcd(a,b). The function also returns integers x and y such that ax + by = gcd(a,b). If gcd(a,b) = 1, then x is a multiplicative inverse of "a mod b" and y is a multiplicative inverse of "b mod a".

ExtendedEuclidean ( int a, int b, int &x, int &y ) : int

Performs the Extended Euclidean algorithm and returns gcd(a,b). The function also returns integers x and y such that ax + by = gcd(a,b). If gcd(a,b) = 1, then x is a multiplicative inverse of "a mod b" and y is a multiplicative inverse of "b mod a".

ExtendedEuclidean ( long a, long b, long &x, long &y ) : long

Performs the Extended Euclidean algorithm and returns gcd(a,b). The function also returns integers x and y such that ax + by = gcd(a,b). If gcd(a,b) = 1, then x is a multiplicative inverse of "a mod b" and y is a multiplicative inverse of "b mod a".

GetBitDecomposition ( System.Numerics.BigInteger a, System.Numerics.BigInteger prime ) : List
GetBitDecomposition ( System.Numerics.BigInteger a, System.Numerics.BigInteger prime, int length ) : List
GetBitLength ( System.Numerics.BigInteger a ) : int

Returns the actual number of bits of a byte array by ignoring trailing zeros in the binary representation.

GetBitLength ( byte a ) : int

Returns the actual number of bits of a byte array by ignoring trailing zeros in the binary representation.

GetBitLength2 ( System.Numerics.BigInteger a ) : int
GetFieldInverse ( int prime ) : int[]

Returns an array containing inverses of all field elements.

GetFieldMinimumPrimitive ( int prime ) : int
IsPrime ( int n ) : bool

Performs a O(sqrt(|n|)) primality test, where |n| is the bit-length of the input.

MillerRabin ( System.Numerics.BigInteger n, int k ) : bool

Implements the Miller-Rabin algorithm for fast compositeness test. Returns true if n is a composite number. If n is prime, returns false with high probability (this probability increases with k).

ModPow ( int powerBase, int exp, int prime ) : int

Performs modular exponentiation.

ModSqrRoot ( System.Numerics.BigInteger val, System.Numerics.BigInteger prime ) : System.Numerics.BigInteger
MultiplicativeInverse ( System.Numerics.BigInteger a, System.Numerics.BigInteger m ) : System.Numerics.BigInteger

Returns the multiplicative inverse of a modulo m.

MultiplicativeInverse ( int a, int m ) : int

Returns the multiplicative inverse of a modulo m.

MultiplicativeInverse ( long a, long m ) : long

Returns the multiplicative inverse of a modulo m.

NextRandomBigInt ( RNGCryptoServiceProvider rng, System.Numerics.BigInteger minValue, System.Numerics.BigInteger maxValue ) : System.Numerics.BigInteger

Returns a random big integer in the specified range. This is based on a simple probabilistic algorithm.

Method Details

CalcInverse() public static method

Returns the inverse of n modulo p.
public static CalcInverse ( System.Numerics.BigInteger n, System.Numerics.BigInteger prime ) : System.Numerics.BigInteger
n System.Numerics.BigInteger
prime System.Numerics.BigInteger
return System.Numerics.BigInteger

DLEncrypt() public static method

Discrete logarithm encryption.
public static DLEncrypt ( BigZp x, System.Numerics.BigInteger p ) : System.Numerics.BigInteger
x BigZp Value to be encrypted.
p System.Numerics.BigInteger Prime modulus. Should be large enough (> 1024 bits) to ensure security.
return System.Numerics.BigInteger

ExtendedEuclidean() public static method

Performs the Extended Euclidean algorithm and returns gcd(a,b). The function also returns integers x and y such that ax + by = gcd(a,b). If gcd(a,b) = 1, then x is a multiplicative inverse of "a mod b" and y is a multiplicative inverse of "b mod a".
public static ExtendedEuclidean ( System.Numerics.BigInteger a, System.Numerics.BigInteger b, System.Numerics.BigInteger &x, System.Numerics.BigInteger &y ) : System.Numerics.BigInteger
a System.Numerics.BigInteger
b System.Numerics.BigInteger
x System.Numerics.BigInteger
y System.Numerics.BigInteger
return System.Numerics.BigInteger

ExtendedEuclidean() public static method

Performs the Extended Euclidean algorithm and returns gcd(a,b). The function also returns integers x and y such that ax + by = gcd(a,b). If gcd(a,b) = 1, then x is a multiplicative inverse of "a mod b" and y is a multiplicative inverse of "b mod a".
public static ExtendedEuclidean ( int a, int b, int &x, int &y ) : int
a int
b int
x int
y int
return int

ExtendedEuclidean() public static method

Performs the Extended Euclidean algorithm and returns gcd(a,b). The function also returns integers x and y such that ax + by = gcd(a,b). If gcd(a,b) = 1, then x is a multiplicative inverse of "a mod b" and y is a multiplicative inverse of "b mod a".
public static ExtendedEuclidean ( long a, long b, long &x, long &y ) : long
a long
b long
x long
y long
return long

GetBitDecomposition() public static method

public static GetBitDecomposition ( System.Numerics.BigInteger a, System.Numerics.BigInteger prime ) : List
a System.Numerics.BigInteger
prime System.Numerics.BigInteger
return List

GetBitDecomposition() public static method

public static GetBitDecomposition ( System.Numerics.BigInteger a, System.Numerics.BigInteger prime, int length ) : List
a System.Numerics.BigInteger
prime System.Numerics.BigInteger
length int
return List

GetBitLength() public static method

Returns the actual number of bits of a byte array by ignoring trailing zeros in the binary representation.
public static GetBitLength ( System.Numerics.BigInteger a ) : int
a System.Numerics.BigInteger
return int

GetBitLength() public static method

Returns the actual number of bits of a byte array by ignoring trailing zeros in the binary representation.
public static GetBitLength ( byte a ) : int
a byte
return int

GetBitLength2() public static method

public static GetBitLength2 ( System.Numerics.BigInteger a ) : int
a System.Numerics.BigInteger
return int

GetFieldInverse() public static method

Returns an array containing inverses of all field elements.
public static GetFieldInverse ( int prime ) : int[]
prime int
return int[]

GetFieldMinimumPrimitive() public static method

public static GetFieldMinimumPrimitive ( int prime ) : int
prime int
return int

IsPrime() public static method

Performs a O(sqrt(|n|)) primality test, where |n| is the bit-length of the input.
public static IsPrime ( int n ) : bool
n int
return bool

MillerRabin() public static method

Implements the Miller-Rabin algorithm for fast compositeness test. Returns true if n is a composite number. If n is prime, returns false with high probability (this probability increases with k).
public static MillerRabin ( System.Numerics.BigInteger n, int k ) : bool
n System.Numerics.BigInteger
k int
return bool

ModPow() public static method

Performs modular exponentiation.
public static ModPow ( int powerBase, int exp, int prime ) : int
powerBase int
exp int
prime int
return int

ModSqrRoot() public static method

public static ModSqrRoot ( System.Numerics.BigInteger val, System.Numerics.BigInteger prime ) : System.Numerics.BigInteger
val System.Numerics.BigInteger
prime System.Numerics.BigInteger
return System.Numerics.BigInteger

MultiplicativeInverse() public static method

Returns the multiplicative inverse of a modulo m.
public static MultiplicativeInverse ( System.Numerics.BigInteger a, System.Numerics.BigInteger m ) : System.Numerics.BigInteger
a System.Numerics.BigInteger
m System.Numerics.BigInteger
return System.Numerics.BigInteger

MultiplicativeInverse() public static method

Returns the multiplicative inverse of a modulo m.
public static MultiplicativeInverse ( int a, int m ) : int
a int
m int
return int

MultiplicativeInverse() public static method

Returns the multiplicative inverse of a modulo m.
public static MultiplicativeInverse ( long a, long m ) : long
a long
m long
return long

NextRandomBigInt() public static method

Returns a random big integer in the specified range. This is based on a simple probabilistic algorithm.
public static NextRandomBigInt ( RNGCryptoServiceProvider rng, System.Numerics.BigInteger minValue, System.Numerics.BigInteger maxValue ) : System.Numerics.BigInteger
rng System.Security.Cryptography.RNGCryptoServiceProvider
minValue System.Numerics.BigInteger Inclusive lower bound.
maxValue System.Numerics.BigInteger Exclusive upper bound.
return System.Numerics.BigInteger

Property Details

DHPrime1536 public static property

public static BigInteger,System.Numerics DHPrime1536
return System.Numerics.BigInteger

DHPrime2048 public static property

public static BigInteger,System.Numerics DHPrime2048
return System.Numerics.BigInteger

DHPrime768 public static property

public static BigInteger,System.Numerics DHPrime768
return System.Numerics.BigInteger