Beispiel #1
0
        public void TestGenerateRandom()
        {
            var order = BigPrime.CreateWithoutChecks(1021);

            var expectedIndex = new BigInteger(19);
            int expectedRaw   = 7;

            var algebraMock = new Mock <ICryptoGroupAlgebra <BigInteger, int> >(MockBehavior.Strict);

            algebraMock.Setup(alg => alg.IsPotentialElement(It.IsAny <int>())).Returns(true);
            algebraMock.Setup(alg => alg.GenerateRandomElement(It.IsAny <RandomNumberGenerator>())).Returns((expectedIndex, expectedRaw));

            var expectedElement = new CryptoGroupElement <BigInteger, int>(expectedRaw, algebraMock.Object);

            var groupMock = new CryptoGroup <BigInteger, int>(algebraMock.Object);

            var rngMock = new Mock <RandomNumberGenerator>();

            var result        = groupMock.GenerateRandom(rngMock.Object);
            var resultIndex   = result.Item1;
            var resultElement = result.Item2;

            Assert.AreEqual(expectedIndex, resultIndex);
            Assert.AreEqual(expectedElement, resultElement);
        }
 /// <summary>
 /// Adds a <see cref="CryptoGroupElement{TScalar, TElement}"/> to the current instance following
 /// the addition law of the associated group.
 /// </summary>
 /// <param name="element">The <see cref="CryptoGroupElement{TScalar, TElement}"/> to add.</param>
 /// <returns>
 /// The <see cref="CryptoGroupElement{TScalar, TElement}"/> that is the group addition
 /// result of the values of <c>this</c> and <c>element</c>.
 /// </returns>
 public CryptoGroupElement <TScalar, TElement> Add(CryptoGroupElement <TScalar, TElement> element)
 {
     if (Algebra != element.Algebra)
     {
         throw new ArgumentException("Added group element must be from the same group!", nameof(element));
     }
     return(new CryptoGroupElement <TScalar, TElement>(Algebra.Add(Value, element.Value), Algebra));
 }
        public void TestGetHashCodeSameForEqual()
        {
            var algebraStub = new Mock <ICryptoGroupAlgebra <BigInteger, int> >(MockBehavior.Strict);

            algebraStub.Setup(alg => alg.IsPotentialElement(It.IsAny <int>())).Returns(true);

            var otherElement = new CryptoGroupElement <BigInteger, int>(5, algebraStub.Object);
            var element      = new CryptoGroupElement <BigInteger, int>(5, algebraStub.Object);

            Assert.AreEqual(element.GetHashCode(), otherElement.GetHashCode());
        }
        public void TestIsSafeTrue()
        {
            var algebraStub = new Mock <ICryptoGroupAlgebra <int, int> >(MockBehavior.Strict);

            algebraStub.Setup(alg => alg.IsPotentialElement(It.IsAny <int>())).Returns(true);
            algebraStub.Setup(alg => alg.IsSafeElement(It.IsAny <int>())).Returns(true);

            var element = new CryptoGroupElement <int, int>(17, algebraStub.Object);

            Assert.IsTrue(element.IsSafe);
        }
        public void TestEqualsFalseForNull()
        {
            var algebraStub = new Mock <ICryptoGroupAlgebra <BigInteger, int> >(MockBehavior.Strict);

            algebraStub.Setup(alg => alg.IsPotentialElement(It.IsAny <int>())).Returns(true);

            var  element = new CryptoGroupElement <BigInteger, int>(0, algebraStub.Object);
            bool result  = element.Equals(null);

            Assert.IsFalse(result);
        }
        public void TestToString()
        {
            var algebraStub = new Mock <ICryptoGroupAlgebra <BigInteger, int> >(MockBehavior.Strict);

            algebraStub.Setup(alg => alg.IsPotentialElement(It.IsAny <int>())).Returns(true);

            var element = new CryptoGroupElement <BigInteger, int>(5, algebraStub.Object);

            var expected = "<CryptoGroupElement: 5>";

            Assert.AreEqual(expected, element.ToString());
        }
        public void TestConstructorSetsValueCorrectly()
        {
            int elementRaw = -3;

            var algebraMock = new Mock <ICryptoGroupAlgebra <BigInteger, int> >(MockBehavior.Strict);

            algebraMock.Setup(alg => alg.IsPotentialElement(It.IsAny <int>())).Returns(true);

            var element = new CryptoGroupElement <BigInteger, int>(elementRaw, algebraMock.Object);

            Assert.AreEqual(elementRaw, element.Value);
            algebraMock.Verify(alg => alg.IsPotentialElement(It.Is <int>(x => x == elementRaw)), Times.Once());
        }
