Library QedkConcrete.reservehash
The precise hash collision behind overlapping reserve records
From Stdlib Require Import String ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import QedkConcrete.reserveencoding QedkConcrete.hashwords
QedkConcrete.wordrange QedkConcrete.bytewords QedkConcrete.bytecodec.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Section Hash.
Context {UR : Basics.Unreachable Numeric.U256.Repr.t} {UF : Basics.Unreachable Numeric.U256.t}
{UU : Basics.Unreachable unit} {US : Basics.Unreachable string}
{UI : Basics.Unreachable int} {UC : Basics.Unreachable ascii}
{UP : Basics.Unreachable (int × int)}
{U32 : Basics.Unreachable Byte_string.B32.(Byte_string.Fixed.Fixed_result.t)}.
Local Abbreviation base := (@Reserve_balance.config_slot).
Local Abbreviation key := (fun addr offset ⇒ @Numeric.U256.to_repr
(@Numeric.U256.op_plus (base addr) (@Numeric.U256.of_int offset))).
Lemma config_base_aligned addr : base addr mod 128 = 0.
Proof.
unfold Reserve_balance.config_slot.
match goal with |- context [@Numeric.U256.logand ?hash _] ⇒ set (digest := hash) end.
change (Z.land (Z.shiftl (Z.land digest (2^256-128)) 0) (Z.ones 256) mod 2^7 = 0).
rewrite Z.shiftl_0_r, <- Z.land_ones by lia.
rewrite <- !Z.land_assoc.
change (Z.land digest 0 = 0). apply Z.land_0_r.
Qed.
Lemma config_slot_collision_iff a b i j :
0 ≤ i < 128 → 0 ≤ j < 128 →
(key a i = key b j ↔ base a = base b ∧ i = j).
Proof.
intros Hi Hj. split.
- intro Heq. apply (@word_encoding_injective) in Heq;
[|apply word_add|apply word_add].
rewrite !config_key_word in Heq by assumption.
pose proof (f_equal (fun n ⇒ n mod 128) Heq) as Hmod.
cbn beta in Hmod.
rewrite (Z.add_mod (base a) i 128), (Z.add_mod (base b) j 128),
!config_base_aligned, !Z.add_0_l,
!Z.mod_mod, !Z.mod_small in Hmod by lia.
split; lia.
- intros [Hbase ->]. rewrite Hbase. reflexivity.
Qed.
Definition configHashInput (addr : Chain.Ethereum.Address.t) : string :=
String.append (@Byte_string.B20.to_bytes32 addr)
(@Byte_string.Fixed.Fixed_result.zeros _ _ _ Byte_string.B32).
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import QedkConcrete.reserveencoding QedkConcrete.hashwords
QedkConcrete.wordrange QedkConcrete.bytewords QedkConcrete.bytecodec.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Section Hash.
Context {UR : Basics.Unreachable Numeric.U256.Repr.t} {UF : Basics.Unreachable Numeric.U256.t}
{UU : Basics.Unreachable unit} {US : Basics.Unreachable string}
{UI : Basics.Unreachable int} {UC : Basics.Unreachable ascii}
{UP : Basics.Unreachable (int × int)}
{U32 : Basics.Unreachable Byte_string.B32.(Byte_string.Fixed.Fixed_result.t)}.
Local Abbreviation base := (@Reserve_balance.config_slot).
Local Abbreviation key := (fun addr offset ⇒ @Numeric.U256.to_repr
(@Numeric.U256.op_plus (base addr) (@Numeric.U256.of_int offset))).
Lemma config_base_aligned addr : base addr mod 128 = 0.
Proof.
unfold Reserve_balance.config_slot.
match goal with |- context [@Numeric.U256.logand ?hash _] ⇒ set (digest := hash) end.
change (Z.land (Z.shiftl (Z.land digest (2^256-128)) 0) (Z.ones 256) mod 2^7 = 0).
rewrite Z.shiftl_0_r, <- Z.land_ones by lia.
rewrite <- !Z.land_assoc.
change (Z.land digest 0 = 0). apply Z.land_0_r.
Qed.
Lemma config_slot_collision_iff a b i j :
0 ≤ i < 128 → 0 ≤ j < 128 →
(key a i = key b j ↔ base a = base b ∧ i = j).
Proof.
intros Hi Hj. split.
- intro Heq. apply (@word_encoding_injective) in Heq;
[|apply word_add|apply word_add].
rewrite !config_key_word in Heq by assumption.
pose proof (f_equal (fun n ⇒ n mod 128) Heq) as Hmod.
cbn beta in Hmod.
rewrite (Z.add_mod (base a) i 128), (Z.add_mod (base b) j 128),
!config_base_aligned, !Z.add_0_l,
!Z.mod_mod, !Z.mod_small in Hmod by lia.
split; lia.
- intros [Hbase ->]. rewrite Hbase. reflexivity.
Qed.
Definition configHashInput (addr : Chain.Ethereum.Address.t) : string :=
String.append (@Byte_string.B20.to_bytes32 addr)
(@Byte_string.Fixed.Fixed_result.zeros _ _ _ Byte_string.B32).
The 249 retained digest bits, represented in their actual slot positions.
Equality here does not require equality of the discarded seven bits.
Definition configHashPage (bytes : string) : Z :=
let digest := Digestif.KECCAK_256.(Digestif.S.to_raw_string)
(Digestif.KECCAK_256.(Digestif.S.digest_string) None None bytes) in
Z.land (OCamlZ.of_bits (@Byte_string.Bytes.reverse digest)) (2^256-128).
Lemma config_base_hash addr : keccak256OutputSize →
base addr = configHashPage (configHashInput addr).
Proof.
intro Hsize.
change (Z.land (Z.shiftl (Z.land
(@Numeric.U256.of_repr (@Crypto.keccak_256 (configHashInput addr)))
(2^256-128)) 0) (Z.ones 256) = configHashPage (configHashInput addr)).
unfold Crypto.keccak_256.
rewrite b32_checked_identity by apply Hsize.
change (Z.land (Z.shiftl (Z.land
(@Numeric.U256.of_z_exn (OCamlZ.of_bits (@Byte_string.Bytes.reverse
(Digestif.KECCAK_256.(Digestif.S.to_raw_string)
(Digestif.KECCAK_256.(Digestif.S.digest_string) None None (configHashInput addr))))))
(2^256-128)) 0) (Z.ones 256) = configHashPage (configHashInput addr)).
rewrite word_of_z by (apply bits_word; rewrite reverse_length, Hsize; lia).
rewrite Z.shiftl_0_r, <- Z.land_assoc.
unfold configHashPage. reflexivity.
Qed.
Lemma padded_address_get addr index : 0 ≤ index < 20 →
@OCamlString.get UC (@Byte_string.B20.to_bytes32 addr) (index + 12) =
Byte_string.B20.op_pointdollarlparenrparen addr index.
Proof.
intro Hindex.
change (@OCamlString.get UC (@OCamlString.init US 32 (fun i ⇒
if Z.ltb i (32-20) then "000"%char
else Byte_string.B20.op_pointdollarlparenrparen addr (i-32+20))) (index+12) =
Byte_string.B20.op_pointdollarlparenrparen addr index).
rewrite init_get by lia.
assert (Htest : Z.ltb (index+12) (32-20) = false) by (apply Z.ltb_ge; lia).
rewrite Htest. replace (index+12-32+20) with index by lia. reflexivity.
Qed.
Lemma append_equal_prefix left right suffix :
Strings.String.length left = Strings.String.length right →
String.append left suffix = String.append right suffix → left = right.
Proof.
revert right. induction left as [|a left IH]; intros [|b right] Hlen Heq;
cbn [Strings.String.length String.append] in *; try discriminate; [reflexivity|].
injection Heq as Hab Heq. subst b. f_equal. apply IH; [lia|exact Heq].
Qed.
Lemma config_hash_input_injective a b :
OCamlString.length a = 20 → OCamlString.length b = 20 →
configHashInput a = configHashInput b → a = b.
Proof.
intros Ha Hb Heq. unfold configHashInput in Heq.
apply append_equal_prefix in Heq.
- apply (@get_ext (Opaque.legacy_witness
(@Byte_string.Fixed._rocq_fallback_op_pointdollarlparenrparen_0 Byte_string.B20.Fixed_include_fargs))); [lia|]. intros index Hindex.
pose proof (f_equal (fun bytes ⇒ @OCamlString.get UC bytes (index+12)) Heq) as Hget.
cbn beta in Hget. rewrite !padded_address_get in Hget by lia. exact Hget.
- apply Nat2Z.inj. change (OCamlString.length (@Byte_string.B20.to_bytes32 a) =
OCamlString.length (@Byte_string.B20.to_bytes32 b)).
transitivity 32; [apply init_length; lia|symmetry; apply init_length; lia].
Qed.
Lemma config_slots_disjoint_iff a b :
@configSlotsDisjoint a b ↔ base a ≠ base b.
Proof.
split.
- intros Hdisjoint Heq. apply (Hdisjoint 0 0 ltac:(lia) ltac:(lia)).
apply config_slot_collision_iff; [lia|lia|]. split; [exact Heq|reflexivity].
- intros Hbase i j Hi Hj Heq. apply Hbase.
apply config_slot_collision_iff in Heq; [exact (proj1 Heq)|lia|lia].
Qed.
Lemma config_slot_collision_masked_keccak a b i j :
keccak256OutputSize →
OCamlString.length a = 20 → OCamlString.length b = 20 → a ≠ b →
0 ≤ i < 3 → 0 ≤ j < 3 → key a i = key b j →
configHashInput a ≠ configHashInput b ∧
configHashPage (configHashInput a) = configHashPage (configHashInput b) ∧ i = j.
Proof.
intros Hsize Ha Hb Hab Hi Hj Heq.
apply config_slot_collision_iff in Heq; [|lia|lia].
destruct Heq as [Hbase Hij]. rewrite !config_base_hash in Hbase by exact Hsize.
split; [|split; assumption]. intro Hinput.
apply Hab. apply config_hash_input_injective; assumption.
Qed.
End Hash.
Print Assumptions config_slot_collision_masked_keccak.
let digest := Digestif.KECCAK_256.(Digestif.S.to_raw_string)
(Digestif.KECCAK_256.(Digestif.S.digest_string) None None bytes) in
Z.land (OCamlZ.of_bits (@Byte_string.Bytes.reverse digest)) (2^256-128).
Lemma config_base_hash addr : keccak256OutputSize →
base addr = configHashPage (configHashInput addr).
Proof.
intro Hsize.
change (Z.land (Z.shiftl (Z.land
(@Numeric.U256.of_repr (@Crypto.keccak_256 (configHashInput addr)))
(2^256-128)) 0) (Z.ones 256) = configHashPage (configHashInput addr)).
unfold Crypto.keccak_256.
rewrite b32_checked_identity by apply Hsize.
change (Z.land (Z.shiftl (Z.land
(@Numeric.U256.of_z_exn (OCamlZ.of_bits (@Byte_string.Bytes.reverse
(Digestif.KECCAK_256.(Digestif.S.to_raw_string)
(Digestif.KECCAK_256.(Digestif.S.digest_string) None None (configHashInput addr))))))
(2^256-128)) 0) (Z.ones 256) = configHashPage (configHashInput addr)).
rewrite word_of_z by (apply bits_word; rewrite reverse_length, Hsize; lia).
rewrite Z.shiftl_0_r, <- Z.land_assoc.
unfold configHashPage. reflexivity.
Qed.
Lemma padded_address_get addr index : 0 ≤ index < 20 →
@OCamlString.get UC (@Byte_string.B20.to_bytes32 addr) (index + 12) =
Byte_string.B20.op_pointdollarlparenrparen addr index.
Proof.
intro Hindex.
change (@OCamlString.get UC (@OCamlString.init US 32 (fun i ⇒
if Z.ltb i (32-20) then "000"%char
else Byte_string.B20.op_pointdollarlparenrparen addr (i-32+20))) (index+12) =
Byte_string.B20.op_pointdollarlparenrparen addr index).
rewrite init_get by lia.
assert (Htest : Z.ltb (index+12) (32-20) = false) by (apply Z.ltb_ge; lia).
rewrite Htest. replace (index+12-32+20) with index by lia. reflexivity.
Qed.
Lemma append_equal_prefix left right suffix :
Strings.String.length left = Strings.String.length right →
String.append left suffix = String.append right suffix → left = right.
Proof.
revert right. induction left as [|a left IH]; intros [|b right] Hlen Heq;
cbn [Strings.String.length String.append] in *; try discriminate; [reflexivity|].
injection Heq as Hab Heq. subst b. f_equal. apply IH; [lia|exact Heq].
Qed.
Lemma config_hash_input_injective a b :
OCamlString.length a = 20 → OCamlString.length b = 20 →
configHashInput a = configHashInput b → a = b.
Proof.
intros Ha Hb Heq. unfold configHashInput in Heq.
apply append_equal_prefix in Heq.
- apply (@get_ext (Opaque.legacy_witness
(@Byte_string.Fixed._rocq_fallback_op_pointdollarlparenrparen_0 Byte_string.B20.Fixed_include_fargs))); [lia|]. intros index Hindex.
pose proof (f_equal (fun bytes ⇒ @OCamlString.get UC bytes (index+12)) Heq) as Hget.
cbn beta in Hget. rewrite !padded_address_get in Hget by lia. exact Hget.
- apply Nat2Z.inj. change (OCamlString.length (@Byte_string.B20.to_bytes32 a) =
OCamlString.length (@Byte_string.B20.to_bytes32 b)).
transitivity 32; [apply init_length; lia|symmetry; apply init_length; lia].
Qed.
Lemma config_slots_disjoint_iff a b :
@configSlotsDisjoint a b ↔ base a ≠ base b.
Proof.
split.
- intros Hdisjoint Heq. apply (Hdisjoint 0 0 ltac:(lia) ltac:(lia)).
apply config_slot_collision_iff; [lia|lia|]. split; [exact Heq|reflexivity].
- intros Hbase i j Hi Hj Heq. apply Hbase.
apply config_slot_collision_iff in Heq; [exact (proj1 Heq)|lia|lia].
Qed.
Lemma config_slot_collision_masked_keccak a b i j :
keccak256OutputSize →
OCamlString.length a = 20 → OCamlString.length b = 20 → a ≠ b →
0 ≤ i < 3 → 0 ≤ j < 3 → key a i = key b j →
configHashInput a ≠ configHashInput b ∧
configHashPage (configHashInput a) = configHashPage (configHashInput b) ∧ i = j.
Proof.
intros Hsize Ha Hb Hab Hi Hj Heq.
apply config_slot_collision_iff in Heq; [|lia|lia].
destruct Heq as [Hbase Hij]. rewrite !config_base_hash in Hbase by exact Hsize.
split; [|split; assumption]. intro Hinput.
apply Hab. apply config_hash_input_injective; assumption.
Qed.
End Hash.
Print Assumptions config_slot_collision_masked_keccak.