public void CSBadSerializeGroupElementArrayTest()
        {
            GroupElement[] input = StaticHelperClass.GenerateRandomBases(10, paramIndex);

            StaticHelperClass.TryBodyDelegate negativeLength =
                new StaticHelperClass.TryBodyDelegate(
                    () => {
                string[] serialized = CryptoSerializer.SerializeGroupElementArray(input, 3, -1, "blah");
            });
            StaticHelperClass.AssertThrowsException(negativeLength, typeof(Exception), "negative output length");

            // zero output length
            string[] output = CryptoSerializer.SerializeGroupElementArray(input, 3, 0, "blah");
            Assert.AreEqual(0, output.Length, "output.Length.");

            // output length too long
            StaticHelperClass.TryBodyDelegate outputLengthTooLarge =
                new StaticHelperClass.TryBodyDelegate(
                    () =>
            {
                string[] serialized = CryptoSerializer.SerializeGroupElementArray(input, 3, 9, "blah");
            });
            StaticHelperClass.AssertThrowsException(outputLengthTooLarge, typeof(Exception), "Output length too large");

            // copy index negative
            StaticHelperClass.TryBodyDelegate startIndexNegative =
                new StaticHelperClass.TryBodyDelegate(
                    () =>
            {
                string[] serialized = CryptoSerializer.SerializeGroupElementArray(input, -1, 4, "blah");
            });
            StaticHelperClass.AssertThrowsException(startIndexNegative, typeof(Exception), "Start index is negative.");
        }
