public void testEncodeDecodeModQ() { int[] coeffs = PolynomialGenerator.GenerateRandom(1000, 2048).coeffs; byte[] data = ArrayEncoder.EncodeModQ(coeffs, 2048); int[] coeffs2 = ArrayEncoder.DecodeModQ(data, 1000, 2048); Assert.True(coeffs.SequenceEqual(coeffs2)); }
public void testEncodeDecodeMod3Tight() { SecureRandom random = new SecureRandom(); int[] coeffs = DenseTernaryPolynomial.GenerateRandom(1000, random).coeffs; byte[] data = ArrayEncoder.EncodeMod3Tight(coeffs); int[] coeffs2 = ArrayEncoder.DecodeMod3Tight(data, 1000); Assert.True(coeffs.SequenceEqual(coeffs2)); }
public void testEncodeDecodeMod3Sves() { Random rng = new Random(); byte[] data = new byte[180]; rng.NextBytes(data); int[] coeffs = ArrayEncoder.DecodeMod3Sves(data, 960); byte[] data2 = ArrayEncoder.EncodeMod3Sves(coeffs); Assert.True(data.SequenceEqual(data2)); }
/// <summary> /// Encodes the polynomial to a byte array writing <c>BITS_PER_INDEX</c> bits for each coefficient /// </summary> /// /// <returns>The encoded polynomial</returns> public byte[] ToBinary() { int maxIndex = 1 << BITS_PER_INDEX; byte[] bin1 = ArrayEncoder.EncodeModQ(m_ones, maxIndex);//13l - (9,2048) byte[] bin2 = ArrayEncoder.EncodeModQ(m_negOnes, maxIndex); byte[] bin = ArrayUtils.Concat(ArrayEncoder.ToByteArray(m_ones.Length), ArrayEncoder.ToByteArray(m_negOnes.Length), bin1, bin2); return(bin); }
private void EncodeDecodeMod3Tight() { int[] coeffs = PolynomialGeneratorForTesting.generateRandom(1000).Coeffs; byte[] data = ArrayEncoder.EncodeMod3Tight(coeffs); int[] coeffs2 = ArrayEncoder.DecodeMod3Tight(data, 1000); if (!Compare.AreEqual(coeffs, coeffs2)) { throw new Exception("ArrayEncoder EncodeDecodeMod3Tight test failed!"); } }
private void EncodeDecodeModQ() { int[] coeffs = PolynomialGeneratorForTesting.generateRandomPositive(1000, 2048).Coeffs; byte[] data = ArrayEncoder.EncodeModQ(coeffs, 2048); int[] coeffs2 = ArrayEncoder.DecodeModQ(data, 1000, 2048); if (!Compare.AreEqual(coeffs, coeffs2)) { throw new Exception("ArrayEncoder EncodeDecodeModQ test failed!"); } }
/** * Encodes the polynomial to a byte array writing <code>BITS_PER_INDEX</code> bits for each coefficient. * * @return the encoded polynomial */ public byte[] ToBinary() { int maxIndex = 1 << BITS_PER_INDEX; byte[] bin1 = ArrayEncoder.EncodeModQ(Ones, maxIndex); byte[] bin2 = ArrayEncoder.EncodeModQ(NegOnes, maxIndex); byte[] bin = new byte[bin1.Length + bin2.Length]; Array.Copy(bin1, bin, bin1.Length); Array.Copy(bin2, 0, bin, bin1.Length, bin2.Length); return(bin); }
/** * Decodes a byte array encoded with {@link #toBinary()} to a ploynomial. * * @param is an input stream containing an encoded polynomial * @param N number of coefficients including zeros * @param numOnes number of coefficients equal to 1 * @param numNegOnes number of coefficients equal to -1 * @return the decoded polynomial * @throws IOException */ public static SparseTernaryPolynomial FromBinary(Stream stream, int N, int numOnes, int numNegOnes) { int maxIndex = 1 << BITS_PER_INDEX; int bitsPerIndex = 32 - Integers.NumberOfLeadingZeros(maxIndex - 1); int data1Len = (numOnes * bitsPerIndex + 7) / 8; byte[] data1 = Util.Util.ReadFullLength(stream, data1Len); int[] ones = ArrayEncoder.DecodeModQ(data1, numOnes, maxIndex); int data2Len = (numNegOnes * bitsPerIndex + 7) / 8; byte[] data2 = Util.Util.ReadFullLength(stream, data2Len); int[] negOnes = ArrayEncoder.DecodeModQ(data2, numNegOnes, maxIndex); return(new SparseTernaryPolynomial(N, ones, negOnes)); }
/// <summary> /// Converts the Private key to an encoded byte array /// </summary> /// /// <returns>The encoded NTRUPrivateKey</returns> public byte[] ToBytes() { int flags = (m_sparse ? 1 : 0) + (m_fastFp ? 2 : 0) + (PolyType == TernaryPolynomialType.PRODUCT ? 4 : 0); byte[] flagsByte = new byte[] { (byte)flags }; byte[] tBin; if (T.GetType().Equals(typeof(ProductFormPolynomial))) { tBin = ((ProductFormPolynomial)T).ToBinary(); } else { tBin = T.ToIntegerPolynomial().ToBinary3Tight(); } return(ArrayUtils.Concat(ArrayEncoder.ToByteArray(N), ArrayEncoder.ToByteArray(Q), flagsByte, tBin)); }
/// <summary> /// Decodes a polynomial encoded with ToBinary() /// </summary> /// /// <param name="InputStream">An input stream containing an encoded polynomial</param> /// <param name="N">Number of coefficients in the polynomial</param> /// /// <returns>The decoded polynomial</returns> public static SparseTernaryPolynomial FromBinary(MemoryStream InputStream, int N) { BinaryReader br = new BinaryReader(InputStream); // number of coefficients equal to 1 int numOnes = IntUtils.ReadShort(InputStream); // number of coefficients equal to -1 int numNegOnes = IntUtils.ReadShort(InputStream); int maxIndex = 1 << BITS_PER_INDEX; int bitsPerIndex = 32 - IntUtils.NumberOfLeadingZeros(maxIndex - 1); int data1Len = (numOnes * bitsPerIndex + 7) / 8; byte[] data1 = ArrayEncoder.ReadFullLength(InputStream, data1Len); int[] ones = ArrayEncoder.DecodeModQ(data1, numOnes, maxIndex); int data2Len = (numNegOnes * bitsPerIndex + 7) / 8; byte[] data2 = ArrayEncoder.ReadFullLength(InputStream, data2Len); int[] negOnes = ArrayEncoder.DecodeModQ(data2, numNegOnes, maxIndex); return(new SparseTernaryPolynomial(N, ones, negOnes)); }
private void EncodeDecodeMod3Sves() { Random rng = new Random(); bool[] skip = new bool[] { true, false }; foreach (bool skipFirst in skip) { for (int i = 0; i < 10; i++) { int N = (rng.Next(1000) + 100) * 16; byte[] data = new byte[N * 3 / 16]; rng.NextBytes(data); data[data.Length - 1] = 0; int[] coeffs = ArrayEncoder.DecodeMod3Sves(data, N, skipFirst); byte[] data2 = ArrayEncoder.EncodeMod3Sves(coeffs, skipFirst); if (!Compare.AreEqual(data, data2)) { throw new Exception("ArrayEncoder EncodeDecodeMod3Sves test failed!"); } } } }
/** * Encodes a polynomial whose coefficients are between 0 and q, to binary. q must be a power of 2. * * @param q * @return the encoded polynomial */ public byte[] ToBinary(int q) { return(ArrayEncoder.EncodeModQ(coeffs, q)); }
/** * Decodes a byte array to a polynomial with <code>N</code> ternary coefficients<br> * Ignores any excess bytes. * * @param data an encoded ternary polynomial * @param N number of coefficients * @return the decoded polynomial */ public static IntegerPolynomial FromBinary3Sves(byte[] data, int N) { return(new IntegerPolynomial(ArrayEncoder.DecodeMod3Sves(data, N))); }
/// <summary> /// Converts the Public key to an encoded byte array /// </summary> /// /// <returns>The encoded NTRUPublicKey</returns> public byte[] ToBytes() { return(ArrayUtils.Concat(ArrayEncoder.ToByteArray(N), ArrayEncoder.ToByteArray(Q), H.ToBinary(Q))); }
/** * Converts a byte array produced by {@link #toBinary3Tight()} to a polynomial. * * @param b a byte array * @param N number of coefficients * @return the decoded polynomial */ public static IntegerPolynomial FromBinary3Tight(byte[] b, int N) { return(new IntegerPolynomial(ArrayEncoder.DecodeMod3Tight(b, N))); }
/** * Encodes a polynomial with ternary coefficients to binary. * <code>coeffs[2*i]</code> and <code>coeffs[2*i+1]</code> must not both equal -1 for any integer <code>i</code>, * so this method is only safe to use with polynomials produced by <code>fromBinary3Sves()</code>. * * @return the encoded polynomial */ public byte[] ToBinary3Sves() { return(ArrayEncoder.EncodeMod3Sves(coeffs)); }
/** * Returns a polynomial with N coefficients between <code>0</code> and <code>q-1</code>.<br> * <code>q</code> must be a power of 2.<br> * Ignores any excess bytes. * * @param is an encoded ternary polynomial * @param N number of coefficients * @param q * @return the decoded polynomial */ public static IntegerPolynomial FromBinary(Stream stream, int N, int q) { return(new IntegerPolynomial(ArrayEncoder.DecodeModQ(stream, N, q))); }
/** * Returns a polynomial with N coefficients between <code>0</code> and <code>q-1</code>.<br> * <code>q</code> must be a power of 2.<br> * Ignores any excess bytes. * * @param data an encoded ternary polynomial * @param N number of coefficients * @param q * @return the decoded polynomial */ public static IntegerPolynomial FromBinary(byte[] data, int N, int q) { return(new IntegerPolynomial(ArrayEncoder.DecodeModQ(data, N, q))); }
/** * Reads data produced by {@link #toBinary3Tight()} from an input stream and converts it to a polynomial. * * @param is an input stream * @param N number of coefficients * @return the decoded polynomial */ public static IntegerPolynomial FromBinary3Tight(Stream stream, int N) { return(new IntegerPolynomial(ArrayEncoder.DecodeMod3Tight(stream, N))); }