Library QedkConcrete.swuprime

Simplified SWU coordinates over the generated prime-field arithmetic

This isolates the algebra from the concrete BLS functor parameters. Its square-root premises are discharged by the concrete BLS proofs; no curve or square-root behavior is postulated globally.
From Stdlib Require Import ZArith Lia Field Zmod.Zmod.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Ec.Curve QedkConcrete.primefieldresidues
  QedkConcrete.swumath QedkConcrete.nativefieldbounds QedkConcrete.primefieldarith.
Set Default Goal Selector "!".
Local Open Scope Z_scope.

Section Map.
Context {field : PF.FArgs} {UI : Basics.Unreachable Numeric.Integer.t}
  {UF : Basics.Unreachable PF.t}.
Local Notation p := (@PF.modulus field).
Local Notation repr := (Zmod.of_Z p).
Local Notation mul := (@PF.op_star field).
Local Notation add := (@PF.op_plus field).
Local Notation sub := (@PF.op_minus field).
Local Notation divide := (@PF.op_div field).
Local Notation equal := (@PF.op_eq field).
Local Definition zero := 0.
Local Definition one := 1.
Variables a b z : Z.
Variable sqrtRatio : Z → Z → bool × Z.
Variable sign : Z → bool.

Definition swuPrimeApply {Result : Type} (finish : Z → Z → Result) u :=
  let t := mul z (mul u u) in
  let w := add (mul t t) t in
  let n := mul b (add w one) in
  let d := mul a (if equal w zero then z else sub zero w) in
  let d2 := mul d d in
  let d3 := mul d2 d in
  let gnum := add (mul (add (mul n n) (mul a d2)) n) (mul b d3) in
  let '(square, root) := sqrtRatio gnum d3 in
  let x := divide (if square then n else mul t n) d in
  let y := if square then root else mul (mul t u) root in
  finish x (if Bool.eqb (sign u) (sign y) then y else sub zero y).

Definition swuPrimeCoordinates := swuPrimeApply (fun x y ⇒ (x,y)).

Hypothesis Hprime : Z.prime p.
Hypotheses (HaBound : primeFieldBound a) (Hanonzero : a ≠ 0)
  (HbBound : primeFieldBound b) (HzBound : primeFieldBound z) (Hznonzero : z ≠ 0).
Hypothesis HratioBound : ∀ top bottom, primeFieldBound (snd (sqrtRatio top bottom)).
Hypothesis HratioCorrect : ∀ top bottom,
  let '(square, root) := sqrtRatio top bottom in
  (root×root) mod p = (if square then divide top bottom else z × divide top bottom) mod p.
Hypothesis Hexceptional :
  let n := mul b (add zero one) in
  let d := mul a z in
  let d2 := mul d d in
  let d3 := mul d2 d in
  fst (sqrtRatio (add (mul (add (mul n n) (mul a d2)) n) (mul b d3)) d3) = true.
Add Field swu_residue_field : (Zmod.field_theory p Hprime).

Theorem swu_prime_coordinates_valid u :
  let '(x,y) := swuPrimeCoordinates u in
  primeFieldBound x ∧ primeFieldBound y ∧
  (repr y × repr y)%Zmod = (repr x × (repr x × repr x + repr a) + repr b)%Zmod.
