Пример #1
0
        internal ModulusPoly[] divide(ModulusPoly other)
        {
            if (!field.Equals(other.field))
            {
                throw new ArgumentException("ModulusPolys do not have same ModulusGF field");
            }
            if (other.isZero)
            {
                throw new ArgumentException("Divide by 0");
            }

            ModulusPoly quotient  = field.getZero();
            ModulusPoly remainder = this;

            int denominatorLeadingTerm        = other.getCoefficient(other.Degree);
            int inverseDenominatorLeadingTerm = field.inverse(denominatorLeadingTerm);

            while (remainder.Degree >= other.Degree && !remainder.isZero)
            {
                int         degreeDifference  = remainder.Degree - other.Degree;
                int         scale             = field.multiply(remainder.getCoefficient(remainder.Degree), inverseDenominatorLeadingTerm);
                ModulusPoly term              = other.multiplyByMonomial(degreeDifference, scale);
                ModulusPoly iterationQuotient = field.buildMonomial(degreeDifference, scale);
                quotient  = quotient.add(iterationQuotient);
                remainder = remainder.subtract(term);
            }

            return(new ModulusPoly[] { quotient, remainder });
        }
Пример #2
0
        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.getZero());
            }
            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));
        }
Пример #3
0
 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.negative()));
 }
Пример #4
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 });
        }
Пример #5
0
        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));
        }