Library QedkConcrete.precompileoutputs
Native return-data bounds
From Stdlib Require Import ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Precompiles MonadVM.Crypto QedkConcrete.bytewords.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Module PC := Precompiles.Utils.Precompile.
Definition sha256OutputSize : Prop :=
∀ bytes, OCamlString.length
(Digestif.SHA256.(Digestif.S.to_raw_string)
(Digestif.SHA256.(Digestif.S.digest_string) None None bytes)) = 32.
Definition ripemd160OutputSize : Prop :=
∀ bytes, OCamlString.length
(Digestif.RMD160.(Digestif.S.to_raw_string)
(Digestif.RMD160.(Digestif.S.digest_string) None None bytes)) = 20.
Lemma sha256_length {US : Basics.Unreachable string} {UI : Basics.Unreachable int} bytes :
sha256OutputSize → OCamlString.length (@Crypto.sha_256 bytes) = 32.
Proof.
intro Hsize. unfold Crypto.sha_256.
rewrite b32_checked_identity by apply Hsize. apply Hsize.
Qed.
Lemma ripemd160_length {U20 : Basics.Unreachable Byte_string.B20.t}
{UI : Basics.Unreachable int} bytes :
ripemd160OutputSize → OCamlString.length (@Crypto.ripemd_160 bytes) = 20.
Proof.
intro Hsize. unfold Crypto.ripemd_160, Byte_string.B20.of_bytes_exn.
cbn -[OCamlString.length].
unfold Byte_string.Fixed.of_bytes_exn, Byte_string.Fixed.of_bytes.
cbn -[OCamlString.length]. unfold Byte_string.Fixed.Impl.of_bytes.
rewrite Hsize. cbn -[OCamlString.length]. apply Hsize.
Qed.
Lemma address_word_length addr :
OCamlString.length (@Byte_string.B20.to_bytes32 addr) = 32.
Proof. apply init_length. lia. Qed.
Definition nativeOutputBound bound (action : PC.t Byte_string.Bytes.t) : Prop :=
∀ state, match fst (action state) with
| inl bytes ⇒ OCamlString.length bytes ≤ bound | inr _ ⇒ True end.
Lemma native_output_bind {A : Set} bound (action : PC.t A)
(next : A → PC.t Byte_string.Bytes.t) :
(∀ value, nativeOutputBound bound (next value)) →
nativeOutputBound bound (PC.op_letdollar action next).
Proof.
intros Hnext state.
change (match fst (let '(result, middle) := action state in
match result with inl value ⇒ next value middle | inr err ⇒ (inr err, middle) end) with
| inl bytes ⇒ OCamlString.length bytes ≤ bound | inr _ ⇒ True end).
destruct (action state) as [[value|err] middle]; [apply Hnext|exact I].
Qed.
Lemma native_output_return bound bytes :
OCamlString.length bytes ≤ bound → nativeOutputBound bound (PC._return bytes).
Proof. intros Hlength state. exact Hlength. Qed.
Lemma native_run_output bound msg action :
0 ≤ bound → nativeOutputBound bound action →
OCamlString.length (Evmc.Result.t.output_data (@PC.run msg action)) ≤ bound.
Proof.
intros Hbound Haction. specialize (Haction (msg, 0)). unfold PC.run.
destruct (action (msg, 0)) as [[bytes|err] [after offset]].
- exact Haction.
- change (0 ≤ bound). exact Hbound.
Qed.
Section BasicPrecompiles.
Context {U64 : Basics.Unreachable MonadVM.Uint64.t} {UG : Basics.Unreachable Gas.t}
{UA : Basics.Unreachable Chain.Ethereum.Address.t} {UU : Basics.Unreachable unit}
{US : Basics.Unreachable string} {UI : Basics.Unreachable int}
{U20 : Basics.Unreachable Byte_string.B20.t} {UC : Basics.Unreachable ascii}.
Lemma sha256_output_bounded msg : sha256OutputSize →
OCamlString.length (Evmc.Result.t.output_data (@Precompiles.sha256 msg)) ≤ 32.
Proof.
intro Hsize. unfold Precompiles.sha256. apply native_run_output; [lia|].
apply native_output_bind. intros []. apply native_output_return.
change (OCamlString.length (@Crypto.sha_256 (Evmc.Message.t.input_data msg)) ≤ 32).
rewrite sha256_length by exact Hsize. lia.
Qed.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Precompiles MonadVM.Crypto QedkConcrete.bytewords.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Module PC := Precompiles.Utils.Precompile.
Definition sha256OutputSize : Prop :=
∀ bytes, OCamlString.length
(Digestif.SHA256.(Digestif.S.to_raw_string)
(Digestif.SHA256.(Digestif.S.digest_string) None None bytes)) = 32.
Definition ripemd160OutputSize : Prop :=
∀ bytes, OCamlString.length
(Digestif.RMD160.(Digestif.S.to_raw_string)
(Digestif.RMD160.(Digestif.S.digest_string) None None bytes)) = 20.
Lemma sha256_length {US : Basics.Unreachable string} {UI : Basics.Unreachable int} bytes :
sha256OutputSize → OCamlString.length (@Crypto.sha_256 bytes) = 32.
Proof.
intro Hsize. unfold Crypto.sha_256.
rewrite b32_checked_identity by apply Hsize. apply Hsize.
Qed.
Lemma ripemd160_length {U20 : Basics.Unreachable Byte_string.B20.t}
{UI : Basics.Unreachable int} bytes :
ripemd160OutputSize → OCamlString.length (@Crypto.ripemd_160 bytes) = 20.
Proof.
intro Hsize. unfold Crypto.ripemd_160, Byte_string.B20.of_bytes_exn.
cbn -[OCamlString.length].
unfold Byte_string.Fixed.of_bytes_exn, Byte_string.Fixed.of_bytes.
cbn -[OCamlString.length]. unfold Byte_string.Fixed.Impl.of_bytes.
rewrite Hsize. cbn -[OCamlString.length]. apply Hsize.
Qed.
Lemma address_word_length addr :
OCamlString.length (@Byte_string.B20.to_bytes32 addr) = 32.
Proof. apply init_length. lia. Qed.
Definition nativeOutputBound bound (action : PC.t Byte_string.Bytes.t) : Prop :=
∀ state, match fst (action state) with
| inl bytes ⇒ OCamlString.length bytes ≤ bound | inr _ ⇒ True end.
Lemma native_output_bind {A : Set} bound (action : PC.t A)
(next : A → PC.t Byte_string.Bytes.t) :
(∀ value, nativeOutputBound bound (next value)) →
nativeOutputBound bound (PC.op_letdollar action next).
Proof.
intros Hnext state.
change (match fst (let '(result, middle) := action state in
match result with inl value ⇒ next value middle | inr err ⇒ (inr err, middle) end) with
| inl bytes ⇒ OCamlString.length bytes ≤ bound | inr _ ⇒ True end).
destruct (action state) as [[value|err] middle]; [apply Hnext|exact I].
Qed.
Lemma native_output_return bound bytes :
OCamlString.length bytes ≤ bound → nativeOutputBound bound (PC._return bytes).
Proof. intros Hlength state. exact Hlength. Qed.
Lemma native_run_output bound msg action :
0 ≤ bound → nativeOutputBound bound action →
OCamlString.length (Evmc.Result.t.output_data (@PC.run msg action)) ≤ bound.
Proof.
intros Hbound Haction. specialize (Haction (msg, 0)). unfold PC.run.
destruct (action (msg, 0)) as [[bytes|err] [after offset]].
- exact Haction.
- change (0 ≤ bound). exact Hbound.
Qed.
Section BasicPrecompiles.
Context {U64 : Basics.Unreachable MonadVM.Uint64.t} {UG : Basics.Unreachable Gas.t}
{UA : Basics.Unreachable Chain.Ethereum.Address.t} {UU : Basics.Unreachable unit}
{US : Basics.Unreachable string} {UI : Basics.Unreachable int}
{U20 : Basics.Unreachable Byte_string.B20.t} {UC : Basics.Unreachable ascii}.
Lemma sha256_output_bounded msg : sha256OutputSize →
OCamlString.length (Evmc.Result.t.output_data (@Precompiles.sha256 msg)) ≤ 32.
Proof.
intro Hsize. unfold Precompiles.sha256. apply native_run_output; [lia|].
apply native_output_bind. intros []. apply native_output_return.
change (OCamlString.length (@Crypto.sha_256 (Evmc.Message.t.input_data msg)) ≤ 32).
rewrite sha256_length by exact Hsize. lia.
Qed.
The serializer always emits 32 bytes, even for an arbitrary raw digest.
ripemd160_length additionally rules out its invalid-length fallback.
Lemma ripemd160_output_bounded msg :
OCamlString.length (Evmc.Result.t.output_data
(@Precompiles.ripemd160 msg)) ≤ 32.
Proof.
unfold Precompiles.ripemd160. apply native_run_output; [lia|].
apply native_output_bind. intros []. apply native_output_return.
change (OCamlString.length (@Byte_string.B20.to_bytes32
(@Crypto.ripemd_160 (Evmc.Message.t.input_data msg))) ≤ 32).
rewrite address_word_length. lia.
Qed.
Lemma identity_output_bounded msg :
OCamlString.length (Evmc.Result.t.output_data (@Precompiles.identity msg)) ≤
OCamlString.length (Evmc.Message.t.input_data msg).
Proof.
unfold Precompiles.identity. apply native_run_output.
- unfold OCamlString.length. lia.
- apply native_output_bind. intros []. apply native_output_return. lia.
Qed.
End BasicPrecompiles.
Print Assumptions sha256_output_bounded.
Print Assumptions ripemd160_output_bounded.
Lemma optional_output_bind {A : Set} bound (action : option A)
(next : A → option Byte_string.Bytes.t) :
0 ≤ bound →
(∀ value, OCamlString.length (PC.Option.value (next value) Byte_string.Bytes.empty) ≤ bound) →
OCamlString.length
(PC.Option.value (PC.Option.op_letdollar action next) Byte_string.Bytes.empty) ≤ bound.
Proof. intros Hbound Hnext. destruct action; [apply Hnext|exact Hbound]. Qed.
Ltac finish_optional_output :=
cbv beta iota zeta;
lazymatch goal with
| |- OCamlString.length (PC.Option.value (PC.Option.op_letdollar _ _) _) ≤ _ ⇒
apply optional_output_bind; [lia|];
let value := fresh "value" in intro value; finish_optional_output
| |- OCamlString.length (PC.Option.value (PC.Option._return ?bytes) _) ≤ _ ⇒
change (OCamlString.length bytes ≤ 32);
apply Z.eq_le_incl;
lazymatch bytes with
| Numeric.U256.to_repr_bytes ?value ⇒ exact (word_bytes_encoding_length value)
| _ ⇒ apply address_word_length
end
| |- OCamlString.length (PC.Option.value (match ?pair with (_, _) ⇒ _ end) _) ≤ _ ⇒
destruct pair; finish_optional_output
end.
Lemma ecrecover_output_bounded
`{_rocq_assumption_0 : RocqOfOCaml.Basics.Unreachable Z.t}
`{_rocq_assumption_1 : RocqOfOCaml.Basics.Unreachable MonadVM.Uint64.t}
`{_rocq_assumption_2 :
RocqOfOCaml.Basics.Unreachable MonadVM.Numeric.U8.Repr.t}
`{_rocq_assumption_3 : RocqOfOCaml.Basics.Unreachable MonadVM.Numeric.U8.t}
`{_rocq_assumption_4 :
RocqOfOCaml.Basics.Unreachable MonadVM.Numeric.U256.Repr.t}
`{_rocq_assumption_5 : RocqOfOCaml.Basics.Unreachable MonadVM.Numeric.U256.t}
`{_rocq_assumption_6 : RocqOfOCaml.Basics.Unreachable MonadVM.Gas.t}
`{_rocq_assumption_7 :
RocqOfOCaml.Basics.Unreachable MonadVM.Chain.Ethereum.Address.t}
`{_rocq_assumption_8 :
RocqOfOCaml.Basics.Unreachable MonadVM.Byte_string.B20.t}
`{_rocq_assumption_9 : RocqOfOCaml.Basics.Unreachable unit}
`{_rocq_assumption_10 : RocqOfOCaml.Basics.Unreachable string}
`{_rocq_assumption_11 : RocqOfOCaml.Basics.Unreachable int}
`{_rocq_assumption_12 : RocqOfOCaml.Basics.Unreachable ascii}
`{_rocq_assumption_13 : RocqOfOCaml.Basics.Unreachable (int × int)}
`{_rocq_assumption_14 :
RocqOfOCaml.Basics.Unreachable
MonadVM.Byte_string.B32.(MonadVM.Byte_string.Fixed.Fixed_result.t)}
(msg : Evmc.Message.t) :
OCamlString.length (Evmc.Result.t.output_data
(@Precompiles.ecrecover msg)) ≤ 32.
Proof.
unfold Precompiles.ecrecover. apply native_run_output; [lia|].
repeat match goal with
| |- nativeOutputBound _ (PC.op_letdollar _ _) ⇒
apply native_output_bind; intro; cbn beta iota zeta
end.
apply native_output_return. finish_optional_output.
Qed.
Lemma secp256r1_output_bounded
(msg : Evmc.Message.t) :
OCamlString.length (Evmc.Result.t.output_data
(@Precompiles.Secp256r1.verify msg)) ≤ 32.
Proof.
unfold Precompiles.Secp256r1.verify. apply native_run_output; [lia|].
repeat match goal with
| |- nativeOutputBound _ (PC.op_letdollar _ _) ⇒
apply native_output_bind; intro; cbn beta iota zeta
end.
apply native_output_return. finish_optional_output.
Qed.
Print Assumptions ecrecover_output_bounded.
Print Assumptions secp256r1_output_bounded.
OCamlString.length (Evmc.Result.t.output_data
(@Precompiles.ripemd160 msg)) ≤ 32.
Proof.
unfold Precompiles.ripemd160. apply native_run_output; [lia|].
apply native_output_bind. intros []. apply native_output_return.
change (OCamlString.length (@Byte_string.B20.to_bytes32
(@Crypto.ripemd_160 (Evmc.Message.t.input_data msg))) ≤ 32).
rewrite address_word_length. lia.
Qed.
Lemma identity_output_bounded msg :
OCamlString.length (Evmc.Result.t.output_data (@Precompiles.identity msg)) ≤
OCamlString.length (Evmc.Message.t.input_data msg).
Proof.
unfold Precompiles.identity. apply native_run_output.
- unfold OCamlString.length. lia.
- apply native_output_bind. intros []. apply native_output_return. lia.
Qed.
End BasicPrecompiles.
Print Assumptions sha256_output_bounded.
Print Assumptions ripemd160_output_bounded.
Lemma optional_output_bind {A : Set} bound (action : option A)
(next : A → option Byte_string.Bytes.t) :
0 ≤ bound →
(∀ value, OCamlString.length (PC.Option.value (next value) Byte_string.Bytes.empty) ≤ bound) →
OCamlString.length
(PC.Option.value (PC.Option.op_letdollar action next) Byte_string.Bytes.empty) ≤ bound.
Proof. intros Hbound Hnext. destruct action; [apply Hnext|exact Hbound]. Qed.
Ltac finish_optional_output :=
cbv beta iota zeta;
lazymatch goal with
| |- OCamlString.length (PC.Option.value (PC.Option.op_letdollar _ _) _) ≤ _ ⇒
apply optional_output_bind; [lia|];
let value := fresh "value" in intro value; finish_optional_output
| |- OCamlString.length (PC.Option.value (PC.Option._return ?bytes) _) ≤ _ ⇒
change (OCamlString.length bytes ≤ 32);
apply Z.eq_le_incl;
lazymatch bytes with
| Numeric.U256.to_repr_bytes ?value ⇒ exact (word_bytes_encoding_length value)
| _ ⇒ apply address_word_length
end
| |- OCamlString.length (PC.Option.value (match ?pair with (_, _) ⇒ _ end) _) ≤ _ ⇒
destruct pair; finish_optional_output
end.
Lemma ecrecover_output_bounded
`{_rocq_assumption_0 : RocqOfOCaml.Basics.Unreachable Z.t}
`{_rocq_assumption_1 : RocqOfOCaml.Basics.Unreachable MonadVM.Uint64.t}
`{_rocq_assumption_2 :
RocqOfOCaml.Basics.Unreachable MonadVM.Numeric.U8.Repr.t}
`{_rocq_assumption_3 : RocqOfOCaml.Basics.Unreachable MonadVM.Numeric.U8.t}
`{_rocq_assumption_4 :
RocqOfOCaml.Basics.Unreachable MonadVM.Numeric.U256.Repr.t}
`{_rocq_assumption_5 : RocqOfOCaml.Basics.Unreachable MonadVM.Numeric.U256.t}
`{_rocq_assumption_6 : RocqOfOCaml.Basics.Unreachable MonadVM.Gas.t}
`{_rocq_assumption_7 :
RocqOfOCaml.Basics.Unreachable MonadVM.Chain.Ethereum.Address.t}
`{_rocq_assumption_8 :
RocqOfOCaml.Basics.Unreachable MonadVM.Byte_string.B20.t}
`{_rocq_assumption_9 : RocqOfOCaml.Basics.Unreachable unit}
`{_rocq_assumption_10 : RocqOfOCaml.Basics.Unreachable string}
`{_rocq_assumption_11 : RocqOfOCaml.Basics.Unreachable int}
`{_rocq_assumption_12 : RocqOfOCaml.Basics.Unreachable ascii}
`{_rocq_assumption_13 : RocqOfOCaml.Basics.Unreachable (int × int)}
`{_rocq_assumption_14 :
RocqOfOCaml.Basics.Unreachable
MonadVM.Byte_string.B32.(MonadVM.Byte_string.Fixed.Fixed_result.t)}
(msg : Evmc.Message.t) :
OCamlString.length (Evmc.Result.t.output_data
(@Precompiles.ecrecover msg)) ≤ 32.
Proof.
unfold Precompiles.ecrecover. apply native_run_output; [lia|].
repeat match goal with
| |- nativeOutputBound _ (PC.op_letdollar _ _) ⇒
apply native_output_bind; intro; cbn beta iota zeta
end.
apply native_output_return. finish_optional_output.
Qed.
Lemma secp256r1_output_bounded
(msg : Evmc.Message.t) :
OCamlString.length (Evmc.Result.t.output_data
(@Precompiles.Secp256r1.verify msg)) ≤ 32.
Proof.
unfold Precompiles.Secp256r1.verify. apply native_run_output; [lia|].
repeat match goal with
| |- nativeOutputBound _ (PC.op_letdollar _ _) ⇒
apply native_output_bind; intro; cbn beta iota zeta
end.
apply native_output_return. finish_optional_output.
Qed.
Print Assumptions ecrecover_output_bounded.
Print Assumptions secp256r1_output_bounded.