Class IntegerPolynomial
- java.lang.Object
-
- org.bouncycastle.pqc.math.ntru.polynomial.IntegerPolynomial
-
- All Implemented Interfaces:
Polynomial
- Direct Known Subclasses:
DenseTernaryPolynomial
public class IntegerPolynomial extends java.lang.Object implements Polynomial
A polynomial withint
coefficients.
Some methods (likeadd
) change the polynomial, others (likemult
) do not but return the result as a new polynomial.
-
-
Field Summary
Fields Modifier and Type Field Description int[]
coeffs
-
Constructor Summary
Constructors Constructor Description IntegerPolynomial(int N)
Constructs a new polynomial withN
coefficients initialized to 0.IntegerPolynomial(int[] coeffs)
Constructs a new polynomial with a given set of coefficients.IntegerPolynomial(BigIntPolynomial p)
Constructs aIntegerPolynomial
from aBigIntPolynomial
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
add(IntegerPolynomial b)
Adds another polynomial which can have a different number of coefficients.void
add(IntegerPolynomial b, int modulus)
Adds another polynomial which can have a different number of coefficients, and takes the coefficient values modmodulus
.void
center0(int q)
Shifts the values of all coefficients to the interval[-q/2, q/2]
.long
centeredNormSq(int q)
Computes the centered euclidean norm of the polynomial.void
clear()
java.lang.Object
clone()
int
count(int value)
Counts the number of coefficients equal to an integervoid
div(int k)
Divides each coefficient byk
and rounds to the nearest integer.void
ensurePositive(int modulus)
Addsmodulus
until all coefficients are above 0.boolean
equals(java.lang.Object obj)
boolean
equalsOne()
Tests ifp(x) = 1
.static IntegerPolynomial
fromBinary(byte[] data, int N, int q)
Returns a polynomial with N coefficients between0
andq-1
.
q
must be a power of 2.
Ignores any excess bytes.static IntegerPolynomial
fromBinary(java.io.InputStream is, int N, int q)
Returns a polynomial with N coefficients between0
andq-1
.
q
must be a power of 2.
Ignores any excess bytes.static IntegerPolynomial
fromBinary3Sves(byte[] data, int N)
Decodes a byte array to a polynomial withN
ternary coefficients
Ignores any excess bytes.static IntegerPolynomial
fromBinary3Tight(byte[] b, int N)
Converts a byte array produced bytoBinary3Tight()
to a polynomial.static IntegerPolynomial
fromBinary3Tight(java.io.InputStream is, int N)
Reads data produced bytoBinary3Tight()
from an input stream and converts it to a polynomial.IntegerPolynomial
invertF3()
Computes the inverse mod 3.IntegerPolynomial
invertFq(int q)
Computes the inverse modq; q
must be a power of 2.
Returnsnull
if the polynomial is not invertible.void
mod(int modulus)
Takes each coefficient modulomodulus
.void
mod3()
Takes each coefficient modulo 3 such that all coefficients are ternary.void
modPositive(int modulus)
Ensures all coefficients are between 0 andmodulus-1
void
mult(int factor)
Multiplies each coefficient by aint
.BigIntPolynomial
mult(BigIntPolynomial poly2)
Multiplies the polynomial by aBigIntPolynomial
, taking the indices mod N.IntegerPolynomial
mult(IntegerPolynomial poly2)
Multiplies the polynomial with another, taking the indices mod NIntegerPolynomial
mult(IntegerPolynomial poly2, int modulus)
Multiplies the polynomial with another, taking the values mod modulus and the indices mod Nvoid
mult3(int modulus)
Multiplies each coefficient by a 2 and applies a modulus.Resultant
resultant()
Resultant of this polynomial withx^n-1
using a probabilistic algorithm.ModularResultant
resultant(int p)
Resultant of this polynomial withx^n-1 mod p
.Resultant
resultantMultiThread()
Multithreaded version ofresultant()
.void
rotate1()
Multiplication byX
inZ[X]/Z[X^n-1]
.void
sub(IntegerPolynomial b)
Subtracts another polynomial which can have a different number of coefficients.void
sub(IntegerPolynomial b, int modulus)
Subtracts another polynomial which can have a different number of coefficients, and takes the coefficient values modmodulus
.int
sumCoeffs()
Returns the sum of all coefficients, i.e. evaluates the polynomial at 0.byte[]
toBinary(int q)
Encodes a polynomial whose coefficients are between 0 and q, to binary. q must be a power of 2.byte[]
toBinary3Sves()
Encodes a polynomial with ternary coefficients to binary.byte[]
toBinary3Tight()
Converts a polynomial with ternary coefficients to binary.IntegerPolynomial
toIntegerPolynomial()
Returns a polynomial that is equal to this polynomial (in the sense thatPolynomial.mult(IntegerPolynomial, int)
returns equalIntegerPolynomial
s).
-
-
-
Constructor Detail
-
IntegerPolynomial
public IntegerPolynomial(int N)
Constructs a new polynomial withN
coefficients initialized to 0.- Parameters:
N
- the number of coefficients
-
IntegerPolynomial
public IntegerPolynomial(int[] coeffs)
Constructs a new polynomial with a given set of coefficients.- Parameters:
coeffs
- the coefficients
-
IntegerPolynomial
public IntegerPolynomial(BigIntPolynomial p)
Constructs aIntegerPolynomial
from aBigIntPolynomial
. The two polynomials are independent of each other.- Parameters:
p
- the original polynomial
-
-
Method Detail
-
fromBinary3Sves
public static IntegerPolynomial fromBinary3Sves(byte[] data, int N)
Decodes a byte array to a polynomial withN
ternary coefficients
Ignores any excess bytes.- Parameters:
data
- an encoded ternary polynomialN
- number of coefficients- Returns:
- the decoded polynomial
-
fromBinary3Tight
public static IntegerPolynomial fromBinary3Tight(byte[] b, int N)
Converts a byte array produced bytoBinary3Tight()
to a polynomial.- Parameters:
b
- a byte arrayN
- number of coefficients- Returns:
- the decoded polynomial
-
fromBinary3Tight
public static IntegerPolynomial fromBinary3Tight(java.io.InputStream is, int N) throws java.io.IOException
Reads data produced bytoBinary3Tight()
from an input stream and converts it to a polynomial.- Parameters:
is
- an input streamN
- number of coefficients- Returns:
- the decoded polynomial
- Throws:
java.io.IOException
-
fromBinary
public static IntegerPolynomial fromBinary(byte[] data, int N, int q)
Returns a polynomial with N coefficients between0
andq-1
.
q
must be a power of 2.
Ignores any excess bytes.- Parameters:
data
- an encoded ternary polynomialN
- number of coefficientsq
-- Returns:
- the decoded polynomial
-
fromBinary
public static IntegerPolynomial fromBinary(java.io.InputStream is, int N, int q) throws java.io.IOException
Returns a polynomial with N coefficients between0
andq-1
.
q
must be a power of 2.
Ignores any excess bytes.- Parameters:
is
- an encoded ternary polynomialN
- number of coefficientsq
-- Returns:
- the decoded polynomial
- Throws:
java.io.IOException
-
toBinary3Sves
public byte[] toBinary3Sves()
Encodes a polynomial with ternary coefficients to binary.coeffs[2*i]
andcoeffs[2*i+1]
must not both equal -1 for any integeri
, so this method is only safe to use with polynomials produced byfromBinary3Sves()
.- Returns:
- the encoded polynomial
-
toBinary3Tight
public byte[] toBinary3Tight()
Converts a polynomial with ternary coefficients to binary.- Returns:
- the encoded polynomial
-
toBinary
public byte[] toBinary(int q)
Encodes a polynomial whose coefficients are between 0 and q, to binary. q must be a power of 2.- Parameters:
q
-- Returns:
- the encoded polynomial
-
mult
public IntegerPolynomial mult(IntegerPolynomial poly2, int modulus)
Multiplies the polynomial with another, taking the values mod modulus and the indices mod N- Specified by:
mult
in interfacePolynomial
- Parameters:
poly2
- a polynomialmodulus
- a modulus to apply- Returns:
- the product of the two polynomials
-
mult
public IntegerPolynomial mult(IntegerPolynomial poly2)
Multiplies the polynomial with another, taking the indices mod N- Specified by:
mult
in interfacePolynomial
- Parameters:
poly2
- a polynomial- Returns:
- the product of the two polynomials
-
mult
public BigIntPolynomial mult(BigIntPolynomial poly2)
Description copied from interface:Polynomial
Multiplies the polynomial by aBigIntPolynomial
, taking the indices mod N. Does not change this polynomial but returns the result as a new polynomial.
Both polynomials must have the same number of coefficients.- Specified by:
mult
in interfacePolynomial
- Parameters:
poly2
- the polynomial to multiply by- Returns:
- a new polynomial
-
invertFq
public IntegerPolynomial invertFq(int q)
Computes the inverse modq; q
must be a power of 2.
Returnsnull
if the polynomial is not invertible.- Parameters:
q
- the modulus- Returns:
- a new polynomial
-
invertF3
public IntegerPolynomial invertF3()
Computes the inverse mod 3. Returnsnull
if the polynomial is not invertible.- Returns:
- a new polynomial
-
resultant
public Resultant resultant()
Resultant of this polynomial withx^n-1
using a probabilistic algorithm.Unlike EESS, this implementation does not compute all resultants modulo primes such that their product exceeds the maximum possible resultant, but rather stops when
NUM_EQUAL_RESULTANTS
consecutive modular resultants are equal.
This means the return value may be incorrect. Experiments show this happens in about 1 out of 100 cases whenN=439
andNUM_EQUAL_RESULTANTS=2
, so the likelyhood of leaving the loop too early is(1/100)^(NUM_EQUAL_RESULTANTS-1)
.Because of the above, callers must verify the output and try a different polynomial if necessary.
- Returns:
(rho, res)
satisfyingres = rho*this + t*(x^n-1)
for some integert
.
-
resultantMultiThread
public Resultant resultantMultiThread()
Multithreaded version ofresultant()
.- Returns:
(rho, res)
satisfyingres = rho*this + t*(x^n-1)
for some integert
.
-
resultant
public ModularResultant resultant(int p)
Resultant of this polynomial withx^n-1 mod p
.- Returns:
(rho, res)
satisfyingres = rho*this + t*(x^n-1) mod p
for some integert
.
-
add
public void add(IntegerPolynomial b, int modulus)
Adds another polynomial which can have a different number of coefficients, and takes the coefficient values modmodulus
.- Parameters:
b
- another polynomial
-
add
public void add(IntegerPolynomial b)
Adds another polynomial which can have a different number of coefficients.- Parameters:
b
- another polynomial
-
sub
public void sub(IntegerPolynomial b, int modulus)
Subtracts another polynomial which can have a different number of coefficients, and takes the coefficient values modmodulus
.- Parameters:
b
- another polynomial
-
sub
public void sub(IntegerPolynomial b)
Subtracts another polynomial which can have a different number of coefficients.- Parameters:
b
- another polynomial
-
mult
public void mult(int factor)
Multiplies each coefficient by aint
. Does not return a new polynomial but modifies this polynomial.- Parameters:
factor
-
-
mult3
public void mult3(int modulus)
Multiplies each coefficient by a 2 and applies a modulus. Does not return a new polynomial but modifies this polynomial.- Parameters:
modulus
- a modulus
-
div
public void div(int k)
Divides each coefficient byk
and rounds to the nearest integer. Does not return a new polynomial but modifies this polynomial.- Parameters:
k
- the divisor
-
mod3
public void mod3()
Takes each coefficient modulo 3 such that all coefficients are ternary.
-
modPositive
public void modPositive(int modulus)
Ensures all coefficients are between 0 andmodulus-1
- Parameters:
modulus
- a modulus
-
mod
public void mod(int modulus)
Takes each coefficient modulomodulus
.
-
ensurePositive
public void ensurePositive(int modulus)
Addsmodulus
until all coefficients are above 0.- Parameters:
modulus
- a modulus
-
centeredNormSq
public long centeredNormSq(int q)
Computes the centered euclidean norm of the polynomial.- Parameters:
q
- a modulus- Returns:
- the centered norm
-
center0
public void center0(int q)
Shifts the values of all coefficients to the interval[-q/2, q/2]
.- Parameters:
q
- a modulus
-
sumCoeffs
public int sumCoeffs()
Returns the sum of all coefficients, i.e. evaluates the polynomial at 0.- Returns:
- the sum of all coefficients
-
equalsOne
public boolean equalsOne()
Tests ifp(x) = 1
.- Returns:
- true iff all coefficients are equal to zero, except for the lowest coefficient which must equal 1
-
count
public int count(int value)
Counts the number of coefficients equal to an integer- Parameters:
value
- an integer- Returns:
- the number of coefficients equal to
value
-
rotate1
public void rotate1()
Multiplication byX
inZ[X]/Z[X^n-1]
.
-
clear
public void clear()
-
toIntegerPolynomial
public IntegerPolynomial toIntegerPolynomial()
Description copied from interface:Polynomial
Returns a polynomial that is equal to this polynomial (in the sense thatPolynomial.mult(IntegerPolynomial, int)
returns equalIntegerPolynomial
s). The new polynomial is guaranteed to be independent of the original.- Specified by:
toIntegerPolynomial
in interfacePolynomial
- Returns:
- a new
IntegerPolynomial
.
-
clone
public java.lang.Object clone()
- Overrides:
clone
in classjava.lang.Object
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equals
in classjava.lang.Object
-
-