public void DLAreBasesEqualBadInputTest()
        {
            GroupElement[]            bases1 = StaticHelperClass.GenerateRandomBases(10, 0);
            ClosedDLRepOfGroupElement udl    = new ClosedDLRepOfGroupElement(bases1, bases1[0], _parameters[0].Group);

            GroupElement[] bases2 = new GroupElement[9];
            for (int i = 0; i < bases2.Length; ++i)
            {
                bases2[i] = bases1[i];
            }
            ClosedDLRepOfGroupElement udlbad = new ClosedDLRepOfGroupElement(bases2, bases1[0], _parameters[0].Group);

            Assert.IsFalse(udl.AreBasesEqual(udlbad), "different bases due to different rep length");
            Assert.IsFalse(udlbad.AreBasesEqual(udl), "different bases due to different rep length");


            udlbad = null;
            Assert.IsFalse(udl.AreBasesEqual(udlbad), "different bases since udlbad is null");


            // testing on array input
            ClosedDLRepOfGroupElement[] udlArray = null;
            Assert.IsFalse(ClosedDLRepOfGroupElement.AreBasesEqual(udlArray), "fails on null input.");

            udlArray = new ClosedDLRepOfGroupElement[0];
            Assert.IsFalse(ClosedDLRepOfGroupElement.AreBasesEqual(udlArray), "fails on empty array input.");

            udlArray = new ClosedDLRepOfGroupElement[1] {
                udl
            };
            Assert.IsTrue(ClosedDLRepOfGroupElement.AreBasesEqual(udlArray), "array of one element should pass");
        }
        public void AreBasesEqualTest()
        {
            for (int i = 0; i < _parameters.Length; ++i)
            {
                GroupElement[] bases0 = GetGenerators(_parameters[i], 6);
                GroupElement[] bases1 = GetGenerators(_parameters[i], 6);
                bases1[5] = bases1[5] * bases1[3];


                ClosedDLRepOfGroupElement dl = new ClosedDLRepOfGroupElement(bases0, _parameters[i].G, _parameters[i].Group);
                ClosedDLRepOfGroupElement dlWithSameBases = new ClosedDLRepOfGroupElement(bases0, _parameters[i].H, _parameters[i].Group);
                ClosedDLRepOfGroupElement dlWithDiffBases = new ClosedDLRepOfGroupElement(bases1, _parameters[i].G, _parameters[i].Group);

                Assert.IsTrue(dl.AreBasesEqual(dlWithSameBases), "bases are the same.");
                Assert.IsTrue(dlWithSameBases.AreBasesEqual(dl), "bases are same");
                Assert.IsFalse(dl.AreBasesEqual(dlWithDiffBases), "bases are different.");

                ClosedDLRepOfGroupElement[] sameBaseArray = new ClosedDLRepOfGroupElement[4] {
                    dlWithSameBases, dl, dlWithSameBases, dl
                };
                ClosedDLRepOfGroupElement[] diffBaseArray = new ClosedDLRepOfGroupElement[4] {
                    dl, dl, dl, dlWithDiffBases
                };
                Assert.IsTrue(ClosedDLRepOfGroupElement.AreBasesEqual(sameBaseArray), "Bases are identical");
                Assert.IsFalse(ClosedDLRepOfGroupElement.AreBasesEqual(diffBaseArray), "Bases are different");
            }
        }
        public void ClosedDLHashCodeTest()
        {
            GroupElement[]            bases = StaticHelperClass.GenerateRandomBases(11, 0);
            GroupElement              value = bases[1];
            ClosedDLRepOfGroupElement udl   = new ClosedDLRepOfGroupElement(bases, value, _parameters[0].Group);

            Assert.AreEqual(bases[0].GetHashCode(), udl.GetHashCode(), "should retrieve hashcode from bases[0]");

            bases = new GroupElement[0];
            udl   = new ClosedDLRepOfGroupElement(bases, value, _parameters[0].Group);
            Assert.AreEqual(0, udl.GetHashCode(), "hash code for 0 replength closed dl rep should be 0");
        }
        public void ClosedDLRepToClosedPedTest()
        {
            GroupElement[]            bases     = StaticHelperClass.GenerateRandomBases(2, 3);
            GroupElement              value     = StaticHelperClass.GenerateRandomValue(3);
            ClosedDLRepOfGroupElement udl       = new ClosedDLRepOfGroupElement(bases, value, _parameters[3].Group);
            ClosedPedersenCommitment  closedPed = new ClosedPedersenCommitment(udl);

            Assert.AreEqual(2, closedPed.RepresentationLength, "representation length should be 2");
            Assert.AreEqual(bases[0], closedPed.G, "G value should be bases[0]");
            Assert.AreEqual(bases[1], closedPed.H, "H value incorrect.");
            Assert.AreEqual(value, closedPed.Value, "value incorrect.");
            Assert.IsTrue(closedPed.Validate(), "should be valid closed pederson commitment.");
        }
        public void ClosedPedConstructorTests()
        {
            GroupElement[]            badbases = StaticHelperClass.GenerateRandomBases(3, 0);
            GroupElement              value    = badbases[2];
            ClosedDLRepOfGroupElement baddl    = new ClosedDLRepOfGroupElement(badbases, value, _parameters[0].Group);
            bool threwException = false;

            try
            {
                ClosedPedersenCommitment ped = new ClosedPedersenCommitment(badbases, value, _parameters[0].Group);
            }
            catch (Exception)
            {
                threwException = true;
            }
            Assert.IsTrue(threwException, "ClosedPedersenCommitment constructor should throw Exception when length of bases is not 2");
            threwException = false;
            try
            {
                ClosedPedersenCommitment ped = new ClosedPedersenCommitment(baddl);
            }
            catch (Exception)
            {
                threwException = true;
            }


            badbases       = StaticHelperClass.GenerateRandomBases(1, 0);
            baddl          = new ClosedDLRepOfGroupElement(badbases, value, _parameters[0].Group);
            threwException = false;
            try
            {
                ClosedPedersenCommitment ped = new ClosedPedersenCommitment(badbases, value, _parameters[0].Group);
            }
            catch (Exception)
            {
                threwException = true;
            }
            Assert.IsTrue(threwException, "ClosedPedersenCommitment constructor should throw Exception when length of bases is 1.");
            threwException = false;
            try
            {
                ClosedPedersenCommitment ped = new ClosedPedersenCommitment(baddl);
            }
            catch (Exception)
            {
                threwException = true;
            }
            Assert.IsTrue(threwException, "ClosedPedersenCommitment constructor should throw Exception when length of bases is 1.");
        }
        public void ExponentiateTest()
        {
            for (int paramIndex = 0; paramIndex < _parameters.Length; ++paramIndex)
            {
                GroupElement[]            bases    = StaticHelperClass.GenerateRandomBases(8, paramIndex);
                FieldZqElement[]          exponent = StaticHelperClass.GenerateRandomExponents(1, paramIndex);
                GroupElement              value    = _parameters[paramIndex].Generators[0];
                ClosedDLRepOfGroupElement udl      = new ClosedDLRepOfGroupElement(bases, value, _parameters[paramIndex].Group);

                ClosedDLRepOfGroupElement actualUDL = udl.Exponentiate(exponent[0]);
                Assert.IsTrue(actualUDL.AreBasesEqual(udl), "bases should be the same.");
                Assert.AreEqual(udl.Value.Exponentiate(exponent[0]), actualUDL.Value, "Value computed incorrectly.");
            }
        }
        public void OpenClosedPairTest()
        {
            GroupElement[]   bases     = StaticHelperClass.GenerateRandomBases(4, 0);
            GroupElement[]   goodBases = StaticHelperClass.GenerateRandomBases(4, 0);
            FieldZqElement[] exponents = StaticHelperClass.GenerateRandomExponents(4, 0);

            DLRepOfGroupElement       dl             = new DLRepOfGroupElement(goodBases, exponents, _parameters[0].Group);
            IStatement                expectedClosed = dl.GetStatement();
            ClosedDLRepOfGroupElement badClosed      = new ClosedDLRepOfGroupElement(bases, dl.Value, dl.Group);

            Assert.IsTrue(DLRepOfGroupElement.IsValidOpenClosedPair(dl, expectedClosed), "should be valid.");
            Assert.IsFalse(DLRepOfGroupElement.IsValidOpenClosedPair(dl, badClosed), "bad pair due to wrong bases.");

            badClosed = new ClosedDLRepOfGroupElement(goodBases, bases[0], _parameters[0].Group);
            Assert.IsFalse(DLRepOfGroupElement.IsValidOpenClosedPair(dl, badClosed), "bad pair due to wrong value.");


            Assert.IsFalse(DLRepOfGroupElement.IsValidOpenClosedPair(null, null), "should fail on null input.");
        }
        public void CSClosedDLRepArrayTest()
        {
            int arrayLength = 10;

            DLRepOfGroupElement[]        openInput = StaticHelperClass.GenerateRandomDLRepOfGroupElementArray(10, 7, this.paramIndex);
            ClosedDLRepOfGroupElement [] input     = new ClosedDLRepOfGroupElement[arrayLength];
            for (int i = 0; i < input.Length; ++i)
            {
                GroupElement[] bases = StaticHelperClass.GenerateRandomBases(10, this.paramIndex);
                GroupElement   value = bases[0];
                input[i] = new ClosedDLRepOfGroupElement(bases, value, crypto.Group);
            }

            string[] serialized = CryptoSerializer.Serialize(input, false);
            ClosedDLRepOfGroupElement[] output = CryptoSerializer.Deserialize <ClosedDLRepOfGroupElement>(serialized, crypto.Group);
            StaticHelperClass.AssertArraysAreEqual(input, output, "no group");


            string[] serialized2 = CryptoSerializer.Serialize(input, true);
            ClosedDLRepOfGroupElement[] output2 = CryptoSerializer.Deserialize <ClosedDLRepOfGroupElement>(serialized, crypto.Group);
            StaticHelperClass.AssertArraysAreEqual(input, output2, "group");
        }
        public void TryStrictMultiplySucceedTest()
        {
            for (int paramIndex = 0; paramIndex < _parameters.Length; ++paramIndex)
            {
                GroupElement[] bases = StaticHelperClass.GenerateRandomBases(8, paramIndex);

                GroupElement[] values = StaticHelperClass.GenerateRandomBases(10, paramIndex);
                ClosedDLRepOfGroupElement[] udlArray = new ClosedDLRepOfGroupElement[values.Length];
                GroupElement expectedProduct         = _parameters[paramIndex].Group.Identity;
                for (int udlIndex = 0; udlIndex < udlArray.Length; ++udlIndex)
                {
                    udlArray[udlIndex] = new ClosedDLRepOfGroupElement(bases, values[udlIndex], _parameters[paramIndex].Group);
                    expectedProduct   *= values[udlIndex];
                }

                ClosedDLRepOfGroupElement product;
                bool success = ClosedDLRepOfGroupElement.TryStrictMultiply(udlArray, out product);
                Assert.IsTrue(success, "TryStrictMultiply should have succeeded.");
                Assert.IsNotNull(product, "product should be set to not null");
                Assert.IsTrue(product.AreBasesEqual(udlArray[0]), "product should have same bases as other members of udlArray");
                Assert.AreEqual(expectedProduct, product.Value, "Product of values computed incorrectly");
            }
        }
        public void ClosedDLTryStrictMultiplyFailTest()
        {
            int paramIndex = 0;

            GroupElement[] bases = StaticHelperClass.GenerateRandomBases(8, paramIndex);

            GroupElement[] values = StaticHelperClass.GenerateRandomBases(10, paramIndex);
            ClosedDLRepOfGroupElement[] udlArray = new ClosedDLRepOfGroupElement[values.Length];
            GroupElement expectedProduct         = _parameters[paramIndex].Group.Identity;

            for (int udlIndex = 0; udlIndex < udlArray.Length; ++udlIndex)
            {
                udlArray[udlIndex] = new ClosedDLRepOfGroupElement(bases, values[udlIndex], _parameters[paramIndex].Group);
                expectedProduct   *= values[udlIndex];
            }

            // fail with different bases
            GroupElement[] wrongBases = StaticHelperClass.GenerateRandomBases(8, paramIndex);
            udlArray[udlArray.Length - 1] = new ClosedDLRepOfGroupElement(wrongBases, values[0], _parameters[paramIndex].Group);
            ClosedDLRepOfGroupElement product = udlArray[0];
            bool success = ClosedDLRepOfGroupElement.TryStrictMultiply(udlArray, out product);

            Assert.IsFalse(success, "TryStrictMultiply should have failed");
            Assert.IsNull(product, "product should be set to null");

            success = ClosedDLRepOfGroupElement.TryStrictMultiply(null, out product);
            Assert.IsFalse(success, "TryStrictMultiply should have failed");
            Assert.IsNull(product, "product should be set to null");

            wrongBases = StaticHelperClass.GenerateRandomBases(18, paramIndex);
            udlArray[udlArray.Length - 1] = new ClosedDLRepOfGroupElement(wrongBases, values[0], _parameters[paramIndex].Group);
            product = udlArray[0];
            success = ClosedDLRepOfGroupElement.TryStrictMultiply(udlArray, out product);
            Assert.IsFalse(success, "TryStrictMultiply should have failed");
            Assert.IsNull(product, "product should be set to null");
        }
