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 intbit(BigInteger a, long k) Retrieves the bit at positionkof the absolute value ofa.static BigIntegergenerateXBitRandom(int bits) Generates a random n-bit positive number.static BigIntegerjacobi(BigInteger a, BigInteger n) Computes the Jacobi symbol (a/n).static BigIntegerPOSMOD(BigInteger x, BigInteger n) Computes the positive modulus of a number.static BigIntegerFinds a quadratic non-residue modulopandq.static BigIntegerGenerates 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
xmodulon.
-
generateXBitRandom
Generates a random n-bit positive number.- Parameters:
bits- The number of bits for the random number.- Returns:
- A
BigIntegerrepresenting 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 <= -1ornis even.
-
quadratic_non_residue
Finds a quadratic non-residue modulopandq.- 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 positionkof 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 ifkis out of range.
-