Library QedkConcrete.blscurvevalidity

The concrete BLS point operations preserve valid coordinates

Reduction bounds make the optimized field arithmetic faithful to a mathematical field. This instantiates the affine-curve lemmas for both generated BLS curves; it does not assume subgroup membership.
From Stdlib Require Import ZArith Lia Zmod.Zmod.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Ec.Bls12_381
  QedkConcrete.curvevalidity QedkConcrete.nativefieldbounds
  QedkConcrete.primefieldarith QedkConcrete.primefieldresidues
  QedkConcrete.blscomplexresidues QedkConcrete.swucomplex
  QedkConcrete.complexresiduefield QedkConcrete.nativecomplexbounds
  QedkConcrete.blsprimality QedkConcrete.blscomplexsqrt.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Module Bls := Ec.Bls12_381.
Module QR := QuadraticResidue.

Section Generated.
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 : Basics.Unreachable Z}.
Local Notation field := (@blscomplexresidues.field).
Local Notation complex := (@blscomplexresidues.complex).
Local Notation p := (@PF.modulus field).
Local Notation curve1 := (@Bls.C_1_fargs).
Local Notation curve2 := (@Bls.C_2_fargs).
Local Notation bounded := (@primeFieldBound field).
Local Notation bounded2 := (@complexBound _ complex bounded).

Local Lemma different_false x y : @PF.op_ltgt field x y = false ↔ x = y.
Proof.
  change (negb (Z.eqb (OCamlZ.compare x y) 0) = false ↔ x = y).
  rewrite Bool.negb_false_iff. apply (@prime_eq_true field).
Qed.

Theorem bls_g1_add_valid left right :
  @curvePointValid _ curve1 bounded left → @curvePointValid _ curve1 bounded right →
  @curvePointValid _ curve1 bounded (@Curve.op_plus _ curve1 left right).
Proof.
  apply (@represented_curve_add_valid _ curve1 bounded
    (Zmod p) Zmod.zero Zmod.one Zmod.add Zmod.mul Zmod.sub Zmod.opp
    Zmod.mdiv Zmod.inv
    (Zmod.field_theory p (@blsprimality.bls_generated_modulus_prime))
    (Zmod.of_Z p)).
  - exact (@blscomplexsqrt.bls_complex_base_closed).
  - change (0 ≤ 0 < p). split; vm_compute; congruence.
  - change (0 ≤ 4 < p). split; vm_compute; congruence.
  - apply (@prime_eq_true field).
  - exact different_false.
  - apply (@prime_residue_injective field).
  - apply Zmod.of_Z_0.
  - vm_compute; reflexivity.
  - vm_compute; reflexivity.
  - vm_compute; congruence.
  - apply (@prime_add_residue field).
  - intros x y _ _. apply (@prime_mul_residue field). vm_compute; reflexivity.
  - apply (@prime_sub_residue field).
  - intros x y _ Hy Hnonzero. apply (@prime_div_residue field).
    + exact (@blsprimality.bls_generated_modulus_prime).
    + exact Hy.
    + intro Hzero. subst y. apply Hnonzero. apply Zmod.of_Z_0.
Qed.

Theorem bls_g1_mul_valid n point :
  @curvePointValid _ curve1 bounded point →
  @curvePointValid _ curve1 bounded (@Curve.op_star _ curve1 n point).
Proof. apply curve_mul_valid_from_add. exact bls_g1_add_valid. Qed.

Local Lemma complex_different_false x y :
  @Complex.op_ltgt _ complex x y = false ↔ x = y.
Proof.
  destruct x as [a b], y as [c d].
  change ((@PF.op_ltgt field a c || @PF.op_ltgt field b d)%bool = false ↔
    @Complex.t.Build _ complex a b = @Complex.t.Build _ complex c d).
  rewrite Bool.orb_false_iff, !different_false. split.
  - intros [-> ->]. reflexivity.
  - intro Heq. inversion Heq. auto.
Qed.

Local Lemma complex_closed : fieldClosed (@Curve.F _ curve2) bounded2.
Proof.
  constructor.
  - apply complex_zero_bound. exact (@bls_complex_base_closed).
  - intro n. apply complex_int_bound. exact (@bls_complex_base_closed).
  - intros a b Ha Hb. apply complex_add_bound;
      [exact (@bls_complex_base_closed)|exact Ha|exact Hb].
  - intros a b Ha Hb. apply complex_sub_bound;
      [exact (@bls_complex_base_closed)|exact Ha|exact Hb].
  - intros a b _ _. apply (@bls_complex_mul_bound).
  - intros a b _ _. apply (@bls_complex_mul_bound).
Qed.

Theorem bls_g2_add_valid left right :
  @curvePointValid _ curve2 bounded2 left → @curvePointValid _ curve2 bounded2 right →
  @curvePointValid _ curve2 bounded2 (@Curve.op_plus _ curve2 left right).
Proof.
  apply (@represented_curve_add_valid _ curve2 bounded2
    (QR.t p) (QR.zero p) (QR.one p) (QR.add p) (QR.mul p) (QR.sub p) (QR.opp p)
    (QR.divide p) (QR.inv p)
    (QR.quadratic_field p (@blsprimality.bls_generated_modulus_prime)
      (@legendretests.bls_modulus_three_mod_four))
    (@blsComplexResidue)).
  - exact complex_closed.
  - change ((0 ≤ 0 < p) ∧ (0 ≤ 0 < p)). repeat split; vm_compute; congruence.
  - change ((0 ≤ 4 < p) ∧ (0 ≤ 4 < p)). repeat split; vm_compute; congruence.
  - exact (@swucomplex.equal_true).
  - exact complex_different_false.
  - exact (@bls_complex_residue_injective).
  - exact (@bls_complex_residue_zero).
  - vm_compute; reflexivity.
  - vm_compute; reflexivity.
  - vm_compute; congruence.
  - exact (@bls_complex_residue_add).
  - intros x y _ _. apply (@bls_complex_residue_mul).
  - exact (@bls_complex_residue_sub).
  - intros x y _ Hy Hnonzero. apply (@bls_complex_residue_div); assumption.
Qed.

Theorem bls_g2_mul_valid n point :
  @curvePointValid _ curve2 bounded2 point →
  @curvePointValid _ curve2 bounded2 (@Curve.op_star _ curve2 n point).
Proof. apply curve_mul_valid_from_add. exact bls_g2_add_valid. Qed.

End Generated.

Print Assumptions bls_g1_mul_valid.
Print Assumptions bls_g2_mul_valid.