Esempio n. 2
0
        public void InequalityParamsSerializationTest()
        {
            for (int i = 0; i < StaticHelperClass.ParameterArray.Length; ++i)
            {
                CryptoParameters   crypto = StaticHelperClass.ParameterArray[i];
                FieldZqElement     x      = crypto.FieldZq.GetElement(1000);
                FieldZqElement     y      = crypto.FieldZq.GetElement(2000);
                PedersenCommitment X      = new PedersenCommitment(x, crypto);
                PedersenCommitment Y      = new PedersenCommitment(y, crypto);

                VerifierInequalityProofParameters verifier = new VerifierInequalityProofParameters(X.Value, Y.Value, crypto);
                string json = CryptoSerializer.Serialize <VerifierInequalityProofParameters>(verifier);
                VerifierInequalityProofParameters deserialized = CryptoSerializer.Deserialize <VerifierInequalityProofParameters>(json);
                StaticHelperClass.AssertProofParametersAreEqual(verifier, deserialized, "Verifier X Y");

                verifier     = new VerifierInequalityProofParameters(X.Value, y, crypto);
                json         = CryptoSerializer.Serialize <VerifierInequalityProofParameters>(verifier);
                deserialized = CryptoSerializer.Deserialize <VerifierInequalityProofParameters>(json);
                StaticHelperClass.AssertProofParametersAreEqual(verifier, deserialized, "Verifier X value");

                ProverInequalityProofParameters prover = new ProverInequalityProofParameters(X, Y, crypto);
                json = CryptoSerializer.Serialize <ProverInequalityProofParameters>(prover);
                ProverInequalityProofParameters dProver = CryptoSerializer.Deserialize <ProverInequalityProofParameters>(json);
                StaticHelperClass.AssertProofParametersAreEqual(dProver, prover, "Prover X Y");

                prover  = new ProverInequalityProofParameters(X, y, crypto);
                json    = CryptoSerializer.Serialize <ProverInequalityProofParameters>(prover);
                dProver = CryptoSerializer.Deserialize <ProverInequalityProofParameters>(json);
                StaticHelperClass.AssertProofParametersAreEqual(dProver, prover, "Prover X value");
            }
        }
        public void CSBadDeserializeFieldZqElementArrayTest()
        {
            FieldZqElement[] input      = StaticHelperClass.GenerateRandomExponents(10, paramIndex);
            string[]         serialized = CryptoSerializer.SerializeFieldZqElementArray(input, 3, 4, "blah");
            Assert.AreEqual(4, serialized.Length, "serialized array length");

            StaticHelperClass.TryBodyDelegate negativeStartIndex =
                new StaticHelperClass.TryBodyDelegate(
                    () =>
            {
                FieldZqElement[] output = CryptoSerializer.DeserializeFieldZqElementArray(serialized, -1, 10, "blah", crypto.Group);
            });
            StaticHelperClass.AssertThrowsException(negativeStartIndex, typeof(Exception), "negative start index");


            StaticHelperClass.TryBodyDelegate startIndexTooLarge =
                new StaticHelperClass.TryBodyDelegate(
                    () =>
            {
                FieldZqElement[] output = CryptoSerializer.DeserializeFieldZqElementArray(serialized, 8, 10, "blah", crypto.Group);
            });
            StaticHelperClass.AssertThrowsException(startIndexTooLarge, typeof(Exception), "start index too large");

            StaticHelperClass.TryBodyDelegate startIndexWaytooLarge =
                new StaticHelperClass.TryBodyDelegate(
                    () =>
            {
                FieldZqElement[] output = CryptoSerializer.DeserializeFieldZqElementArray(serialized, 11, 10, "blah", crypto.Group);
            });
            StaticHelperClass.AssertThrowsException(startIndexWaytooLarge, typeof(Exception), "start index greater than output length ");
        }
        public void DLGetHashCodeTest()
        {
            FieldZqElement[]    exponents = StaticHelperClass.GenerateRandomExponents(8, 1);
            DLRepOfGroupElement dl        = new DLRepOfGroupElement(exponents, _parameters[1]);

            Assert.AreEqual(dl.BaseAtIndex(0).GetHashCode(), dl.GetHashCode());
        }
        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 CSDLRepArrayTestWithGroup()
        {
            DLRepOfGroupElement[] input          = StaticHelperClass.GenerateRandomDLRepOfGroupElementArray(10, 7, this.paramIndex);
            FieldZqElement[][]    inputExponents = new FieldZqElement[10][];
            for (int dlIndex = 0; dlIndex < 10; ++dlIndex)
            {
                inputExponents[dlIndex] = new FieldZqElement[7];
                for (int exponentIndex = 0; exponentIndex < 7; ++exponentIndex)
                {
                    inputExponents[dlIndex][exponentIndex] = input[dlIndex].ExponentAtIndex(exponentIndex);
                }
            }


            string[] serializedTT          = CryptoSerializer.Serialize(input, true, true);
            DLRepOfGroupElement[] outputTT = CryptoSerializer.Deserialize <DLRepOfGroupElement>(serializedTT, null, null);
            StaticHelperClass.AssertArraysAreEqual(input, outputTT, "outputTT");

            string[]              serializedNT = CryptoSerializer.Serialize(input, true, false);
            GroupElement[]        newBases     = StaticHelperClass.GenerateRandomBases(input[0].RepresentationLength, this.paramIndex);
            DLRepOfGroupElement[] outputNT     = CryptoSerializer.Deserialize <DLRepOfGroupElement>(serializedNT, null, newBases);
            Assert.AreEqual(input.Length, outputNT.Length, "outputNT.Length");
            for (int i = 0; i < outputNT.Length; ++i)
            {
                DLRepOfGroupElement expected = new DLRepOfGroupElement(newBases, inputExponents[i], this.crypto.Group);
                expected.Value = input[i].Value;
                Assert.AreEqual(expected, outputNT[i], "outputNT " + i);
            }

            FieldZqElement[] commitments = StaticHelperClass.GenerateRandomExponents(10, this.paramIndex);
            FieldZqElement[] openings    = StaticHelperClass.GenerateRandomExponents(10, this.paramIndex);
            input = PedersenCommitment.GetCommitments(this.crypto, commitments, openings);
        }
