public void TryRetrieveIntIndexTest()
        {
            PrettyName alpha0 = new PrettyName("alpha", 0);
            PrettyName alpha1 = new PrettyName("alpha", 1);

            EqualityMap map = new EqualityMap();

            Assert.AreEqual(0, map.CountEquationAndExponentIndices);
            Assert.AreEqual(0, map.CountPrettyName);

            // add double indexes with pretty name alpha0 and alpha1
            int actualIndex;

            for (int dlIndex = 0; dlIndex < 10; ++dlIndex)
            {
                for (int baseIndex = 0; baseIndex < 10; ++baseIndex)
                {
                    DoubleIndex di0      = new DoubleIndex(dlIndex, baseIndex);
                    DoubleIndex di0clone = new DoubleIndex(dlIndex, baseIndex);
                    map.Add(alpha0, di0);
                    Assert.IsTrue(map.TryRetrieveIntIndex(di0, out actualIndex));
                    Assert.AreEqual(0, actualIndex, "could not retrieve correct index for di0");
                    Assert.IsTrue(map.TryRetrieveIntIndex(di0clone, out actualIndex));
                    Assert.AreEqual(0, actualIndex, "could not retrieve correct index for di0clone");

                    DoubleIndex di1      = new DoubleIndex(dlIndex + 100, baseIndex);
                    DoubleIndex di1clone = new DoubleIndex(dlIndex + 100, baseIndex);
                    map.Add(alpha1, di1);
                    Assert.IsTrue(map.TryRetrieveIntIndex(di1, out actualIndex));
                    Assert.AreEqual(1, actualIndex, "could not retrieve correct index for di1clone");
                    Assert.IsTrue(map.TryRetrieveIntIndex(di1clone, out actualIndex));
                    Assert.AreEqual(1, actualIndex, "could not retrieve correct index for di1clone");
                }
            }
        }
예제 #2
0
        public void EQEndToEndTest2()
        {
            int length = 20;

            FieldZqElement[]     committedValues = _crypto.FieldZq.GetRandomElements(length, true);
            FieldZqElement[]     openings        = _crypto.FieldZq.GetRandomElements(length, true);
            PedersenCommitment[] ped             = PedersenCommitment.GetCommitments(_crypto, committedValues, openings);

            EqualityMap map = new EqualityMap();

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

            ProverEqualityParameters prover = new ProverEqualityParameters(ped, map, _crypto);

            Assert.IsTrue(prover.Verify());

            EqualityProof proof = new EqualityProof(prover);

            Assert.IsTrue(proof.Verify(prover));
        }
예제 #3
0
        public void EQEndToEndTest1()
        {
            // create two pedersen commitments to 1
            DLRepOfGroupElement[] dlarray = new DLRepOfGroupElement[2]
            {
                new PedersenCommitment(_parameters.FieldZq.One, _parameters),
                new PedersenCommitment(_parameters.FieldZq.One, _parameters)
            };
            PrettyName  alpha = new PrettyName("alpha", 0);
            DoubleIndex d1    = new DoubleIndex(0, 0); // dlarray[0].BaseAtIndex(0)
            DoubleIndex d2    = new DoubleIndex(1, 0); // dlarray[1].BaseAtIndex(0)
            EqualityMap map   = new EqualityMap();

            map.Add(alpha, d1);
            map.Add(alpha, d2);

            int index;

            Assert.IsTrue(map.TryRetrieveIntIndex(d1, out index));
            Assert.AreEqual(0, index);
            Assert.IsTrue(map.TryRetrieveIntIndex(d2, out index));
            Assert.AreEqual(0, index);


            ProverEqualityParameters peParameters = new ProverEqualityParameters(
                dlarray,
                map,
                _parameters);
            EqualityProof proof = new EqualityProof(peParameters);

            Assert.IsTrue(proof.Verify(peParameters));
        }
