Library QedkConcrete.transactionchecksproofs

Public checks, exact sender nonce/code, and fee backing exhaust the concrete validator. No premise assumes validation or VM success.
From Stdlib Require Import Bool ZArith NArith List Lia.
From stdpp Require Import decidable numbers strings.
Require Import RocqOfOCaml.RocqOfOCaml QedkConcrete.transactionchecks
  QedkConcrete.publictransaction.
Set Default Goal Selector "!".
Import ListNotations.
Module E := Execution.Make.
Module B := State.BlockState.
Module W := State.WorldState.
Module A := Chain.Ethereum.Account.
Module T := Chain.Ethereum.Transaction.

Local Lemma asbool_N_eq (x y : N) :
  bool_decide (x = y) = negb (Support.UintN.op_ltgt x y).
Proof.
  change (bool_decide (x = y) = negb (negb (N.eqb x y))).
  rewrite Bool.negb_involutive. destruct (N.eqb x y) eqn:Heq.
  - apply bool_decide_eq_true_2. now apply N.eqb_eq.
  - apply bool_decide_eq_false_2. now apply N.eqb_neq.
Qed.

Local Lemma asbool_U64_ne (x y : Numeric.U64.t) :
  bool_decide (x ≠ y) = negb (Numeric.U64.op_eq x y).
Proof.
  change (bool_decide (x ≠ y) =
    negb (Z.eqb (match Z.compare x y with Eq ⇒ 0 | Lt ⇒ -1 | Gt ⇒ 1 end) 0)).
  destruct (Z.compare_spec x y); cbn [Z.eqb negb].
  - apply bool_decide_eq_false_2. congruence.
  - apply bool_decide_eq_true_2. lia.
  - apply bool_decide_eq_true_2. lia.
Qed.

Local Lemma asbool_U64_eq (x y : Numeric.U64.t) :
  bool_decide (x = y) = negb (Numeric.U64.op_ltgt x y).
Proof.
  change (bool_decide (x = y) =
    negb (negb (Z.eqb (match Z.compare x y with Eq ⇒ 0 | Lt ⇒ -1 | Gt ⇒ 1 end) 0))).
  rewrite Bool.negb_involutive. destruct (Z.compare_spec x y); cbn [Z.eqb].
  - apply bool_decide_eq_true_2. assumption.
  - apply bool_decide_eq_false_2. lia.
  - apply bool_decide_eq_false_2. lia.
Qed.

Local Lemma asbool_Z_le (x y : Z) :
  bool_decide (x ≤ y)%Z = negb (Z.gtb x y).
Proof.
  destruct (Z.gtb x y) eqn:Hgt; cbn [negb].
  - apply bool_decide_eq_false_2. apply Z.gtb_lt in Hgt. lia.
  - apply bool_decide_eq_true_2. rewrite Z.gtb_ltb in Hgt. now apply Z.ltb_ge in Hgt.
Qed.

Local Lemma asbool_N_le (x y : N) :
  bool_decide (x ≤ y)%N = negb (N.ltb y x).
Proof.
  destruct (N.ltb y x) eqn:Hlt; cbn [negb].
  - apply bool_decide_eq_false_2. apply N.ltb_lt in Hlt. lia.
  - apply bool_decide_eq_true_2. now apply N.ltb_ge in Hlt.
Qed.

Section Validation.
Context {F : E.FArgs}.
Local Abbreviation readAccount := (fun world addr ⇒
  State.op_pointcaretlparenrparen world (@W.account None addr)).

The readable comparisons are exactly the previous native-operator checks, for all inputs, without representation bounds or additional premises.
Lemma transaction_checks_native header tx expectedNonce :
  transactionChecks header tx expectedNonce =
  negb (match T.chain_id tx with
    | Some id ⇒ Support.UintN.op_ltgt id (@E.Params F).(Execution.Make_Params_signature.chain_id)
    | None ⇒ false end) &&
  negb (Numeric.U64.op_eq (T.nonce tx) Numeric.U64.max_t) &&
  negb (Numeric.U64.op_ltgt expectedNonce (T.nonce tx)) &&
  negb (Z.gtb (match T.call_or_create_value tx with
    | T.Create {| T.call_or_create.Create.initcode := code |} ⇒ Byte_string.Bytes.length code
    | _ ⇒ 0%Z end) Chain.Monad.Constants.max_init_code_size) &&
  negb (Gas.op_gt (@Gas.tx_intrinsic_gas tx) (T.gas_limit tx)) &&
  negb (Gas.op_gt (T.gas_limit tx) (@Gas.tx_max_gas_limit)) &&
  negb (Gas.op_gt (@Gas.tx_floor_gas tx) (T.gas_limit tx)) &&
  (match tx with T.SetCode t ⇒ match t.(T.set_code_tx.authorization_list) with
     | [] ⇒ false | _ :: _ ⇒ true end | _ ⇒ true end) &&
  (match @Gas.tx_effective_gas_price header.(Chain.Ethereum.Block.Header.t.base_fee_per_gas) tx with
   | Some price ⇒ negb (Gas.op_lt price header.(Chain.Ethereum.Block.Header.t.base_fee_per_gas))
   | None ⇒ false end).
