Class NTL
java.lang.Object
edu.fiu.adwise.homomorphic_encryption.misc.NTL
- All Implemented Interfaces:
CipherConstants
This class provides utility methods for cryptographic operations, inspired by the NTL (Number Theory Library).
It includes methods for modular arithmetic, random number generation, and mathematical operations
such as the Jacobi symbol and quadratic non-residue calculations.
This is the Java implementation of the C++ NTL Library Please refer to this site for NTL documentation: Tour of C++ NTL NTL C++ BigIntegers
Credit to Samet Tonyali for helping on revising the code/debugging it.
-
Field Summary
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic int
bit
(BigInteger a, long k) Retrieves the bit at positionk
of the absolute value ofa
.static BigInteger
generateXBitRandom
(int bits) Generates a random n-bit positive number.static BigInteger
jacobi
(BigInteger a, BigInteger n) Computes the Jacobi symbol (a/n).static BigInteger
POSMOD
(BigInteger x, BigInteger n) Computes the positive modulus of a number.static BigInteger
Finds a quadratic non-residue modulop
andq
.static BigInteger
Generates a pseudo-random number in the range [0..n-1].
-
Constructor Details
-
NTL
public NTL()
-
-
Method Details
-
POSMOD
Computes the positive modulus of a number.- Parameters:
x
- The dividend as aBigInteger
.n
- The divisor as aBigInteger
.- Returns:
- The positive modulus of
x
modulon
.
-
generateXBitRandom
Generates a random n-bit positive number.- Parameters:
bits
- The number of bits for the random number.- Returns:
- A
BigInteger
representing the n-bit random number.
-
RandomBnd
Generates a pseudo-random number in the range [0..n-1].- Parameters:
n
- The upper bound as aBigInteger
.- Returns:
- A pseudo-random number in the range [0..n-1], or 0 if
n <= 0
.
-
jacobi
Computes the Jacobi symbol (a/n). Medium article on Goldwasser Micali- Parameters:
a
- The numerator as aBigInteger
.n
- The denominator as aBigInteger
.- Returns:
- The Jacobi symbol as a
BigInteger
. - Throws:
IllegalArgumentException
- Ifa <= -1
orn
is even.
-
quadratic_non_residue
Finds a quadratic non-residue modulop
andq
.- Parameters:
p
- A prime number as aBigInteger
.q
- Another prime number as aBigInteger
.- Returns:
- A quadratic non-residue as a
BigInteger
.
-
bit
Retrieves the bit at positionk
of the absolute value ofa
.- Parameters:
a
- The number as aBigInteger
.k
- The bit position (0-based).- Returns:
- The bit value (0 or 1) at position
k
, or 0 ifk
is out of range.
-