Exemple #1
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));
        }
Exemple #2
0
 /// <inheritdoc />
 public bool IsPotentialElement(ECPoint element)
 {
     using (var ctx = BigNumberContextHandle.Create())
     {
         return(ECPointHandle.IsOnCurve(Handle, element.Handle, ctx));
     }
 }
Exemple #3
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);
     }
 }
Exemple #4
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);
     }
 }
Exemple #5
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);
            }
        }
Exemple #6
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);
     }
 }
Exemple #7
0
 /// <summary>
 /// Computes this <see cref="BigNumber" /> to the power
 /// of <paramref name="exponent"/> 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="exponent"/> and <c>m</c> the
 /// value of <paramref name="modulo"/>.
 ///
 /// The computation is not secure. For a secure variant see
 /// <see cref="BigNumber.ModExp(SecureBigNumber, BigNumber)" />.
 /// </summary>
 /// <param name="exponent">The exponent which to raise this <see cref="BigNumber" /> to.</param>
 /// <param name="modulo">The modulo for the exponentiation.</param>
 /// <returns>
 /// A <see cref="BigNumber" /> instance with value<c>z</c>.
 /// </returns>
 public BigNumber ModExp(BigNumber exponent, BigNumber modulo)
 {
     using (var ctx = BigNumberContextHandle.CreateSecure())
     {
         var result = new BigNumber();
         BigNumberHandle.ModExp(result.Handle, Handle, exponent.Handle, modulo.Handle, ctx);
         return(result);
     }
 }
Exemple #8
0
 /// <summary>
 /// Computes the multiplicative inverse of this <see cref="BigNumber" />
 /// modulo <paramref name="modulo"/> and returns the result.
 ///
 /// Precisely, computes <c>z</c> such that <c>x * z % m = 1</c>, where
 /// <c>x</c> is the value of this <see cref="BigNumber" /> instance
 /// and <c>m</c> the value of <paramref name="modulo"/>.
 /// </summary>
 /// <param name="modulo">The modulo for computing the multiplicative inverse.</param>
 /// <returns>
 /// A <see cref="BigNumber" /> instance with value<c>z</c>.
 /// </returns>
 public BigNumber ModReciprocal(BigNumber modulo)
 {
     using (var ctx = BigNumberContextHandle.CreateSecure())
     {
         var result = new BigNumber();
         BigNumberHandle.ModInverse(result.Handle, Handle, modulo.Handle, ctx);
         return(result);
     }
 }
Exemple #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);
     }
 }
Exemple #10
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);
     }
 }
Exemple #11
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);
     }
 }
Exemple #12
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);
     }
 }
Exemple #13
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);
     }
 }
Exemple #14
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));
            }
        }
Exemple #15
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));
            }
        }
Exemple #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!");
        }
Exemple #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));
        }
Exemple #18
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));
        }
Exemple #19
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));
        }
Exemple #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));
        }
 public void SetUp()
 {
     groupHandle    = ECGroupHandle.CreateByCurveNID((int)EllipticCurveID.Prime256v1);
     rawPointHandle = ECGroupHandle.GetGenerator(groupHandle);
     ctx            = BigNumberContextHandle.Create();
 }