Library QedkConcrete.ellipticadditionmath

The affine addition formulas preserve the curve equation

The secant slope is justified by subtracting the two input equations. The tangent slope is justified by the derivative at the input point. Both reduce to the same polynomial identity; denominator tests remain explicit so this also applies to the generated division operations.
From Stdlib Require Import Ring Field.
Set Default Goal Selector "!".

Section Field.
Context {R : Type} (zero one : R) (add mul sub : R → R → R)
  (opp : R → R) (divide : R → R → R) (inverse : R → R).
Variable laws : field_theory zero one add mul sub opp divide inverse (@eq R).
Add Field coordinate_field : laws.
Local Notation "0" := zero.
Local Notation "1" := one.
Local Infix "+" := add.
Local Infix "×" := mul.
Local Infix "-" := sub.
Local Infix "/" := divide.

Definition weierstrassPolynomial a b x := x*(x×x+a)+b.

Lemma weierstrass_addition_from_slope a b x1 y1 x2 m :
  y1×y1 = weierstrassPolynomial a b x1 →
  m×m*(x2-x1)+(1+1)*m×y1 = x2×x2+x1×x2+x1×x1+a →
  let xx := m×m-x1-x2 in
  let yy := m*(x1-xx)-y1 in
  yy×yy = weierstrassPolynomial a b xx.
Proof.
  intros Hcurve Hslope. cbv zeta.
  assert (Hidentity :
    (m*(x1-(m×m-x1-x2))-y1)*(m*(x1-(m×m-x1-x2))-y1) -
      weierstrassPolynomial a b (m×m-x1-x2) =
    (y1×y1-weierstrassPolynomial a b x1)+
      (m×m-x1-x2-x1)*
        (m×m*(x2-x1)+(1+1)*m×y1-(x2×x2+x1×x2+x1×x1+a))).
  { unfold weierstrassPolynomial. ring. }
  rewrite Hcurve, Hslope in Hidentity.
  replace ((weierstrassPolynomial a b x1-weierstrassPolynomial a b x1)+
    (m×m-x1-x2-x1)*
      (x2×x2+x1×x2+x1×x1+a-(x2×x2+x1×x2+x1×x1+a))) with 0 in Hidentity by ring.
  replace ((m*(x1-(m×m-x1-x2))-y1)*(m*(x1-(m×m-x1-x2))-y1)) with
    (((m*(x1-(m×m-x1-x2))-y1)*(m*(x1-(m×m-x1-x2))-y1) -
      weierstrassPolynomial a b (m×m-x1-x2)) +
      weierstrassPolynomial a b (m×m-x1-x2)) by ring.
  rewrite Hidentity. ring.
Qed.

Lemma weierstrass_secant a b x1 y1 x2 y2 :
  y1×y1 = weierstrassPolynomial a b x1 →
  y2×y2 = weierstrassPolynomial a b x2 → x2-x1 ≠ 0 →
  let m := (y2-y1)/(x2-x1) in
  let xx := m×m-x1-x2 in
  let yy := m*(x1-xx)-y1 in
  yy×yy = weierstrassPolynomial a b xx.
Proof.
  intros Hleft Hright Hden. cbv zeta.
  set (m := (y2-y1)/(x2-x1)).
  apply weierstrass_addition_from_slope; [exact Hleft|].
  assert (Hslope : m*(x2-x1) = y2-y1) by (unfold m; field; exact Hden).
  assert (Hdifference : (x2-x1)*
    (m×m*(x2-x1)+(1+1)*m×y1-(x2×x2+x1×x2+x1×x1+a)) = 0).
  { replace ((x2-x1)*
      (m×m*(x2-x1)+(1+1)*m×y1-(x2×x2+x1×x2+x1×x1+a))) with
      ((m*(x2-x1))*(m*(x2-x1))+(1+1)*(m*(x2-x1))*y1-
        (weierstrassPolynomial a b x2-weierstrassPolynomial a b x1))
      by (unfold weierstrassPolynomial; ring).
    rewrite Hslope, <- Hleft, <- Hright. ring. }
  assert (Hzero : m×m*(x2-x1)+(1+1)*m×y1-(x2×x2+x1×x2+x1×x1+a) = 0).
  { replace (m×m*(x2-x1)+(1+1)*m×y1-(x2×x2+x1×x2+x1×x1+a)) with
      (((x2-x1)*(m×m*(x2-x1)+(1+1)*m×y1-(x2×x2+x1×x2+x1×x1+a)))/(x2-x1))
      by (field; exact Hden).
    rewrite Hdifference. field; exact Hden. }
  replace (m×m*(x2-x1)+(1+1)*m×y1) with
    ((m×m*(x2-x1)+(1+1)*m×y1-(x2×x2+x1×x2+x1×x1+a))+
      (x2×x2+x1×x2+x1×x1+a)) by ring.
  rewrite Hzero. ring.
Qed.

Lemma weierstrass_tangent a b x y :
  y×y = weierstrassPolynomial a b x → (1+1)*y ≠ 0 →
  let m := ((1+1+1)*x×x+a)/((1+1)*y) in
  let xx := m×m-x-x in
  let yy := m*(x-xx)-y in
  yy×yy = weierstrassPolynomial a b xx.
Proof.
  intros Hcurve Hden. cbv zeta.
  set (m := ((1+1+1)*x×x+a)/((1+1)*y)).
  apply weierstrass_addition_from_slope; [exact Hcurve|].
  assert (Hslope : m*((1+1)*y) = (1+1+1)*x×x+a).
  { unfold m. field. split; intro Hzero; apply Hden; rewrite Hzero; ring. }
  replace (m×m*(x-x)+(1+1)*m×y) with (m*((1+1)*y)) by ring.
  rewrite Hslope. ring.
Qed.
End Field.