Proof.
  unfold swuPrimeCoordinates, swuPrimeApply.
  set (t := mul z (mul u u)).
  set (w := add (mul t t) t).
  set (n := mul b (add w one)).
  set (d := mul a (if equal w zero then z else sub zero w)).
  set (d2 := mul d d).
  set (d3 := mul d2 d).
  set (gnum := add (mul (add (mul n n) (mul a d2)) n) (mul b d3)).
  pose proof (Z.prime_ge_2 _ Hprime) as Hp.
  assert (Ha : 0 < a < p) by (unfold primeFieldBound in HaBound; lia).
  assert (Haz : @primeFieldBound field a) by exact HaBound.
  assert (Hbz : @primeFieldBound field b) by exact HbBound.
  assert (Hzz : @primeFieldBound field z) by exact HzBound.
  assert (H0 : @primeFieldBound field zero) by (unfold primeFieldBound, zero; lia).
  assert (H1 : @primeFieldBound field one) by (unfold primeFieldBound, one; lia).
  assert (Hzero : zero = 0) by reflexivity.
  assert (Hone : one = 1) by reflexivity.
  assert (Ht : @primeFieldBound field t) by (apply prime_mul_bound; lia).
  assert (Hw : @primeFieldBound field w).
  { apply prime_add_bound; [apply prime_mul_bound; lia|exact Ht]. }
  assert (Hn : @primeFieldBound field n) by (apply prime_mul_bound; lia).
  assert (Hd : @primeFieldBound field d) by (apply prime_mul_bound; lia).
  assert (Hd2 : @primeFieldBound field d2) by (apply prime_mul_bound; lia).
  assert (Hd3 : @primeFieldBound field d3) by (apply prime_mul_bound; lia).
  assert (Hnum : @primeFieldBound field gnum).
  { apply prime_add_bound; apply prime_mul_bound; lia. }
  assert (Hanz : repr a ≠ Zmod.zero).
  { apply Zmod.of_Z_nz. rewrite Z.mod_small by exact Haz. lia. }
  assert (Hdnz : repr d ≠ Zmod.zero).
  { unfold d. rewrite prime_mul_residue by (try assumption; try lia; vm_compute; reflexivity).
    intro H. apply (Zmod.mul_0_iff_prime Hprime) in H. destruct H as [H|H]; [contradiction|].
    destruct (@PF.op_eq field w zero) eqn:Htest.
    - apply Zmod.to_Z_inj_iff in H.
      rewrite Zmod.to_Z_of_Z, Zmod.to_Z_0, Z.mod_small in H by exact Hzz. lia.
    - rewrite prime_sub_residue in H by assumption.
      rewrite Hzero, Zmod.of_Z_0, Zmod.sub_0_l in H.
      assert (Hwzero : repr w = Zmod.zero).
      { replace (repr w) with (Zmod.opp (Zmod.opp (repr w))) by ring.
        rewrite H, Zmod.opp_0. reflexivity. }
      assert (Heq : w = zero).
      { apply (@prime_residue_injective field w zero); [exact Hw|exact H0|].
        rewrite Hzero, Zmod.of_Z_0. exact Hwzero. }
      assert (@PF.op_eq field w zero = true) by (apply prime_eq_true; exact Heq).
      congruence. }
  assert (Hd3nz : repr d3 ≠ Zmod.zero).
  { unfold d3, d2. rewrite !prime_mul_residue by (try assumption; try lia; vm_compute; reflexivity).
    rewrite !(Zmod.mul_0_iff_prime Hprime). tauto. }
  assert (Hdvalue : d ≠ 0).
  { intro H. apply Hdnz. rewrite H. apply Zmod.of_Z_0. }
  assert (Hd3value : d3 ≠ 0).
  { intro H. apply Hd3nz. rewrite H. apply Zmod.of_Z_0. }
  assert (HtR : repr t = (repr z × repr u × repr u)%Zmod).
  { unfold t. rewrite !prime_mul_residue by (try assumption; try lia; vm_compute; reflexivity). ring. }
  assert (HwR : repr w = (repr t × repr t + repr t)%Zmod).
  { unfold w. rewrite prime_add_residue;
      [rewrite prime_mul_residue by lia; reflexivity|apply prime_mul_bound; lia|exact Ht]. }
  assert (HnR : repr n = (repr b × (repr w + 1))%Zmod).
  { unfold n. rewrite prime_mul_residue, prime_add_residue by (try assumption; lia).
    rewrite Hone, Zmod.of_Z_1. reflexivity. }
  assert (HqR : repr (@PF.op_div field gnum d3) =
    (Zmod.mdiv (repr n) (repr d) ×
      (Zmod.mdiv (repr n) (repr d) × Zmod.mdiv (repr n) (repr d) + repr a) + repr b)%Zmod).
  { rewrite prime_div_residue by assumption. unfold gnum, d3, d2.
    repeat first [rewrite prime_add_residue by
      (repeat first [assumption | apply prime_mul_bound; lia | apply prime_add_bound]) |
      rewrite prime_mul_residue by lia].
    replace (((repr n × repr n + repr a × (repr d × repr d)) × repr n) +
      repr b × (repr d × repr d × repr d))%Zmod with
      ((repr n × repr n × repr n + repr a × repr n × (repr d × repr d)) +
      repr b × (repr d × repr d × repr d))%Zmod by ring.
    apply swu_first_candidate; assumption. }
  pose proof (HratioBound gnum d3) as HrootBound.
  pose proof (HratioCorrect gnum d3) as HrootSquare.
  destruct (sqrtRatio gnum d3) as [square root] eqn:Hratio.
  cbn [snd] in HrootBound.
  assert (Hspecial : @PF.op_eq field w zero = true → square = true).
  { intro Htest. pose proof ((proj1 (@prime_eq_true field w zero)) Htest) as Hwzero.
    assert (Hfirst : fst (sqrtRatio gnum d3) = true).
    { unfold gnum, n, d3, d2, d. rewrite Htest, Hwzero.
      exact Hexceptional. }
    rewrite Hratio in Hfirst. exact Hfirst. }
  set (x := @PF.op_div field
    (if square then n else @PF.op_star field t n) d).
  set (y := if square then root else @PF.op_star field
    (@PF.op_star field t u) root).
  change (let finalY := if Bool.eqb (sign u) (sign y) then y else sub zero y in
    primeFieldBound x ∧ primeFieldBound finalY ∧
    (repr finalY × repr finalY)%Zmod =
      (repr x × (repr x × repr x + repr a) + repr b)%Zmod).
  assert (Hx : @primeFieldBound field x) by (apply prime_div_bound; lia).
  assert (Hy : @primeFieldBound field y).
  { unfold y. destruct square; [exact HrootBound|apply prime_mul_bound; lia]. }
  assert (HcurveR : (repr y × repr y)%Zmod =
    (repr x × (repr x × repr x + repr a) + repr b)%Zmod).
  { destruct square.
    - unfold x, y.
      rewrite prime_div_residue by assumption.
      rewrite <- HqR.
      apply (proj2 (Zmod.of_Z_inj _ _)) in HrootSquare.
      rewrite Zmod.of_Z_mul in HrootSquare. exact HrootSquare.
    - assert (Htest : @PF.op_eq field w zero = false).
      { destruct (@PF.op_eq field w zero) eqn:Htest; [|reflexivity].
        specialize (Hspecial eq_refl). discriminate. }
      assert (HwRnz : (repr t × repr t + repr t)%Zmod ≠ Zmod.zero).
      { rewrite <- HwR. intro H.
        assert (Heq : w = zero).
        { apply (@prime_residue_injective field w zero); [exact Hw|exact H0|].
          rewrite Hzero, Zmod.of_Z_0. exact H. }
        assert (@PF.op_eq field w zero = true) by (apply prime_eq_true; exact Heq).
        congruence. }
      assert (HdR : repr d = (-repr a × (repr t × repr t + repr t))%Zmod).
      { unfold d. rewrite Htest, prime_mul_residue, prime_sub_residue by (try assumption; lia).
        rewrite Hzero, Zmod.of_Z_0, HwR. ring. }
      assert (Hscale :
        ((repr t × Zmod.mdiv (repr n) (repr d)) ×
          ((repr t × Zmod.mdiv (repr n) (repr d)) ×
           (repr t × Zmod.mdiv (repr n) (repr d)) + repr a) + repr b)%Zmod =
        ((repr t × repr t × repr t) ×
          (Zmod.mdiv (repr n) (repr d) ×
            (Zmod.mdiv (repr n) (repr d) × Zmod.mdiv (repr n) (repr d) + repr a) + repr b))%Zmod).
      { rewrite HnR, HwR, HdR. apply swu_second_candidate; assumption. }
      unfold x, y.
      rewrite prime_div_residue by assumption. rewrite !prime_mul_residue by (try assumption; try lia; vm_compute; reflexivity).
      replace (Zmod.mdiv (repr t × repr n) (repr d)) with
        (repr t × Zmod.mdiv (repr n) (repr d))%Zmod by (unfold Zmod.mdiv; ring).
      rewrite Hscale, <- HqR, HtR.
      apply swu_scaled_root; [exact Hprime|].
      apply (proj2 (Zmod.of_Z_inj _ _)) in HrootSquare.
      rewrite !Zmod.of_Z_mul in HrootSquare. exact HrootSquare. }
  clearbody x y.
  set (finalY := if Bool.eqb
    (sign u)
    (sign y)
    then y else @PF.op_minus field zero y).
  assert (Hfinal : @primeFieldBound field finalY).
  { unfold finalY. destruct (Bool.eqb _ _);
      [exact Hy|apply prime_sub_bound; assumption]. }
  assert (HfinalR : (repr finalY × repr finalY)%Zmod =
    (repr x × (repr x × repr x + repr a) + repr b)%Zmod).
  { unfold finalY. destruct (Bool.eqb _ _); [exact HcurveR|].
    rewrite prime_sub_residue by (try assumption; lia). rewrite Hzero, Zmod.of_Z_0.
    replace ((0-repr y)*(0-repr y))%Zmod with (repr y×repr y)%Zmod by ring.
    exact HcurveR. }
  change (primeFieldBound x ∧ primeFieldBound finalY ∧
    (repr finalY × repr finalY)%Zmod =
      (repr x × (repr x × repr x + repr a) + repr b)%Zmod).
  split; [exact Hx|]. split; assumption.