Esempio n. 7
0
        public void PPSerializationTest()
        {
            int             paramIndex = 4;
            ProofParameters original   = new ProofParameters(StaticHelperClass.ParameterArray[paramIndex]);

            DLRepOfGroupElement [] witnesses = new DLRepOfGroupElement[10];
            GroupElement[]         bases     = new GroupElement[5] {
                original.Generators[0],
                original.Generators[1],
                original.Generators[2],
                original.Generators[3],
                original.Generators[4]
            };
            for (int i = 0; i < 10; ++i)
            {
                FieldZqElement[] exponents = StaticHelperClass.GenerateRandomExponents(5, paramIndex);
                witnesses[i] = new DLRepOfGroupElement(bases, exponents, original.Group);
            }
            original.setProverParameters(witnesses);

            string          serializedParams = CryptoSerializer.Serialize <ProofParameters>(original);
            ProofParameters deserialized     = CryptoSerializer.Deserialize <ProofParameters>(serializedParams);

            Assert.AreEqual(original.ProverParameters, deserialized.ProverParameters, "ProverParameters field improperly deserialized");
            StaticHelperClass.AssertArraysAreEqual(original.Generators, deserialized.Generators, "Generators.");
            StaticHelperClass.AssertArraysAreEqual(original.PublicValues, deserialized.PublicValues, "Public Values.");
            StaticHelperClass.AssertArraysAreEqual(original.Witnesses, deserialized.Witnesses, "Witnesses");
            Assert.AreEqual(original.Group.G, deserialized.Group.G, "Group.group.g");
        }
Esempio n. 8
0
        public void EQTwoTokenEqualityTest()
        {
            // Both tokens will not hash attributes
            // but example also works if hashAttributes=true
            bool hashAttributes = true;

            // Setting up IssuerParameters for token1
            byte[]   uidP1             = new byte[] { 1, 1, 2, 3, 5, 7 };
            byte[]   tokenInformation1 = new byte[] { 1, 2, 3, 4, 5, 6, 7 };
            byte[][] attributes1       = new byte[][]
            {
                _encoding.GetBytes("Attribute 1"),
                _encoding.GetBytes("Attribute 2"),
                _encoding.GetBytes("Target Attribute"), // this is the attribute we'll compare
                _encoding.GetBytes("Attribute 4")
            };

            // Setting up IssuerParameters for token2
            byte[]   tokenInformation2 = new byte[] { 12, 13, 14, 15, 0, 10 };
            byte[]   uidP2             = new byte[] { 3, 1, 4, 1, 5 };
            byte[][] attributes2       = new byte[][]
            {
                _encoding.GetBytes("Target Attribute"), // this is the attribute we'll compare
                _encoding.GetBytes("Attribute 6"),
                _encoding.GetBytes("Attribute 7"),
                _encoding.GetBytes("Attribute 8")
            };

            // generate tokens
            ProverPresentationProtocolParameters   prover1, prover2;
            VerifierPresentationProtocolParameters verifier1, verifier2;

            StaticHelperClass.GetUProveParameters(hashAttributes, out prover1, out verifier1, tokenInformation1, attributes1, null, uidP1);
            StaticHelperClass.GetUProveParameters(hashAttributes, out prover2, out verifier2, tokenInformation2, attributes2, null, uidP2);

            // Create equality proof
            EqualityProof eqProof = new EqualityProof(
                prover1, // token1
                3,       // target attribute in token1
                prover2, // token2
                1);      // target attribute in token2

            // ....
            // Send data to verifier, including eqProof
            // ....
            // Verify tokens
            // ...


            // Verify equality proof
            bool success = eqProof.Verify(
                verifier1,
                3,
                verifier2,
                1);

            Assert.IsTrue(success, "Could not verify proof.");
        }
Esempio n. 9
0
        public void SMBadConstructorTest()
        {
            ProverSetMembershipParameters badProver = new ProverSetMembershipParameters(_cryptoParameters);

            StaticHelperClass.AssertThrowsException(
                new StaticHelperClass.TryBodyDelegate(() => { SetMembershipProof proof = new SetMembershipProof(badProver); }),
                typeof(Exception),
                "Constructor SetMembershipProof called with bad prover parameters.");
        }
