Library QedkConcrete.blsisogeny11

Certificates for the generated BLS degree-eleven isogeny

The denominator tables are a fifth-degree polynomial squared and cubed. Checking these identities and the curve identity on coefficients avoids trusting a symbolic algebra system or enumerating input points.
From Stdlib Require Import ZArith Lia List Field Zmod.Zmod.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Ec.Bls12_381
  QedkConcrete.nativefieldbounds QedkConcrete.primefieldresidues
  QedkConcrete.primepolynomials QedkConcrete.blsswumap
  QedkConcrete.blsisogeny3 QedkConcrete.primefieldarith
  QedkConcrete.curvevalidity QedkConcrete.blscurvevalidity.
Import ListNotations.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Module Bls := Ec.Bls12_381.
Module Poly := Algebra.Polynomial_ring.
Module PP := PrimePolynomial.

Section Isogeny.
Context {UZ : Basics.Unreachable Z} {UU : Basics.Unreachable Support.UintN.t}
  {UI : Basics.Unreachable Numeric.Integer.t}
  {UB : Basics.Unreachable Numeric.IntegerBase.(Numeric.Make.Make_result.t)}
  {UF : Z} {UA : Basics.Unreachable (list Z)}.
Local Definition field := @Bls.F_p.MonadVM_Ec_Algebra_Prime_field_include_fargs .
Local Definition auxiliary := @Bls.F_p_SWU_curve.MonadVM_Ec_Curve_Make_include_fargs .
Local Definition target := @Bls.C_1_fargs .
Local Notation p := (@PF.modulus field).
Local Notation repr := (Zmod.of_Z p).
Local Notation bounded := (@primeFieldBound field).
Local Notation poly := (@Bls.F_p_SWU_curve.F_px_fargs).
Local Notation eval := (@Poly.eval _ poly).
Local Notation mul := (@PF.op_star field).
Local Notation add := (@PF.op_plus field).
Local Notation divide := (@PF.op_div field).
Local Definition xnum := @Bls.F_p_SWU_curve.x_num .
Local Definition xden := @Bls.F_p_SWU_curve.x_den .
Local Definition ynum := @Bls.F_p_SWU_curve.y_num .
Local Definition yden := @Bls.F_p_SWU_curve.y_den .
Local Definition sourceA := (@Curve.P _ auxiliary).(Ec.Curve.Make_P_signature.a_value).
Local Definition sourceB := (@Curve.P _ auxiliary).(Ec.Curve.Make_P_signature.b_value).
Add Field residue_field : (Zmod.field_theory p (@blsprimality.bls_generated_modulus_prime)).

Lemma bls_prime_polynomial_bound coeffs x :
  Forall bounded coeffs → bounded (eval coeffs x).
Proof.
  intro Hcoeffs. induction Hcoeffs as [|a tl Ha Htl IH].
  - apply prime_zero_bound. vm_compute; reflexivity.
  - change (bounded (add a (mul x (eval tl x)))).
    apply prime_add_bound; [exact Ha|]. apply prime_mul_bound. vm_compute; reflexivity.
Qed.

Lemma bls_prime_polynomial_residue coeffs x :
  Forall bounded coeffs → repr (eval coeffs x) = PP.eval p coeffs (repr x).
Proof.
  intro Hcoeffs. induction Hcoeffs as [|a tl Ha Htl IH].
  - apply Zmod.of_Z_0.
  - change (repr (add a (mul x (eval tl x))) =
      (repr a + repr x × PP.eval p tl (repr x))%Zmod).
    rewrite prime_add_residue by (try exact Ha; apply prime_mul_bound; vm_compute; reflexivity).
    rewrite prime_mul_residue, IH by (try assumption; vm_compute; reflexivity). reflexivity.
Qed.

Lemma bls_iso11_coefficients_bounded :
  Forall bounded xnum ∧ Forall bounded xden ∧ Forall bounded ynum ∧ Forall bounded yden.
