private void Setup() { _seed = new byte[100]; new Random().NextBytes(_seed); _parameters = NTRUParamSets.APR2011743; _gen = new IndexGenerator(_seed, _parameters); _indices = initIndices(); }
private void BitStringEquals(IndexGenerator.BitString bs, byte[] arr) { if (bs.Bytes.Length < arr.Length) throw new Exception("BitString equality test failed!"); arr = arr.CopyOf(bs.Bytes.Length); if (!Compare.AreEqual(arr, bs.Bytes)) throw new Exception("BitString equality test failed!"); }
private void Repeatability() { _gen = new IndexGenerator(_seed, _parameters); int[] indices2 = initIndices(); if (!Compare.AreEqual(_indices, indices2)) throw new Exception("IndexGenerator repeatability test failed!"); }
/// <summary> /// Deterministically generates a blinding polynomial from a seed and a message representative /// </summary> /// /// <param name="Seed">The seed value</param> /// /// <returns>A blinding polynomial</returns> private IPolynomial GenerateBlindingPoly(byte[] Seed) { int N = _encParams.N; IndexGenerator ig = new IndexGenerator(Seed, _encParams); if (_encParams.PolyType == TernaryPolynomialType.PRODUCT) //.8, .6 { SparseTernaryPolynomial r1 = SparseTernaryPolynomial.GenerateBlindingPoly(ig, N, _encParams.DR1); SparseTernaryPolynomial r2 = SparseTernaryPolynomial.GenerateBlindingPoly(ig, N, _encParams.DR2); SparseTernaryPolynomial r3 = SparseTernaryPolynomial.GenerateBlindingPoly(ig, N, _encParams.DR3); return new ProductFormPolynomial(r1, r2, r3); } else { if (_encParams.Sparse) return SparseTernaryPolynomial.GenerateBlindingPoly(ig, N, _encParams.DR); else return DenseTernaryPolynomial.GenerateBlindingPoly(ig, N, _encParams.DR); } }
/// <summary> /// Generates a blinding polynomial using an IndexGenerator /// </summary> /// /// <param name="Ig">An Index Generator</param> /// <param name="N">The number of coefficients</param> /// <param name="Dr">The number of ones / negative ones</param> /// /// <returns>A blinding polynomial</returns> public static SparseTernaryPolynomial GenerateBlindingPoly(IndexGenerator Ig, int N, int Dr) { int[] coeffs = new int[N]; // an IntegerPolynomial-style representation of the new polynomial int[] ones = new int[Dr]; int i = 0; while (i < Dr) { int r = Ig.NextIndex(); if (coeffs[r] == 0) { ones[i] = r; coeffs[r] = 1; i++; } } int[] negOnes = new int[Dr]; i = 0; while (i < Dr) { int r = Ig.NextIndex(); if (coeffs[r] == 0) { negOnes[i] = r; coeffs[r] = -1; i++; } } return new SparseTernaryPolynomial(N, ones, negOnes); }
/// <summary> /// Generates a blinding polynomial using an IndexGenerator /// </summary> /// /// <param name="Ig">An Index Generator</param> /// <param name="N">The number of coefficients</param> /// <param name="Dr">The number of ones / negative ones</param> /// /// <returns>A blinding polynomial</returns> public static DenseTernaryPolynomial GenerateBlindingPoly(IndexGenerator Ig, int N, int Dr) { return new DenseTernaryPolynomial(GenerateBlindingCoeffs(Ig, N, Dr)); }
/// <summary> /// Generates an <c>int</c> array containing <c>dr</c> elements equal to <c>1</c> /// and <c>dr</c> elements equal to <c>-1</c> using an index generator. /// </summary> /// /// <param name="Ig">An Index Generator</param> /// <param name="N">The number of coefficients</param> /// <param name="Dr">The number of ones / negative ones</param> /// /// <returns>An array containing numbers between <c>-1</c> and <c>1</c></returns> private static int[] GenerateBlindingCoeffs(IndexGenerator Ig, int N, int Dr) { int[] r = new int[N]; for (int coeff = -1; coeff <= 1; coeff += 2) { int t = 0; while (t < Dr) { int i = Ig.NextIndex(); if (r[i] == 0) { r[i] = coeff; t++; } } } return r; }