Esempio n. 10
0
        public void EQTokenAndDLTest()
        {
            // In this example, the token hashes the attribute
            // but example also works if hashAttributes=false
            bool hashAttributes = true;

            // Setting up attributes for token
            byte[][] attributes = new byte[][]
            {
                _encoding.GetBytes("Attribute 1"),
                _encoding.GetBytes("Attribute 2"),
                _encoding.GetBytes("Teaching Assistant"), // this is the attribute we'll compare
                _encoding.GetBytes("Attribute 4")
            };

            // generate token
            ProverPresentationProtocolParameters   prover;
            VerifierPresentationProtocolParameters verifier;

            StaticHelperClass.GetUProveParameters(hashAttributes, out prover, out verifier, null, attributes);
            OpenUProveToken token = new OpenUProveToken(prover);

            Assert.IsTrue(token.Validate(null), "validate token.");


            // generate pedersen commitment to Teaching Assistant
            PedersenCommitment ped = new PedersenCommitment(prover.IP, 3, _encoding.GetBytes("Teaching Assistant"));

            // Verify they are equal
            Assert.AreEqual(token.AttributeXI(3), ped.CommittedValue, "Token and PedersenCommitment different.");

            // Create a proof that the 3rd attribute in token is equal to the committed value in ped.
            ProverEqualityParameters eqProver = new ProverEqualityParameters(
                token, // token
                3,     // 3rd attribute is the 3rd exponent
                ped,   // pedersen commitment
                0,     // committed value is the 0th exponent
                new CryptoParameters(prover.IP));
            EqualityProof proof = new EqualityProof(eqProver);

            // Verify proof
            ClosedUProveToken closedToken = new ClosedUProveToken(verifier);

            Assert.IsTrue(closedToken.AreBasesEqual(token), "token bases.");
            IStatement closedPed = ped.GetStatement();

            Assert.IsTrue(closedPed.AreBasesEqual(ped), "token bases.");
            VerifierEqualityParameters eqVerifier = new VerifierEqualityParameters(
                closedToken, // verifier token information
                3,           // 3rd attribute is the 3nd exponent
                closedPed,   // verifier information about ped
                0,           // committed value is the 0th exponent
                new CryptoParameters(prover.IP));

            Assert.IsTrue(proof.Verify(eqVerifier));
        }
        public void CSFieldZqElementArrayTest()
        {
            FieldZqElement[] input  = StaticHelperClass.GenerateRandomExponents(10, paramIndex);
            string[]         output = CryptoSerializer.SerializeFieldZqElementArray(input, "blah");

            Assert.AreEqual(input.Length, output.Length, "serialized array length different.");

            FieldZqElement[] deserialized = CryptoSerializer.DeserializeFieldZqElementArray(output, "blah", crypto.Group);
            StaticHelperClass.AssertArraysAreEqual(input, deserialized, "deserialized");
        }
        public void CSCheckDeserializationInputTest()
        {
            PrivateType serializer = new PrivateType(typeof(CryptoSerializer));

            object [] parameters = new object[] { new string[] { "b", "a" }, 0, 0, "lalla", null };
            StaticHelperClass.AssertThrowsException(
                () => { serializer.InvokeStatic("CheckDeserializationInput", parameters); },
                typeof(SerializationException),
                "Group is null");
        }
 public static void AssertProofParametersAreEqual(ProofParameters A, ProofParameters B, string msg)
 {
     Assert.AreEqual(A.ProverParameters, B.ProverParameters, msg + " Prover parameters");
     Assert.AreEqual(A.GetType(), B.GetType(), msg + " different classes");
     Assert.AreEqual(A.Group.GroupName, B.Group.GroupName, msg + " Groups");
     Assert.AreEqual(A.Group.Q, B.Group.Q, msg + " Q");
     StaticHelperClass.AssertArraysAreEqual(A.Generators, B.Generators, msg + " Generators");
     StaticHelperClass.AssertArraysAreEqual <GroupElement>(A.PublicValues, B.PublicValues, msg + " Public values");
     StaticHelperClass.AssertArraysAreEqual <DLRepOfGroupElement>(A.Witnesses, B.Witnesses, msg + " Witnesses");
 }
        public void CSGroupElementArrayTest2()
        {
            GroupElement[] input      = StaticHelperClass.GenerateRandomBases(10, paramIndex);
            string[]       serialized = CryptoSerializer.SerializeGroupElementArray(input, 3, 4, "blah");
            Assert.AreEqual(4, serialized.Length, "serialized array length");

            GroupElement[]  output         = CryptoSerializer.DeserializeGroupElementArray(serialized, 3, 10, "output", crypto.Group);
            GroupElement [] expectedOutput = new GroupElement[10]
            {
                null,
                null,
                null,
                input[3],
                input[4],
                input[5],
                input[6],
                null,
                null,
                null
            };
            StaticHelperClass.AssertArraysAreEqual(expectedOutput, output, "output");


            GroupElement[] output2         = CryptoSerializer.DeserializeGroupElementArray(serialized, 0, 10, "output2", crypto.Group);
            GroupElement[] expectedOutput2 = new GroupElement[10]
            {
                input[3],
                input[4],
                input[5],
                input[6],
                null,
                null,
                null,
                null,
                null,
                null
            };
            StaticHelperClass.AssertArraysAreEqual(expectedOutput2, output2, "output2");

            GroupElement[] output3         = CryptoSerializer.DeserializeGroupElementArray(serialized, 6, 10, "output3", crypto.Group);
            GroupElement[] expectedOutput3 = new GroupElement[10]
            {
                null,
                null,
                null,
                null,
                null,
                null,
                input[3],
                input[4],
                input[5],
                input[6]
            };
            StaticHelperClass.AssertArraysAreEqual(expectedOutput3, output3, "output3");
        }
