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);
        }
        public void CSBadDeserializeGroupElementArrayTest()
        {
            GroupElement[] input      = StaticHelperClass.GenerateRandomBases(10, paramIndex);
            string[]       serialized = CryptoSerializer.SerializeGroupElementArray(input, 3, 4, "blah");
            Assert.AreEqual(4, serialized.Length, "serialized array length");

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


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

            StaticHelperClass.TryBodyDelegate startIndexWaytooLarge =
                new StaticHelperClass.TryBodyDelegate(
                    () => {
                GroupElement[] output = CryptoSerializer.DeserializeGroupElementArray(serialized, 11, 10, "blah", crypto.Group);
            });
            StaticHelperClass.AssertThrowsException(startIndexWaytooLarge, typeof(Exception), "start index greater than output length ");
        }
        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.");
        }
        public void CSGroupElementArrayTest()
        {
            GroupElement[] input  = StaticHelperClass.GenerateRandomBases(10, paramIndex);
            string[]       output = CryptoSerializer.SerializeGroupElementArray(input, "blah");

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

            GroupElement[] deserialized = CryptoSerializer.DeserializeGroupElementArray(output, "blah", crypto.Group);
            StaticHelperClass.AssertArraysAreEqual(input, deserialized, "deserialized");
        }
        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");
        }
 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 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 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 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 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 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 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 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 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 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 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");
        }
 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));
 }
        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");
        }