Debian Codegraph @

src/Bedrock/Field/Synthesis/Generic/Bignum.v

raw
1 From Coq Require Import ZArith.
2 From Coq Require Import List.
3 From Coq Require Import Lia.
4 Require Import coqutil.Datatypes.List.
5 Require Import bedrock2.Array.
6 Require Import bedrock2.ArrayCasts.
7 Require Import bedrock2.Scalars.
8 Require Import bedrock2.Map.Separation.
9 Require Import bedrock2.Map.SeparationLogic.
10 Require Import coqutil.Word.Interface.
11 Require Import coqutil.Word.Properties.
12 Require Import coqutil.Map.Interface.
13 Require Import Crypto.COperationSpecifications.
14 Require Import Crypto.Bedrock.Field.Common.Tactics.
15 Require Import Crypto.Bedrock.Field.Common.Types.
16 Require Import Crypto.Bedrock.Field.Common.Util.
17 Require Import Crypto.Bedrock.Field.Common.Arrays.MaxBounds.
18 Require Import Crypto.Bedrock.Field.Common.Arrays.ByteBounds.
19 Local Open Scope Z_scope.
20
21 Section Bignum.
22 Import bedrock2.Memory.
23 Context {width} {word : Interface.word width} {mem : map.map word Init.Byte.byte}.
24
25 Definition Bignum (n : nat) (px : word) (x : list word) : mem -> Prop :=
26 sep (emp (length x = n)) (array scalar (word.of_Z (bytes_per_word width)) px x).
27
28 Definition EncodedBignum
29 (n_bytes : nat) (px : word) (x : list Byte.byte)
30 : mem -> Prop :=
31 sep (emp (length x = n_bytes)) (array ptsto (word.of_Z 1) px x).
32
33 Section Proofs.
34 Context {word_ok : @word.ok width word} {map_ok : @map.ok word Init.Byte.byte mem}
35 {BW : coqutil.Word.Bitwidth.Bitwidth width}.
36
37 Lemma Bignum_of_bytes n :
38 forall addr bs,
39 length bs = (n * Z.to_nat (bytes_per_word width))%nat ->
40 Lift1Prop.iff1
41 (array ptsto (word.of_Z 1) addr bs)
42 (Bignum n addr (bs2ws (Z.to_nat (bytes_per_word width)) bs)).
43 Proof.
44 pose Types.word_size_in_bytes_pos.
45
46 cbv [Bignum].
47 intros. rewrite words_of_bytes.
48 2: { rewrite H. apply Nat.Div0.mod_mul. }
49 unfold bytes_per in *.
50 replace (Z.of_nat (BinIntDef.Z.to_nat (bytes_per_word width))) with (bytes_per_word width) in *; try lia.
51 change BinIntDef.Z.to_nat with Z.to_nat in *.
52 split; intros.
53 {
54 extract_ex1_and_emp_in_goal. split.
55 - assumption.
56 - rewrite bs2ws_length; try lia.
57 + rewrite H. apply Nat.div_mul. lia.
58 + rewrite H. apply Nat.Div0.mod_mul.
59 }
60 {
61 extract_ex1_and_emp_in H0.
62 assumption.
63 }
64 Qed.
65
66 Lemma Bignum_to_bytes n :
67 forall addr x,
68 Lift1Prop.iff1
69 (Bignum n addr x)
70 (sep (emp (length (ws2bs (Z.to_nat (bytes_per_word width)) x) = (n * Z.to_nat (bytes_per_word width))%nat))
71 (array ptsto (word.of_Z 1) addr (ws2bs (Z.to_nat (bytes_per_word width)) x))).
72 Proof.
73 intros.
74 cbv [Bignum]. rewrite <- bytes_of_words.
75
76 assert (length (ws2bs (Z.to_nat (bytes_per_word width)) x) = ((length x) * Z.to_nat (bytes_per_word width))%nat).
77 { rewrite ws2bs_length. lia. }
78
79 pose Types.word_size_in_bytes_pos.
80 unfold bytes_per in *.
81 replace (Z.of_nat (BinIntDef.Z.to_nat (bytes_per_word width))) with (bytes_per_word width) in *; try lia.
82 change BinIntDef.Z.to_nat with Z.to_nat in *.
83 split.
84 { intros. extract_ex1_and_emp_in_hyps. extract_ex1_and_emp_in_goal. split.
85 {assumption. }
86 {lia. }
87 }
88 { intros. extract_ex1_and_emp_in_goal. extract_ex1_and_emp_in_hyps. split.
89 {assumption. }
90 { nia. }
91 }
92 Qed.
93 End Proofs.
94 End Bignum.
95 Notation BignumSuchThat :=
96 (fun n addr ws P =>
97 let xs := map word.unsigned ws in
98 sep (emp (P xs)) (Bignum n addr ws)).
99 Notation EncodedBignumSuchThat :=
100 (fun n addr ws P =>
101 let xs := map Byte.byte.unsigned ws in
102 sep (emp (P xs)) (EncodedBignum n addr ws)).