Esempio n. 15
0
        public void SMUProveIntegrationTestWithSMRandom()
        {
            // In this example, the token hashes the attribute
            // but example also works if hashAttributes=false
            bool hashAttributes = true;

            // Setting up attributes for token
            byte[][] tokenAttributes = new byte[][]
            {
                _encoding.GetBytes("Attribute 1"),
                _encoding.GetBytes("Attribute 2"),
                _encoding.GetBytes("Teaching Assistant"), // this is the attribute we'll compare
                _encoding.GetBytes("Attribute 4")
            };

            // We will prove that the target token attribute is in this set
            byte[][] setValues = new byte[][]
            {
                _encoding.GetBytes("Teaching Assistant"),
                _encoding.GetBytes("Student"),
                _encoding.GetBytes("Professor"), // this is the attribute we'll compare
                _encoding.GetBytes("Dean")
            };

            // generate token
            ProverPresentationProtocolParameters   prover;
            VerifierPresentationProtocolParameters verifier;

            StaticHelperClass.GetUProveParameters(hashAttributes, out prover, out verifier, null, tokenAttributes);

            // Get random data from revocation proof
            CryptoParameters crypto = new CryptoParameters(prover.IP);
            SetMembershipProofGenerationRandomData randomData = SetMembershipProofGenerationRandomData.Generate(crypto.FieldZq, setValues.Length - 1);

            // Create set membership proof
            SetMembershipProof setProof = new SetMembershipProof(
                prover,             // token
                3,                  // target attribute in token
                setValues,          // claim: target attribute is in this set
                randomData);


            // ...
            // Send token and set membership proof to verifier
            // ...

            bool success = setProof.Verify(
                verifier,                           // verifier token description
                3,                                  // target attribute index
                setValues);                         // check target attribute is in this set

            Assert.IsTrue(success, "Could not verify proof.");
        }
 public void Get_G_H_Tests()
 {
     for (int i = 0; i < _parameters.Length; ++i)
     {
         GroupElement[]           bases = StaticHelperClass.GenerateRandomBases(2, i);
         GroupElement             value = _parameters[i].Generators[0];
         ClosedPedersenCommitment ped   = new ClosedPedersenCommitment(bases, value, _parameters[i].Group);
         Assert.AreEqual(bases[0], ped.G, "Failed to get G.");
         Assert.AreEqual(bases[1], ped.H, "Failed to get H.");
         Assert.IsTrue(ped.Validate());
     }
 }
        public void BDVerifierSerializationTest()
        {
            VerifierBitDecompositionParameters verifier = new VerifierBitDecompositionParameters(
                _parameters[0].G, _parameters[0].Generators, _parameters[0]);

            string jsonString = CryptoSerializer.Serialize <VerifierBitDecompositionParameters>(verifier);
            VerifierBitDecompositionParameters deserializedVerifier = CryptoSerializer.Deserialize <VerifierBitDecompositionParameters>(jsonString);

            Assert.AreEqual(verifier.Group.GroupName, deserializedVerifier.Group.GroupName);
            StaticHelperClass.AssertArraysAreEqual(verifier.PublicValues, deserializedVerifier.PublicValues, "PublicValues");
            StaticHelperClass.AssertArraysAreEqual <DLRepOfGroupElement>(verifier.Witnesses, deserializedVerifier.Witnesses, "Witnesses");
        }
        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");
        }