Beispiel #8
0
        public void TestGeneratorAccessor()
        {
            var expectedRaw = 3;
            var algebraMock = new Mock <ICryptoGroupAlgebra <BigInteger, int> >(MockBehavior.Strict);

            algebraMock.Setup(alg => alg.IsPotentialElement(It.IsAny <int>())).Returns(true);
            algebraMock.Setup(alg => alg.IsSafeElement(It.Is <int>(x => x == expectedRaw))).Returns(true);
            algebraMock.Setup(alg => alg.Generator).Returns(expectedRaw);

            var expected = new CryptoGroupElement <BigInteger, int>(expectedRaw, algebraMock.Object);
            var group    = new CryptoGroup <BigInteger, int>(algebraMock.Object);

            Assert.AreEqual(expected, group.Generator);
        }
Beispiel #9
0
        public void TestNegateRejectsDifferentGroupElement()
        {
            var algebraStub = new Mock <ICryptoGroupAlgebra <BigInteger, int> >(MockBehavior.Strict);
            var group       = new CryptoGroup <BigInteger, int>(algebraStub.Object);

            var otherAlgebra = new Mock <ICryptoGroupAlgebra <BigInteger, int> >(MockBehavior.Strict);

            otherAlgebra.Setup(algebra => algebra.IsPotentialElement(It.IsAny <int>())).Returns(true);
            var element = new CryptoGroupElement <BigInteger, int>(3, otherAlgebra.Object);

            Assert.Throws <ArgumentException>(
                () => group.Negate(element)
                );
        }
Beispiel #10
0
        public void TestMultiplyScalarRejectsDifferentGroupElement()
        {
            var algebraStub = new Mock <ICryptoGroupAlgebra <BigInteger, int> >(MockBehavior.Strict);
            var groupMock   = new Mock <CryptoGroup <BigInteger, int> >(MockBehavior.Strict, algebraStub.Object);

            var otherAlgebraStub = new Mock <ICryptoGroupAlgebra <BigInteger, int> >(MockBehavior.Strict);

            otherAlgebraStub.Setup(algebra => algebra.IsPotentialElement(It.IsAny <int>())).Returns(true);
            var elementStub = new CryptoGroupElement <BigInteger, int>(3, otherAlgebraStub.Object);

            Assert.Throws <ArgumentException>(
                () => groupMock.Object.MultiplyScalar(elementStub, new BigInteger(8))
                );
        }
        public void TestCloneConstructor()
        {
            int value = 3;

            var algebraStub = new Mock <ICryptoGroupAlgebra <BigInteger, int> >(MockBehavior.Strict);

            algebraStub.Setup(alg => alg.IsPotentialElement(It.IsAny <int>())).Returns(true);

            var element = new CryptoGroupElement <BigInteger, int>(value, algebraStub.Object);
            var clone   = new CryptoGroupElement <BigInteger, int>(element);

            Assert.AreEqual(element, clone);
            Assert.AreNotSame(element, clone);
        }
        public void TestConstructorFromBytesCorrect()
        {
            byte[] buffer        = new byte[0];
            int    expectedValue = 9;

            var algebraMock = new Mock <ICryptoGroupAlgebra <BigInteger, int> >(MockBehavior.Strict);

            algebraMock.Setup(alg => alg.IsPotentialElement(It.IsAny <int>())).Returns(true);
            algebraMock.Setup(alg => alg.FromBytes(It.IsAny <byte[]>())).Returns(expectedValue);

            var element = new CryptoGroupElement <BigInteger, int>(buffer, algebraMock.Object);

            Assert.AreEqual(expectedValue, element.Value);
            algebraMock.Verify(alg => alg.FromBytes(It.Is <byte[]>(x => x == buffer)));
        }
        public void TestToBytesCallsAlgebra()
        {
            var value    = 3;
            var expected = new byte[0];

            var algebraMock = new Mock <ICryptoGroupAlgebra <BigInteger, int> >(MockBehavior.Strict);

            algebraMock.Setup(alg => alg.IsPotentialElement(It.IsAny <int>())).Returns(true);
            algebraMock.Setup(alg => alg.ToBytes(It.IsAny <int>())).Returns(expected);

            var element = new CryptoGroupElement <BigInteger, int>(value, algebraMock.Object);

            element.ToBytes();

            algebraMock.Verify(alg => alg.ToBytes(It.Is <int>(x => x == value)), Times.Once());
        }
