Exemplo n.º 1
0
 public static BiVarSymmetricPolynomial Random(int degree)
 {
     return(new BiVarSymmetricPolynomial(
                degree,
                Enumerable.Range(0, degree + 1)
                .SelectMany(i => Enumerable.Range(0, i + 1).Select(j => Fr.GetRandom()))
                ));
 }
Exemplo n.º 2
0
        public void TestGetRandom()
        {
            var rnd = Fr.GetRandom();

            Assert.IsTrue(rnd.IsValid());
            rnd.Clear();
            Assert.AreEqual(Fr.Zero, rnd);
        }
Exemplo n.º 3
0
        public void DeserializationG2Test()
        {
            var x   = Fr.GetRandom();
            var a   = G2.Generator * x;
            var enc = a.ToBytes();
            var b   = G2.FromBytes(enc);

            Assert.True(a.Equals(b));
        }
Exemplo n.º 4
0
        public void DeserializationFrTest()
        {
            var a   = Fr.GetRandom();
            var enc = a.ToBytes();

            Assert.AreEqual(enc.Length, 32);
            var b = Fr.FromBytes(enc);

            Assert.True(a.Equals(b));
        }
Exemplo n.º 5
0
        public void AdditionG1Test()
        {
            var a = Fr.GetRandom();
            var b = Fr.GetRandom();

            var A = G1.Generator * a;
            var B = G1.Generator * b;
            var C = G1.Generator * (a + b);

            Assert.True(C.Equals(A + B), $"Addition of {a}G + {b}G failed");
        }
Exemplo n.º 6
0
        public void EvalFrPolyConstantTest()
        {
            var poly = new Fr[] { Fr.GetRandom() };
            var v0   = MclBls12381.EvaluatePolynomial(poly, Fr.Zero);
            var v1   = MclBls12381.EvaluatePolynomial(poly, Fr.One);
            var v2   = MclBls12381.EvaluatePolynomial(poly, Fr.FromInt(319948));

            Assert.AreEqual(poly[0], v0);
            Assert.AreEqual(poly[0], v1);
            Assert.AreEqual(poly[0], v2);
        }
Exemplo n.º 7
0
        public void SerializationRoundTrip()
        {
            var x = G2.Generator * Fr.GetRandom();

            Assert.IsTrue(x.IsValid());
            var serialized = x.ToBytes();

            Assert.AreEqual(96, serialized.Length);
            var restored = G2.FromBytes(serialized);

            Assert.AreEqual(x, restored);
        }
Exemplo n.º 8
0
        public void PairingTest()
        {
            var a = Fr.GetRandom();
            var b = Fr.GetRandom();

            var A = G1.Generator * a;
            var B = G2.Generator * b;

            var X = GT.Pairing(A, B);
            var Y = GT.Pow(GT.Pairing(G1.Generator, G2.Generator), a * b);

            Assert.True(X.Equals(Y));
        }
Exemplo n.º 9
0
 public EncryptedShare Encrypt(IRawShare rawShare)
 {
     return(EncryptBenchmark.Benchmark(() =>
     {
         var r = Fr.GetRandom();
         var u = G1.Generator * r;
         var shareBytes = rawShare.ToBytes();
         var t = _y * r;
         var v = Utils.XorWithHash(t, shareBytes);
         var w = Utils.HashToG2(u, v) * r;
         return new EncryptedShare(u, v, w, rawShare.Id);
     }));
 }
Exemplo n.º 10
0
        public TrustedKeyGen(int n, int f)
        {
            if (n <= 3 * f)
            {
                throw new ArgumentException($"n should be greater than 3*f, but {n} <= 3 * {f} = {3 * f}");
            }
            _degree = f;
            _coeffs = new Fr[_degree];

            for (var i = 0; i < _degree; ++i)
            {
                _coeffs[i] = Fr.GetRandom();
            }
        }
Exemplo n.º 11
0
        public void TestPairing()
        {
            var fr1   = Fr.GetRandom();
            var fr2   = Fr.GetRandom();
            var rndG1 = G1.Generator * fr1;
            var rndG2 = G2.Generator * fr2;

            Assert.AreEqual(GT.One, GT.Pairing(G1.Zero, G2.Zero));
            Assert.AreEqual(GT.One, GT.Pairing(G1.Zero, rndG2));
            Assert.AreEqual(GT.One, GT.Pairing(rndG1, G2.Zero));
            var generatorsPairing = GT.Pairing(G1.Generator, G2.Generator);

            Assert.AreEqual(GT.Pow(generatorsPairing, fr1 * fr2), GT.Pairing(rndG1, rndG2));
        }