Esempio n. 19
0
        public void InequalityUProveIntegrationTestUKV()
        {
            // Both tokens will hash attributes
            // but example also works if hashAttributes=false
            bool hashAttributes = true;

            // Setting up IssuerParameters for token1
            byte[]   uidP1             = new byte[] { 1, 1, 2, 3, 5, 8 };
            byte[]   tokenInformation1 = new byte[] { 1, 2, 3, 4, 5, 6, 7 };
            byte[][] attributes1       = new byte[][]
            {
                _encoding.GetBytes("Attribute 1"),
                _encoding.GetBytes("Attribute 2"),
                _encoding.GetBytes("Teaching Assistant"), // this is the attribute we'll compare
                _encoding.GetBytes("Attribute 4")
            };

            // Setting up IssuerParameters for token2
            byte[]   tokenInformation2 = new byte[] { 12, 13, 14, 15, 0, 10 };
            byte[]   uidP2             = new byte[] { 3, 1, 4, 1, 5 };
            byte[][] attributes2       = new byte[][]
            {
                _encoding.GetBytes("Student"), // this is the attribute we'll compare
                _encoding.GetBytes("Attribute 2"),
                _encoding.GetBytes("Attribute 3"),
                _encoding.GetBytes("Attribute 4")
            };

            // generate tokens
            ProverPresentationProtocolParameters   prover1, prover2;
            VerifierPresentationProtocolParameters verifier1, verifier2;

            StaticHelperClass.GetUProveParameters(hashAttributes, out prover1, out verifier1, tokenInformation1, attributes1, null, uidP1);
            StaticHelperClass.GetUProveParameters(hashAttributes, out prover2, out verifier2, tokenInformation2, attributes2, null, uidP2);

            // Create Inequality Proof
            InequalityProof ieqProof = new InequalityProof(
                prover1,
                3,
                prover2,
                1);

            // .... Send inequality proof to Verifier ....

            // Verify proof
            bool success = ieqProof.Verify(
                verifier1,
                3,
                verifier2,
                1);

            Assert.IsTrue(success, "Could not verify proof.");
        }
        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.");
        }