Esempio n. 11
0
        public void EQSerializationTest()
        {
            int length     = 20;
            int fullLength = length * 2;

            DLRepOfGroupElement [] openEquations    = StaticHelperClass.GenerateRandomDLRepOfGroupElementArray(length, 5, _paramIndex);
            DLRepOfGroupElement [] allOpenEquations = new DLRepOfGroupElement[fullLength];
            for (int i = 0; i < openEquations.Length; ++i)
            {
                allOpenEquations[i]          = openEquations[i];
                allOpenEquations[i + length] = openEquations[i];
            }

            // create equality map
            EqualityMap map = new EqualityMap();

            for (int i = 0; i < length; ++i)
            {
                map.Add(
                    new PrettyName("chi", i),
                    new DoubleIndex(i, 0)
                    );

                map.Add(
                    new PrettyName("chi", i),
                    new DoubleIndex(length + i, 0)
                    );
                map.Add(
                    new PrettyName("delta", i),
                    new DoubleIndex(i, 4)
                    );
                map.Add(
                    new PrettyName("delta", i),
                    new DoubleIndex(length + i, 4)
                    );

                map.Add(
                    new PrettyName("beta", i),
                    new DoubleIndex(i, 2)
                    );
                map.Add(
                    new PrettyName("beta", i),
                    new DoubleIndex(length + i, 2)
                    );
            }

            // create proverParameters
            ProverEqualityParameters prover = new ProverEqualityParameters(allOpenEquations, map, _crypto);
            string jsonProver = CryptoSerializer.Serialize <ProverEqualityParameters>(prover);
            ProverEqualityParameters deserializedProver = CryptoSerializer.Deserialize <ProverEqualityParameters>(jsonProver);

            StaticHelperClass.AssertArraysAreEqual(prover.Statements, deserializedProver.Statements, "Prover closedDLEquations");
            StaticHelperClass.AssertArraysAreEqual(prover.Witnesses, deserializedProver.Witnesses, "Prover Witnesses");
            StaticHelperClass.AssertArraysAreEqual(prover.HashDigest, deserializedProver.HashDigest, "equality map hash.");
            StaticHelperClass.AssertArraysAreEqual(prover.Generators, deserializedProver.Generators, "prover Generators");
            for (int i = 0; i < prover.Statements.Length; ++i)
            {
                Assert.IsTrue(prover.Statements[i].AreBasesEqual(deserializedProver.Statements[i]), "unequal bases for equation " + i);
            }
            Assert.IsTrue(deserializedProver.Verify(), "deserializedProver.Verify()");

            // create proof
            EqualityProof proof = new EqualityProof(prover);

            Assert.IsTrue(proof.Verify(prover), "Proof.Verify(prover)");
            Assert.IsTrue(proof.Verify(deserializedProver), "proof.Verify(deserializedProver)");
            string jsonProof = CryptoSerializer.Serialize <EqualityProof>(proof);
            // TODO: switch to using ip-based de-serialization; need to harmonize with U-Prove SDK code
            IssuerParameters ip = new IssuerParameters();

            ip.Gq = prover.Group;
            EqualityProof deserializedProof = ip.Deserialize <EqualityProof>(jsonProof); // CryptoSerializer.Deserialize<EqualityProof>(jsonProof);

            Assert.IsTrue(deserializedProof.Verify(prover), "deserializedProof.Verify(prover)");
            Assert.IsTrue(deserializedProof.Verify(deserializedProver), "deserializedProof.Verify(deserializedProver)");

            // create verifier
            IStatement[] closedEquations = new ClosedDLRepOfGroupElement[allOpenEquations.Length];
            for (int i = 0; i < allOpenEquations.Length; ++i)
            {
                closedEquations[i] = allOpenEquations[i].GetStatement();
            }
            VerifierEqualityParameters verifier = new VerifierEqualityParameters(closedEquations, map, _crypto);

            Assert.IsTrue(proof.Verify(verifier), "Proof.Verify(verifier)");
            Assert.IsTrue(deserializedProof.Verify(verifier), "proof.Verify(verifier)");
            string jsonVerifier = CryptoSerializer.Serialize <VerifierEqualityParameters>(verifier);
            VerifierEqualityParameters deserializedVerifier = CryptoSerializer.Deserialize <VerifierEqualityParameters>(jsonVerifier);

            Assert.IsTrue(deserializedVerifier.Verify(), "deserializedVerifier.Verify()");
            Assert.IsTrue(deserializedProof.Verify(deserializedVerifier), "deserializedProof.Verify(deserializedVerifier)");

            // create proof from deserialized prover
            EqualityProof newProof = new EqualityProof(deserializedProver);

            Assert.IsTrue(newProof.Verify(deserializedProver), "newProof.verify(deserializedProver)");
            Assert.IsTrue(newProof.Verify(verifier), "newProof.Verify(verifier)");
        }