Class SparseTernaryPolynomial
java.lang.Object
org.bouncycastle.pqc.legacy.math.ntru.polynomial.SparseTernaryPolynomial
- All Implemented Interfaces:
Polynomial
,TernaryPolynomial
A
TernaryPolynomial
with a "low" number of nonzero coefficients.-
Constructor Summary
ConstructorDescriptionSparseTernaryPolynomial
(int[] coeffs) Constructs a newSparseTernaryPolynomial
with a given set of coefficients.SparseTernaryPolynomial
(IntegerPolynomial intPoly) Constructs aDenseTernaryPolynomial
from aIntegerPolynomial
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
boolean
static SparseTernaryPolynomial
fromBinary
(InputStream is, int N, int numOnes, int numNegOnes) Decodes a byte array encoded withtoBinary()
to a ploynomial.static SparseTernaryPolynomial
generateRandom
(int N, int numOnes, int numNegOnes, SecureRandom random) Generates a random polynomial withnumOnes
coefficients equal to 1,numNegOnes
coefficients equal to -1, and the rest equal to 0.int[]
int[]
getOnes()
int
hashCode()
mult
(BigIntPolynomial poly2) Multiplies the polynomial by aBigIntPolynomial
, taking the indices mod N.mult
(IntegerPolynomial poly2) Multiplies the polynomial by anIntegerPolynomial
, taking the indices mod Nmult
(IntegerPolynomial poly2, int modulus) Multiplies the polynomial by anIntegerPolynomial
, taking the coefficient values modmodulus
and the indices modN
.int
size()
Returns the maximum number of coefficients the polynomial can havebyte[]
toBinary()
Encodes the polynomial to a byte array writingBITS_PER_INDEX
bits for each coefficient.Returns a polynomial that is equal to this polynomial (in the sense thatPolynomial.mult(IntegerPolynomial, int)
returns equalIntegerPolynomial
s).
-
Constructor Details
-
SparseTernaryPolynomial
Constructs aDenseTernaryPolynomial
from aIntegerPolynomial
. The two polynomials are independent of each other.- Parameters:
intPoly
- the original polynomial
-
SparseTernaryPolynomial
public SparseTernaryPolynomial(int[] coeffs) Constructs a newSparseTernaryPolynomial
with a given set of coefficients.- Parameters:
coeffs
- the coefficients
-
-
Method Details
-
fromBinary
public static SparseTernaryPolynomial fromBinary(InputStream is, int N, int numOnes, int numNegOnes) throws IOException Decodes a byte array encoded withtoBinary()
to a ploynomial.- Parameters:
is
- an input stream containing an encoded polynomialN
- number of coefficients including zerosnumOnes
- number of coefficients equal to 1numNegOnes
- number of coefficients equal to -1- Returns:
- the decoded polynomial
- Throws:
IOException
-
generateRandom
public static SparseTernaryPolynomial generateRandom(int N, int numOnes, int numNegOnes, SecureRandom random) Generates a random polynomial withnumOnes
coefficients equal to 1,numNegOnes
coefficients equal to -1, and the rest equal to 0.- Parameters:
N
- number of coefficientsnumOnes
- number of 1'snumNegOnes
- number of -1's
-
mult
Description copied from interface:TernaryPolynomial
Multiplies the polynomial by anIntegerPolynomial
, taking the indices mod N- Specified by:
mult
in interfacePolynomial
- Specified by:
mult
in interfaceTernaryPolynomial
- Parameters:
poly2
- a polynomial- Returns:
- the product of the two polynomials
-
mult
Description copied from interface:Polynomial
Multiplies the polynomial by anIntegerPolynomial
, taking the coefficient values modmodulus
and the indices modN
.- Specified by:
mult
in interfacePolynomial
- Parameters:
poly2
- a polynomialmodulus
- a modulus to apply- 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
-
getOnes
public int[] getOnes()- Specified by:
getOnes
in interfaceTernaryPolynomial
-
getNegOnes
public int[] getNegOnes()- Specified by:
getNegOnes
in interfaceTernaryPolynomial
-
toBinary
public byte[] toBinary()Encodes the polynomial to a byte array writingBITS_PER_INDEX
bits for each coefficient.- Returns:
- the encoded polynomial
-
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
.
-
size
public int size()Description copied from interface:TernaryPolynomial
Returns the maximum number of coefficients the polynomial can have- Specified by:
size
in interfaceTernaryPolynomial
-
clear
public void clear()- Specified by:
clear
in interfaceTernaryPolynomial
-
hashCode
public int hashCode() -
equals
-