Esempio n. 1
0
        /// <summary>
        /// Construct a random GF2Vector of the given length with the specified number of non-zero coefficients
        /// </summary>
        ///
        /// <param name="Length">The length of the vector</param>
        /// <param name="T">The number of non-zero coefficients</param>
        /// <param name="SecRnd">The source of randomness</param>
        public GF2Vector(int Length, int T, IRandom SecRnd)
        {
            if (T > Length)
            {
                throw new ArithmeticException("The hamming weight is greater than the length of vector.");
            }

            this.Length = Length;

            int size = (Length + 31) >> 5;

            m_elements = new int[size];

            int[] help = new int[Length];
            for (int i = 0; i < Length; i++)
            {
                help[i] = i;
            }

            int m = Length;

            for (int i = 0; i < T; i++)
            {
                int j = RandomDegree.NextInt(SecRnd, m);
                SetBit(help[j]);
                m--;
                help[j] = help[m];
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Create a random permutation of the given size
        /// </summary>
        ///
        /// <param name="N">The size of the permutation</param>
        /// <param name="SecRnd">The source of randomness</param>
        public Permutation(int N, IRandom SecRnd)
        {
            if (N <= 0)
            {
                throw new ArgumentException("Permutation: Invalid length!");
            }

            m_perm = new int[N];
            int[] help = new int[N];

            for (int i = 0; i < N; i++)
            {
                help[i] = i;
            }

            int k = N;

            for (int j = 0; j < N; j++)
            {
                int i = RandomDegree.NextInt(SecRnd, k);
                k--;
                m_perm[j] = help[i];
                help[i]   = help[k];
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Create a random non-zero field element
        /// </summary>
        ///
        /// <param name="SecRnd">The IRandom instance</param>
        ///
        /// <returns>A random non zero element</returns>
        public int GetRandomNonZeroElement(IRandom SecRnd)
        {
            int controltime = 1 << 20;
            int count       = 0;
            int result      = RandomDegree.NextInt(SecRnd, 1 << m_degree);

            while ((result == 0) && (count < controltime))
            {
                result = RandomDegree.NextInt(SecRnd, 1 << m_degree);
                count++;
            }

            if (count == controltime)
            {
                result = 1;
            }

            return(result);
        }
        /// <summary>
        /// Create an irreducible polynomial with the given degree over the field <c>GF(2^m)</c>
        /// </summary>
        ///
        /// <param name="Degree">The polynomial degree</param>
        /// <param name="SecRnd">The source of randomness</param>
        ///
        /// <returns>he generated irreducible polynomial</returns>
        private int[] CreateRandomIrreduciblePolynomial(int Degree, IRandom SecRnd)
        {
            int[] resCoeff = new int[Degree + 1];
            int[] resTemp  = new int[Degree + 1];

            resCoeff[Degree] = 1;
            resCoeff[0]      = _field.GetRandomNonZeroElement(SecRnd);

            if (ParallelUtils.IsParallel)
            {
                Parallel.For(0, Degree, i =>
                             resCoeff[i] = GetRandomElement(SecRnd, _field));
            }
            else
            {
                for (int i = 1; i < Degree; i++)
                {
                    resCoeff[i] = GetRandomElement(SecRnd, _field);
                }
            }

            while (!IsIrreducible(resCoeff, _field))
            {
                int n = RandomDegree.NextInt(SecRnd, Degree);

                if (n != 0)
                {
                    resCoeff[n] = GetRandomElement(SecRnd, _field);
                }
                else
                {
                    resCoeff[0] = _field.GetRandomNonZeroElement(SecRnd);
                }
            }

            return(resCoeff);
        }
Esempio n. 5
0
 /// <summary>
 /// Create a random field element using PRNG
 /// </summary>
 ///
 /// <param name="SecRnd">The IRandom instance</param>
 ///
 /// <returns>A random element</returns>
 public int GetRandomElement(IRandom SecRnd)
 {
     return(RandomDegree.NextInt(SecRnd, 1 << m_degree));
 }
 /// <summary>
 /// Get a randome element over degree Gf2
 /// </summary>
 ///
 /// <param name="SecRnd">The source of randomness</param>
 /// <param name="GFM">The Gf2 field</param>
 ///
 /// <returns>A random element</returns>
 private static int GetRandomElement(IRandom SecRnd, GF2mField GFM)
 {
     return(RandomDegree.NextInt(SecRnd, 1 << GFM.Degree));
 }