Beispiel #1
0
 /// <inheritdoc />
 public bool IsPotentialElement(ECPoint element)
 {
     using (var ctx = BigNumberContextHandle.Create())
     {
         return(ECPointHandle.IsOnCurve(Handle, element.Handle, ctx));
     }
 }
Beispiel #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));
        }
Beispiel #3
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);
     }
 }
Beispiel #4
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);
     }
 }
Beispiel #5
0
 /// <summary>
 /// Multiplies this <see cref="BigNumber" /> with <paramref name="other"/>
 /// modulo <paramref name="modulo"/> and returns the result.
 ///
 /// Precisely, the returned value is <c>z = x * y % m</c>, where
 /// <c>x</c> is the value of this <see cref="BigNumber" /> instance,
 /// <c>y</c> the value of <paramref name="other"/> and <c>m</c> the
 /// value of <paramref name="modulo"/>.
 /// </summary>
 /// <param name="other">The number with which to multiply.</param>
 /// <param name="modulo">The modulo for the multiplication.</param>
 /// <returns>
 /// A <see cref="BigNumber" /> instance with value<c>z</c>.
 /// </returns>
 public BigNumber ModMul(BigNumber other, BigNumber modulo)
 {
     using (var ctx = BigNumberContextHandle.Create())
     {
         var result = new BigNumber();
         BigNumberHandle.ModMul(result.Handle, Handle, other.Handle, modulo.Handle, ctx);
         return(result);
     }
 }
Beispiel #6
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);
     }
 }
Beispiel #7
0
        /// <summary>
        /// Creates a new <see cref="EllipticCurveAlgebra" /> instance
        /// for the curve identified by <paramref name="curveId"/>.
        /// </summary>
        /// <param name="curveId">Identifier of the elliptic curve.</param>
        public EllipticCurveAlgebra(EllipticCurveID curveId)
        {
            Handle = ECGroupHandle.CreateByCurveNID((int)curveId);

            using (var ctx = BigNumberContextHandle.Create())
            {
                ECGroupHandle.PrecomputeGeneratorMultiples(Handle, ctx);
            }
        }
Beispiel #8
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);
     }
 }
Beispiel #9
0
        /// <inheritdoc />
        public override bool Equals(object?obj)
        {
            EllipticCurveAlgebra?other = obj as EllipticCurveAlgebra;

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

            using (var ctx = BigNumberContextHandle.Create())
            {
                return(ECGroupHandle.Compare(this.Handle, other.Handle, ctx));
            }
        }
Beispiel #10
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);
     }
 }
Beispiel #11
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));
            }
        }
Beispiel #12
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 #13
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 #14
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 #15
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 #16
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));
        }
 public void SetUp()
 {
     groupHandle    = ECGroupHandle.CreateByCurveNID((int)EllipticCurveID.Prime256v1);
     rawPointHandle = ECGroupHandle.GetGenerator(groupHandle);
     ctx            = BigNumberContextHandle.Create();
 }