Proof.
  unfold xnum, xden, ynum, yden.
  unfold Bls.F_p_SWU_curve.x_num, Bls.F_p_SWU_curve.x_den,
    Bls.F_p_SWU_curve.y_num, Bls.F_p_SWU_curve.y_den.
  cbn beta iota zeta delta [Bls.F_p_SWU_curve.F_px Poly.functor
    Poly.Polynomial_ring_result.of_coeffs].
  rewrite !polynomial_coefficients_nonzero by (vm_compute; reflexivity).
  repeat first [apply conj | apply Forall_cons | apply Forall_nil].
  all: vm_compute; congruence.
Qed.

Local Definition kernel : list Z :=
  [2955185177626169647543871026716143749798776473583611826109464091094397474242531465369792566109106337274460167892487;
   368290328279919699380600918531504139062319804596715301915067438724165980997443279928362378653594520746075110864816;
   2956695361339193066682181833380513374227982887255347166494009902960246484641139874171795235175561134113346246778169;
   1426262776564302641609273031767820829543220110075666692709214249985201803031422328413319637086750536774929261013316;
   721398571713745716315313195033211010796752582794315199168745550044278639029030032465331939076562082409261408087685;
   1].

Lemma bls_iso11_coefficient_certificates :
  xden = PP.mul p kernel kernel ∧
  yden = PP.mul p (PP.mul p kernel kernel) kernel ∧
  PP.mul p [sourceB;sourceA;0%Z;1%Z] (PP.mul p ynum ynum) =
    PP.add p (PP.mul p xnum (PP.mul p xnum xnum))
      (PP.scale p 4 (PP.mul p xden (PP.mul p xden xden))).
Proof.
  unfold xnum, xden, ynum, yden.
  unfold Bls.F_p_SWU_curve.x_num, Bls.F_p_SWU_curve.x_den,
    Bls.F_p_SWU_curve.y_num, Bls.F_p_SWU_curve.y_den.
  cbn beta iota zeta delta [Bls.F_p_SWU_curve.F_px Poly.functor
    Poly.Polynomial_ring_result.of_coeffs].
  rewrite !polynomial_coefficients_nonzero by (vm_compute; reflexivity).
  vm_compute. repeat split; reflexivity.
Qed.

Lemma bls_iso11_evaluation x :
  let k := PP.eval p kernel (repr x) in
  let xn := repr (eval xnum x) in
  let yn := repr (eval ynum x) in
  repr (eval xden x) = (k×k)%Zmod ∧
  repr (eval yden x) = (k×k×k)%Zmod ∧
  ((repr x*(repr x×repr x+repr sourceA)+repr sourceB)*yn×yn)%Zmod =
    (xn×xn×xn + repr 4*(k×k×k×k×k×k))%Zmod.
Proof.
  cbv zeta.
  destruct bls_iso11_coefficients_bounded as [Hxn [Hxd [Hyn Hyd]]].
  destruct bls_iso11_coefficient_certificates as [HxdR [HydR Hcurve]].
  pose proof (f_equal (fun cs ⇒ PP.eval p cs (repr x)) Hcurve) as HcurveR.
  cbv beta in HcurveR.
  rewrite PP.eval_add, PP.eval_scale, !PP.eval_mul in HcurveR.
  rewrite HxdR, !PP.eval_mul in HcurveR.
  repeat split.
  - rewrite (bls_prime_polynomial_residue _ _ Hxd), HxdR, PP.eval_mul. reflexivity.
  - rewrite (bls_prime_polynomial_residue _ _ Hyd), HydR, !PP.eval_mul. reflexivity.
  - rewrite (bls_prime_polynomial_residue _ _ Hxn), (bls_prime_polynomial_residue _ _ Hyn).
    replace ((repr x*(repr x×repr x+repr sourceA)+repr sourceB)*
      PP.eval p ynum (repr x)×PP.eval p ynum (repr x))%Zmod with
      (PP.eval p [sourceB;sourceA;0%Z;1%Z] (repr x) ×
        (PP.eval p ynum (repr x)×PP.eval p ynum (repr x)))%Zmod
      by (cbn [PP.eval]; rewrite Zmod.of_Z_0, Zmod.of_Z_1; ring).
    rewrite HcurveR. ring.
Qed.