Qed.
A continuation avoids duplicating the generated coordinate constructor.
Theorem swu_prime_apply_valid {Result : Type} (finish : Z → Z → Result)
    (P : Result → Prop) u :
  (∀ x y, primeFieldBound x → primeFieldBound y →
    (repr y × repr y)%Zmod = (repr x × (repr x × repr x + repr a) + repr b)%Zmod →
    P (finish x y)) → P (swuPrimeApply finish u).
Proof.
  intro Hfinish. pose proof (swu_prime_coordinates_valid u) as Hvalid.
  unfold swuPrimeCoordinates, swuPrimeApply in ×.
  match goal with |- context [sqrtRatio ?top ?bottom] ⇒
    destruct (sqrtRatio top bottom) as [square root] end.
  destruct Hvalid as [Hx [Hy Heq]]. apply Hfinish; assumption.
Qed.
End Map.

Normalize the generated functor once, before instantiating its operations with BLS constants. This avoids reducing large field expressions during conversion checking. All operation equalities are computational.
Module SWU := Ec.Curve.SWU_method.
Section Generated.
Context {field : PF.FArgs} {UI : Basics.Unreachable Numeric.Integer.t}
  {UF : PF.t} {Point : Set} {S : @SWU.FArgs Z Point}
  {UP : Point}.
