Class ArrayEncoder
java.lang.Object
org.bouncycastle.pqc.legacy.math.ntru.util.ArrayEncoder
Converts a coefficient array to a compact byte array and vice versa.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic int[]
decodeMod3Sves
(byte[] data, int N) Decodes abyte
array encoded withencodeMod3Sves(int[])
back to anint
array withN
coefficients between-1
and1
.
Ignores any excess bytes.
See P1363.1 section 9.2.2.static int[]
decodeMod3Tight
(byte[] b, int N) Converts a byte array produced byencodeMod3Tight(int[])
back to anint
array.static int[]
decodeMod3Tight
(InputStream is, int N) Converts data produced byencodeMod3Tight(int[])
back to anint
array.static int[]
decodeModQ
(byte[] data, int N, int q) Decodes abyte
array encoded withencodeModQ(int[], int)
back to anint
array.
N
is the number of coefficients.static int[]
decodeModQ
(InputStream is, int N, int q) Decodes data encoded withencodeModQ(int[], int)
back to anint
array.
N
is the number of coefficients.static byte[]
encodeMod3Sves
(int[] arr) Encodes anint
array whose elements are between-1
and1
, to a byte array.static byte[]
encodeMod3Tight
(int[] intArray) Encodes anint
array whose elements are between-1
and1
, to a byte array.static byte[]
encodeModQ
(int[] a, int q) Encodes an int array whose elements are between 0 andq
, to a byte array leaving no gaps between bits.
q
must be a power of 2.
-
Constructor Details
-
ArrayEncoder
public ArrayEncoder()
-
-
Method Details
-
encodeModQ
public static byte[] encodeModQ(int[] a, int q) Encodes an int array whose elements are between 0 andq
, to a byte array leaving no gaps between bits.
q
must be a power of 2.- Parameters:
a
- the input arrayq
- the modulus- Returns:
- the encoded array
-
decodeModQ
public static int[] decodeModQ(byte[] data, int N, int q) Decodes abyte
array encoded withencodeModQ(int[], int)
back to anint
array.
N
is the number of coefficients.q
must be a power of2
.
Ignores any excess bytes.- Parameters:
data
- an encoded ternary polynomialN
- number of coefficientsq
-- Returns:
- an array containing
N
coefficients between0
andq-1
-
decodeModQ
Decodes data encoded withencodeModQ(int[], int)
back to anint
array.
N
is the number of coefficients.q
must be a power of2
.
Ignores any excess bytes.- Parameters:
is
- an encoded ternary polynomialN
- number of coefficientsq
-- Returns:
- the decoded polynomial
- Throws:
IOException
-
decodeMod3Sves
public static int[] decodeMod3Sves(byte[] data, int N) Decodes abyte
array encoded withencodeMod3Sves(int[])
back to anint
array withN
coefficients between-1
and1
.
Ignores any excess bytes.
See P1363.1 section 9.2.2.- Parameters:
data
- an encoded ternary polynomialN
- number of coefficients- Returns:
- the decoded coefficients
-
encodeMod3Sves
public static byte[] encodeMod3Sves(int[] arr) Encodes anint
array whose elements are between-1
and1
, to a byte array.coeffs[2*i]
andcoeffs[2*i+1]
must not both equal -1 for any integeri
, so this method is only safe to use with arrays produced bydecodeMod3Sves(byte[], int)
.
See P1363.1 section 9.2.3.- Parameters:
arr
-- Returns:
- the encoded array
-
encodeMod3Tight
public static byte[] encodeMod3Tight(int[] intArray) Encodes anint
array whose elements are between-1
and1
, to a byte array.- Returns:
- the encoded array
-
decodeMod3Tight
public static int[] decodeMod3Tight(byte[] b, int N) Converts a byte array produced byencodeMod3Tight(int[])
back to anint
array.- Parameters:
b
- a byte arrayN
- number of coefficients- Returns:
- the decoded array
-
decodeMod3Tight
Converts data produced byencodeMod3Tight(int[])
back to anint
array.- Parameters:
is
- an input stream containing the data to decodeN
- number of coefficients- Returns:
- the decoded array
- Throws:
IOException
-