Library QedkConcrete.blscomplexresidues

The generated BLS extension arithmetic implements a field

Coordinate bounds justify the optimized additions and subtractions. The inverse equation follows from a nonzero norm and the concrete extended-GCD inverse, not an assumed cryptographic primitive.
From Stdlib Require Import ZArith Lia Field Zmod.Zmod.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Ec.Bls12_381 QedkConcrete.nativecomplexbounds
  QedkConcrete.nativefieldbounds QedkConcrete.primefieldresidues QedkConcrete.blscomplexsqrt
  QedkConcrete.complexresiduefield QedkConcrete.blsprimality.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Module Bls := Ec.Bls12_381.
Module QR := QuadraticResidue.

Section Residues.
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 Definition field := @Bls.F_p.MonadVM_Ec_Algebra_Prime_field_include_fargs .
Local Definition complex := @Bls.F_p2.MonadVM_Ec_Algebra_Complex_extension_include_fargs .
Local Notation p := (@PF.modulus field).
Local Notation repr := (Zmod.of_Z p).
Local Notation bounded := (@complexBound _ complex (@primeFieldBound field)).
Add Ring base_ring : (Zmod.ring_theory p).

Definition blsComplexResidue (x : @Complex.t _ complex) : QR.t p :=
  (repr x.(Complex.t.re), repr x.(Complex.t.im)).
Local Notation residue := blsComplexResidue.
Local Lemma positive : 0 < p.
Proof.
  pose proof (@bls_generated_modulus_prime) as H.
  apply Z.prime_ge_2 in H. change (2 ≤ p) in H. lia.
Qed.

Lemma bls_complex_residue_zero : residue (@Complex.zero _ complex) = QR.zero p.
Proof. change ((repr 0, repr 0) = QR.zero p). rewrite Zmod.of_Z_0. reflexivity. Qed.

Lemma bls_complex_residue_one : residue (@Complex.op_tildedollar _ complex 1) = QR.one p.
Proof.
  change ((repr 1, repr 0) = QR.one p). rewrite Zmod.of_Z_0, Zmod.of_Z_1. reflexivity.
Qed.

Lemma bls_complex_residue_add x y : bounded x → bounded y →
  residue (@Complex.op_plus _ complex x y) = QR.add p (residue x) (residue y).
Proof.
  destruct x as [a b], y as [c d]. intros [Ha Hb] [Hc Hd].
  change ((repr (@PF.op_plus field a c), repr (@PF.op_plus field b d)) =
    ((repr a + repr c)%Zmod, (repr b + repr d)%Zmod)).
  rewrite !prime_add_residue by assumption. reflexivity.
Qed.

Lemma bls_complex_residue_sub x y : bounded x → bounded y →
  residue (@Complex.op_minus _ complex x y) = QR.sub p (residue x) (residue y).
Proof.
  destruct x as [a b], y as [c d]. intros [Ha Hb] [Hc Hd].
  change ((repr (@PF.op_minus field a c), repr (@PF.op_minus field b d)) =
    ((repr a + -repr c)%Zmod, (repr b + -repr d)%Zmod)).
  rewrite !prime_sub_residue by assumption. f_equal; ring.
Qed.

Lemma bls_complex_residue_mul x y :
  residue (@Complex.op_star _ complex x y) = QR.mul p (residue x) (residue y).
Proof.
  destruct x as [a b], y as [c d].
  change ((repr (@PF.op_minus field (@PF.op_star field a c) (@PF.op_star field b d)),
    repr (@PF.op_plus field (@PF.op_star field a d) (@PF.op_star field b c))) =
    ((repr a × repr c - repr b × repr d)%Zmod, (repr a × repr d + repr b × repr c)%Zmod)).
  rewrite prime_sub_residue, prime_add_residue by (apply prime_mul_bound; exact positive).
  rewrite !prime_mul_residue by (try assumption; try lia; vm_compute; reflexivity). reflexivity.
Qed.

Lemma bls_complex_residue_norm x :
  repr (@Complex.norm _ complex x) = QR.norm p (residue x).
Proof.
  destruct x as [a b].
  change (repr (@PF.op_plus field (@PF.op_star field a a) (@PF.op_star field b b)) =
    (repr a × repr a + repr b × repr b)%Zmod).
  rewrite prime_add_residue by (apply prime_mul_bound; exact positive).
  rewrite !prime_mul_residue by (try assumption; try lia; vm_compute; reflexivity). reflexivity.
Qed.

Lemma bls_complex_residue_injective x y : bounded x → bounded y → residue x = residue y → x = y.
Proof.
  destruct x as [a b], y as [c d]. intros [Ha Hb] [Hc Hd] Heq.
  change ((repr a, repr b) = (repr c, repr d)) in Heq.
  pose proof (f_equal (@fst _ _) Heq) as Hac.
  pose proof (f_equal (@snd _ _) Heq) as Hbd.
  cbn [fst snd] in Hac, Hbd.
  f_equal; apply (@prime_residue_injective field); assumption.
Qed.

Lemma bls_complex_residue_inverse x : bounded x → residue x ≠ QR.zero p →
  residue (@Complex.inv _ complex x) = QR.inv p (residue x).
Proof.
  intros Hx Hnonzero.
  assert (Hnorm : QR.norm p (residue x) ≠ Zmod.zero).
  { apply QR.norm_nonzero; [exact (@bls_generated_modulus_prime)|
      exact (@legendretests.bls_modulus_three_mod_four)|exact Hnonzero]. }
  assert (HnormValue : @Complex.norm _ complex x ≠ 0).
  { intro Hzero. rewrite <- bls_complex_residue_norm, Hzero, Zmod.of_Z_0 in Hnorm.
    contradiction. }
  assert (HnormBound : @primeFieldBound field (@Complex.norm _ complex x)).
  { destruct x. apply prime_add_bound; apply prime_mul_bound; exact positive. }
  pose proof (@prime_inv_residue field (@Complex.norm _ complex x)
    (@bls_generated_modulus_prime) HnormBound HnormValue) as Hinverse.
  rewrite bls_complex_residue_norm in Hinverse.
  destruct x as [a b]. destruct Hx as [Ha Hb].
  change ((repr (@PF.op_star field a (@PF.inv field (@Complex.norm _ complex
      (@Complex.t.Build _ complex a b)))),
    repr (@PF.op_star field (@PF.op_minus field 0 b)
      (@PF.inv field (@Complex.norm _ complex (@Complex.t.Build _ complex a b))))) =
    QR.inv p (residue (@Complex.t.Build _ complex a b))).
  rewrite !prime_mul_residue, prime_sub_residue by
    (try assumption; unfold primeFieldBound; pose proof positive; lia).
  rewrite Hinverse, Zmod.of_Z_0.
  reflexivity.
Qed.

Lemma bls_complex_residue_div x y : bounded y → residue y ≠ QR.zero p →
  residue (@Complex.op_div _ complex x y) = QR.divide p (residue x) (residue y).
Proof.
  intros Hy Hnonzero. unfold Complex.op_div.
  rewrite bls_complex_residue_mul, bls_complex_residue_inverse by assumption. reflexivity.
Qed.
End Residues.