Beispiel #14
0
        public void TestFromBytesCallsAlgebra()
        {
            byte[] inputBuffer = new byte[0];

            var algebraMock = new Mock <ICryptoGroupAlgebra <BigInteger, int> >(MockBehavior.Strict);

            algebraMock.Setup(algebra => algebra.IsPotentialElement(It.IsAny <int>())).Returns(true);
            algebraMock.Setup(algebra => algebra.FromBytes(It.IsAny <byte[]>())).Returns(0);

            var group = new CryptoGroup <BigInteger, int>(algebraMock.Object);

            var expected = new CryptoGroupElement <BigInteger, int>(0, algebraMock.Object);

            Assert.AreEqual(expected, group.FromBytes(inputBuffer));
            algebraMock.Verify(algebra => algebra.FromBytes(It.Is <byte[]>(b => b == inputBuffer)), Times.Once());
        }
Beispiel #15
0
        public void TestAddCallsAlgebra()
        {
            var algebraMock = new Mock <ICryptoGroupAlgebra <BigInteger, int> >(MockBehavior.Strict);

            algebraMock.Setup(algebra => algebra.Add(2, 6)).Returns(8);
            algebraMock.Setup(algebra => algebra.IsPotentialElement(It.IsAny <int>())).Returns(true);

            var left     = new CryptoGroupElement <BigInteger, int>(2, algebraMock.Object);
            var right    = new CryptoGroupElement <BigInteger, int>(6, algebraMock.Object);
            var expected = new CryptoGroupElement <BigInteger, int>(8, algebraMock.Object);

            var group = new CryptoGroup <BigInteger, int>(algebraMock.Object);

            Assert.AreEqual(expected, group.Add(left, right));
            algebraMock.Verify(algebra => algebra.Add(2, 6), Times.Once());
        }
        public void TestAddRejectsElementFromDifferentGroup()
        {
            var otherAlgebraStub = new Mock <ICryptoGroupAlgebra <BigInteger, int> >(MockBehavior.Strict);

            otherAlgebraStub.Setup(alg => alg.IsPotentialElement(It.IsAny <int>())).Returns(true);
            var otherElement = new CryptoGroupElement <BigInteger, int>(0, otherAlgebraStub.Object);

            var algebraStub = new Mock <ICryptoGroupAlgebra <BigInteger, int> >(MockBehavior.Strict);

            algebraStub.Setup(alg => alg.IsPotentialElement(It.IsAny <int>())).Returns(true);

            var element = new CryptoGroupElement <BigInteger, int>(0, algebraStub.Object);

            Assert.Throws <ArgumentException>(
                () => element.Add(otherElement)
                );
        }
        public void TestOperatorUnaryMinus()
        {
            int value    = 3;
            int expected = -value;

            var algebraMock = new Mock <ICryptoGroupAlgebra <BigInteger, int> >(MockBehavior.Strict);

            algebraMock.Setup(alg => alg.IsPotentialElement(It.IsAny <int>())).Returns(true);
            algebraMock.Setup(alg => alg.Negate(It.IsAny <int>())).Returns((int x) => - x);

            var element = new CryptoGroupElement <BigInteger, int>(value, algebraMock.Object);

            var result = -element;

            Assert.AreEqual(value, element.Value);
            Assert.AreEqual(expected, result.Value);
            algebraMock.Verify(alg => alg.Negate(It.Is <int>(x => x == value)), Times.Once());
        }
        public void TestOperatorMultiplyRight()
        {
            int value    = 3;
            var scalar   = new BigInteger(7);
            int expected = value * (int)scalar;

            var algebraMock = new Mock <ICryptoGroupAlgebra <BigInteger, int> >(MockBehavior.Strict);

            algebraMock.Setup(alg => alg.IsPotentialElement(It.IsAny <int>())).Returns(true);
            algebraMock.Setup(alg => alg.MultiplyScalar(It.IsAny <int>(), It.IsAny <BigInteger>())).Returns(expected);

            var element = new CryptoGroupElement <BigInteger, int>(value, algebraMock.Object);

            var result = scalar * element;

            Assert.AreEqual(value, element.Value);
            Assert.AreEqual(expected, result.Value);
            algebraMock.Verify(alg => alg.MultiplyScalar(It.Is <int>(x => x == value), It.Is <BigInteger>(x => x == scalar)), Times.Once());
        }
Beispiel #19
0
        public void TestGenerateCallsAlgebra()
        {
            var index       = new BigInteger(7);
            int expectedRaw = 3;

            var algebraMock = new Mock <ICryptoGroupAlgebra <BigInteger, int> >(MockBehavior.Strict);

            algebraMock.Setup(algebra => algebra.IsPotentialElement(It.IsAny <int>())).Returns(true);
            algebraMock.Setup(algebra => algebra.GenerateElement(It.IsAny <BigInteger>())).Returns(expectedRaw);

            var group    = new CryptoGroup <BigInteger, int>(algebraMock.Object);
            var expected = new CryptoGroupElement <BigInteger, int>(expectedRaw, algebraMock.Object);

            Assert.AreEqual(expected, group.Generate(index));

            algebraMock.Verify(
                algebra => algebra.GenerateElement(It.Is <BigInteger>(x => x == index)),
                Times.Once()
                );
        }
