Top Description Fields Constructors Methods
org.bouncycastle.pqc.legacy.math.ntru.util

public Class ArrayEncoder

extends Object
Class Inheritance
Imports
java.io.IOException, .InputStream, java.math.BigInteger, org.bouncycastle.util.Arrays

Converts a coefficient array to a compact byte array and vice versa.

Field Summary

Modifier and TypeField and Description
private static final int[]
BIT1_TABLE

Coefficient to bit string conversion table from P1363.1.

private static final int[]
private static final int[]
private static final int[]
COEFF1_TABLE

Bit string to coefficient conversion table from P1363.1.

private static final int[]

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static int[]

Returns:

the decoded coefficients
decodeMod3Sves
(byte[]
an encoded ternary polynomial
data
,
int
number of coefficients
N
)

Decodes a byte array encoded with encodeMod3Sves(int[]) back to an int array with N coefficients between -1 and 1.

public static int[]

Returns:

the decoded array
decodeMod3Tight
(byte[]
a byte array
b
,
int
number of coefficients
N
)

Converts a byte array produced by encodeMod3Tight(int[]) back to an int array.

public static int[]

Returns:

the decoded array
decodeMod3Tight
(InputStream
an input stream containing the data to decode
is
,
int
number of coefficients
N
)

Converts data produced by encodeMod3Tight(int[]) back to an int array.

public static int[]

Returns:

an array containing N coefficients between 0 and q-1
decodeModQ
(byte[]
an encoded ternary polynomial
data
,
int
number of coefficients
N
,
int q)

Decodes a byte array encoded with encodeModQ(int[], int) back to an int array.

public static int[]

Returns:

the decoded polynomial
decodeModQ
(InputStream
an encoded ternary polynomial
is
,
int
number of coefficients
N
,
int q)

Decodes data encoded with encodeModQ(int[], int) back to an int array.

public static byte[]

Returns:

the encoded array
encodeMod3Sves
(int[] arr)

Encodes an int array whose elements are between -1 and 1, to a byte array.

public static byte[]

Returns:

the encoded array
encodeMod3Tight
(int[] intArray)

Encodes an int array whose elements are between -1 and 1, to a byte array.

public static byte[]

Returns:

the encoded array
encodeModQ
(int[]
the input array
a
,
int
the modulus
q
)

Encodes an int array whose elements are between 0 and q, to a byte array leaving no gaps between bits.

private static int
getBit(byte[] arr, int bitIndex)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

BIT1_TABLEback to summary
private static final int[] BIT1_TABLE

Coefficient to bit string conversion table from P1363.1. Also found at https://stackoverflow.com/questions/1562548/how-to-make-a-message-into-a-polynomial

Convert each set of two ternary coefficients to three bits as follows, and concatenate the resulting bit quantities to obtain [the output]:

{-1, -1} -> set "fail" to 1 and set bit string to {1, 1, 1} {-1, 0} -> {1, 1, 0}
{-1, 1} -> {1, 1, 1}
{0, -1} -> {0, 1, 0}
{0, 0} -> {0, 0, 0}
{0, 1} -> {0, 0, 1}
{1, -1} -> {1, 0, 1}
{1, 0} -> {0, 1, 1}
{1, 1} -> {1, 0, 0}
\

BIT2_TABLEback to summary
private static final int[] BIT2_TABLE
BIT3_TABLEback to summary
private static final int[] BIT3_TABLE
COEFF1_TABLEback to summary
private static final int[] COEFF1_TABLE

Bit string to coefficient conversion table from P1363.1. Also found at https://stackoverflow.com/questions/1562548/how-to-make-a-message-into-a-polynomial

Convert each three-bit quantity to two ternary coefficients as follows, and concatenate the resulting ternary quantities to obtain [the output].

{0, 0, 0} -> {0, 0}
{0, 0, 1} -> {0, 1}
{0, 1, 0} -> {0, -1}
{0, 1, 1} -> {1, 0}
{1, 0, 0} -> {1, 1}
{1, 0, 1} -> {1, -1}
{1, 1, 0} -> {-1, 0}
{1, 1, 1} -> {-1, 1}

COEFF2_TABLEback to summary
private static final int[] COEFF2_TABLE

Constructor Detail

ArrayEncoderback to summary
public ArrayEncoder()

Method Detail

decodeMod3Svesback to summary
public static int[] decodeMod3Sves(byte[] data, int N)

Decodes a byte array encoded with encodeMod3Sves(int[]) back to an int array with N coefficients between -1 and 1.
Ignores any excess bytes.
See P1363.1 section 9.2.2.

Parameters
data:byte[]

an encoded ternary polynomial

N:int

number of coefficients

Returns:int[]

the decoded coefficients

decodeMod3Tightback to summary
public static int[] decodeMod3Tight(byte[] b, int N)

Converts a byte array produced by encodeMod3Tight(int[]) back to an int array.

Parameters
b:byte[]

a byte array

N:int

number of coefficients

Returns:int[]

the decoded array

decodeMod3Tightback to summary
public static int[] decodeMod3Tight(InputStream is, int N) throws IOException

Converts data produced by encodeMod3Tight(int[]) back to an int array.

Parameters
is:InputStream

an input stream containing the data to decode

N:int

number of coefficients

Returns:int[]

the decoded array

decodeModQback to summary
public static int[] decodeModQ(byte[] data, int N, int q)

Decodes a byte array encoded with encodeModQ(int[], int) back to an int array.
N is the number of coefficients. q must be a power of 2.
Ignores any excess bytes.

Parameters
data:byte[]

an encoded ternary polynomial

N:int

number of coefficients

Returns:int[]

an array containing N coefficients between 0 and q-1

decodeModQback to summary
public static int[] decodeModQ(InputStream is, int N, int q) throws IOException

Decodes data encoded with encodeModQ(int[], int) back to an int array.
N is the number of coefficients. q must be a power of 2.
Ignores any excess bytes.

Parameters
is:InputStream

an encoded ternary polynomial

N:int

number of coefficients

Returns:int[]

the decoded polynomial

encodeMod3Svesback to summary
public static byte[] encodeMod3Sves(int[] arr)

Encodes an int array whose elements are between -1 and 1, to a byte array. coeffs[2*i] and coeffs[2*i+1] must not both equal -1 for any integer i, so this method is only safe to use with arrays produced by decodeMod3Sves(byte[], int).
See P1363.1 section 9.2.3.

Returns:byte[]

the encoded array

encodeMod3Tightback to summary
public static byte[] encodeMod3Tight(int[] intArray)

Encodes an int array whose elements are between -1 and 1, to a byte array.

Returns:byte[]

the encoded array

encodeModQback to summary
public static byte[] encodeModQ(int[] a, int q)

Encodes an int array whose elements are between 0 and q, to a byte array leaving no gaps between bits.
q must be a power of 2.

Parameters
a:int[]

the input array

q:int

the modulus

Returns:byte[]

the encoded array

getBitback to summary
private static int getBit(byte[] arr, int bitIndex)