public void TestCopyConstructor()
        {
            var point = new ECPoint(groupHandle, rawPointHandle);

            Assert.That(!point.Handle.Equals(rawPointHandle));
            Assert.That(ECPointHandle.Compare(groupHandle, rawPointHandle, point.Handle, ctx));
        }
Example #2
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));
        }
Example #3
0
 /// <inheritdoc />
 public bool IsPotentialElement(ECPoint element)
 {
     using (var ctx = BigNumberContextHandle.Create())
     {
         return(ECPointHandle.IsOnCurve(Handle, element.Handle, ctx));
     }
 }
        public void TestGetCoordinatesWithPointAtInfinity()
        {
            var point = new ECPoint(groupHandle);

            ECPointHandle.SetToInfinity(groupHandle, point.Handle);

            Assert.Throws <InvalidOperationException>(() => point.GetCoordinates());
        }
Example #5
0
 /// <inheritdoc />
 public ECPoint GenerateElement(SecureBigNumber index)
 {
     using (var ctx = BigNumberContextHandle.CreateSecure())
     {
         var res = new ECPoint(Handle);
         ECPointHandle.Multiply(Handle, res.Handle, index.Handle, ECPointHandle.Null, BigNumberHandle.Null, ctx);
         return(res);
     }
 }
Example #6
0
 /// <inheritdoc />
 public ECPoint Add(ECPoint left, ECPoint right)
 {
     using (var ctx = BigNumberContextHandle.Create())
     {
         var res = new ECPoint(Handle);
         ECPointHandle.Add(Handle, res.Handle, left.Handle, right.Handle, ctx);
         return(res);
     }
 }
Example #7
0
 /// <inheritdoc />
 public ECPoint MultiplyScalar(ECPoint e, SecureBigNumber k)
 {
     using (var ctx = BigNumberContextHandle.CreateSecure())
     {
         var res = new ECPoint(Handle);
         ECPointHandle.Multiply(Handle, res.Handle, BigNumberHandle.Null, e.Handle, k.Handle, ctx);
         return(res);
     }
 }
Example #8
0
 /// <inheritdoc />
 public ECPoint Negate(ECPoint element)
 {
     using (var ctx = BigNumberContextHandle.Create())
     {
         var p = new ECPoint(Handle, element.Handle);
         ECPointHandle.InvertInPlace(Handle, p.Handle, ctx);
         return(p);
     }
 }
Example #9
0
 /// <summary>
 /// Decodes a <see cref="ECPoint" /> instance from a byte buffer.
 /// </summary>
 /// <param name="ecGroupHandle">Native handle for the group the point lies on.</param>
 /// <param name="buffer">Byte array containing an encoding of the curve point.</param>
 /// <returns><see cref="ECPoint" /> instance of the point encoded in the buffer.</returns>
 internal static ECPoint CreateFromBytes(ECGroupHandle ecGroupHandle, byte[] buffer)
 {
     using (var ctx = BigNumberContextHandle.Create())
     {
         var point = new ECPoint(ecGroupHandle);
         ECPointHandle.FromByteBuffer(point._ecGroup, point.Handle, buffer, ctx);
         return(point);
     }
 }
Example #10
0
 /// <summary>
 /// Encodes the curve point in a byte buffer.
 /// </summary>
 /// <param name="encoding">The encoding scheme used to encode the point in the buffer.</param>
 /// <returns>Byte buffer containing the encoded curve point.</returns>
 public byte[] ToBytes(PointEncoding encoding = PointEncoding.Compressed)
 {
     using (var ctx = BigNumberContextHandle.Create())
     {
         int    requiredBufferLength = ECPointHandle.ToByteBuffer(_ecGroup, Handle, encoding, null, ctx);
         byte[] buffer       = new byte[requiredBufferLength];
         int    writtenBytes = ECPointHandle.ToByteBuffer(_ecGroup, Handle, encoding, buffer, ctx);
         Debug.Assert(writtenBytes == requiredBufferLength);
         return(buffer);
     }
 }
        public void TestIsAtInfinity()
        {
            var validPoint = new ECPoint(groupHandle, rawPointHandle);

            Assert.That(!validPoint.IsAtInfinity);

            var pointAtInf = new ECPoint(groupHandle);

            ECPointHandle.SetToInfinity(groupHandle, pointAtInf.Handle);
            Assert.That(pointAtInf.IsAtInfinity);
        }
