Exemplo n.º 1
0
        /// <summary>
        /// Multiply by another Modulus
        /// </summary>
        /// <param name="other">Other.</param>
        internal ModulusPoly multiply(ModulusPoly other)
        {
            if (!field.Equals(other.field))
            {
                throw new ArgumentException("ModulusPolys do not have same ModulusGF field");
            }
            if (isZero || other.isZero)
            {
                return(field.Zero);
            }
            int[] aCoefficients = this.coefficients;
            int   aLength       = aCoefficients.Length;

            int[] bCoefficients = other.coefficients;
            int   bLength       = bCoefficients.Length;

            int[] product = new int[aLength + bLength - 1];
            for (int i = 0; i < aLength; i++)
            {
                int aCoeff = aCoefficients[i];
                for (int j = 0; j < bLength; j++)
                {
                    product[i + j] = field.add(product[i + j], field.multiply(aCoeff, bCoefficients[j]));
                }
            }
            return(new ModulusPoly(field, product));
        }
Exemplo n.º 2
0
 /// <summary>
 /// Subtract another Modulus
 /// </summary>
 /// <param name="other">Other.</param>
 internal ModulusPoly subtract(ModulusPoly other)
 {
     if (!field.Equals(other.field))
     {
         throw new ArgumentException("ModulusPolys do not have same ModulusGF field");
     }
     if (other.isZero)
     {
         return(this);
     }
     return(add(other.getNegative()));
 }
Exemplo n.º 3
0
        public ModulusGF(int modulus, int generator)
        {
            this.modulus = modulus;
            expTable     = new int[modulus];
            logTable     = new int[modulus];
            int x = 1;

            for (int i = 0; i < modulus; i++)
            {
                expTable[i] = x;
                x           = (x * generator) % modulus;
            }
            for (int i = 0; i < modulus - 1; i++)
            {
                logTable[expTable[i]] = i;
            }
            // logTable[0] == 0 but this should never be used
            Zero = new ModulusPoly(this, new int[] { 0 });
            One  = new ModulusPoly(this, new int[] { 1 });
        }
Exemplo n.º 4
0
        /// <summary>
        /// Adds another Modulus
        /// </summary>
        /// <param name="other">Other.</param>
        internal ModulusPoly add(ModulusPoly other)
        {
            if (!field.Equals(other.field))
            {
                throw new ArgumentException("ModulusPolys do not have same ModulusGF field");
            }
            if (isZero)
            {
                return(other);
            }
            if (other.isZero)
            {
                return(this);
            }

            int[] smallerCoefficients = this.coefficients;
            int[] largerCoefficients  = other.coefficients;
            if (smallerCoefficients.Length > largerCoefficients.Length)
            {
                int[] temp = smallerCoefficients;
                smallerCoefficients = largerCoefficients;
                largerCoefficients  = temp;
            }
            int[] sumDiff    = new int[largerCoefficients.Length];
            int   lengthDiff = largerCoefficients.Length - smallerCoefficients.Length;

            // Copy high-order terms only found in higher-degree polynomial's coefficients
            Array.Copy(largerCoefficients, 0, sumDiff, 0, lengthDiff);

            for (int i = lengthDiff; i < largerCoefficients.Length; i++)
            {
                sumDiff[i] = field.add(smallerCoefficients[i - lengthDiff], largerCoefficients[i]);
            }

            return(new ModulusPoly(field, sumDiff));
        }