Class SparseTernaryPolynomial
- java.lang.Object
-
- org.bouncycastle.pqc.math.ntru.polynomial.SparseTernaryPolynomial
-
- All Implemented Interfaces:
Polynomial,TernaryPolynomial
public class SparseTernaryPolynomial extends java.lang.Object implements TernaryPolynomial
ATernaryPolynomialwith a "low" number of nonzero coefficients.
-
-
Constructor Summary
Constructors Constructor Description SparseTernaryPolynomial(int[] coeffs)Constructs a newSparseTernaryPolynomialwith a given set of coefficients.SparseTernaryPolynomial(IntegerPolynomial intPoly)Constructs aDenseTernaryPolynomialfrom aIntegerPolynomial.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()booleanequals(java.lang.Object obj)static SparseTernaryPolynomialfromBinary(java.io.InputStream is, int N, int numOnes, int numNegOnes)Decodes a byte array encoded withtoBinary()to a ploynomial.static SparseTernaryPolynomialgenerateRandom(int N, int numOnes, int numNegOnes, java.security.SecureRandom random)Generates a random polynomial withnumOnescoefficients equal to 1,numNegOnescoefficients equal to -1, and the rest equal to 0.int[]getNegOnes()int[]getOnes()inthashCode()BigIntPolynomialmult(BigIntPolynomial poly2)Multiplies the polynomial by aBigIntPolynomial, taking the indices mod N.IntegerPolynomialmult(IntegerPolynomial poly2)Multiplies the polynomial by anIntegerPolynomial, taking the indices mod NIntegerPolynomialmult(IntegerPolynomial poly2, int modulus)Multiplies the polynomial by anIntegerPolynomial, taking the coefficient values modmodulusand the indices modN.intsize()Returns the maximum number of coefficients the polynomial can havebyte[]toBinary()Encodes the polynomial to a byte array writingBITS_PER_INDEXbits for each coefficient.IntegerPolynomialtoIntegerPolynomial()Returns a polynomial that is equal to this polynomial (in the sense thatPolynomial.mult(IntegerPolynomial, int)returns equalIntegerPolynomials).
-
-
-
Constructor Detail
-
SparseTernaryPolynomial
public SparseTernaryPolynomial(IntegerPolynomial intPoly)
Constructs aDenseTernaryPolynomialfrom aIntegerPolynomial. The two polynomials are independent of each other.- Parameters:
intPoly- the original polynomial
-
SparseTernaryPolynomial
public SparseTernaryPolynomial(int[] coeffs)
Constructs a newSparseTernaryPolynomialwith a given set of coefficients.- Parameters:
coeffs- the coefficients
-
-
Method Detail
-
fromBinary
public static SparseTernaryPolynomial fromBinary(java.io.InputStream is, int N, int numOnes, int numNegOnes) throws java.io.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:
java.io.IOException
-
generateRandom
public static SparseTernaryPolynomial generateRandom(int N, int numOnes, int numNegOnes, java.security.SecureRandom random)
Generates a random polynomial withnumOnescoefficients equal to 1,numNegOnescoefficients equal to -1, and the rest equal to 0.- Parameters:
N- number of coefficientsnumOnes- number of 1'snumNegOnes- number of -1's
-
mult
public IntegerPolynomial mult(IntegerPolynomial poly2)
Description copied from interface:TernaryPolynomialMultiplies the polynomial by anIntegerPolynomial, taking the indices mod N- Specified by:
multin interfacePolynomial- Specified by:
multin interfaceTernaryPolynomial- Parameters:
poly2- a polynomial- Returns:
- the product of the two polynomials
-
mult
public IntegerPolynomial mult(IntegerPolynomial poly2, int modulus)
Description copied from interface:PolynomialMultiplies the polynomial by anIntegerPolynomial, taking the coefficient values modmodulusand the indices modN.- Specified by:
multin interfacePolynomial- Parameters:
poly2- a polynomialmodulus- a modulus to apply- 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
-
getOnes
public int[] getOnes()
- Specified by:
getOnesin interfaceTernaryPolynomial
-
getNegOnes
public int[] getNegOnes()
- Specified by:
getNegOnesin interfaceTernaryPolynomial
-
toBinary
public byte[] toBinary()
Encodes the polynomial to a byte array writingBITS_PER_INDEXbits for each coefficient.- Returns:
- the encoded polynomial
-
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.
-
size
public int size()
Description copied from interface:TernaryPolynomialReturns the maximum number of coefficients the polynomial can have- Specified by:
sizein interfaceTernaryPolynomial
-
clear
public void clear()
- Specified by:
clearin interfaceTernaryPolynomial
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equalsin classjava.lang.Object
-
-