Proof.
  unfold transactionChecks.
  rewrite asbool_U64_ne, asbool_U64_eq, asbool_Z_le, !asbool_N_le.
  destruct (T.chain_id tx); [rewrite asbool_N_eq|].
  all: destruct (Gas.tx_effective_gas_price _ tx); [rewrite asbool_N_le|]; reflexivity.
Qed.

Sender-code eligibility is supplied by the execution invariant, not by transactionChecks. This separates the old conjunction without adding a condition to the end-to-end consensus theorem.
Theorem transaction_checks_validate state tx sender fee :
  transactionQuote (Chain.Ethereum.Block.t.header (B.t.current_block state)) tx = Some (sender, fee) →
  transactionChecks (Chain.Ethereum.Block.t.header (B.t.current_block state)) tx
    (A.t.nonce (readAccount (B.t.world_state state) sender)) = true →
  @A.is_smart_contract (readAccount (B.t.world_state state) sender) = false →
  (Z.of_N fee ≤ Numeric.U256.to_z
    (A.t.balance (readAccount (B.t.world_state state) sender)))%Z →
  ∃ validation, @E.validate_transaction F state tx = inl validation.
Proof.
  unfold transactionQuote.
  destruct (transactionSender tx) as [recovered|] eqn:Hsender; [|discriminate].
  destruct (@Gas.tx_effective_gas_price (Chain.Ethereum.Block.Header.t.base_fee_per_gas
      (Chain.Ethereum.Block.t.header (B.t.current_block state))) tx)
    as [price|] eqn:Hprice; [|discriminate].
  intro Hquote. injection Hquote as <- <-. intros Hchecks Hcode Hbalance.
  rewrite transaction_checks_native, Hprice in Hchecks.
  repeat rewrite Bool.andb_true_iff in Hchecks.
  destruct Hchecks as [[[[[[[[Hchain Hoverflow] Hnonce] Hinit] Hintrinsic] Hcap] Hfloor] Hauth] Hbid].
  apply Bool.negb_true_iff in Hchain, Hoverflow, Hnonce, Hinit, Hintrinsic, Hcap, Hfloor, Hbid.
  assert (Hfee : Support.UintN.op_gt
    (@Support.UintN.op_star (T.gas_limit tx) price)
    (@Numeric.U256.to_uint (A.t.balance (readAccount (B.t.world_state state) recovered))) = false).
  { apply N.ltb_ge. change (@Support.UintN.op_star (T.gas_limit tx) price)
      with ((T.gas_limit tx × price)%N).
    rewrite reservecheck.u256_to_uint by (pose proof (N2Z.is_nonneg (T.gas_limit tx × price)); lia).
    apply N2Z.inj_le. rewrite Z2N.id by (pose proof (N2Z.is_nonneg (T.gas_limit tx × price)); lia).
    exact Hbalance. }
  assert (Hauthorization : @E.validate_authorizations F (B.t.current_block state) tx = inl tt).
  { unfold E.validate_authorizations; destruct tx; cbn beta iota zeta in Hauth |- *;
      try reflexivity.
    destruct s; cbn in Hauth |- ×.
    match type of Hauth with context [match ?l with [] ⇒ _ | _ :: _ ⇒ _ end] ⇒
      destruct l eqn:Hlist end; [discriminate|].
    reflexivity. }
  unfold E.validate_transaction. cbn zeta. fold (transactionSender tx).
  change (@Result.op_letdollar) with
    (fun (A Error C : Set) (first : A + Error) (next : A → C + Error) ⇒
      match first with inl value ⇒ next value | inr error ⇒ inr error end).
  change (@Result._return) with (fun (A Error : Set) (value : A) ⇒ @inl A Error value).
  change (@Result.when_) with (fun (Error : Set) (condition : bool) (failure : unit + Error) ⇒
    if condition then failure else inl tt).
  unfold A.is_smart_contract in Hcode. cbv beta iota zeta in Hcode.
  destruct (T.chain_id tx) as [id|] eqn:Hid; cbn beta iota zeta in Hchain |- ×.
  all: try rewrite Hchain.
  all: rewrite Hoverflow, Hsender.
  all: cbv beta iota zeta.
  all: destruct (T.call_or_create_value tx) as [call|[code]];
    cbv beta iota zeta delta [T.call_or_create.Create.initcode] in Hinit |- ×.
  all: rewrite Hnonce, Hinit, Hintrinsic, Hcap, Hfloor.
  all: match goal with |- context [if andb ?a ?b then _ else _] ⇒
    assert (Hcode' : andb a b = false) by exact Hcode; rewrite Hcode' end.
  all: rewrite Hauthorization, Hprice.
  all: cbv beta iota zeta.
  all: rewrite Hbid, Hfee; cbv beta iota zeta; eexists; reflexivity.
Qed.

Lemma system_transaction_checks_native tx expectedNonce :
  systemTransactionChecks tx expectedNonce =
  (match tx with
   | T.Legacy txImpl ⇒
       match txImpl.(T.legacy_tx.to_) with
       | Some to_ ⇒ bool_decide (to_ = Staking.staking_address)
       | None ⇒ false
       end
   | _ ⇒ false
   end) &&
  negb (Numeric.U64.op_eq (T.nonce tx) Numeric.U64.max_t) &&
  negb (Numeric.U64.op_ltgt expectedNonce (T.nonce tx)) &&
  negb (Gas.op_gt (@Gas.tx_intrinsic_gas tx) (Gas.of_int 2000000%Z)) &&
  negb (Gas.op_gt (@Gas.tx_floor_gas tx) (Gas.of_int 2000000%Z)).
Proof.
  unfold systemTransactionChecks.
  rewrite asbool_U64_ne, asbool_U64_eq, !asbool_N_le.
  reflexivity.
Qed.

Theorem system_transaction_checks_validate state tx :
  systemTransactionChecks tx
    (A.t.nonce (readAccount (B.t.world_state state) Chain.Monad.system_sender)) = true →
  @A.is_smart_contract (readAccount (B.t.world_state state) Chain.Monad.system_sender) = false →
  @E.validate_monad_system_transaction F state tx Chain.Monad.system_sender = inl tt.
Proof.
  intros Hchecks Hcode.
  rewrite system_transaction_checks_native in Hchecks.
  repeat rewrite Bool.andb_true_iff in Hchecks.
  destruct Hchecks as [[[[Hto Hoverflow] Hnonce] Hintrinsic] Hfloor].
  apply Bool.negb_true_iff in Hoverflow, Hnonce, Hintrinsic, Hfloor.
  destruct tx as [txImpl| | |]; [|discriminate..].
  destruct txImpl; cbn [T.legacy_tx.to_] in Hto.
  destruct to_ as [to_|]; [|discriminate].
  apply bool_decide_eq_true_1 in Hto; subst to_.
  unfold E.validate_monad_system_transaction. cbn zeta.
  change (@Result.op_letdollar) with
    (fun (A Error C : Set) (first : A + Error) (next : A → C + Error) ⇒
      match first with inl value ⇒ next value | inr error ⇒ inr error end).
  change (@Result._return) with (fun (A Error : Set) (value : A) ⇒ @inl A Error value).
  change (@Result.when_) with (fun (Error : Set) (condition : bool) (failure : unit + Error) ⇒
    if condition then failure else inl tt).
  assert (Hsender_eq :
    Chain.Ethereum.Address.op_ltgt Chain.Monad.system_sender Chain.Monad.system_sender = false)
    by reflexivity.
  assert (Hmem :
    negb (Chain.Ethereum.Address._Set.(OCamlSet.S.mem) Staking.staking_address
      (@E.Precompiles F).(Precompiles.Make.Make_result.syscall_endpoint_addresses)) = false)
    by reflexivity.
  cbn [T.legacy_tx.to_ T.chain_id T.call_or_create_value T.to_].
  rewrite Hsender_eq, Hmem.
  cbv beta iota zeta.
  change (State.op_pointcaretlparenrparen state (B.account None Chain.Monad.system_sender))
    with (readAccount (B.t.world_state state) Chain.Monad.system_sender).
  rewrite Hoverflow, Hnonce, Hintrinsic, Hfloor.
  unfold A.is_smart_contract in Hcode. cbv beta iota zeta in Hcode.
  cbv beta iota zeta.
  match goal with |- context [if andb ?a ?b then _ else _] ⇒
    assert (Hcode' : andb a b = false) by exact Hcode; rewrite Hcode' end.
  reflexivity.
Qed.
End Validation.