Library QedkConcrete.primefieldarith
Arithmetic equations for the generated prime field
From Stdlib Require Import ZArith Lia Zmod.Zmod.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Ec.Algebra QedkConcrete.nativefieldbounds.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Section Arithmetic.
Context {F : PF.FArgs} {UI : Basics.Unreachable Numeric.Integer.t}
{UF : Basics.Unreachable PF.t}.
Lemma prime_add_mod left right :
primeFieldBound left → primeFieldBound right →
@PF.op_plus F left right = (left + right) mod PF.modulus.
Proof.
unfold primeFieldBound. intros Hleft Hright.
change ((if (OCamlZ.compare (left + right) PF.modulus >=? 0)
then left + right - PF.modulus else left + right) = (left + right) mod PF.modulus).
unfold OCamlZ.compare. destruct (Z.compare_spec (left + right) PF.modulus); cbn.
- apply Z.mod_unique_pos with (q := 1); lia.
- symmetry. apply Z.mod_small; lia.
- apply Z.mod_unique_pos with (q := 1); lia.
Qed.
Lemma prime_sub_mod left right :
primeFieldBound left → primeFieldBound right →
@PF.op_minus F left right = (left - right) mod PF.modulus.
Proof.
unfold primeFieldBound. intros Hleft Hright.
change ((if (OCamlZ.compare (left - right) 0 <? 0)
then left - right + PF.modulus else left - right) = (left - right) mod PF.modulus).
unfold OCamlZ.compare. destruct (Z.compare_spec (left - right) 0); cbn.
- symmetry. apply Z.mod_small; lia.
- apply Z.mod_unique_pos with (q := -1); lia.
- symmetry. apply Z.mod_small; lia.
Qed.
Lemma prime_eq_true left right : @PF.op_eq F left right = true ↔ left = right.
Proof.
change (Z.eqb (OCamlZ.compare left right) 0 = true ↔ left = right).
unfold OCamlZ.compare. destruct (Z.compare_spec left right); cbn; intuition lia.
Qed.
Lemma prime_half_mod value : Z.prime PF.modulus → 2 < PF.modulus →
(2 × @PF.op_div F value 2) mod PF.modulus = value mod PF.modulus.
Proof.
intros Hprime Hmodulus.
pose proof (OCamlZ.invert_prime_modular_inverse 2 PF.modulus
(proj1 (Znumtheory.prime_alt _) Hprime) ltac:(lia)) as Hinverse.
change ((2 × @PF.reduce F (value × OCamlZ.invert 2 PF.modulus)) mod PF.modulus =
value mod PF.modulus).
rewrite prime_reduce_mod by lia.
rewrite Z.mul_mod_idemp_r by lia.
replace (2 × (value × OCamlZ.invert 2 PF.modulus)) with
(value × (2 × OCamlZ.invert 2 PF.modulus)) by ring.
rewrite Z.mul_mod, Hinverse, Z.mul_1_r, Z.mod_mod by lia. reflexivity.
Qed.
End Arithmetic.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Ec.Algebra QedkConcrete.nativefieldbounds.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Section Arithmetic.
Context {F : PF.FArgs} {UI : Basics.Unreachable Numeric.Integer.t}
{UF : Basics.Unreachable PF.t}.
Lemma prime_add_mod left right :
primeFieldBound left → primeFieldBound right →
@PF.op_plus F left right = (left + right) mod PF.modulus.
Proof.
unfold primeFieldBound. intros Hleft Hright.
change ((if (OCamlZ.compare (left + right) PF.modulus >=? 0)
then left + right - PF.modulus else left + right) = (left + right) mod PF.modulus).
unfold OCamlZ.compare. destruct (Z.compare_spec (left + right) PF.modulus); cbn.
- apply Z.mod_unique_pos with (q := 1); lia.
- symmetry. apply Z.mod_small; lia.
- apply Z.mod_unique_pos with (q := 1); lia.
Qed.
Lemma prime_sub_mod left right :
primeFieldBound left → primeFieldBound right →
@PF.op_minus F left right = (left - right) mod PF.modulus.
Proof.
unfold primeFieldBound. intros Hleft Hright.
change ((if (OCamlZ.compare (left - right) 0 <? 0)
then left - right + PF.modulus else left - right) = (left - right) mod PF.modulus).
unfold OCamlZ.compare. destruct (Z.compare_spec (left - right) 0); cbn.
- symmetry. apply Z.mod_small; lia.
- apply Z.mod_unique_pos with (q := -1); lia.
- symmetry. apply Z.mod_small; lia.
Qed.
Lemma prime_eq_true left right : @PF.op_eq F left right = true ↔ left = right.
Proof.
change (Z.eqb (OCamlZ.compare left right) 0 = true ↔ left = right).
unfold OCamlZ.compare. destruct (Z.compare_spec left right); cbn; intuition lia.
Qed.
Lemma prime_half_mod value : Z.prime PF.modulus → 2 < PF.modulus →
(2 × @PF.op_div F value 2) mod PF.modulus = value mod PF.modulus.
Proof.
intros Hprime Hmodulus.
pose proof (OCamlZ.invert_prime_modular_inverse 2 PF.modulus
(proj1 (Znumtheory.prime_alt _) Hprime) ltac:(lia)) as Hinverse.
change ((2 × @PF.reduce F (value × OCamlZ.invert 2 PF.modulus)) mod PF.modulus =
value mod PF.modulus).
rewrite prime_reduce_mod by lia.
rewrite Z.mul_mod_idemp_r by lia.
replace (2 × (value × OCamlZ.invert 2 PF.modulus)) with
(value × (2 × OCamlZ.invert 2 PF.modulus)) by ring.
rewrite Z.mul_mod, Hinverse, Z.mul_1_r, Z.mod_mod by lia. reflexivity.
Qed.
End Arithmetic.