예제 #1
0
        public static FiniteField operator *(FiniteField a, FiniteField b)
        {
            if (a.Prime != b.Prime)
            {
                throw new ArgumentException("The prime number must be the same");
            }

            var value = ModuloOperation.Run(a.Value * b.Value, a.Prime);

            return(new FiniteField(value, a.Prime));
        }
예제 #2
0
        public static FiniteField operator /(FiniteField a, FiniteField b)
        {
            if (a.Prime != b.Prime)
            {
                throw new ArgumentException("The prime number must be the same");
            }

            //Converts the division into multiplication using Fermat's little theorem
            //prime = 19 ===>  2/7 = 2*7 pow (19 – 2)
            var expNumber = a.Prime - 2;

            long expResult = a.Value;

            for (long i = 0; i < expNumber; i++)
            {
                expResult = ModuloOperation.Run(expResult * b.Value, a.Prime);
            }

            return(new FiniteField(expResult, a.Prime));
        }
예제 #3
0
        public static FiniteField Pow(FiniteField a, long pow)
        {
            long value = a.Value;

            long adaptedPow = pow;

            //Convert negative pow into positive using Fermat's little theorem
            if (pow < 0)
            {
                while (adaptedPow < 0)
                {
                    adaptedPow += a.Prime - 1;
                }
                adaptedPow -= 1;
            }

            for (long i = 0; i < adaptedPow; i++)
            {
                value = ModuloOperation.Run(value * a.Value, a.Prime);
            }

            return(new FiniteField(value, a.Prime));
        }