Exemplo n.º 12
0
        public void TestPolyEvaluationFr()
        {
            const int degree = 100;
            var       coeffs = Enumerable.Range(0, degree)
                               .Select(i => Fr.GetRandom())
                               .ToArray();
            var pt  = Fr.GetRandom();
            var res = Fr.Zero;

            for (var i = degree - 1; i >= 0; --i)
            {
                res = res * pt + coeffs[i];
            }
            Assert.AreEqual(res, MclBls12381.EvaluatePolynomial(coeffs, pt));
        }
Exemplo n.º 13
0
        public void TestPolyInterpolationG2()
        {
            const int degree = 100;
            var       coeffs = Enumerable.Range(0, degree)
                               .Select(i => G2.Generator * Fr.GetRandom())
                               .ToArray();
            var xs = Enumerable.Range(1, degree)
                     .Select(i => Fr.GetRandom())
                     .ToArray();
            var ys = xs
                     .Select(x => MclBls12381.EvaluatePolynomial(coeffs, x))
                     .ToArray();
            var intercept = MclBls12381.EvaluatePolynomial(coeffs, Fr.FromInt(0));

            Assert.AreEqual(intercept, MclBls12381.LagrangeInterpolate(xs, ys));
            Assert.Throws <ArgumentException>(() => MclBls12381.LagrangeInterpolate(xs, ys.Take(degree - 1).ToArray()));
        }
Exemplo n.º 14
0
        public void TestZero()
        {
            Assert.IsTrue(G2.Zero.IsValid());
            Assert.IsTrue(G2.Zero.IsZero());
            Assert.AreEqual(G2.Zero, G2.Zero);
            Assert.AreNotEqual(G2.Zero, G2.Generator);
            Assert.IsTrue(G2.Zero != G2.Generator);
            Assert.AreEqual(G2.Zero, G2.Zero + G2.Zero);
            var rnd = G2.GetGenerator() * Fr.GetRandom();

            Assert.AreEqual(G2.Zero + rnd, rnd);
            Assert.AreEqual(rnd + G2.Zero, rnd);
            Assert.AreEqual(rnd * Fr.Zero, G2.Zero);
            Assert.AreEqual(
                "G2(000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000)",
                G2.Zero.ToString()
                );
            Assert.AreNotEqual(G2.Zero, G1.Zero);
        }
Exemplo n.º 15
0
        public void Test_Mcl_Serializations()
        {
            Assert.AreEqual(
                "0x0000000000000000000000000000000000000000000000000000000000000000",
                Fr.FromInt(0).ToHex()
                );
            Assert.AreEqual(
                "0x0100000000000000000000000000000000000000000000000000000000000000",
                Fr.FromInt(1).ToHex()
                );
            var fr = Fr.GetRandom();

            Assert.AreEqual(fr, Fr.FromBytes(fr.ToBytes()));

            Assert.AreEqual(
                "0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
                G1.Zero.ToHex()
                );
            Assert.AreEqual(
                "0xe9328f8eb8185341f22adaf2bf41f66258d97b2b5b2dbd2c27a77c81d9b5d76dd119bf7b1cd5d57b1273f9c4a654540e",
                G1.Generator.ToHex()
                );
            Assert.AreEqual(
                "0x2ac325b200d53184871fb8f8f5e5e43b302349ae6172de2899e88d2961f0bd2593b427667f5f85b4b59296ae8dcfc918",
                (G1.Generator * Fr.FromInt(2)).ToHex()
                );
            var g1 = G1.Generator * Fr.GetRandom();

            Assert.AreEqual(g1, G1.FromBytes(g1.ToBytes()));

            Assert.AreEqual(
                "0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
                G2.Zero.ToHex()
                );
            Assert.AreEqual(
                "0xf1437606f337b000f00d69507434dbbd3b2abfa8a291d88551d92309fe3c222e0790fc847e849eb984bb807cba59170a0a63480e9457a375705ea2aba224a711e717ecddb224feeb738630945581bda24389f8fecf7865724361aec550e44919",
                G2.Generator.ToHex()
                );
            Assert.AreEqual(
                "0xd63f18cd560a715faef9b96c31e08eead86809ffd81642f6cdb55ed88ea2eca32cb2a28d818b7887cb35f1a7d4ab8a04eacc640851cb34b8a615729cd6d8cc4317125959b2012444b435508679e0a2f07d691d91aef2a18400a6696c6a3ae18d",
                (G2.Generator * Fr.FromInt(2)).ToHex()
                );
        }
