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)); }
/// <inheritdoc /> public bool IsPotentialElement(ECPoint element) { using (var ctx = BigNumberContextHandle.Create()) { return(ECPointHandle.IsOnCurve(Handle, element.Handle, ctx)); } }
/// <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); } }
/// <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); } }
/// <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); } }
/// <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); } }
/// <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); } }
/// <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); } }
/// <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); } }
/// <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); } }
/// <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); } }
/// <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); } }
/// <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); } }
/// <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)); } }
/// <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)); } }
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 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)); }
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)); }
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)); }
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(); }