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 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 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); } }
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 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 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 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)); }