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 withintcoefficients.
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 withNcoefficients initialized to 0.IntegerPolynomial(int[] coeffs)Constructs a new polynomial with a given set of coefficients.IntegerPolynomial(BigIntPolynomial p)Constructs aIntegerPolynomialfrom aBigIntPolynomial.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(IntegerPolynomial b)Adds another polynomial which can have a different number of coefficients.voidadd(IntegerPolynomial b, int modulus)Adds another polynomial which can have a different number of coefficients, and takes the coefficient values modmodulus.voidcenter0(int q)Shifts the values of all coefficients to the interval[-q/2, q/2].longcenteredNormSq(int q)Computes the centered euclidean norm of the polynomial.voidclear()java.lang.Objectclone()intcount(int value)Counts the number of coefficients equal to an integervoiddiv(int k)Divides each coefficient bykand rounds to the nearest integer.voidensurePositive(int modulus)Addsmodulusuntil all coefficients are above 0.booleanequals(java.lang.Object obj)booleanequalsOne()Tests ifp(x) = 1.static IntegerPolynomialfromBinary(byte[] data, int N, int q)Returns a polynomial with N coefficients between0andq-1.
qmust be a power of 2.
Ignores any excess bytes.static IntegerPolynomialfromBinary(java.io.InputStream is, int N, int q)Returns a polynomial with N coefficients between0andq-1.
qmust be a power of 2.
Ignores any excess bytes.static IntegerPolynomialfromBinary3Sves(byte[] data, int N)Decodes a byte array to a polynomial withNternary coefficients
Ignores any excess bytes.static IntegerPolynomialfromBinary3Tight(byte[] b, int N)Converts a byte array produced bytoBinary3Tight()to a polynomial.static IntegerPolynomialfromBinary3Tight(java.io.InputStream is, int N)Reads data produced bytoBinary3Tight()from an input stream and converts it to a polynomial.IntegerPolynomialinvertF3()Computes the inverse mod 3.IntegerPolynomialinvertFq(int q)Computes the inverse modq; qmust be a power of 2.
Returnsnullif the polynomial is not invertible.voidmod(int modulus)Takes each coefficient modulomodulus.voidmod3()Takes each coefficient modulo 3 such that all coefficients are ternary.voidmodPositive(int modulus)Ensures all coefficients are between 0 andmodulus-1voidmult(int factor)Multiplies each coefficient by aint.BigIntPolynomialmult(BigIntPolynomial poly2)Multiplies the polynomial by aBigIntPolynomial, taking the indices mod N.IntegerPolynomialmult(IntegerPolynomial poly2)Multiplies the polynomial with another, taking the indices mod NIntegerPolynomialmult(IntegerPolynomial poly2, int modulus)Multiplies the polynomial with another, taking the values mod modulus and the indices mod Nvoidmult3(int modulus)Multiplies each coefficient by a 2 and applies a modulus.Resultantresultant()Resultant of this polynomial withx^n-1using a probabilistic algorithm.ModularResultantresultant(int p)Resultant of this polynomial withx^n-1 mod p.ResultantresultantMultiThread()Multithreaded version ofresultant().voidrotate1()Multiplication byXinZ[X]/Z[X^n-1].voidsub(IntegerPolynomial b)Subtracts another polynomial which can have a different number of coefficients.voidsub(IntegerPolynomial b, int modulus)Subtracts another polynomial which can have a different number of coefficients, and takes the coefficient values modmodulus.intsumCoeffs()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.IntegerPolynomialtoIntegerPolynomial()Returns a polynomial that is equal to this polynomial (in the sense thatPolynomial.mult(IntegerPolynomial, int)returns equalIntegerPolynomials).
-
-
-
Constructor Detail
-
IntegerPolynomial
public IntegerPolynomial(int N)
Constructs a new polynomial withNcoefficients 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 aIntegerPolynomialfrom 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 withNternary 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 between0andq-1.
qmust 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 between0andq-1.
qmust 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:
multin 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:
multin interfacePolynomial- Parameters:
poly2- a polynomial- Returns:
- the product of the two polynomials
-
mult
public BigIntPolynomial mult(BigIntPolynomial poly2)
Description copied from interface:PolynomialMultiplies 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:
multin interfacePolynomial- Parameters:
poly2- the polynomial to multiply by- Returns:
- a new polynomial
-
invertFq
public IntegerPolynomial invertFq(int q)
Computes the inverse modq; qmust be a power of 2.
Returnsnullif the polynomial is not invertible.- Parameters:
q- the modulus- Returns:
- a new polynomial
-
invertF3
public IntegerPolynomial invertF3()
Computes the inverse mod 3. Returnsnullif the polynomial is not invertible.- Returns:
- a new polynomial
-
resultant
public Resultant resultant()
Resultant of this polynomial withx^n-1using 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_RESULTANTSconsecutive modular resultants are equal.
This means the return value may be incorrect. Experiments show this happens in about 1 out of 100 cases whenN=439andNUM_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 pfor 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 bykand 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)
Addsmodulusuntil 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 byXinZ[X]/Z[X^n-1].
-
clear
public void clear()
-
toIntegerPolynomial
public IntegerPolynomial toIntegerPolynomial()
Description copied from interface:PolynomialReturns a polynomial that is equal to this polynomial (in the sense thatPolynomial.mult(IntegerPolynomial, int)returns equalIntegerPolynomials). The new polynomial is guaranteed to be independent of the original.- Specified by:
toIntegerPolynomialin interfacePolynomial- Returns:
- a new
IntegerPolynomial.
-
clone
public java.lang.Object clone()
- Overrides:
clonein classjava.lang.Object
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equalsin classjava.lang.Object
-
-