Beispiel #1
0
        public void TestEqualsTrue()
        {
            var algebra      = new EllipticCurveAlgebra(EllipticCurveID.Prime256v1);
            var otherAlgebra = new EllipticCurveAlgebra(EllipticCurveID.Prime256v1);

            Assert.That(algebra.Equals(otherAlgebra));
        }
Beispiel #2
0
        public void TestSecurityLevel()
        {
            var algebra  = new EllipticCurveAlgebra(EllipticCurveID.Prime256v1);
            var expected = algebra.OrderBitLength / 2;

            Assert.That(expected == algebra.SecurityLevel);
        }
Beispiel #3
0
        public void TestEqualsFalseForUnrelatedObject()
        {
            var algebra      = new EllipticCurveAlgebra(EllipticCurveID.Prime256v1);
            var otherAlgebra = new object();

            Assert.That(!algebra.Equals(otherAlgebra));
        }
Beispiel #4
0
        public void TestGetHashCodeSameForEqual()
        {
            var algebra      = new EllipticCurveAlgebra(EllipticCurveID.Prime256v1);
            var otherAlgebra = new EllipticCurveAlgebra(EllipticCurveID.Prime256v1);

            Assert.That(algebra.GetHashCode() == otherAlgebra.GetHashCode());
        }
Beispiel #5
0
        public void TestEqualsFalseForOtherAlgebra()
        {
            var algebra      = new EllipticCurveAlgebra(EllipticCurveID.Prime256v1);
            var otherAlgebra = new EllipticCurveAlgebra(EllipticCurveID.Prime239v3);

            Assert.That(!algebra.Equals(otherAlgebra));
        }
Beispiel #6
0
        public void TestCreateCryptoGroup()
        {
            var expectedGroupAlgebra = new EllipticCurveAlgebra(EllipticCurveID.Prime256v1);
            var group = EllipticCurveAlgebra.CreateCryptoGroup(EllipticCurveID.Prime256v1);

            Assert.That(group.Algebra.Equals(expectedGroupAlgebra));
        }
Beispiel #7
0
        public void TestIsElementForPointAtInfinity()
        {
            var algebra = new EllipticCurveAlgebra(EllipticCurveID.Prime256v1);

            Assert.That(algebra.IsPotentialElement(algebra.NeutralElement), "point at infinity not accepted by IsPotentialElement!");
            Assert.That(!algebra.IsSafeElement(algebra.NeutralElement), "point at infinity accepted by IsSafeElement!");
        }
Beispiel #8
0
        public void TestAdd()
        {
            var algebra = new EllipticCurveAlgebra(EllipticCurveID.Prime256v1);

            var generator = algebra.Generator;

            var groupHandle = ECGroupHandle.CreateByCurveNID((int)EllipticCurveID.Prime256v1);
            var ctx         = BigNumberContextHandle.Create();

            var kA     = new BigInteger(27);
            var kB     = new BigInteger(13);
            var pointA = new ECPoint(groupHandle);

            ECPointHandle.Multiply(groupHandle, pointA.Handle, new BigNumber(kA).Handle, ECPointHandle.Null, BigNumberHandle.Null, ctx);
            var pointB = new ECPoint(groupHandle);

            ECPointHandle.Multiply(groupHandle, pointB.Handle, new BigNumber(kB).Handle, ECPointHandle.Null, BigNumberHandle.Null, ctx);
            var expected = new ECPoint(groupHandle);

            ECPointHandle.Multiply(groupHandle, expected.Handle, new BigNumber(kA + kB).Handle, ECPointHandle.Null, BigNumberHandle.Null, ctx);

            var result = algebra.Add(pointA, pointB);

            Assert.That(result.Equals(expected));
        }
Beispiel #9
0
        public void TestOrder()
        {
            var algebra = new EllipticCurveAlgebra(EllipticCurveID.Prime256v1);

            var expected = NISTP256Reference.Order;

            Assert.That(algebra.Order.Equals(expected));
        }
Beispiel #10
0
        public void TestCofactor()
        {
            var algebra = new EllipticCurveAlgebra(EllipticCurveID.Prime256v1);

            var expected = NISTP256Reference.Cofactor;

            var cofactor = algebra.Cofactor;

            Assert.That(cofactor.Equals(expected));
        }
Beispiel #11
0
        public void TestNeutralElement()
        {
            var algebra = new EllipticCurveAlgebra(EllipticCurveID.Prime256v1);

            var neutralElement = algebra.NeutralElement;

            var result = algebra.Add(neutralElement, neutralElement);

            Assert.That(result.Equals(neutralElement));
        }
Beispiel #12
0
        public void TestElementBitLength()
        {
            var algebra = new EllipticCurveAlgebra(EllipticCurveID.Prime256v1);

            var expected = NISTP256Reference.ElementBitLength + 8;

            var length = algebra.ElementBitLength;

            Assert.That(length.Equals(expected));
        }
Beispiel #13
0
        public void TestDispose()
        {
            var algebra = new EllipticCurveAlgebra(EllipticCurveID.Prime256v1);

            Assert.That(!algebra.Handle.IsClosed);

            algebra.Dispose();
            Assert.That(algebra.Handle.IsClosed);

            Assert.DoesNotThrow(algebra.Dispose);
        }