Lemma bls_iso11_coordinates_algebra x y xn xd yn yd k :
  @Curve.in_curve _ auxiliary x y = true →
  @PF.op_eq field xd 0 = false →
  bounded xd → bounded yd →
  repr xd = (k×k)%Zmod → repr yd = (k×k×k)%Zmod →
  ((repr x*(repr x×repr x+repr sourceA)+repr sourceB)*repr yn×repr yn)%Zmod =
    (repr xn×repr xn×repr xn+repr 4*(k×k×k×k×k×k))%Zmod →
  let xx := divide xn xd in
  let yy := mul (divide yn yd) y in
  bounded xx ∧ bounded yy ∧ @Curve.in_curve _ target xx yy = true.
Proof.
  intros Hsource Hnonkernel Hxdb Hydb HxdR HydR Hpoly. cbv zeta.
  pose proof (@blsprimality.bls_generated_modulus_prime) as Hprime.
  change (Z.prime p) in Hprime.
  assert (Hp : 0 < p) by (pose proof (Z.prime_ge_2 _ Hprime); lia).
  assert (Hxdnz : xd ≠ 0).
  { intro Hzero. assert (Heq : @PF.op_eq field (xd) 0 = true)
      by (apply primefieldarith.prime_eq_true; exact Hzero). congruence. }
  assert (HxdRnz : repr (xd) ≠ 0%Zmod).
  { apply Zmod.of_Z_nz. rewrite Z.mod_small; [exact Hxdnz|].
    exact Hxdb. }
  assert (Hk : k ≠ 0%Zmod).
  { intro Hzero. apply HxdRnz. rewrite HxdR, Hzero. ring. }
  assert (HydRnz : repr (yd) ≠ 0%Zmod).
  { rewrite HydR. intro Hzero.
    repeat rewrite Zmod.mul_0_iff_prime in Hzero by exact Hprime. tauto. }
  assert (Hydnz : yd ≠ 0).
  { intro Hzero. apply HydRnz. rewrite Hzero, Zmod.of_Z_0. reflexivity. }
  assert (Ha : bounded sourceA) by (vm_compute; split; congruence).
  assert (Hb : bounded sourceB) by (vm_compute; split; congruence).
  assert (HcurveR : (repr y × repr y)%Zmod =
    (repr x*(repr x×repr x+repr sourceA)+repr sourceB)%Zmod).
  { change (@PF.op_eq field (mul y y) (add (mul x (add (mul x x) sourceA)) sourceB) = true) in Hsource.
    apply primefieldarith.prime_eq_true in Hsource. apply (f_equal repr) in Hsource.
    repeat first [rewrite prime_add_residue in Hsource by
      (repeat first [assumption | apply prime_mul_bound; exact Hp | apply prime_add_bound]) |
      rewrite prime_mul_residue in Hsource by exact Hp].
    exact Hsource. }
  rewrite <- HcurveR in Hpoly.
  set (xx := divide (xn) (xd)).
  set (yy := mul (divide (yn) (yd)) y).
  assert (Hxx : repr xx = Zmod.mdiv (repr (xn)) (k×k)%Zmod).
  { unfold xx. rewrite prime_div_residue by
      assumption.
    rewrite HxdR. reflexivity. }
  assert (Hyy : repr yy = (Zmod.mdiv (repr (yn)) (k×k×k)×repr y)%Zmod).
  { unfold yy. rewrite prime_mul_residue, prime_div_residue by
      assumption.
    rewrite HydR. reflexivity. }
  assert (Htarget : (repr yy×repr yy)%Zmod =
    (repr xx×repr xx×repr xx+repr 4)%Zmod).
  { rewrite Hxx, Hyy. apply prime_isogeny_rational_curve; assumption. }
  split; [apply prime_div_bound; exact Hp|].
  split; [apply prime_mul_bound; exact Hp|].
  clearbody xx yy.
  change (@PF.op_eq field (mul yy yy)
    (add (mul xx (add (mul xx xx) (@Curve.P _ target).(Ec.Curve.Make_P_signature.a_value)))
      (@Curve.P _ target).(Ec.Curve.Make_P_signature.b_value)) = true).
  assert (Hta : (@Curve.P _ target).(Ec.Curve.Make_P_signature.a_value) = 0)
    by (vm_compute; reflexivity).
  assert (Htb : (@Curve.P _ target).(Ec.Curve.Make_P_signature.b_value) = 4)
    by (vm_compute; reflexivity).
  rewrite Hta, Htb.
  apply primefieldarith.prime_eq_true, (@prime_residue_injective field).
  - apply prime_mul_bound; exact Hp.
  - apply prime_add_bound; [apply prime_mul_bound; exact Hp|vm_compute; split; congruence].
  - rewrite prime_mul_residue by (try assumption; try lia; vm_compute; reflexivity).
    rewrite prime_add_residue by
      (first [apply prime_mul_bound; exact Hp | vm_compute; split; congruence]).
    rewrite prime_mul_residue, prime_add_residue by
      (first [exact Hp | apply prime_mul_bound; exact Hp | change (0 ≤ 0 < p); lia]).
    rewrite prime_mul_residue, Zmod.of_Z_0, Htarget by exact Hp. ring.
