Library QedkConcrete.nativefieldbounds
Coordinate ranges from the implemented field arithmetic
From Stdlib Require Import ZArith Lia Bool.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Ec.Algebra MonadVM.Ec.Curve.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Module PF := Ec.Algebra.Prime_field.
Module Field := Ec.Algebra.FIELD.
Module Curve := Ec.Curve.Make.
Definition primeFieldBound {F : PF.FArgs} (x : PF.t) : Prop :=
0 ≤ x < PF.modulus.
Section PrimeField.
Context {F : PF.FArgs} {UI : Basics.Unreachable Numeric.Integer.t}
{UF : Basics.Unreachable PF.t}.
Lemma prime_reduce_mod value : 0 < PF.modulus →
@PF.reduce F value = value mod PF.modulus.
Proof.
intro Hmodulus. change ((if Z.eqb PF.modulus 0 then
@Numeric.Make._rocq_fallback_of_string_0 Numeric.IntegerBase_fargs
else value mod Z.abs PF.modulus) = value mod PF.modulus).
assert (Hnonzero : Z.eqb (@PF.modulus F) 0 = false)
by (apply Z.eqb_neq; lia).
rewrite Hnonzero.
rewrite Z.abs_eq by lia. reflexivity.
Qed.
Lemma prime_reduce_bound value : 0 < PF.modulus →
primeFieldBound (@PF.reduce F value).
Proof.
intro Hmodulus. unfold primeFieldBound. rewrite prime_reduce_mod by exact Hmodulus.
apply Z.mod_pos_bound; exact Hmodulus.
Qed.
Lemma prime_zero_bound : 0 < PF.modulus → primeFieldBound (@PF.zero F).
Proof. intro Hmodulus. apply prime_reduce_bound; exact Hmodulus. Qed.
Lemma prime_int_bound value : 0 < PF.modulus →
primeFieldBound (@PF.op_tildedollar F value).
Proof. intro Hmodulus. apply prime_reduce_bound; exact Hmodulus. Qed.
Lemma prime_add_bound left right :
primeFieldBound left → primeFieldBound right →
primeFieldBound (@PF.op_plus F left right).
Proof.
unfold primeFieldBound. intros Hleft Hright.
change (0 ≤ (if (OCamlZ.compare (left + right) PF.modulus >=? 0) then left + right - PF.modulus
else left + right) < PF.modulus).
unfold OCamlZ.compare. destruct (Z.compare_spec (left + right) PF.modulus); cbn; lia.
Qed.
Lemma prime_sub_bound left right :
primeFieldBound left → primeFieldBound right →
primeFieldBound (@PF.op_minus F left right).
Proof.
unfold primeFieldBound. intros Hleft Hright.
change (0 ≤ (if (OCamlZ.compare (left - right) 0 <? 0) then left - right + PF.modulus
else left - right) < PF.modulus).
unfold OCamlZ.compare. destruct (Z.compare_spec (left - right) 0); cbn; lia.
Qed.
Lemma prime_mul_bound left right : 0 < PF.modulus →
primeFieldBound (@PF.op_star F left right).
Proof. intro Hmodulus. apply prime_reduce_bound; exact Hmodulus. Qed.
Lemma prime_div_bound left right : 0 < PF.modulus →
primeFieldBound (@PF.op_div F left right).
Proof. intro Hmodulus. apply prime_reduce_bound; exact Hmodulus. Qed.
Lemma prime_inv_bound value : 0 < PF.modulus →
primeFieldBound (@PF.inv F value).
Proof. intro Hmodulus. apply prime_reduce_bound; exact Hmodulus. Qed.
Lemma prime_parse_bound input output : 0 < PF.modulus →
@PF.of_uint_opt F input = Some output → primeFieldBound output.
Proof.
intros Hmodulus. unfold PF.of_uint_opt.
destruct (Numeric.Integer.op_gteq _ _); [discriminate|].
intros Hout. inversion Hout; subst. apply prime_reduce_bound; exact Hmodulus.
Qed.
End PrimeField.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Ec.Algebra MonadVM.Ec.Curve.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Module PF := Ec.Algebra.Prime_field.
Module Field := Ec.Algebra.FIELD.
Module Curve := Ec.Curve.Make.
Definition primeFieldBound {F : PF.FArgs} (x : PF.t) : Prop :=
0 ≤ x < PF.modulus.
Section PrimeField.
Context {F : PF.FArgs} {UI : Basics.Unreachable Numeric.Integer.t}
{UF : Basics.Unreachable PF.t}.
Lemma prime_reduce_mod value : 0 < PF.modulus →
@PF.reduce F value = value mod PF.modulus.
Proof.
intro Hmodulus. change ((if Z.eqb PF.modulus 0 then
@Numeric.Make._rocq_fallback_of_string_0 Numeric.IntegerBase_fargs
else value mod Z.abs PF.modulus) = value mod PF.modulus).
assert (Hnonzero : Z.eqb (@PF.modulus F) 0 = false)
by (apply Z.eqb_neq; lia).
rewrite Hnonzero.
rewrite Z.abs_eq by lia. reflexivity.
Qed.
Lemma prime_reduce_bound value : 0 < PF.modulus →
primeFieldBound (@PF.reduce F value).
Proof.
intro Hmodulus. unfold primeFieldBound. rewrite prime_reduce_mod by exact Hmodulus.
apply Z.mod_pos_bound; exact Hmodulus.
Qed.
Lemma prime_zero_bound : 0 < PF.modulus → primeFieldBound (@PF.zero F).
Proof. intro Hmodulus. apply prime_reduce_bound; exact Hmodulus. Qed.
Lemma prime_int_bound value : 0 < PF.modulus →
primeFieldBound (@PF.op_tildedollar F value).
Proof. intro Hmodulus. apply prime_reduce_bound; exact Hmodulus. Qed.
Lemma prime_add_bound left right :
primeFieldBound left → primeFieldBound right →
primeFieldBound (@PF.op_plus F left right).
Proof.
unfold primeFieldBound. intros Hleft Hright.
change (0 ≤ (if (OCamlZ.compare (left + right) PF.modulus >=? 0) then left + right - PF.modulus
else left + right) < PF.modulus).
unfold OCamlZ.compare. destruct (Z.compare_spec (left + right) PF.modulus); cbn; lia.
Qed.
Lemma prime_sub_bound left right :
primeFieldBound left → primeFieldBound right →
primeFieldBound (@PF.op_minus F left right).
Proof.
unfold primeFieldBound. intros Hleft Hright.
change (0 ≤ (if (OCamlZ.compare (left - right) 0 <? 0) then left - right + PF.modulus
else left - right) < PF.modulus).
unfold OCamlZ.compare. destruct (Z.compare_spec (left - right) 0); cbn; lia.
Qed.
Lemma prime_mul_bound left right : 0 < PF.modulus →
primeFieldBound (@PF.op_star F left right).
Proof. intro Hmodulus. apply prime_reduce_bound; exact Hmodulus. Qed.
Lemma prime_div_bound left right : 0 < PF.modulus →
primeFieldBound (@PF.op_div F left right).
Proof. intro Hmodulus. apply prime_reduce_bound; exact Hmodulus. Qed.
Lemma prime_inv_bound value : 0 < PF.modulus →
primeFieldBound (@PF.inv F value).
Proof. intro Hmodulus. apply prime_reduce_bound; exact Hmodulus. Qed.
Lemma prime_parse_bound input output : 0 < PF.modulus →
@PF.of_uint_opt F input = Some output → primeFieldBound output.
Proof.
intros Hmodulus. unfold PF.of_uint_opt.
destruct (Numeric.Integer.op_gteq _ _); [discriminate|].
intros Hout. inversion Hout; subst. apply prime_reduce_bound; exact Hmodulus.
Qed.
End PrimeField.
A reusable arithmetic invariant, discharged for the actual fields below.
It does not assert group laws or assume anything about VM execution.
Record fieldClosed {A : Set} (field : Ec.Algebra.FIELD (t := A)) (P : A → Prop) : Prop := {
field_zero_closed : P field.(Field.zero);
field_int_closed : ∀ n, P (field.(Field.op_tildedollar) n);
field_add_closed : ∀ x y, P x → P y → P (field.(Field.op_plus) x y);
field_sub_closed : ∀ x y, P x → P y → P (field.(Field.op_minus) x y);
field_mul_closed : ∀ x y, P x → P y → P (field.(Field.op_star) x y);
field_div_closed : ∀ x y, P x → P y → P (field.(Field.op_div) x y)
}.
Lemma fix_preserves {A B : Set} (R : A → A → Prop) (wf : well_founded R)
(body : ∀ x, (∀ y, R y x → B) → B)
(I : A → Prop) (P : B → Prop) :
(∀ x recur, I x → (∀ y edge, I y → P (recur y edge)) → P (body x recur)) →
∀ x, I x → P (Init.Wf.Fix wf (fun _ ⇒ B) body x).
Proof.
intro Hbody.
assert (Hfix : ∀ x (access : Acc R x), I x →
P (Init.Wf.Fix_F (fun _ ⇒ B) body access)).
{ fix IH 2. intros x [children] Hx.
cbn [Init.Wf.Fix_F]. apply Hbody; [exact Hx|].
intros y edge Hy. apply IH; exact Hy. }
intros x Hx. apply Hfix; exact Hx.
Qed.
Lemma dependent_bool_property {A : Type} (P : A → Prop) (test : bool)
(yes : test = true → A) (no : test = false → A) :
(∀ eq, P (yes eq)) → (∀ eq, P (no eq)) →
P ((if test as b return (test = b → A) then yes else no) eq_refl).
Proof. intros Hyes Hno. destruct test; [apply Hyes|apply Hno]. Qed.
Lemma dependent_pair_property {A B C : Type} (P : C → Prop) (pair : A × B)
(next : ∀ x y, pair = (x,y) → C) :
(∀ x y eq, P (next x y eq)) →
P ((match pair as v return (pair = v → C) with (x,y) ⇒ next x y end) eq_refl).
Proof. intro Hnext. destruct pair. apply Hnext. Qed.
Section CurveBounds.
Context {A : Set} {F : @Curve.FArgs A} (P : A → Prop).
Definition curvePointBound (point : @Curve.t A F) : Prop :=
match point with Curve.Point x y ⇒ P x ∧ P y | Curve.Infinity ⇒ True end.
Lemma curve_parse_bound x y point : P x → P y →
@Curve.of_coords A F x y = Some point → curvePointBound point.
Proof.
intros Hx Hy. unfold Curve.of_coords.
destruct (andb _ _); [intro H; inversion H; exact I|].
destruct (Curve.in_curve x y); [intro H; inversion H; subst; split; assumption|discriminate].
Qed.
Lemma curve_coords_bound point : P (Curve.F.(Field.zero)) → curvePointBound point →
let coords := @Curve.coords A F point in P (fst coords) ∧ P (snd coords).
Proof. intros Hzero Hpoint. destruct point; [exact Hpoint|split; exact Hzero]. Qed.
Lemma curve_add_bound left right :
fieldClosed Curve.F P → P Curve.P.(Ec.Curve.Make_P_signature.a_value) →
curvePointBound left → curvePointBound right →
curvePointBound (@Curve.op_plus A F left right).
Proof.
intros Hfield Ha Hleft Hright.
destruct Hfield as [Hzero Hint Hadd Hsub Hmul Hdiv].
destruct left as [x1 y1|], right as [x2 y2|];
try exact Hleft; try exact Hright; try exact I.
destruct Hleft as [Hx1 Hy1], Hright as [Hx2 Hy2].
unfold Curve.op_plus. cbn beta iota zeta.
destruct (Curve.Underlying.(Field.op_ltgt) x1 x2);
destruct (Curve.Underlying.(Field.op_ltgt) y1 y2);
destruct (Curve.Underlying.(Field.op_ltgt) y1 Curve.Underlying.(Field.zero));
cbn [curvePointBound Curve.three Curve.two Curve.Underlying Curve.Params];
repeat split; repeat first [assumption|apply Hsub|apply Hmul|apply Hdiv|apply Hadd|apply Hint].
Qed.
Lemma curve_mul_bound n point :
fieldClosed Curve.F P → P Curve.P.(Ec.Curve.Make_P_signature.a_value) →
curvePointBound point → curvePointBound (@Curve.op_star A F n point).
Proof.
intros Hfield Ha Hpoint. unfold Curve.op_star.
eapply fix_preserves with
(I := fun item ⇒ curvePointBound (snd (fst item)) ∧ curvePointBound (snd item)).
- intros [[scalar current] acc] recur [Hcurrent Hacc] Hrecur.
cbn beta iota zeta in ×.
eapply dependent_bool_property.
+ intros _. exact Hacc.
+ intro Hnonzero. eapply dependent_pair_property. intros next remainder Hdivision.
apply Hrecur. split.
× apply curve_add_bound; assumption.
× destruct (Support.UintN.op_eq remainder (@Support.UintN.one));
[apply curve_add_bound; assumption|exact Hacc].
- split; [exact Hpoint|exact I].
Qed.
End CurveBounds.
field_zero_closed : P field.(Field.zero);
field_int_closed : ∀ n, P (field.(Field.op_tildedollar) n);
field_add_closed : ∀ x y, P x → P y → P (field.(Field.op_plus) x y);
field_sub_closed : ∀ x y, P x → P y → P (field.(Field.op_minus) x y);
field_mul_closed : ∀ x y, P x → P y → P (field.(Field.op_star) x y);
field_div_closed : ∀ x y, P x → P y → P (field.(Field.op_div) x y)
}.
Lemma fix_preserves {A B : Set} (R : A → A → Prop) (wf : well_founded R)
(body : ∀ x, (∀ y, R y x → B) → B)
(I : A → Prop) (P : B → Prop) :
(∀ x recur, I x → (∀ y edge, I y → P (recur y edge)) → P (body x recur)) →
∀ x, I x → P (Init.Wf.Fix wf (fun _ ⇒ B) body x).
Proof.
intro Hbody.
assert (Hfix : ∀ x (access : Acc R x), I x →
P (Init.Wf.Fix_F (fun _ ⇒ B) body access)).
{ fix IH 2. intros x [children] Hx.
cbn [Init.Wf.Fix_F]. apply Hbody; [exact Hx|].
intros y edge Hy. apply IH; exact Hy. }
intros x Hx. apply Hfix; exact Hx.
Qed.
Lemma dependent_bool_property {A : Type} (P : A → Prop) (test : bool)
(yes : test = true → A) (no : test = false → A) :
(∀ eq, P (yes eq)) → (∀ eq, P (no eq)) →
P ((if test as b return (test = b → A) then yes else no) eq_refl).
Proof. intros Hyes Hno. destruct test; [apply Hyes|apply Hno]. Qed.
Lemma dependent_pair_property {A B C : Type} (P : C → Prop) (pair : A × B)
(next : ∀ x y, pair = (x,y) → C) :
(∀ x y eq, P (next x y eq)) →
P ((match pair as v return (pair = v → C) with (x,y) ⇒ next x y end) eq_refl).
Proof. intro Hnext. destruct pair. apply Hnext. Qed.
Section CurveBounds.
Context {A : Set} {F : @Curve.FArgs A} (P : A → Prop).
Definition curvePointBound (point : @Curve.t A F) : Prop :=
match point with Curve.Point x y ⇒ P x ∧ P y | Curve.Infinity ⇒ True end.
Lemma curve_parse_bound x y point : P x → P y →
@Curve.of_coords A F x y = Some point → curvePointBound point.
Proof.
intros Hx Hy. unfold Curve.of_coords.
destruct (andb _ _); [intro H; inversion H; exact I|].
destruct (Curve.in_curve x y); [intro H; inversion H; subst; split; assumption|discriminate].
Qed.
Lemma curve_coords_bound point : P (Curve.F.(Field.zero)) → curvePointBound point →
let coords := @Curve.coords A F point in P (fst coords) ∧ P (snd coords).
Proof. intros Hzero Hpoint. destruct point; [exact Hpoint|split; exact Hzero]. Qed.
Lemma curve_add_bound left right :
fieldClosed Curve.F P → P Curve.P.(Ec.Curve.Make_P_signature.a_value) →
curvePointBound left → curvePointBound right →
curvePointBound (@Curve.op_plus A F left right).
Proof.
intros Hfield Ha Hleft Hright.
destruct Hfield as [Hzero Hint Hadd Hsub Hmul Hdiv].
destruct left as [x1 y1|], right as [x2 y2|];
try exact Hleft; try exact Hright; try exact I.
destruct Hleft as [Hx1 Hy1], Hright as [Hx2 Hy2].
unfold Curve.op_plus. cbn beta iota zeta.
destruct (Curve.Underlying.(Field.op_ltgt) x1 x2);
destruct (Curve.Underlying.(Field.op_ltgt) y1 y2);
destruct (Curve.Underlying.(Field.op_ltgt) y1 Curve.Underlying.(Field.zero));
cbn [curvePointBound Curve.three Curve.two Curve.Underlying Curve.Params];
repeat split; repeat first [assumption|apply Hsub|apply Hmul|apply Hdiv|apply Hadd|apply Hint].
Qed.
Lemma curve_mul_bound n point :
fieldClosed Curve.F P → P Curve.P.(Ec.Curve.Make_P_signature.a_value) →
curvePointBound point → curvePointBound (@Curve.op_star A F n point).
Proof.
intros Hfield Ha Hpoint. unfold Curve.op_star.
eapply fix_preserves with
(I := fun item ⇒ curvePointBound (snd (fst item)) ∧ curvePointBound (snd item)).
- intros [[scalar current] acc] recur [Hcurrent Hacc] Hrecur.
cbn beta iota zeta in ×.
eapply dependent_bool_property.
+ intros _. exact Hacc.
+ intro Hnonzero. eapply dependent_pair_property. intros next remainder Hdivision.
apply Hrecur. split.
× apply curve_add_bound; assumption.
× destruct (Support.UintN.op_eq remainder (@Support.UintN.one));
[apply curve_add_bound; assumption|exact Hacc].
- split; [exact Hpoint|exact I].
Qed.
End CurveBounds.