Beispiel #1
0
        static void TestFr()
        {
            Console.WriteLine("TestFr");
            Fr x = new Fr();

            assert("x.isZero", x.IsZero());
            x.Clear();
            assert("0", x.GetStr(10) == "0");
            assert("0.IzZero", x.IsZero());
            assert("!0.IzOne", !x.IsOne());
            x.SetInt(1);
            assert("1", x.GetStr(10) == "1");
            assert("!1.IzZero", !x.IsZero());
            assert("1.IzOne", x.IsOne());
            x.SetInt(3);
            assert("3", x.GetStr(10) == "3");
            assert("!3.IzZero", !x.IsZero());
            assert("!3.IzOne", !x.IsOne());
            x.SetInt(-5);
            x = -x;
            assert("5", x.GetStr(10) == "5");
            x.SetInt(4);
            x = x * x;
            assert("16", x.GetStr(10) == "16");
            assert("10", x.GetStr(16) == "10");
            Fr y;

            y = x;
            assert("x == y", x.Equals(y));
            x.SetInt(123);
            assert("123", x.GetStr(10) == "123");
            assert("7b", x.GetStr(16) == "7b");
            assert("y != x", !x.Equals(y));
            Console.WriteLine("exception test");
            try {
                x.SetStr("1234567891234x", 10);
                Console.WriteLine("x = {0}", x);
            } catch (Exception e) {
                Console.WriteLine("OK ; expected exception: {0}", e);
            }
            x.SetStr("1234567891234", 10);
            assert("1234567891234", x.GetStr(10) == "1234567891234");
            {
                byte[] buf = x.Serialize();
                y.Deserialize(buf);
                assert("x == y", x.Equals(y));
            }
        }
Beispiel #2
0
            public static Fr One()
            {
                var x = new Fr();

                x.SetInt(1);
                return(x);
            }
Beispiel #3
0
        static void TestG1()
        {
            Console.WriteLine("TestG1");
            G1 P = new G1();

            P.Clear();
            assert("P = 0", P.ToString() == "0");
            assert("P.IsValid", P.IsValid());
            assert("P.IsZero", P.IsZero());
            P.HashAndMapTo("abc");
            assert("P.IsValid", P.IsValid());
            assert("!P.IsZero", !P.IsZero());
            G1 Q = new G1();

            Q = P.Clone();
            assert("P == Q", Q.Equals(P));
            G1.Neg(Q, P);
            G1.Add(Q, Q, P);
            assert("P = Q", Q.IsZero());
            G1.Dbl(Q, P);
            G1 R = new G1();

            G1.Add(R, P, P);
            assert("Q == R", Q.Equals(R));
            Fr x = new Fr();

            x.SetInt(3);
            G1.Add(R, R, P);
            G1.Mul(Q, P, x);
            assert("Q == R", Q.Equals(R));
        }
Beispiel #4
0
        static void TestG2()
        {
            Console.WriteLine("TestG2");
            G2 P = new G2();

            P.Clear();
            assert("P = 0", P.ToString() == "0");
            assert("P is valid", P.IsValid());
            assert("P is zero", P.IsZero());
            P.HashAndMapTo("abc");
            assert("P is valid", P.IsValid());
            assert("P is not zero", !P.IsZero());
            G2 Q = new G2();

            Q = P.Clone();
            assert("P == Q", Q.Equals(P));
            G2.Neg(Q, P);
            G2.Add(Q, Q, P);
            assert("Q is zero", Q.IsZero());
            G2.Dbl(Q, P);
            G2 R = new G2();

            G2.Add(R, P, P);
            assert("Q == R", Q.Equals(R));
            Fr x = new Fr();

            x.SetInt(3);
            G2.Add(R, R, P);
            G2.Mul(Q, P, x);
            assert("Q == R", Q.Equals(R));
        }
Beispiel #5
0
        static void TestG1()
        {
            Console.WriteLine("TestG1");
            G1 P = new G1();

            P.Clear();
            assert("P.IsValid", P.IsValid());
            assert("P.IsZero", P.IsZero());
            P.HashAndMapTo("abc");
            assert("P.IsValid", P.IsValid());
            assert("!P.IsZero", !P.IsZero());
            G1 Q = new G1();

            Q = P;
            assert("P == Q", Q.Equals(P));
            Q.Neg(P);
            Q.Add(Q, P);
            assert("P = Q", Q.IsZero());
            Q.Dbl(P);
            G1 R = new G1();

            R.Add(P, P);
            assert("Q == R", Q.Equals(R));
            Fr x = new Fr();

            x.SetInt(3);
            R.Add(R, P);
            Q.Mul(P, x);
            assert("Q == R", Q.Equals(R));
        }