Beispiel #20
0
        public void TestSpecificNegateCallsAlgebra()
        {
            int expectedRaw = 3;
            int elementRaw  = 8;

            var algebraMock = new Mock <ICryptoGroupAlgebra <BigInteger, int> >(MockBehavior.Strict);

            algebraMock.Setup(algebra => algebra.IsPotentialElement(It.IsAny <int>())).Returns(true);
            algebraMock.Setup(algebra => algebra.Negate(It.IsAny <int>())).Returns(expectedRaw);

            var groupMock = new CryptoGroup <BigInteger, int>(algebraMock.Object);
            var expected  = new CryptoGroupElement <BigInteger, int>(expectedRaw, algebraMock.Object);
            var element   = new CryptoGroupElement <BigInteger, int>(elementRaw, algebraMock.Object);

            Assert.AreEqual(expected, groupMock.Negate(element));

            algebraMock.Verify(
                algebra => algebra.Negate(It.Is <int>(x => x == elementRaw)),
                Times.Once()
                );
        }
        public void TestOperatorPlus()
        {
            int otherValue = 3;
            int value      = 7;
            int expected   = value + otherValue;

            var algebraMock = new Mock <ICryptoGroupAlgebra <BigInteger, int> >(MockBehavior.Strict);

            algebraMock.Setup(alg => alg.IsPotentialElement(It.IsAny <int>())).Returns(true);
            algebraMock.Setup(alg => alg.Add(It.IsAny <int>(), It.IsAny <int>())).Returns(expected);

            var otherElement = new CryptoGroupElement <BigInteger, int>(otherValue, algebraMock.Object);
            var element      = new CryptoGroupElement <BigInteger, int>(value, algebraMock.Object);

            var result = element + otherElement;

            Assert.AreEqual(otherValue, otherElement.Value);
            Assert.AreEqual(value, element.Value);
            Assert.AreEqual(expected, result.Value);
            algebraMock.Verify(alg => alg.Add(It.Is <int>(x => x == value), It.Is <int>(x => x == otherValue)), Times.Once());
        }
Beispiel #22
0
        public void TestMultiplyScalarCallsAlgebra()
        {
            var k           = new BigInteger(7);
            int expectedRaw = 3;
            int elementRaw  = 8;

            var algebraMock = new Mock <ICryptoGroupAlgebra <BigInteger, int> >(MockBehavior.Strict);

            algebraMock.Setup(algebra => algebra.IsPotentialElement(It.IsAny <int>())).Returns(true);
            algebraMock.Setup(algebra => algebra.MultiplyScalar(It.IsAny <int>(), It.IsAny <BigInteger>())).Returns(expectedRaw);

            var group    = new CryptoGroup <BigInteger, int>(algebraMock.Object);
            var expected = new CryptoGroupElement <BigInteger, int>(expectedRaw, algebraMock.Object);
            var element  = new CryptoGroupElement <BigInteger, int>(elementRaw, algebraMock.Object);

            Assert.AreEqual(expected, group.MultiplyScalar(element, k));

            algebraMock.Verify(
                algebra => algebra.MultiplyScalar(It.Is <int>(x => x == elementRaw), It.Is <BigInteger>(x => x == k)),
                Times.Once()
                );
        }
 /// <summary>
 /// Initializes a new <see cref="CryptoGroupElement{TScalar, TElement}"/> instance
 /// that is an exact copy of a given <paramref name="other"/>
 /// instance of <see cref="CryptoGroupElement{TScalar, TElement}"/>.
 /// </summary>
 /// <param name="other">
 /// The <see cref="CryptoGroupElement{TScalar, TElement}"/> instance to clone.
 /// </param>
 public CryptoGroupElement(CryptoGroupElement <TScalar, TElement> other)
 {
     Algebra = other.Algebra;
     Value   = other.Value;
 }
 /// <summary>
 /// Determines whether the specified <see cref="CryptoGroupElement{TScalar, TElement}"/>
 /// is equal to the current <see cref="CryptoGroupElement{TScalar, TElement}"/>.
 /// </summary>
 /// <param name="other">
 /// The <see cref="CryptoGroupElement{TScalar, TElement}"/> to compare with the
 /// current <see cref="CryptoGroupElement{TScalar, TElement}"/>.
 /// </param>
 /// <returns><c>true</c> no equality; otherwise, <c>false</c>.</returns>
 public bool Equals(CryptoGroupElement <TScalar, TElement>?other)
 {
     return(other != null && Algebra.Equals(other.Algebra) && Value.Equals(other.Value));
 }