コード例 #1
0
        //[TestMethod]
        public void SmallDegreePolyGF2PrimesTest()
        {
            var last = 3ul;
            int len  = 0;

            foreach (var p in SmallDegreePolyGF2.Primes().Skip(2))
            {
                if (p.Coefficients > ushort.MaxValue)
                {
                    break;
                }

                var s = ((p.Coefficients - last) / 2 - 1).ToString() + ", ";

                if (len + s.Length > 100)
                {
                    Trace.WriteLine("");
                    len = 0;
                }

                Trace.Write(s);
                len += s.Length;
                last = p.Coefficients;
            }
        }
コード例 #2
0
        public void SmallDegreePolyGF2MultTest()
        {
            var p = new SmallDegreePolyGF2(0, 2, 5, 17);
            var q = new SmallDegreePolyGF2(0, 2, 3);
            var z = p * q;

            Assert.AreEqual(20, z.Degree);
            Assert.AreEqual(0x1A0199ul, z.Coefficients);
        }
コード例 #3
0
 public void SobolCoefficients()
 {
     Trace.WriteLine("s\ta\tP");
     foreach (var p in SmallDegreePolyGF2.Primes().Skip(1).Take(1000))
     {
         var a = p.Coefficients;
         a  ^= 1ul << p.Degree;
         a >>= 1;
         Trace.WriteLine($"{p.Degree}\t{a}\t{p}");
     }
 }
コード例 #4
0
        public void SmallDegreePolyGF2DivRemTest()
        {
            var p = new SmallDegreePolyGF2(0x1A0199ul);
            var q = new SmallDegreePolyGF2(0, 2, 3);
            SmallDegreePolyGF2 r;
            var d = SmallDegreePolyGF2.DivRem(p, q, out r);

            Assert.AreEqual(0ul, r.Coefficients);
            Assert.AreEqual(0x20025ul, d.Coefficients);
            d = SmallDegreePolyGF2.DivRem(p, d, out r);
            Assert.AreEqual(0ul, r.Coefficients);
            Assert.AreEqual(0xDul, d.Coefficients);
        }
コード例 #5
0
        /// <summary>
        /// Sequence of irreducible polynomials over GF(2).
        /// </summary>
        public static IEnumerable <SmallDegreePolyGF2> Primes()
        {
            yield return(new SmallDegreePolyGF2(2ul));   // x

            yield return(new SmallDegreePolyGF2(3ul));   // x + 1

            yield return(new SmallDegreePolyGF2(7ul));   // x² + x + 1

            for (ulong u = 9; u > 0; u += 2)
            {
                var p = new SmallDegreePolyGF2(u);
                if (p.IsIrreducible())
                {
                    yield return(p);
                }
            }
        }
コード例 #6
0
        public void SmallDegreePolyGF2FactorsTest()
        {
            foreach (var p in Enumerable.Range(2, 100).Select(i => new SmallDegreePolyGF2((ulong)i)))
            {
                Trace.Write($"{p} = ");

                foreach (var f in p.GetFactors())
                {
                    if (f.Value.Coefficients == 2)
                    {
                        Trace.Write(f.Value);
                    }
                    else
                    {
                        Trace.Write($"({f.Value})");
                    }
                    Trace.Write(SmallDegreePolyGF2.Superscript(f.Exponent));
                }

                Trace.WriteLine("");
            }
        }
コード例 #7
0
        /// <summary>
        /// Divide with remainder.
        /// </summary>
        /// <param name="p">Dividend.</param>
        /// <param name="q">Divisor.</param>
        /// <param name="rem">Remainder.</param>
        /// <returns>Quotient.</returns>
        public static SmallDegreePolyGF2 DivRem(SmallDegreePolyGF2 p, SmallDegreePolyGF2 q, out SmallDegreePolyGF2 rem)
        {
            if (q == Zero)
            {
                throw new DivideByZeroException();
            }

            if (p.Degree < q.Degree)
            {
                rem = p;
                return(Zero);
            }

            int shift = p.Degree - q.Degree;
            var d     = new SmallDegreePolyGF2(1ul << shift);
            var e     = new SmallDegreePolyGF2(1ul << p.Degree);

            q <<= shift;
            var qu = new SmallDegreePolyGF2(0ul);

            while (shift-- >= 0)
            {
                if ((p & e) != Zero)
                {
                    p  += q;
                    qu += d;
                }

                q >>= 1;
                d >>= 1;
                e >>= 1;
            }

            rem = p;
            return(qu);
        }
コード例 #8
0
        public void SmallDegreePolyGF2DegreeTest()
        {
            var p = new SmallDegreePolyGF2(0x17Bul);

            Assert.AreEqual(8, p.Degree);
        }