예제 #4
0
        public void EQBadVerifierParametersTest()
        {
            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, 19)
                    );
                map.Add(
                    new PrettyName("delta", i),
                    new DoubleIndex(length + i, 19)
                    );

                map.Add(
                    new PrettyName("beta", i),
                    new DoubleIndex(i, 5)
                    );
                map.Add(
                    new PrettyName("beta", i),
                    new DoubleIndex(length + i, 5)
                    );
            }
        }
        public void EQMapHashTest2()
        {
            int length = 20;
            // 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)
                    );
            }

            byte[] hash1 = map.Hash(DefaultHashFunction);
            byte[] hash2 = map.Hash(DefaultHashFunction);
            StaticHelperClass.AssertArraysAreEqual <byte>(hash1, hash2, "same map hash.");

            // create parallel map in different order
            EqualityMap map2 = new EqualityMap();

            for (int i = 0; i < length; ++i)
            {
                map2.Add(
                    new PrettyName("chi", i),
                    new DoubleIndex(length + i, 0)
                    );
            }
            for (int i = 0; i < length; ++i)
            {
                map2.Add(
                    new PrettyName("chi", i),
                    new DoubleIndex(i, 0)
                    );
            }
            byte[] hash3 = map2.Hash(DefaultHashFunction);
            byte[] hash4 = map2.Hash(DefaultHashFunction);
            StaticHelperClass.AssertArraysAreEqual <byte>(hash3, hash4, "map2 hash.");
            StaticHelperClass.AssertArraysAreEqual <byte>(hash1, hash3, "hash1 vs hash 3.");
        }
        public void MapAddCountTest()
        {
            PrettyName alpha0 = new PrettyName("alpha", 0);
            PrettyName alpha1 = new PrettyName("alpha", 1);
            PrettyName beta0  = new PrettyName("beta", 0);
            PrettyName beta1  = new PrettyName("beta", 1);

            EqualityMap map = new EqualityMap();

            Assert.AreEqual(0, map.CountEquationAndExponentIndices);
            Assert.AreEqual(0, map.CountPrettyName);

            // add double indexes with pretty name alpha0
            int expectedDoubleIndexCount = 0;

            for (int dlIndex = 0; dlIndex < 10; ++dlIndex)
            {
                for (int baseIndex = 0; baseIndex < 10; ++baseIndex)
                {
                    map.Add(alpha0, new DoubleIndex(dlIndex, baseIndex));
                    ++expectedDoubleIndexCount;
                    Assert.AreEqual(expectedDoubleIndexCount, map.CountEquationAndExponentIndices);
                    Assert.AreEqual(1, map.CountPrettyName);
                }
            }

            // add double indexes with pretty name alpha1
            map.Add(alpha1, new DoubleIndex(10, 0));
            ++expectedDoubleIndexCount;
            Assert.AreEqual(expectedDoubleIndexCount, map.CountEquationAndExponentIndices);
            Assert.AreEqual(2, map.CountPrettyName);

            map.Add(beta0, new DoubleIndex(10, 1));
            ++expectedDoubleIndexCount;
            Assert.AreEqual(expectedDoubleIndexCount, map.CountEquationAndExponentIndices);
            Assert.AreEqual(3, map.CountPrettyName);

            map.Add(beta1, new DoubleIndex(10, 2));
            ++expectedDoubleIndexCount;
            Assert.AreEqual(expectedDoubleIndexCount, map.CountEquationAndExponentIndices);
            Assert.AreEqual(4, map.CountPrettyName);
        }
예제 #7
0
        public void EQEndToEndTest3()
        {
            int length = 20;

            FieldZqElement[]     committedValues = _crypto.FieldZq.GetRandomElements(length, true);
            FieldZqElement[]     openings1       = _crypto.FieldZq.GetRandomElements(length, true);
            FieldZqElement[]     openings2       = _crypto.FieldZq.GetRandomElements(length, true);
            PedersenCommitment[] ped1            = PedersenCommitment.GetCommitments(_crypto, committedValues, openings1);
            PedersenCommitment[] ped2            = PedersenCommitment.GetCommitments(_crypto, committedValues, openings2);

            // combine all commitments into allPed
            PedersenCommitment[] allPed = new PedersenCommitment[2 * length];
            for (int i = 0; i < length; ++i)
            {
                allPed[i]          = ped1[i];
                allPed[i + length] = ped2[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)
                    );
            }

            ProverEqualityParameters prover = new ProverEqualityParameters(allPed, map, _crypto);

            Assert.IsTrue(prover.Verify());

            EqualityProof proof = new EqualityProof(prover);

            Assert.IsTrue(proof.Verify(prover));
        }
        public void EQMapHashTest()
        {
            EqualityMap map1 = new EqualityMap();

            map1.Add(new PrettyName("alpha", 0), new DoubleIndex(0, 0));
            map1.Add(new PrettyName("alpha", 2), new DoubleIndex(1, 1));
            byte[] hash1 = map1.Hash(CryptoParameters.DefaultHashFunctionName);
            byte[] hash2 = map1.Hash(CryptoParameters.DefaultHashFunctionName);
            StaticHelperClass.AssertArraysAreEqual <byte>(hash1, hash2, "hash1 vs hash2 of same map.");

            EqualityMap map2 = new EqualityMap();

            map2.Add(new PrettyName("alpha", 2), new DoubleIndex(1, 1));
            map2.Add(new PrettyName("alpha", 0), new DoubleIndex(0, 0));
            byte[] hash3 = map2.Hash(CryptoParameters.DefaultHashFunctionName);
            byte[] hash4 = map2.Hash(CryptoParameters.DefaultHashFunctionName);

            StaticHelperClass.AssertArraysAreEqual <byte>(hash3, hash4, "hash3 vs hash4 of same map.");
            StaticHelperClass.AssertArraysAreEqual <byte>(hash1, hash3, "hash1 vs hash3.");
        }
        public void EQMapSerializationTest()
        {
            int length = 20;
            // 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)
                    );
            }

            string      jsonString      = CryptoSerializer.Serialize <EqualityMap>(map);
            EqualityMap deserializedMap = CryptoSerializer.Deserialize <EqualityMap>(jsonString);

            byte[] hash1 = map.Hash(DefaultHashFunction);
            byte[] hash2 = deserializedMap.Hash(DefaultHashFunction);
            StaticHelperClass.AssertArraysAreEqual(hash1, hash2, "hash of deserialized map.");
        }
예제 #10
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)");
        }