Hypotheses
  (Hmul : (@SWU.F _ _ S).(Algebra.FIELD.op_star) = @PF.op_star field )
  (Hadd : (@SWU.F _ _ S).(Algebra.FIELD.op_plus) = @PF.op_plus field )
  (Hsub : (@SWU.F _ _ S).(Algebra.FIELD.op_minus) = @PF.op_minus field )
  (Hdiv : (@SWU.F _ _ S).(Algebra.FIELD.op_div) = @PF.op_div field )
  (Heq : (@SWU.F _ _ S).(Algebra.FIELD.op_eq) = @PF.op_eq field)
  (Hzero : (@SWU.F _ _ S).(Algebra.FIELD.zero) = 0)
  (Hone : (@SWU.F _ _ S).(Algebra.FIELD.one) = 1).

Lemma swu_generated_apply u :
  @SWU.map_to_curve_simple_swu _ _ S UF UP u =
  @swuPrimeApply field (@SWU.a_value _ _ S) (@SWU.b_value _ _ S)
    (@SWU.P _ _ S).(Ec.Curve.SWU_method_P_signature.z_value)
    (@SWU.sqrt_ratio _ _ S UF)
    (@SWU.P _ _ S).(Ec.Curve.SWU_method_P_signature.sgn0)
    Point (fun x y ⇒ @MonadVM.Option.get _ UP
      ((@SWU.C _ _ S).(Ec.Curve.SIG.of_coords) x y)) u.
Proof.
  destruct S as [C P] in ×.
  destruct C as [ops ca cb cg Point' cz cis cp cm cc ccoords] in ×.
  destruct ops in ×.
  change (op_star = @PF.op_star field ) in Hmul.
  change (op_plus = @PF.op_plus field ) in Hadd.
  change (op_minus = @PF.op_minus field ) in Hsub.
  change (op_div = @PF.op_div field ) in Hdiv.
  change (op_eq = @PF.op_eq field) in Heq.
  change (zero0 = 0) in Hzero. change (one0 = 1) in Hone.
  subst.
  reflexivity.
Qed.
End Generated.