예제 #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));
        }
예제 #2
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);
            }
        }
예제 #3
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);
     }
 }
예제 #4
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));
            }
        }
예제 #5
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));
        }
예제 #6
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));
        }
예제 #7
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));
        }
예제 #8
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();
 }
예제 #10
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);
 }
예제 #11
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);
 }