Qed.

The certificate proves the exact fractions used by the generated code satisfy the target equation whenever the denominator is nonzero.
Theorem bls_iso11_coordinates_valid x y :
  @Curve.in_curve _ auxiliary x y = true →
  @PF.op_eq field (eval xden x) 0 = false →
  let xx := divide (eval xnum x) (eval xden x) in
  let yy := mul (divide (eval ynum x) (eval yden x)) y in
  bounded xx ∧ bounded yy ∧ @Curve.in_curve _ target xx yy = true.
Proof.
  intros Hsource Hnonkernel.
  destruct bls_iso11_coefficients_bounded as [_ [Hxd [_ Hyd]]].
  destruct (bls_iso11_evaluation x) as [HxdR [HydR Hpoly]].
  eapply bls_iso11_coordinates_algebra.
  - exact Hsource.
  - exact Hnonkernel.
  - apply bls_prime_polynomial_bound. exact Hxd.
  - apply bls_prime_polynomial_bound. exact Hyd.
  - exact HxdR.
  - exact HydR.
  - exact Hpoly.
Qed.

Lemma bls_iso11_curve_construct_and_clear_valid x y :
  bounded x → bounded y → @Curve.in_curve _ target x y = true →
  ∃ point, @Curve.of_coords _ target x y = Some point ∧
    @curvePointValid _ target bounded
      (@Curve.op_star _ target (@Bls.F_p_SWU_curve.h_eff) point).
Proof.
  intros Hx Hy Hcurve.
  destruct (curve_construct_valid bounded x y Hx Hy Hcurve)
    as [point [Hparse Hvalid]].
  ∃ point. split; [exact Hparse|].
  apply bls_g1_mul_valid. exact Hvalid.
Qed.

Lemma bls_iso11_curve_construct_and_clear x y :
  bounded x → bounded y → @Curve.in_curve _ target x y = true →
  ∃ point, @Curve.of_coords _ target x y = Some point ∧
    @curvePointBound _ target bounded
      (@Curve.op_star _ target (@Bls.F_p_SWU_curve.h_eff) point).
Proof.
  intros Hx Hy Hcurve.
  destruct (bls_iso11_curve_construct_and_clear_valid x y Hx Hy Hcurve)
    as [point [Hparse [Hbound _]]].
  ∃ point. split; assumption.
Qed.

The coordinate constructor succeeds and cofactor multiplication keeps the coordinates reduced and on the curve. This does not assume its subgroup test passes.
Theorem bls_iso11_construct_and_clear_valid x y :
  @Curve.in_curve _ auxiliary x y = true →
  @PF.op_eq field (eval xden x) 0 = false →
  let xx := divide (eval xnum x) (eval xden x) in
  let yy := mul (divide (eval ynum x) (eval yden x)) y in
  ∃ point, @Curve.of_coords _ target xx yy = Some point ∧
    @curvePointValid _ target bounded
      (@Curve.op_star _ target (@Bls.F_p_SWU_curve.h_eff) point).
