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 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 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);
        }
Exemple #4
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");
        }
        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 CSFieldZqElementArrayTest2()
        {
            FieldZqElement[] input      = StaticHelperClass.GenerateRandomExponents(10, paramIndex);
            string[]         serialized = CryptoSerializer.SerializeFieldZqElementArray(input, 3, 4, "blah");
            Assert.AreEqual(4, serialized.Length, "serialized array length");

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


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

            FieldZqElement[] output3         = CryptoSerializer.DeserializeFieldZqElementArray(serialized, 6, 10, "output3", crypto.Group);
            FieldZqElement[] expectedOutput3 = new FieldZqElement[10]
            {
                null,
                null,
                null,
                null,
                null,
                null,
                input[3],
                input[4],
                input[5],
                input[6]
            };
            StaticHelperClass.AssertArraysAreEqual(expectedOutput3, output3, "output3");
        }
        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 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 BaseAndExponentAtIndexTest()
        {
            int replen = 25;

            for (int paramIndex = 0; paramIndex < _parameters.Length; ++paramIndex)
            {
                GroupElement []     bases     = StaticHelperClass.GenerateRandomBases(replen, paramIndex);
                FieldZqElement []   exponents = StaticHelperClass.GenerateRandomExponents(replen, paramIndex);
                DLRepOfGroupElement dl        = new DLRepOfGroupElement(bases, exponents, _parameters[paramIndex].Group);

                Assert.AreEqual(replen, dl.RepresentationLength, "incorrect representation length");
                for (int i = 0; i < replen; ++i)
                {
                    Assert.AreEqual(bases[i], dl.BaseAtIndex(i), "wrong base");
                    Assert.AreEqual(exponents[i], dl.ExponentAtIndex(i), "wrong exponent");
                }
            }
        }
        public void DLtoPedConstructorTest()
        {
            for (int paramIndex = 0; paramIndex < _parameters.Length; ++paramIndex)
            {
                GroupElement[]      bases     = StaticHelperClass.GenerateRandomBases(2, paramIndex);
                FieldZqElement[]    exponents = StaticHelperClass.GenerateRandomExponents(2, paramIndex);
                DLRepOfGroupElement dl        = new DLRepOfGroupElement(bases, exponents, _parameters[paramIndex].Group);
                PedersenCommitment  ped       = new PedersenCommitment(dl);

                Assert.IsTrue(ped.Validate(), "ped should be a valid pedersen commitment.");
                Assert.AreEqual(bases[0], ped.G, "Wrong G value.");
                Assert.AreEqual(bases[1], ped.H, "Wrong H value.");
                Assert.AreEqual(exponents[0], ped.CommittedValue, "wrong committed value.");
                Assert.AreEqual(exponents[1], ped.Opening, "wrong opening.");

                GroupElement expectedValue = _parameters[paramIndex].Group.MultiExponentiate(bases, exponents);
                Assert.AreEqual(expectedValue, ped.Value, "wrong value.");
            }
        }
        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 ComputeValueTest()
        {
            for (int paramIndex = 0; paramIndex < _parameters.Length; ++paramIndex)
            {
                int replen = 10;
                FieldZqElement[] exponents = StaticHelperClass.GenerateRandomExponents(replen, paramIndex);
                GroupElement[]   bases     = StaticHelperClass.GenerateRandomBases(replen, paramIndex);

                DLRepOfGroupElement actualDL      = new DLRepOfGroupElement(bases, exponents, _parameters[paramIndex].Group);
                GroupElement        expectedValue = _parameters[paramIndex].Group.MultiExponentiate(bases, exponents);

                Assert.AreEqual(expectedValue, actualDL.Value, "different values");
                Assert.AreEqual(exponents.Length, actualDL.RepresentationLength, "different number of bases");
                for (int baseIndex = 0; baseIndex < actualDL.RepresentationLength; ++baseIndex)
                {
                    Assert.AreEqual(bases[baseIndex], actualDL.BaseAtIndex(baseIndex), "different base");
                    Assert.AreEqual(exponents[baseIndex], actualDL.ExponentAtIndex(baseIndex), "different exponent");
                }
            }
        }
        public void BaseConstructorTest()
        {
            for (int paramIndex = 0; paramIndex < _parameters.Length; ++paramIndex)
            {
                int replen = 10;
                FieldZqElement[]    exponents = StaticHelperClass.GenerateRandomExponents(replen, paramIndex);
                GroupElement[]      bases     = StaticHelperClass.GenerateRandomBases(replen, paramIndex);
                DLRepOfGroupElement dl        = new DLRepOfGroupElement(bases, exponents, _parameters[paramIndex].Group);

                // check bases and exponents got copied correctly, and value was computed correctly
                GroupElement expectedValue = _parameters[paramIndex].Group.Identity;
                for (int baseIndex = 0; baseIndex < bases.Length; ++baseIndex)
                {
                    Assert.AreEqual(bases[baseIndex], dl.BaseAtIndex(baseIndex), "Wrong base");
                    Assert.AreEqual(exponents[baseIndex], dl.ExponentAtIndex(baseIndex), "wrong exponent");
                    expectedValue = expectedValue * bases[baseIndex].Exponentiate(exponents[baseIndex]);
                }
                Assert.AreEqual(expectedValue, dl.Value, "Incorrect Value");
            }
        }
        public void ComputeValueWithParametersTest()
        {
            for (int paramIndex = 0; paramIndex < _parameters.Length; ++paramIndex)
            {
                int              replen    = 14;
                GroupElement[]   bases     = GetGenerators(_parameters[paramIndex], replen);
                FieldZqElement[] exponents = StaticHelperClass.GenerateRandomExponents(replen, paramIndex);

                DLRepOfGroupElement actualDL   = new DLRepOfGroupElement(exponents, _parameters[paramIndex]);
                DLRepOfGroupElement expectedDL = new DLRepOfGroupElement(bases, exponents, _parameters[paramIndex].Group);

                //compare expectedDL and actualDL
                Assert.IsNotNull(actualDL, "actualDl null");
                Assert.IsNotNull(expectedDL, "expectedDL null");
                Assert.AreEqual(expectedDL.Value, actualDL.Value, "different values");
                Assert.AreEqual(expectedDL.RepresentationLength, actualDL.RepresentationLength, "different number of bases");

                Assert.AreEqual(expectedDL, actualDL, "ComputeValue created different objects");
            }
        }
        public void CSBadSerializeFieldZqElementArrayTest()
        {
            FieldZqElement[] input = StaticHelperClass.GenerateRandomExponents(10, paramIndex);

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

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

            // output length too long
            StaticHelperClass.TryBodyDelegate outputLengthTooLarge =
                new StaticHelperClass.TryBodyDelegate(
                    () =>
            {
                string[] serialized = CryptoSerializer.SerializeFieldZqElementArray(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.SerializeFieldZqElementArray(input, -1, 4, "blah");
            });
            StaticHelperClass.AssertThrowsException(startIndexNegative, typeof(Exception), "Start index is negative.");

            string[] jsonStrings = new string[32];
            StaticHelperClass.AssertThrowsException(
                () => { CryptoSerializer.DeserializeFieldZqElementArray(jsonStrings, 0, 0, "field", null); },
                typeof(SerializationException),
                "Group is null.");
        }
        public void ComputeValueBadInputTest()
        {
            GroupElement[]    bases     = StaticHelperClass.GenerateRandomBases(8, 1);
            FieldZqElement [] exponents = StaticHelperClass.GenerateRandomExponents(9, 1);

            bool threwException = false;

            try
            {
                DLRepOfGroupElement dl = new DLRepOfGroupElement(bases, exponents, _parameters[1].Group);
            }
            catch (Exception)
            {
                threwException = true;
            }
            Assert.IsTrue(threwException, "should throw exception when given 8 bases and 9 exponents.");


            exponents = StaticHelperClass.GenerateRandomExponents(8, 1);

            threwException = false;
            try
            {
                DLRepOfGroupElement dl = new DLRepOfGroupElement(null, exponents, _parameters[1].Group);
            }
            catch (Exception)
            {
                threwException = true;
            }
            Assert.IsTrue(threwException, "should throw exception on null bases");

            threwException = false;
            try
            {
                DLRepOfGroupElement dl = new DLRepOfGroupElement(bases, null, _parameters[1].Group);
            }
            catch (Exception)
            {
                threwException = true;
            }
            Assert.IsTrue(threwException, "should throw exception on null exponents");

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

            threwException = false;
            try
            {
                DLRepOfGroupElement dl = new DLRepOfGroupElement(bases, exponents, _parameters[4].Group);
            }
            catch (Exception)
            {
                threwException = true;
            }
            Assert.IsTrue(threwException, "should throw exception on wrong Group");

            threwException = false;
            bases          = new GroupElement[0];
            exponents      = new FieldZqElement[0];
            try
            {
                DLRepOfGroupElement dl = new DLRepOfGroupElement(bases, exponents, _parameters[4].Group);
            }
            catch (Exception)
            {
                threwException = true;
            }
            Assert.IsTrue(threwException, "should throw exception on zero length bases and exponents arrays");
        }
 public static DLRepOfGroupElement GenerateRandomDLRepOfGroupElement(int representationLength, int paramIndex)
 {
     GroupElement[]   bases     = StaticHelperClass.GenerateRandomBases(representationLength, paramIndex);
     FieldZqElement[] exponents = StaticHelperClass.GenerateRandomExponents(representationLength, paramIndex);
     return(new DLRepOfGroupElement(bases, exponents, ParameterArray[paramIndex].Group));
 }