Example #1
0
        public PolynomialOverFiniteField(Polynomial[] arr, Polynomial mod, BigInteger fieldChar)
        {
            var polyMath = new PolynomialMath(fieldChar);

            _deg = arr.Length - 1;
            for (int i = _deg; i >= 0; i--)
            {
                if (polyMath.Rem(arr[i], mod) == new Polynomial(new BigInteger[] { 0 }))
                {
                    _deg--;
                }
                else
                {
                    break;
                }
            }

            var length = _deg + 1;

            if (length == 0)
            {
                length++;
            }
            _coefficients = new Polynomial[length];
            for (int i = 0; i <= _deg; i++)
            {
                _coefficients[i] = polyMath.Rem(arr[i], mod);
            }
        }
Example #2
0
        public Polynomial Calculate(Polynomial firstArg, Polynomial secondArg, BigInteger mod)
        {
            if (firstArg.Deg == secondArg.Deg && firstArg.Deg == -1)
            {
                throw new InvalidOperationException();
            }
            if (firstArg.Deg == 0 || secondArg.Deg == 0)
            {
                return(new Polynomial(new BigInteger[] { 1 }));
            }
            if (firstArg.Deg == -1)
            {
                return(secondArg);
            }
            if (secondArg.Deg == -1)
            {
                return(firstArg);
            }
            if (firstArg.Deg < secondArg.Deg || secondArg.Deg == -1)
            {
                var tmp = secondArg;
                secondArg = firstArg;
                firstArg  = tmp;
            }
            var polyMath = new PolynomialMath(mod);

            firstArg  = firstArg.Reduce(mod);
            secondArg = secondArg.Reduce(mod);
            while (secondArg.Deg != -1)
            {
                var tmp = firstArg;
                firstArg  = secondArg;
                secondArg = polyMath.Rem(tmp, secondArg);
            }
            return(polyMath.Normalize(firstArg));
        }