public WeierstrassCurvePoint(FiniteFieldElement x, FiniteFieldElement y, ICurve curve) { if (!(curve is WeierstrassCurve)) throw new ArgumentException("A weierstrass curve point should only be placed on a weierstrass curve, but was placed on " + curve + ".", "curve"); _x = x; _y = y; _curve = curve as WeierstrassCurve; }
public FiniteFieldElement Add(FiniteFieldElement f2) { var resultingElement = _field.Add(f2._field) as FpFieldElement; return new FiniteFieldElement(resultingElement); }
public bool TrySqrt(out FiniteFieldElement result) { var potential_result = _field.Sqrt(); result = new FiniteFieldElement(potential_result as FpFieldElement); if (potential_result == null) { return false; } return true; }
public FiniteFieldElement Subtract(FiniteFieldElement f2) { var resultingElement = _field.Subtract(f2._field) as FpFieldElement; return new FiniteFieldElement(resultingElement); }
public FiniteFieldElement Multiply(BigInteger x) { var xInnerField = new FpFieldElement(this._field.Q, x.ToBouncyCastleBigInteger()); var xAsFiniteFieldElement = new FiniteFieldElement(xInnerField); return Multiply(xAsFiniteFieldElement); }
public FiniteFieldElement DivideBy(FiniteFieldElement f2) { var resultingElement = _field.Divide(f2._field) as FpFieldElement; return new FiniteFieldElement(resultingElement); }
public override Point Add(Point other) { if(!(other is WeierstrassCurvePoint)) throw new ArgumentException("Cannot add two points that belong to different curve-types. Second point must be WeierstrassCurvePoint.", "other"); var q = other as WeierstrassCurvePoint; if (this._curve != q._curve) throw new ArgumentException("Cannot add two points on different curves.", "other"); //p + infinity = p if (q.Equals(_curve.Infinity)) return this; if (this.Equals(_curve.Infinity)) return q; //p + -p = infinity if (this.Equals(-q)) return _curve.Infinity; FiniteFieldElement x3, y3; //if p + q where p = q, do point doubling if (this.Equals(q)) { var a = new FiniteFieldElement(_curve.A, _curve.Prime); var lambda = ((3 * ((this.X) ^ 2) + a) / (2 * this.Y)); x3 = (lambda ^ 2) - (2 * this.X); y3 = (lambda) * (this.X - x3) - this.Y; } else { //adding two distinct points. var lambda = (q.Y - this.Y) / (q.X - this.X); x3 = (lambda ^ 2) - this.X - q.X; y3 = (lambda) * (this.X - x3) - this.Y; } return new WeierstrassCurvePoint(x3, y3, _curve); }