Library QedkConcrete.dispatchoutputs
Native output bounds at the concrete dispatch table
From Stdlib Require Import ZArith Lia List.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Precompiles QedkConcrete.assemblychecked
QedkConcrete.precompilesframe QedkConcrete.precompileoutputs
QedkConcrete.fixedprecompileoutputs QedkConcrete.altoutputs QedkConcrete.blakeoutputs
QedkConcrete.blspointoutputs QedkConcrete.modexpoutputs
QedkConcrete.reserveprecompile QedkConcrete.hostchecked
QedkConcrete.vmcallchecked QedkConcrete.hostcomposition QedkConcrete.hostframe
QedkConcrete.hostaccountformat QedkConcrete.stakingaccounting
QedkConcrete.blsmapbounds.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
#[local] Hint Extern 100 (Basics.Unreachable _) ⇒
(apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat) : typeclass_instances.
Section Maps.
Context {Key Value : Set} (order : OCamlMap.OrderedType (t := Key)) (P : Value → Prop).
Lemma map_add_values key value table :
P value → Forall (fun entry ⇒ P (snd entry)) table →
Forall (fun entry ⇒ P (snd entry)) (OCamlMap.Make.add order key value table).
Proof.
intros Hvalue Htable. induction Htable as [|[k v] tl Hv Htl IH].
- constructor; [exact Hvalue|constructor].
- cbn [OCamlMap.Make.add].
destruct (OCamlMap.Make.key_before order key k);
[constructor; [exact Hvalue|constructor; assumption]|].
destruct (OCamlMap.Make.key_equal order key k); constructor; assumption.
Qed.
Lemma map_of_list_values table :
Forall (fun entry ⇒ P (snd entry)) table →
Forall (fun entry ⇒ P (snd entry)) (OCamlMap.Make.of_list order table).
Proof.
intro Htable. unfold OCamlMap.Make.of_list.
assert (Hfold : ∀ acc, Forall (fun entry ⇒ P (snd entry)) acc →
Forall (fun entry ⇒ P (snd entry))
(Lists.List.fold_left (fun acc entry ⇒ OCamlMap.Make.add order (fst entry) (snd entry) acc) table acc)).
{ induction Htable as [|[k v] tl Hv Htl IH]; intros acc Hacc; cbn.
- exact Hacc.
- apply IH, map_add_values; assumption. }
apply Hfold. constructor.
Qed.
Lemma map_find_values table key value :
Forall (fun entry ⇒ P (snd entry)) table →
OCamlMap.Make.find_opt order key table = Some value → P value.
Proof.
intro Htable. induction Htable as [|[k v] tl Hv Htl IH]; cbn [OCamlMap.Make.find_opt].
- discriminate.
- destruct (OCamlMap.Make.key_before order key k); [discriminate|].
destruct (OCamlMap.Make.key_equal order key k); [intro Heq; inversion Heq; subst; exact Hv|exact IH].
Qed.
End Maps.
Section Dispatch.
Context {HF : Host.Instantiate.FArgs}.
Local Abbreviation mapG1 := (@Precompiles.Bls12_381.map_fp_to_g1).
Local Definition precompileParams : Precompiles.Make.FArgs :=
{| Precompiles.Make.Revision :=
{| Precompiles.Make_Revision_signature.revision :=
HF.(Host.Instantiate.ChainParams).(Chain.Monad.PARAMS.revision) |} |}.
Local Abbreviation mapG2 := (@Precompiles.Bls12_381.map_fp2_to_g2
(@Precompiles.Make._rocq_fallback_ethereum_precompiles_4 precompileParams)).
Definition blsMapOutputBounds : Prop :=
(∀ msg, resultOutputBound (mapG1 msg)) ∧
(∀ msg, resultOutputBound (mapG2 msg)).
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Precompiles QedkConcrete.assemblychecked
QedkConcrete.precompilesframe QedkConcrete.precompileoutputs
QedkConcrete.fixedprecompileoutputs QedkConcrete.altoutputs QedkConcrete.blakeoutputs
QedkConcrete.blspointoutputs QedkConcrete.modexpoutputs
QedkConcrete.reserveprecompile QedkConcrete.hostchecked
QedkConcrete.vmcallchecked QedkConcrete.hostcomposition QedkConcrete.hostframe
QedkConcrete.hostaccountformat QedkConcrete.stakingaccounting
QedkConcrete.blsmapbounds.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
#[local] Hint Extern 100 (Basics.Unreachable _) ⇒
(apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat) : typeclass_instances.
Section Maps.
Context {Key Value : Set} (order : OCamlMap.OrderedType (t := Key)) (P : Value → Prop).
Lemma map_add_values key value table :
P value → Forall (fun entry ⇒ P (snd entry)) table →
Forall (fun entry ⇒ P (snd entry)) (OCamlMap.Make.add order key value table).
Proof.
intros Hvalue Htable. induction Htable as [|[k v] tl Hv Htl IH].
- constructor; [exact Hvalue|constructor].
- cbn [OCamlMap.Make.add].
destruct (OCamlMap.Make.key_before order key k);
[constructor; [exact Hvalue|constructor; assumption]|].
destruct (OCamlMap.Make.key_equal order key k); constructor; assumption.
Qed.
Lemma map_of_list_values table :
Forall (fun entry ⇒ P (snd entry)) table →
Forall (fun entry ⇒ P (snd entry)) (OCamlMap.Make.of_list order table).
Proof.
intro Htable. unfold OCamlMap.Make.of_list.
assert (Hfold : ∀ acc, Forall (fun entry ⇒ P (snd entry)) acc →
Forall (fun entry ⇒ P (snd entry))
(Lists.List.fold_left (fun acc entry ⇒ OCamlMap.Make.add order (fst entry) (snd entry) acc) table acc)).
{ induction Htable as [|[k v] tl Hv Htl IH]; intros acc Hacc; cbn.
- exact Hacc.
- apply IH, map_add_values; assumption. }
apply Hfold. constructor.
Qed.
Lemma map_find_values table key value :
Forall (fun entry ⇒ P (snd entry)) table →
OCamlMap.Make.find_opt order key table = Some value → P value.
Proof.
intro Htable. induction Htable as [|[k v] tl Hv Htl IH]; cbn [OCamlMap.Make.find_opt].
- discriminate.
- destruct (OCamlMap.Make.key_before order key k); [discriminate|].
destruct (OCamlMap.Make.key_equal order key k); [intro Heq; inversion Heq; subst; exact Hv|exact IH].
Qed.
End Maps.
Section Dispatch.
Context {HF : Host.Instantiate.FArgs}.
Local Abbreviation mapG1 := (@Precompiles.Bls12_381.map_fp_to_g1).
Local Definition precompileParams : Precompiles.Make.FArgs :=
{| Precompiles.Make.Revision :=
{| Precompiles.Make_Revision_signature.revision :=
HF.(Host.Instantiate.ChainParams).(Chain.Monad.PARAMS.revision) |} |}.
Local Abbreviation mapG2 := (@Precompiles.Bls12_381.map_fp2_to_g2
(@Precompiles.Make._rocq_fallback_ethereum_precompiles_4 precompileParams)).
Definition blsMapOutputBounds : Prop :=
(∀ msg, resultOutputBound (mapG1 msg)) ∧
(∀ msg, resultOutputBound (mapG2 msg)).
Approved mathematical premises for the actual generated point operations,
rather than a VM, serializer or precompile-output contract.
Local Abbreviation g1CofactorOrder := blsG1CofactorOrder.
Local Abbreviation g2CofactorOrder := blsG2CofactorOrder.
Lemma bls_map_g1_output_from_order msg : g1CofactorOrder →
OCamlString.length (Evmc.Result.t.output_data (mapG1 msg)) ≤ 128.
Proof.
intro Horder. unfold Precompiles.Bls12_381.map_fp_to_g1.
apply native_run_output; [lia|].
do 2 (apply native_output_bind; intros []).
apply native_output_bind. intro element.
apply native_ensures_return.
cbn beta iota zeta delta [Precompiles.Bls12_381.opened_module_143
nativepointbounds.EP.functor nativepointbounds.EP.Ec_precompile_utils_result.delta_1_inv].
rewrite (@bls_point_encoding Precompiles.Bls12_381._rocq_fallback_g1_add_0
Precompiles.Bls12_381._rocq_fallback_g1_add_1); [lia|].
apply bls_map_g1_bound. exact Horder.
Qed.
Lemma bls_map_g2_output_from_order msg : g2CofactorOrder →
OCamlString.length (Evmc.Result.t.output_data (mapG2 msg)) ≤ 256.
Proof.
intro Horder. unfold Precompiles.Bls12_381.map_fp2_to_g2.
apply native_run_output; [lia|].
do 2 (apply native_output_bind; intros []).
apply native_output_bind. intro element.
apply native_ensures_return.
cbn beta iota zeta delta [Precompiles.Bls12_381.opened_module_143
nativepointbounds.EP.functor nativepointbounds.EP.Ec_precompile_utils_result.delta_2_inv].
rewrite (@bls_point2_encoding Precompiles.Bls12_381._rocq_fallback_g1_add_0
Precompiles.Bls12_381._rocq_fallback_g1_add_1); [lia|].
apply bls_map_g2_bound. exact Horder.
Qed.
Lemma bls_map_output_bounds_from_cofactor_orders :
g1CofactorOrder → g2CofactorOrder → blsMapOutputBounds.
Proof.
intros H1 H2. split; intro msg; unfold resultOutputBound.
- eapply Z.le_lt_trans; [apply bls_map_g1_output_from_order; exact H1|lia].
- eapply Z.le_lt_trans; [apply bls_map_g2_output_from_order; exact H2|lia].
Qed.
Lemma native_output_bounds_from_bls_maps :
HF.(Host.Instantiate.ChainParams).(Chain.Monad.PARAMS.revision) = Support.Variants.Revision.Eleven →
sha256OutputSize → blsMapOutputBounds → @nativeOutputBounds HF.
Proof.
intros Hrevision Hsha [Hmap1 Hmap2] vm msg addr action Hmsg Hselected state Hformat.
unfold nativeDispatch, Precompiles.Make.try_precompile in Hselected.
match type of Hselected with context [@Chain.Ethereum.Address.Map.find_opt ?T addr ?entries] ⇒
destruct (@Chain.Ethereum.Address.Map.find_opt T addr entries) as [pure|] eqn:Hethereum in Hselected end.
- assert (Hbound : resultOutputBound (pure msg)).
{ eapply (map_find_values _ (fun callback ⇒ resultOutputBound (callback msg)));
[|exact Hethereum].
unfold Precompiles.Make.ethereum_precompiles.
apply (map_of_list_values stringmaps.stringOrder (fun callback ⇒ resultOutputBound (callback msg))).
repeat first [apply Forall_cons | apply Forall_nil].
all: cbn beta iota zeta delta [snd].
all: unfold resultOutputBound.
+ eapply Z.le_lt_trans; [apply ecrecover_output_bounded|lia].
+ eapply Z.le_lt_trans; [apply sha256_output_bounded; exact Hsha|lia].
+ eapply Z.le_lt_trans; [apply ripemd160_output_bounded|lia].
+ eapply Z.le_lt_trans; [apply identity_output_bounded|pose proof (proj1 (proj2 (proj2 Hmsg))); lia].
+ eapply Z.le_lt_trans; [apply modexp_output_bounded; exact Hrevision|lia].
+ eapply Z.le_lt_trans; [apply alt_add_output_bounded|lia].
+ eapply Z.le_lt_trans; [apply alt_mul_output_bounded|lia].
+ eapply Z.le_lt_trans; [apply alt_pairing_output_bounded|lia].
+ eapply Z.le_lt_trans; [apply blake2f_output_bounded|lia].
+ eapply Z.le_lt_trans; [apply kzg_output_bounded|lia].
+ eapply Z.le_lt_trans; [apply bls_g1_add_output_bounded|lia].
+ eapply Z.le_lt_trans; [apply bls_g1_msm_output_bounded; exact (proj1 (proj2 (proj2 Hmsg)))|lia].
+ eapply Z.le_lt_trans; [apply bls_g2_add_output_bounded|lia].
+ eapply Z.le_lt_trans; [apply bls_g2_msm_output_bounded; exact (proj1 (proj2 (proj2 Hmsg)))|lia].
+ eapply Z.le_lt_trans; [apply bls_pairing_output_bounded|lia].
+ exact (Hmap1 msg).
+ exact (Hmap2 msg).
+ eapply Z.le_lt_trans; [apply secp256r1_output_bounded|lia].
}
destruct (Evmc.Message.t.delegated msg) in Hselected; inversion Hselected; subst action.
+ change (0 < 2^256). lia.
+ exact Hbound.
- unfold Precompiles.Make.monad_precompiles in Hselected.
change (@Precompiles.Make.Revision (@H.Precompiles_fargs
(H.Build_FArgs HF.(Host.Instantiate.ChainParams) vm))).(Precompiles.Make_Revision_signature.revision)
with HF.(Host.Instantiate.ChainParams).(Chain.Monad.PARAMS.revision) in Hselected.
rewrite Hrevision in Hselected.
match type of Hselected with context [@Chain.Ethereum.Address.Map.find_opt ?T addr ?entries] ⇒
destruct (@Chain.Ethereum.Address.Map.find_opt T addr entries) as [native|] eqn:Hnative in Hselected end.
2: discriminate.
apply precompilesframe.find_pair_value in Hnative as [[_ ->] | [_ ->]];
match type of Hselected with context [if ?reject then _ else _] ⇒ destruct reject in Hselected end;
inversion Hselected; subst action.
+ change (0 < 2^256). lia.
+ unfold resultOutputBound. eapply Z.le_lt_trans; [apply precompile_output_bounded|]. lia.
+ change (0 < 2^256). lia.
+ unfold resultOutputBound.
apply staking_contract_output_lt_2_256.
exact (stakingStateWords_of_world state (proj1 Hformat)).
Qed.
End Dispatch.
Local Abbreviation g2CofactorOrder := blsG2CofactorOrder.
Lemma bls_map_g1_output_from_order msg : g1CofactorOrder →
OCamlString.length (Evmc.Result.t.output_data (mapG1 msg)) ≤ 128.
Proof.
intro Horder. unfold Precompiles.Bls12_381.map_fp_to_g1.
apply native_run_output; [lia|].
do 2 (apply native_output_bind; intros []).
apply native_output_bind. intro element.
apply native_ensures_return.
cbn beta iota zeta delta [Precompiles.Bls12_381.opened_module_143
nativepointbounds.EP.functor nativepointbounds.EP.Ec_precompile_utils_result.delta_1_inv].
rewrite (@bls_point_encoding Precompiles.Bls12_381._rocq_fallback_g1_add_0
Precompiles.Bls12_381._rocq_fallback_g1_add_1); [lia|].
apply bls_map_g1_bound. exact Horder.
Qed.
Lemma bls_map_g2_output_from_order msg : g2CofactorOrder →
OCamlString.length (Evmc.Result.t.output_data (mapG2 msg)) ≤ 256.
Proof.
intro Horder. unfold Precompiles.Bls12_381.map_fp2_to_g2.
apply native_run_output; [lia|].
do 2 (apply native_output_bind; intros []).
apply native_output_bind. intro element.
apply native_ensures_return.
cbn beta iota zeta delta [Precompiles.Bls12_381.opened_module_143
nativepointbounds.EP.functor nativepointbounds.EP.Ec_precompile_utils_result.delta_2_inv].
rewrite (@bls_point2_encoding Precompiles.Bls12_381._rocq_fallback_g1_add_0
Precompiles.Bls12_381._rocq_fallback_g1_add_1); [lia|].
apply bls_map_g2_bound. exact Horder.
Qed.
Lemma bls_map_output_bounds_from_cofactor_orders :
g1CofactorOrder → g2CofactorOrder → blsMapOutputBounds.
Proof.
intros H1 H2. split; intro msg; unfold resultOutputBound.
- eapply Z.le_lt_trans; [apply bls_map_g1_output_from_order; exact H1|lia].
- eapply Z.le_lt_trans; [apply bls_map_g2_output_from_order; exact H2|lia].
Qed.
Lemma native_output_bounds_from_bls_maps :
HF.(Host.Instantiate.ChainParams).(Chain.Monad.PARAMS.revision) = Support.Variants.Revision.Eleven →
sha256OutputSize → blsMapOutputBounds → @nativeOutputBounds HF.
Proof.
intros Hrevision Hsha [Hmap1 Hmap2] vm msg addr action Hmsg Hselected state Hformat.
unfold nativeDispatch, Precompiles.Make.try_precompile in Hselected.
match type of Hselected with context [@Chain.Ethereum.Address.Map.find_opt ?T addr ?entries] ⇒
destruct (@Chain.Ethereum.Address.Map.find_opt T addr entries) as [pure|] eqn:Hethereum in Hselected end.
- assert (Hbound : resultOutputBound (pure msg)).
{ eapply (map_find_values _ (fun callback ⇒ resultOutputBound (callback msg)));
[|exact Hethereum].
unfold Precompiles.Make.ethereum_precompiles.
apply (map_of_list_values stringmaps.stringOrder (fun callback ⇒ resultOutputBound (callback msg))).
repeat first [apply Forall_cons | apply Forall_nil].
all: cbn beta iota zeta delta [snd].
all: unfold resultOutputBound.
+ eapply Z.le_lt_trans; [apply ecrecover_output_bounded|lia].
+ eapply Z.le_lt_trans; [apply sha256_output_bounded; exact Hsha|lia].
+ eapply Z.le_lt_trans; [apply ripemd160_output_bounded|lia].
+ eapply Z.le_lt_trans; [apply identity_output_bounded|pose proof (proj1 (proj2 (proj2 Hmsg))); lia].
+ eapply Z.le_lt_trans; [apply modexp_output_bounded; exact Hrevision|lia].
+ eapply Z.le_lt_trans; [apply alt_add_output_bounded|lia].
+ eapply Z.le_lt_trans; [apply alt_mul_output_bounded|lia].
+ eapply Z.le_lt_trans; [apply alt_pairing_output_bounded|lia].
+ eapply Z.le_lt_trans; [apply blake2f_output_bounded|lia].
+ eapply Z.le_lt_trans; [apply kzg_output_bounded|lia].
+ eapply Z.le_lt_trans; [apply bls_g1_add_output_bounded|lia].
+ eapply Z.le_lt_trans; [apply bls_g1_msm_output_bounded; exact (proj1 (proj2 (proj2 Hmsg)))|lia].
+ eapply Z.le_lt_trans; [apply bls_g2_add_output_bounded|lia].
+ eapply Z.le_lt_trans; [apply bls_g2_msm_output_bounded; exact (proj1 (proj2 (proj2 Hmsg)))|lia].
+ eapply Z.le_lt_trans; [apply bls_pairing_output_bounded|lia].
+ exact (Hmap1 msg).
+ exact (Hmap2 msg).
+ eapply Z.le_lt_trans; [apply secp256r1_output_bounded|lia].
}
destruct (Evmc.Message.t.delegated msg) in Hselected; inversion Hselected; subst action.
+ change (0 < 2^256). lia.
+ exact Hbound.
- unfold Precompiles.Make.monad_precompiles in Hselected.
change (@Precompiles.Make.Revision (@H.Precompiles_fargs
(H.Build_FArgs HF.(Host.Instantiate.ChainParams) vm))).(Precompiles.Make_Revision_signature.revision)
with HF.(Host.Instantiate.ChainParams).(Chain.Monad.PARAMS.revision) in Hselected.
rewrite Hrevision in Hselected.
match type of Hselected with context [@Chain.Ethereum.Address.Map.find_opt ?T addr ?entries] ⇒
destruct (@Chain.Ethereum.Address.Map.find_opt T addr entries) as [native|] eqn:Hnative in Hselected end.
2: discriminate.
apply precompilesframe.find_pair_value in Hnative as [[_ ->] | [_ ->]];
match type of Hselected with context [if ?reject then _ else _] ⇒ destruct reject in Hselected end;
inversion Hselected; subst action.
+ change (0 < 2^256). lia.
+ unfold resultOutputBound. eapply Z.le_lt_trans; [apply precompile_output_bounded|]. lia.
+ change (0 < 2^256). lia.
+ unfold resultOutputBound.
apply staking_contract_output_lt_2_256.
exact (stakingStateWords_of_world state (proj1 Hformat)).
Qed.
End Dispatch.