/// <summary> /// Negates a given point on the curve. /// </summary> /// <param name="point"><see cref="CurvePoint"/> to negate.</param> /// <returns><see cref="CurvePoint"/> instance of the negation of <paramref name="point"/> on the curve.</returns> public virtual CurvePoint Negate(CurvePoint point) { if (point.Equals(CurvePoint.PointAtInfinity)) { return(point); } return(new CurvePoint(point.X, Field.Mod(-point.Y))); }
/// <inheritdoc/> public override CurvePoint Add(CurvePoint left, CurvePoint right) { BigInteger x1 = left.X; BigInteger x2 = right.X; BigInteger y1 = left.Y; BigInteger y2 = right.Y; BigInteger lambda; if (left.Equals(right)) { lambda = Field.Mod((3 * Field.Square(x1) + 2 * A * x1 + 1) * Field.InvertMult(2 * B * y1)); } else { lambda = Field.Mod((y2 - y1) * Field.InvertMult(x2 - x1)); } BigInteger x3 = Field.Mod(B * Field.Square(lambda) - x1 - x2 - A); BigInteger y3 = Field.Mod(lambda * (x1 - x3) - y1); CurvePoint result = CurvePoint.PointAtInfinity; bool pointsAreNegations = AreNegations(left, right); if (left.IsAtInfinity && right.IsAtInfinity) { result = CurvePoint.PointAtInfinity; } if (left.IsAtInfinity && !right.IsAtInfinity) { result = right.Clone(); } if (right.IsAtInfinity && !left.IsAtInfinity) { result = left.Clone(); } if (!left.IsAtInfinity && !right.IsAtInfinity && pointsAreNegations) { result = CurvePoint.PointAtInfinity; } if (!left.IsAtInfinity && !right.IsAtInfinity && !pointsAreNegations) { result = new CurvePoint(x3, y3); } return(result); }