Proof.
  intros Hsource Hnonkernel.
  destruct (bls_iso11_coordinates_valid x y Hsource Hnonkernel) as [Hxx [Hyy Hcurve]].
  apply bls_iso11_curve_construct_and_clear_valid; assumption.
Qed.

Theorem bls_iso11_construct_and_clear x y :
  @Curve.in_curve _ auxiliary x y = true →
  @PF.op_eq field (eval xden x) 0 = false →
  let xx := divide (eval xnum x) (eval xden x) in
  let yy := mul (divide (eval ynum x) (eval yden x)) y in
  ∃ point, @Curve.of_coords _ target xx yy = Some point ∧
    @curvePointBound _ target bounded
      (@Curve.op_star _ target (@Bls.F_p_SWU_curve.h_eff) point).
Proof.
  intros Hsource Hnonkernel.
  destruct (bls_iso11_construct_and_clear_valid x y Hsource Hnonkernel)
    as [point [Hparse [Hbound _]]].
  ∃ point. split; assumption.
Qed.

Corollary bls_swu_isogeny_construct_and_clear_valid
    {UP : @Curve.t _ auxiliary} u :
  let swu := @Bls.F_p_SWU_fargs in
  let '(x,y) := @Curve.coords _ auxiliary
    (@Ec.Curve.SWU_method.map_to_curve_simple_swu _ _ swu UF UP u) in
  @PF.op_eq field (eval xden x) 0 = false →
  ∃ point,
    @Curve.of_coords _ target (divide (eval xnum x) (eval xden x))
      (mul (divide (eval ynum x) (eval yden x)) y) = Some point ∧
    @curvePointValid _ target bounded
      (@Curve.op_star _ target (@Bls.F_p_SWU_curve.h_eff) point).
Proof.
  pose proof (@bls_swu_map_coordinates UF UP u) as Hcoords.
  cbv zeta.
  destruct (@Curve.coords _ auxiliary
    (@Ec.Curve.SWU_method.map_to_curve_simple_swu _ _
      (@Bls.F_p_SWU_fargs) UF UP u)) as [x y] eqn:Hxy.
  change (let '(x,y) := @Curve.coords _ auxiliary
    (@Ec.Curve.SWU_method.map_to_curve_simple_swu _ _
      (@Bls.F_p_SWU_fargs) UF UP u) in
    bounded x ∧ bounded y ∧ @Curve.in_curve _ auxiliary x y = true) in Hcoords.
  rewrite Hxy in Hcoords. destruct Hcoords as [_ [_ Hcurve]].
  exact (bls_iso11_construct_and_clear_valid x y Hcurve).
Qed.

Corollary bls_swu_isogeny_construct_and_clear
    {UP : @Curve.t _ auxiliary} u :
  let swu := @Bls.F_p_SWU_fargs in
  let '(x,y) := @Curve.coords _ auxiliary
    (@Ec.Curve.SWU_method.map_to_curve_simple_swu _ _ swu UF UP u) in
  @PF.op_eq field (eval xden x) 0 = false →
  ∃ point,
    @Curve.of_coords _ target (divide (eval xnum x) (eval xden x))
      (mul (divide (eval ynum x) (eval yden x)) y) = Some point ∧
    @curvePointBound _ target bounded
      (@Curve.op_star _ target (@Bls.F_p_SWU_curve.h_eff) point).
Proof.
  cbv zeta.
  destruct (@Curve.coords _ auxiliary
    (@Ec.Curve.SWU_method.map_to_curve_simple_swu _ _
      (@Bls.F_p_SWU_fargs) UF UP u)) as [x y] eqn:Hxy.
  intro Hkernel.
  pose proof (@bls_swu_isogeny_construct_and_clear_valid UP u) as Hvalid.
  cbv zeta in Hvalid. rewrite Hxy in Hvalid.
  destruct (Hvalid Hkernel) as [point [Hparse [Hbound _]]].
  ∃ point. split; assumption.
Qed.
End Isogeny.
Print Assumptions bls_iso11_coordinates_valid.

Print Assumptions bls_swu_isogeny_construct_and_clear.

Print Assumptions bls_swu_isogeny_construct_and_clear_valid.