Class IntegerPolynomial
java.lang.Object
org.bouncycastle.pqc.legacy.math.ntru.polynomial.IntegerPolynomial
- All Implemented Interfaces:
Polynomial
- Direct Known Subclasses:
DenseTernaryPolynomial
A polynomial with
Some methods (like
int
coefficients.Some methods (like
add
) change the polynomial, others (like mult
) do
not but return the result as a new polynomial.-
Field Summary
-
Constructor Summary
ConstructorDescriptionIntegerPolynomial
(int N) Constructs a new polynomial withN
coefficients initialized to 0.IntegerPolynomial
(int[] coeffs) Constructs a new polynomial with a given set of coefficients.Constructs aIntegerPolynomial
from aBigIntPolynomial
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
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()
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
boolean
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
(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
(InputStream is, int N) Reads data produced bytoBinary3Tight()
from an input stream and converts it to a polynomial.invertF3()
Computes the inverse mod 3.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
.mult
(BigIntPolynomial poly2) Multiplies the polynomial by aBigIntPolynomial
, taking the indices mod N.mult
(IntegerPolynomial poly2) Multiplies the polynomial with another, taking the indices mod Nmult
(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 of this polynomial withx^n-1
using a probabilistic algorithm.resultant
(int p) Resultant of this polynomial withx^n-1 mod p
.Multithreaded version ofresultant()
.void
rotate1()
Multiplication byX
inZ[X]/Z[X^n-1]
.void
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
Returns the sum of all coefficients, i.e.byte[]
toBinary
(int q) Encodes a polynomial whose coefficients are between 0 and q, to binary.byte[]
Encodes a polynomial with ternary coefficients to binary.byte[]
Converts a polynomial with ternary coefficients to binary.Returns a polynomial that is equal to this polynomial (in the sense thatPolynomial.mult(IntegerPolynomial, int)
returns equalIntegerPolynomial
s).
-
Field Details
-
coeffs
public int[] coeffs
-
-
Constructor Details
-
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
Constructs aIntegerPolynomial
from aBigIntPolynomial
. The two polynomials are independent of each other.- Parameters:
p
- the original polynomial
-
-
Method Details
-
fromBinary3Sves
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
Converts a byte array produced bytoBinary3Tight()
to a polynomial.- Parameters:
b
- a byte arrayN
- number of coefficients- Returns:
- the decoded polynomial
-
fromBinary3Tight
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:
IOException
-
fromBinary
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
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:
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
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
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
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
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
Computes the inverse mod 3. Returnsnull
if the polynomial is not invertible.- Returns:
- a new polynomial
-
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
Multithreaded version ofresultant()
.- Returns:
(rho, res)
satisfyingres = rho*this + t*(x^n-1)
for some integert
.
-
resultant
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
Adds another polynomial which can have a different number of coefficients, and takes the coefficient values modmodulus
.- Parameters:
b
- another polynomial
-
add
Adds another polynomial which can have a different number of coefficients.- Parameters:
b
- another polynomial
-
sub
Subtracts another polynomial which can have a different number of coefficients, and takes the coefficient values modmodulus
.- Parameters:
b
- another polynomial
-
sub
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
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
-
equals
-