Beispiel #1
0
        /// <summary>
        /// Divide by another modulus
        /// </summary>
        /// <param name="other">Other.</param>
        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 DivideByZeroException();
            }

            ModulusPoly quotient  = field.Zero;
            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 });
        }
Beispiel #2
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));
        }
Beispiel #3
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()));
 }
Beispiel #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 });
 }
        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 });
        }
Beispiel #6
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));
        }
Beispiel #7
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 };
      }
Beispiel #8
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);
 }
Beispiel #9
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());
 }
Beispiel #10
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);
      }