Example #12
0
 /// <summary>
 /// Returns the affine coordinates of the curve point.
 /// </summary>
 /// <returns>Tuple containing x- and y-coordinate.</returns>
 public (BigNumber, BigNumber) GetCoordinates()
 {
     if (IsAtInfinity)
     {
         throw new InvalidOperationException("Cannot get coordinates from point at infinity!");
     }
     using (var ctx = BigNumberContextHandle.Create())
     {
         var x = new BigNumber();
         var y = new BigNumber();
         ECPointHandle.GetAffineCoordinates(_ecGroup, Handle, x.Handle, y.Handle, ctx);
         return(x, y);
     }
 }
Example #13
0
        /// <inheritdoc />
        public override bool Equals(object?obj)
        {
            ECPoint?other = obj as ECPoint;

            if (other == null)
            {
                return(false);
            }

            using (var ctx = BigNumberContextHandle.Create())
            {
                return(ECPointHandle.Compare(_ecGroup, this.Handle, other.Handle, ctx));
            }
        }
Example #14
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!");
        }
        public void TestEqualsAndHash()
        {
            var point = new ECPoint(groupHandle, rawPointHandle);

            var equalPoint = new ECPoint(groupHandle, rawPointHandle);

            Assert.That(!point.Handle.Equals(equalPoint.Handle), "Distinct points share same handle!");
            Assert.That(point.Equals(equalPoint), "Equal points are not seen as equal!");
            Assert.That(point.GetHashCode().Equals(equalPoint.GetHashCode()), "Equals points do not share same hash code!");

            var unequalPoint = new ECPoint(groupHandle);

            ECPointHandle.Add(groupHandle, unequalPoint.Handle, rawPointHandle, rawPointHandle, ctx);
            Assert.That(!point.Equals(unequalPoint), "Unequal points are seen as equal!");

            Assert.That(!point.Equals(new {}), "Point equal to anonymous object!");
        }
Example #16
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));
        }
Example #17
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));
        }
Example #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));
        }
Example #19
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));
        }
Example #20
0
 /// <summary>
 /// Creates a new default <see cref="ECPoint" />
 /// on the curve defined by a valid <see cref="ECGroupHandle" />.
 ///
 /// The coordinates of the point are unspecified.
 /// </summary>
 /// <param name="ecGroupHandle">Native handle for the group the point lies on.</param>
 internal ECPoint(ECGroupHandle ecGroupHandle)
 {
     Debug.Assert(!ecGroupHandle.IsInvalid);
     _ecGroup = ecGroupHandle;
     Handle   = ECPointHandle.Create(_ecGroup);
 }
 public void SetUp()
 {
     groupHandle    = ECGroupHandle.CreateByCurveNID((int)EllipticCurveID.Prime256v1);
     rawPointHandle = ECGroupHandle.GetGenerator(groupHandle);
     ctx            = BigNumberContextHandle.Create();
 }
Example #22
0
 /// <summary>
 /// Creates a new <see cref="ECPoint" /> instance for a given
 /// valid <see cref="ECPointHandle" />
 /// on the curve defined by a valid <see cref="ECGroupHandle" />.
 ///
 /// The structure pointed to by <paramref name="pointHandle"/> is copied into
 /// this <see cref="ECPoint" /> instance.
 /// </summary>
 /// <param name="ecGroupHandle">Native handle for the group the point lies on.</param>
 /// <param name="pointHandle">Native handle for the point on the curve.</param>
 internal ECPoint(ECGroupHandle ecGroupHandle, ECPointHandle pointHandle) : this(ecGroupHandle)
 {
     Debug.Assert(!pointHandle.IsInvalid);
     ECPointHandle.Copy(Handle, pointHandle);
 }