Beispiel #6
0
            public static Fr One()
            {
                var fr = new Fr();

                fr.SetInt(1);

                return(fr);
            }
Beispiel #7
0
        static void TestG2()
        {
            Console.WriteLine("TestG2");
            G2 P = new G2();

            assert("P.isZero", P.IsZero());
            P.Clear();
            assert("P is valid", P.IsValid());
            assert("P is zero", P.IsZero());
            P.HashAndMapTo("abc");
            assert("P is valid", P.IsValid());
            assert("P is not zero", !P.IsZero());
            G2 Q = new G2();

            Q = P;
            assert("P == Q", Q.Equals(P));
            Q.Neg(P);
            Q.Add(Q, P);
            assert("Q is zero", Q.IsZero());
            Q.Dbl(P);
            G2 R = new G2();

            R.Add(P, P);
            assert("Q == R", Q.Equals(R));
            Fr x = new Fr();

            x.SetInt(3);
            R.Add(R, P);
            Q.Mul(P, x);
            assert("Q == R", Q.Equals(R));
            {
                byte[] buf = P.Serialize();
                Q.Clear();
                Q.Deserialize(buf);
                assert("P == Q", P.Equals(Q));
            }
            {
                const int n    = 5;
                G2[]      xVec = new G2[n];
                Fr[]      yVec = new Fr[n];
                P.Clear();
                for (int i = 0; i < n; i++)
                {
                    xVec[i].HashAndMapTo(i.ToString());
                    yVec[i].SetByCSPRNG();
                    Q.Mul(xVec[i], yVec[i]);
                    P.Add(P, Q);
                }
                MulVec(ref Q, xVec, yVec);
                assert("mulVecG2", P.Equals(Q));
            }
            G2 W = G2.Zero();

            assert("W.IsZero", W.IsZero());
        }
Beispiel #8
0
        static void TestFr()
        {
            Console.WriteLine("TestFr");
            Fr x = new Fr();

            x.Clear();
            assert("0", x.ToString() == "0");
            assert("0.IzZero", x.IsZero());
            assert("!0.IzOne", !x.IsOne());
            x.SetInt(1);
            assert("1", x.ToString() == "1");
            assert("!1.IzZero", !x.IsZero());
            assert("1.IzOne", x.IsOne());
            x.SetInt(3);
            assert("3", x.ToString() == "3");
            assert("!3.IzZero", !x.IsZero());
            assert("!3.IzOne", !x.IsOne());
            x.SetInt(-5);
            x = -x;
            assert("5", x.ToString() == "5");
            x.SetInt(4);
            x = x * x;
            assert("16", x.ToString() == "16");
            Fr y;

            y = x.Clone();
            assert("x == y", x.Equals(y));
            x.SetInt(123);
            assert("123", x.ToString() == "123");
            assert("y != x", !x.Equals(y));
            try {
                x.SetStr("1234567891234x");
                Console.WriteLine("x = {0}", x);
            } catch (Exception e) {
                Console.WriteLine("exception test OK\n'{0}'", e);
            }
            x.SetStr("1234567891234");
            assert("1234567891234", x.ToString() == "1234567891234");
        }
Beispiel #9
0
        static void TestG2()
        {
            Console.WriteLine("TestG2");
            G2 P = new G2();

            P.Clear();
            assert("P is valid", P.IsValid());
            assert("P is zero", P.IsZero());
            P.HashAndMapTo("abc");
            assert("P is valid", P.IsValid());
            assert("P is not zero", !P.IsZero());
            G2 Q = new G2();

            Q = P;
            assert("P == Q", Q.Equals(P));
            Q.Neg(P);
            Q.Add(Q, P);
            assert("Q is zero", Q.IsZero());
            Q.Dbl(P);
            G2 R = new G2();

            R.Add(P, P);
            assert("Q == R", Q.Equals(R));
            Fr x = new Fr();

            x.SetInt(3);
            R.Add(R, P);
            Q.Mul(P, x);
            assert("Q == R", Q.Equals(R));
            {
                byte[] buf = P.Serialize();
                Q.Clear();
                Q.Deserialize(buf);
                assert("P == Q", P.Equals(Q));
            }
        }