Esempio n. 21
0
        public void SMVerifierParamSerializationTest()
        {
            // prover test
            VerifierSetMembershipParameters verifier = new VerifierSetMembershipParameters(_cryptoParameters.G, ValidDaysOfTheWeek, _cryptoParameters);

            string jsonString = CryptoSerializer.Serialize <VerifierSetMembershipParameters>(verifier);
            VerifierSetMembershipParameters deserializedVerifier = CryptoSerializer.Deserialize <ProverSetMembershipParameters>(jsonString);

            Assert.AreEqual(verifier.Group.GroupName, deserializedVerifier.Group.GroupName);
            StaticHelperClass.AssertArraysAreEqual(verifier.MemberSet, deserializedVerifier.MemberSet, "MemberSet");
            StaticHelperClass.AssertArraysAreEqual(verifier.PublicValues, deserializedVerifier.PublicValues, "PublicValues");
            StaticHelperClass.AssertArraysAreEqual <DLRepOfGroupElement>(verifier.Witnesses, deserializedVerifier.Witnesses, "Witnesses");
        }
        public void DLRepEqualsWrongExponentTest()
        {
            GroupElement[]   bases     = StaticHelperClass.GenerateRandomBases(8, 0);
            FieldZqElement[] exponents = StaticHelperClass.GenerateRandomExponents(8, 0);

            DLRepOfGroupElement dl = new DLRepOfGroupElement(bases, exponents, _parameters[0].Group);

            exponents[7] = exponents[2];
            DLRepOfGroupElement badDL = new DLRepOfGroupElement(bases, exponents, _parameters[0].Group);

            Assert.IsFalse(dl.Equals(badDL), "should fail due to bad exponent.");
            Assert.IsFalse(badDL.Equals(dl), "should fail due to bad exponent.");
        }
        public void TryStrictMultiplyWithExponentsTest()
        {
            bool success;

            DLRepOfGroupElement [] dlArray;
            FieldZqElement []      exponents;
            DLRepOfGroupElement    product;

            for (int paramIndex = 0; paramIndex < _parameters.Length; ++paramIndex)
            {
                dlArray = new DLRepOfGroupElement[128];
                GroupElement expectedProduct = _parameters[paramIndex].Group.Identity;
                for (int i = 0; i < dlArray.Length; ++i)
                {
                    exponents        = StaticHelperClass.GenerateRandomExponents(5, paramIndex);
                    dlArray[i]       = new DLRepOfGroupElement(exponents, _parameters[paramIndex]);
                    expectedProduct *= dlArray[i].Value;
                }

                DLRepOfGroupElement actualProduct;
                success = DLRepOfGroupElement.TryStrictMultiply(dlArray, out actualProduct);
                Assert.IsTrue(success, "TryStrictMultiply should have succeeded.");
                Assert.IsNotNull(actualProduct, "actualProduct should be set to a value.");
                Assert.IsTrue(actualProduct.AreBasesEqual(dlArray[0]), "Bases should be the same.");
                Assert.AreEqual(expectedProduct, actualProduct.Value, "Value computed incorrectly.");
            }

            // fail on null/ empty input
            DLRepOfGroupElement dl;

            success = DLRepOfGroupElement.TryStrictMultiply(null, out dl);
            Assert.IsFalse(success);

            DLRepOfGroupElement[] emptyArray = new DLRepOfGroupElement[0];
            success = DLRepOfGroupElement.TryStrictMultiply(emptyArray, out dl);
            Assert.IsFalse(success);

            // fail because bases are different
            dlArray = new DLRepOfGroupElement[5];
            GroupElement [] bases = StaticHelperClass.GenerateRandomBases(8, 5);
            for (int dlIndex = 0; dlIndex < dlArray.Length - 1; ++dlIndex)
            {
                exponents        = StaticHelperClass.GenerateRandomExponents(8, 5);
                dlArray[dlIndex] = new DLRepOfGroupElement(bases, exponents, _parameters[5].Group);
            }
            GroupElement [] badBases = StaticHelperClass.GenerateRandomBases(8, 5);
            exponents = StaticHelperClass.GenerateRandomExponents(8, 5);
            dlArray[dlArray.Length - 1] = new DLRepOfGroupElement(badBases, exponents, _parameters[5].Group);
            success = DLRepOfGroupElement.TryStrictMultiply(dlArray, out product);
            Assert.IsFalse(success, "should fail since one of the elements in dlArray has different bases.");
        }
        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 PedCommitmentBadConstructorTest()
        {
            int paramIndex = 6;

            GroupElement[]     bases     = null;
            FieldZqElement[]   exponents = null;
            Group              group     = _parameters[paramIndex].Group;
            PedersenCommitment ped;

            // null input
            bool threwException = false;

            try
            {
                ped = new PedersenCommitment(bases, exponents, group);
            }
            catch (Exception)
            {
                threwException = true;
            }
            Assert.IsTrue(threwException, "should throw exception on null input.");

            // wrong length bases & exponent arrays
            threwException = false;
            bases          = StaticHelperClass.GenerateRandomBases(1, paramIndex);
            exponents      = StaticHelperClass.GenerateRandomExponents(1, paramIndex);
            try
            {
                ped = new PedersenCommitment(bases, exponents, group);
            }
            catch (Exception)
            {
                threwException = true;
            }
            Assert.IsTrue(threwException, "should throw exception when bases and exponents are arrays of length 1.");

            // wrong length bases & exponent arrays
            threwException = false;
            bases          = StaticHelperClass.GenerateRandomBases(3, paramIndex);
            exponents      = StaticHelperClass.GenerateRandomExponents(3, paramIndex);
            try
            {
                ped = new PedersenCommitment(bases, exponents, group);
            }
            catch (Exception)
            {
                threwException = true;
            }
            Assert.IsTrue(threwException, "should throw exception when bases and exponents are arrays of length 3.");
        }
        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 TokenConstructorTest()
        {
            // generate prover parameters using recommended parameters.
            ProverPresentationProtocolParameters   proverParams;
            VerifierPresentationProtocolParameters verifierParams;

            StaticHelperClass.GetUProveParameters(false, out proverParams, out verifierParams);

            // create token
            OpenUProveToken token = new OpenUProveToken(proverParams);

            // get expected public key
            Assert.AreEqual(proverParams.KeyAndToken.Token.H, token.PublicKey, "check public key");
            Assert.IsTrue(token.Validate(null), "validate token.");
        }
        public static void Init(TestContext context)
        {
            // create parameter sets
            _parameters    = new CryptoParameters[6];
            _parameters[0] = new CryptoParameters(ECParameterSets.ParamSet_EC_P256_V1, null);
            _parameters[1] = new CryptoParameters(ECParameterSets.ParamSet_EC_P384_V1, null);
            _parameters[2] = new CryptoParameters(ECParameterSets.ParamSet_EC_P521_V1, null);
            _parameters[3] = new CryptoParameters(SubgroupParameterSets.ParamSetL1024N160V1, null);
            _parameters[4] = new CryptoParameters(SubgroupParameterSets.ParamSetL2048N256V1, null);
            _parameters[5] = new CryptoParameters(SubgroupParameterSets.ParamSetL3072N256V1, null);
//            _parameters[6] = new CryptoParameters(ECParameterSets.ParamSet_EC_BN254_V1, null);


            // generate prover parameters using recommended parameters.
            StaticHelperClass.GetUProveParameters(true, out _proverParams, out _verifierParams);
        }
        public void CryptoUProveConstructorTest()
        {
            ProverPresentationProtocolParameters   prover;
            VerifierPresentationProtocolParameters verifier;

            StaticHelperClass.GetUProveParameters(false, out prover, out verifier);

            CryptoParameters crypto = new CryptoParameters(prover.IP);

            Assert.AreEqual(prover.IP.Gq, crypto.Group);
            Assert.AreEqual(prover.IP.UidH, crypto.HashFunctionName);
            Assert.AreEqual(prover.IP.Gq.G, crypto.G);
            Assert.AreEqual(prover.IP.G[1], crypto.H);
            Assert.AreEqual(prover.IP.Zq.One, crypto.FieldZq.One);
            Assert.IsTrue(crypto.Verify(), "Verify should pass.");
        }
        public void BDBadProverParameterTest()
        {
            ProverBitDecompositionParameters prover = new ProverBitDecompositionParameters(
                new PedersenCommitment(_parameters[2].FieldZq.GetElement(10), _parameters[2]),
                4,
                _parameters[2]);


            prover.Witnesses[3] = new PedersenCommitment(_parameters[2].FieldZq.GetElement(2), _parameters[2]);
            StaticHelperClass.AssertThrowsException
            (
                () => { BitDecompositionProof proof = new BitDecompositionProof(prover); },
                typeof(Exception),
                "Bad prover parameters."
            );
        }