Exemplo n.º 16
0
        public void TestMillerLoop()
        {
            var p = G1.Generator * Fr.GetRandom();
            var q = G2.Generator * Fr.GetRandom();

            Assert.AreEqual(
                GT.Pairing(p, q),
                GT.FinalExp(GT.MillerLoop(p, q))
                );
            var r      = G1.Generator * Fr.GetRandom();
            var s      = G2.Generator * Fr.GetRandom();
            var pqLoop = GT.MillerLoop(p, q);
            var rsLoop = GT.MillerLoop(r, s);

            Assert.AreEqual(
                GT.Pairing(p, q) * GT.Pairing(r, s),
                GT.FinalExp(pqLoop * rsLoop)
                );
        }
Exemplo n.º 17
0
        public void EvalInterpolateTestFr()
        {
            const int n      = 10;
            var       poly   = Enumerable.Range(0, n).Select(_ => Fr.GetRandom()).ToArray();
            var       values = Enumerable.Range(100, n + 1)
                               .Select(i => MclBls12381.EvaluatePolynomial(poly, Fr.FromInt(i)))
                               .ToArray();

            for (var i = 0; i < n + 1; ++i)
            {
                Assert.AreEqual(DummyEval(poly, Fr.FromInt(100 + i)), values[i]);
            }
            var intercept = MclBls12381.LagrangeInterpolate(
                Enumerable.Range(100, n + 1).Select(Fr.FromInt).ToArray(),
                values
                );

            Assert.AreEqual(poly[0], intercept);
        }
Exemplo n.º 18
0
        public void TestOne()
        {
            Assert.IsTrue(Fr.One.IsValid());
            Assert.IsFalse(Fr.One.IsZero());
            Assert.IsTrue(Fr.One.IsOne());
            Assert.AreEqual(Fr.One, Fr.One);
            Assert.AreNotEqual(Fr.Zero, Fr.One);
            Assert.IsTrue(Fr.Zero != Fr.One);
            Assert.AreEqual(Fr.One - Fr.One, Fr.Zero);
            var rnd = Fr.GetRandom();

            Assert.AreEqual(Fr.One * rnd, rnd);
            Assert.AreEqual(rnd * Fr.One, rnd);
            Assert.AreEqual(rnd / Fr.One, rnd);
            Assert.AreEqual(Fr.One, Fr.One.Inverse());
            Assert.AreEqual(
                "Fr(0100000000000000000000000000000000000000000000000000000000000000)",
                Fr.One.ToString()
                );
            Assert.AreNotEqual(Fr.One, G1.Generator);
            Assert.AreNotEqual(Fr.One, G2.Generator);
        }
Exemplo n.º 19
0
        public void TestZero()
        {
            Assert.IsTrue(Fr.Zero.IsValid());
            Assert.IsTrue(Fr.Zero.IsZero());
            Assert.IsFalse(Fr.Zero.IsOne());
            Assert.AreEqual(Fr.Zero, Fr.Zero);
            Assert.AreEqual(Fr.Zero, Fr.FromInt(0));
            Assert.AreNotEqual(Fr.Zero, Fr.One);
            Assert.IsTrue(Fr.Zero != Fr.One);
            Assert.AreEqual(Fr.Zero + Fr.Zero, Fr.Zero);
            var rnd = Fr.GetRandom();

            Assert.AreEqual(Fr.Zero + rnd, rnd);
            Assert.AreEqual(rnd + Fr.Zero, rnd);
            Assert.AreEqual(Fr.Zero * rnd, Fr.Zero);
            Assert.AreEqual(rnd * Fr.Zero, Fr.Zero);
            Assert.AreEqual(Fr.Zero, Fr.Zero.Inverse()); // NB
            Assert.AreEqual(
                "Fr(0000000000000000000000000000000000000000000000000000000000000000)",
                Fr.Zero.ToString()
                );
            Assert.AreNotEqual(Fr.Zero, G1.Zero);
            Assert.AreNotEqual(Fr.Zero, G2.Zero);
        }