Ejemplo n.º 1
0
        /// <summary>
        /// Returns a Vandermonde matrix in the field (each element is modulu prime).
        /// </summary>
        public static ZpMatrix GetShamirRecombineMatrix(int matrixSize, int prime)
        {
            var A = new ZpMatrix(matrixSize, matrixSize, prime);

            if (matrixSize == 1)
            {
                A.data[0][0] = 1;
                return(A);
            }

            for (int i = 0; i < matrixSize; i++)
            {
                A.data[i][0] = 1;
            }

            for (int i = 0; i < matrixSize; i++)
            {
                A.data[i][1] = i + 1;
            }

            for (int i = 0; i < matrixSize; i++)
            {
                for (int j = 2; j < matrixSize; j++)
                {
                    A.data[i][j] = Zp.Modulo(A.data[i][j - 1] * A.data[i][1], prime);
                }
            }
            return(A);
        }
Ejemplo n.º 2
0
        public static ZpMatrix GetVandermondeMatrix(int rowNum, int colNum, long prime)
        {
            var A = new ZpMatrix(rowNum, colNum, prime);

            for (int j = 0; j < colNum; j++)
            {
                A.data[0][j] = 1;
            }

            if (rowNum == 1)
            {
                return(A);
            }

            for (int j = 0; j < colNum; j++)
            {
                A.data[1][j] = j + 1;
            }

            for (int j = 0; j < colNum; j++)
            {
                for (int i = 2; i < rowNum; i++)
                {
                    A.data[i][j] = Zp.Modulo(A.data[i - 1][j] * A.data[1][j], prime);
                }
            }
            return(A);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Creates and return a random rowNum-by-colNum  matrix with values between '0' and 'prime-1'.
        /// </summary>
        public static ZpMatrix GetRandomMatrix(int rowNum, int colNum, long prime)
        {
            var A = new ZpMatrix(rowNum, colNum, prime);

            for (int i = 0; i < rowNum; i++)
            {
                for (int j = 0; j < colNum; j++)
                {
                    A.data[i][j] = Zp.Modulo((long)(StaticRandom.NextDouble() * (prime)), prime);
                }
            }
            return(A);
        }
Ejemplo n.º 4
0
        public static ZpMatrix GetPrimitiveVandermondeMatrix(int rowNum, int colNum, long prime)
        {
            int primitive = NumTheoryUtils.GetFieldMinimumPrimitive(prime);

            if (primitive == 0)
            {
                throw new ArgumentException("Cannot create a primitive Vandermonde matrix from a non-prime number. ");
            }

            var A = new ZpMatrix(rowNum, colNum, prime);

            for (int j = 0; j < colNum; j++)
            {
                A.data[0][j] = 1;
            }

            if (rowNum == 1)
            {
                return(A);
            }

            /*  This variable represents  primitive^j  for the j-th player*/
            long primitive_j = 1;

            for (int j = 0; j < colNum; j++)
            {
                A.data[1][j] = primitive_j;
                primitive_j  = Zp.Modulo(primitive_j * primitive, prime);
            }

            for (int j = 0; j < colNum; j++)
            {
                for (int i = 2; i < rowNum; i++)
                {
                    A.data[i][j] = Zp.Modulo(A.data[i - 1][j] * A.data[1][j], prime);
                }
            }

            return(A);
        }
Ejemplo n.º 5
0
        public static ZpMatrix GetVandermondeMatrix(int rowNum, IList <Zp> values, int prime)
        {
            int colNum = values.Count;
            var A      = new ZpMatrix(rowNum, colNum, prime);

            for (int j = 0; j < colNum; j++)
            {
                A.data[0][j] = 1;
            }

            if (rowNum == 1)
            {
                return(A);
            }

            for (int j = 0; j < colNum; j++)
            {
                for (int i = 1; i < rowNum; i++)
                {
                    A.data[i][j] = Zp.Modulo(A.data[i - 1][j] * values[j].Value, prime);
                }
            }
            return(A);
        }
Ejemplo n.º 6
0
        /* calculate i mod prime */

        private long Modulo(long i)
        {
            return(Zp.Modulo(i, Prime));
        }