java.lang.Object
edu.fiu.adwise.homomorphic_encryption.misc.NTL
All Implemented Interfaces:
CipherConstants

public final class NTL extends Object implements 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.

  • Constructor Details

    • NTL

      public NTL()
  • Method Details

    • POSMOD

      public static BigInteger POSMOD(BigInteger x, BigInteger n)
      Computes the positive modulus of a number.
      Parameters:
      x - The dividend as a BigInteger.
      n - The divisor as a BigInteger.
      Returns:
      The positive modulus of x modulo n.
    • generateXBitRandom

      public static BigInteger generateXBitRandom(int bits)
      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

      public static BigInteger RandomBnd(BigInteger n)
      Generates a pseudo-random number in the range [0..n-1].
      Parameters:
      n - The upper bound as a BigInteger.
      Returns:
      A pseudo-random number in the range [0..n-1], or 0 if n <= 0.
    • jacobi

      public static BigInteger jacobi(BigInteger a, BigInteger n)
      Computes the Jacobi symbol (a/n). Medium article on Goldwasser Micali
      Parameters:
      a - The numerator as a BigInteger.
      n - The denominator as a BigInteger.
      Returns:
      The Jacobi symbol as a BigInteger.
      Throws:
      IllegalArgumentException - If a <= -1 or n is even.
    • quadratic_non_residue

      public static BigInteger quadratic_non_residue(BigInteger p, BigInteger q)
      Finds a quadratic non-residue modulo p and q.
      Parameters:
      p - A prime number as a BigInteger.
      q - Another prime number as a BigInteger.
      Returns:
      A quadratic non-residue as a BigInteger.
    • bit

      public static int bit(BigInteger a, long k)
      Retrieves the bit at position k of the absolute value of a.
      Parameters:
      a - The number as a BigInteger.
      k - The bit position (0-based).
      Returns:
      The bit value (0 or 1) at position k, or 0 if k is out of range.