Beispiel #14
0
        public void TestGenerateRandomElement()
        {
            var algebra = new EllipticCurveAlgebra(EllipticCurveID.Prime256v1);

            var generator = algebra.Generator;

            (var index, var point) = algebra.GenerateRandomElement(RandomNumberGenerator.Create());

            var expected = algebra.GenerateElement(index);

            Assert.That(point.Equals(expected));
        }
Beispiel #15
0
        public void TestGenerator()
        {
            var algebra = new EllipticCurveAlgebra(EllipticCurveID.Prime256v1);

            var expectedX = NISTP256Reference.generatorX;
            var expectedY = NISTP256Reference.generatorY;

            var generator = algebra.Generator;

            (var x, var y) = generator.GetCoordinates();
            Assert.That(x.ToBigInteger().Equals(expectedX));
            Assert.That(y.ToBigInteger().Equals(expectedY));
        }
Beispiel #16
0
        public void TestIsElement()
        {
            var algebra = new EllipticCurveAlgebra(EllipticCurveID.Prime256v1);

            var groupHandle = algebra.Handle;

            var ctx   = BigNumberContextHandle.Create();
            var index = new BigInteger(3);

            var point = new ECPoint(groupHandle);

            ECPointHandle.Multiply(groupHandle, point.Handle, new BigNumber(index).Handle, ECPointHandle.Null, BigNumberHandle.Null, ctx);

            Assert.That(algebra.IsPotentialElement(point), "valid point not accepted by IsPotentialElement!");
            Assert.That(algebra.IsSafeElement(point), "valid point not accepted by IsSafeElement!");
        }
Beispiel #17
0
        public void TestFromToBytes()
        {
            var algebra = new EllipticCurveAlgebra(EllipticCurveID.Prime256v1);

            var generator = algebra.Generator;

            var groupHandle = ECGroupHandle.CreateByCurveNID((int)EllipticCurveID.Prime256v1);

            var ctx   = BigNumberContextHandle.Create();
            var index = new BigInteger(3);
            var point = new ECPoint(groupHandle);

            ECPointHandle.Multiply(groupHandle, point.Handle, new BigNumber(index).Handle, ECPointHandle.Null, BigNumberHandle.Null, ctx);

            byte[] buffer = algebra.ToBytes(point);
            var    result = algebra.FromBytes(buffer);

            Assert.That(result.Equals(point));
        }
Beispiel #18
0
        public void TestNegate()
        {
            var algebra = new EllipticCurveAlgebra(EllipticCurveID.Prime256v1);

            var generator = algebra.Generator;

            var groupHandle = ECGroupHandle.CreateByCurveNID((int)EllipticCurveID.Prime256v1);

            var ctx   = BigNumberContextHandle.Create();
            var index = new BigInteger(3);

            var point = new ECPoint(groupHandle);

            ECPointHandle.Multiply(groupHandle, point.Handle, new BigNumber(index).Handle, ECPointHandle.Null, BigNumberHandle.Null, ctx);

            var result = algebra.Negate(point);

            var pointPlusNegated = algebra.Add(point, result);

            Assert.That(pointPlusNegated.Equals(algebra.NeutralElement));
        }
Beispiel #19
0
        public void TestGenerateElement()
        {
            var algebra = new EllipticCurveAlgebra(EllipticCurveID.Prime256v1);

            var generator = algebra.Generator;

            var groupHandle = ECGroupHandle.CreateByCurveNID((int)EllipticCurveID.Prime256v1);

            var ctx   = BigNumberContextHandle.Create();
            var index = SecureBigNumber.FromBigNumber(new BigNumber(
                                                          BigInteger.Parse("97752369786356789875745735", System.Globalization.NumberStyles.Integer)
                                                          ));

            var expected = new ECPoint(groupHandle);

            ECPointHandle.Multiply(groupHandle, expected.Handle, index.Handle, ECPointHandle.Null, BigNumberHandle.Null, ctx);

            var result = algebra.GenerateElement(index);

            Assert.That(result.Equals(expected));
        }
Beispiel #20
0
        public void TestMultipy()
        {
            var algebra = new EllipticCurveAlgebra(EllipticCurveID.Prime256v1);

            var generator = algebra.Generator;

            var groupHandle = ECGroupHandle.CreateByCurveNID((int)EllipticCurveID.Prime256v1);
            var ctx         = BigNumberContextHandle.Create();

            var factor = SecureBigNumber.FromBigNumber(new BigNumber(13));

            var basePointFactor = new BigInteger(27);
            var point           = new ECPoint(groupHandle);

            ECPointHandle.Multiply(groupHandle, point.Handle, new BigNumber(basePointFactor).Handle, ECPointHandle.Null, BigNumberHandle.Null, ctx);

            var expected = new ECPoint(groupHandle);

            ECPointHandle.Multiply(groupHandle, expected.Handle, BigNumberHandle.Null, point.Handle, factor.Handle, ctx);

            var result = algebra.MultiplyScalar(point, factor);

            Assert.That(result.Equals(expected));
        }
Beispiel #21
0
        public void TestEqualsFalseForNull()
        {
            var algebra = new EllipticCurveAlgebra(EllipticCurveID.Prime256v1);

            Assert.That(!algebra.Equals(null));
        }