1 (**************************************************************************)
4 (* ||A|| A project by Andrea Asperti *)
6 (* ||I|| Developers: *)
7 (* ||T|| The HELM team. *)
8 (* ||A|| http://helm.cs.unibo.it *)
10 (* \ / This file is distributed under the terms of the *)
11 (* v GNU General Public License Version 2 *)
13 (**************************************************************************)
15 set "baseuri" "cic:/matita/assembly/byte".
17 include "exadecimal.ma".
19 record byte : Type ≝ {
25 λb,b'. eqex (bh b) (bh b') ∧ eqex (bl b) (bl b').
29 match plusex (bl b1) (bl b2) c with
31 match plusex (bh b1) (bh b2) c' with
32 [ couple h c'' ⇒ couple ? ? (mk_byte h l) c'' ]].
34 definition nat_of_byte ≝ λb:byte. 16*(bh b) + (bl b).
36 coercion cic:/matita/assembly/byte/nat_of_byte.con.
38 definition byte_of_nat ≝
39 λn. mk_byte (exadecimal_of_nat (n / 16)) (exadecimal_of_nat n).
41 lemma byte_of_nat_nat_of_byte: ∀b. byte_of_nat (nat_of_byte b) = b.
49 lemma lt_nat_of_byte_256: ∀b. nat_of_byte b < 256.
52 letin H ≝ (lt_nat_of_exadecimal_16 (bh b)); clearbody H;
53 letin K ≝ (lt_nat_of_exadecimal_16 (bl b)); clearbody K;
55 letin H' ≝ (le_S_S_to_le ? ? H); clearbody H'; clear H;
56 letin K' ≝ (le_S_S_to_le ? ? K); clearbody K'; clear K;
58 cut (16*bh b ≤ 16*15);
59 [ letin Hf ≝ (le_plus ? ? ? ? Hcut K'); clearbody Hf;
60 simplify in Hf:(? ? %);
66 lemma nat_of_byte_byte_of_nat: ∀n. nat_of_byte (byte_of_nat n) = n \mod 256.
68 letin H ≝ (lt_nat_of_byte_256 (byte_of_nat n)); clearbody H;
69 rewrite < (lt_to_eq_mod ? ? H); clear H;
72 change with ((16*(exadecimal_of_nat (n/16)) + exadecimal_of_nat n) \mod 256 = n \mod 256);
73 letin H ≝ (div_mod n 16 ?); clearbody H; [ autobatch | ];
74 rewrite > symmetric_times in H;
75 rewrite > nat_of_exadecimal_exadecimal_of_nat in ⊢ (? ? (? (? % ?) ?) ?);
76 rewrite > nat_of_exadecimal_exadecimal_of_nat in ⊢ (? ? (? (? ? %) ?) ?);
77 rewrite > H in ⊢ (? ? ? (? % ?)); clear H;
78 rewrite > mod_plus in ⊢ (? ? % ?);
79 rewrite > mod_plus in ⊢ (? ? ? %);
80 apply eq_mod_to_eq_plus_mod;
81 rewrite < mod_mod in ⊢ (? ? ? %); [ | autobatch];
82 rewrite < mod_mod in ⊢ (? ? % ?); [ | autobatch];
83 rewrite < (eq_mod_times_times_mod ? ? 16 256) in ⊢ (? ? (? % ?) ?); [2: reflexivity | ];
84 rewrite < mod_mod in ⊢ (? ? % ?);
90 axiom eq_nat_of_byte_n_nat_of_byte_mod_n_256:
91 ∀n. byte_of_nat n = byte_of_nat (n \mod 256).
95 match plusbyte b1 b2 c with
96 [ couple r c' ⇒ b1 + b2 + nat_of_bool c = nat_of_byte r + nat_of_bool c' * 256
100 generalize in match (plusex_ok (bl b1) (bl b2) c);
101 elim (plusex (bl b1) (bl b2) c);
103 generalize in match (plusex_ok (bh b1) (bh b2) t1);
104 elim (plusex (bh b1) (bh b2) t1);
106 change in ⊢ (? ? ? (? (? % ?) ?)) with (16 * t2);
108 letin K ≝ (eq_f ? ? (λy.16*y) ? ? H1); clearbody K; clear H1;
109 rewrite > distr_times_plus in K:(? ? ? %);
110 rewrite > symmetric_times in K:(? ? ? (? ? (? ? %)));
111 rewrite < associative_times in K:(? ? ? (? ? %));
112 normalize in K:(? ? ? (? ? (? % ?)));
113 rewrite > symmetric_times in K:(? ? ? (? ? %));
114 rewrite > sym_plus in ⊢ (? ? ? (? % ?));
115 rewrite > associative_plus in ⊢ (? ? ? %);
116 letin K' ≝ (eq_f ? ? (plus t) ? ? K); clearbody K'; clear K;
117 apply transitive_eq; [3: apply K' | skip | ];
119 rewrite > sym_plus in ⊢ (? ? (? (? ? %) ?) ?);
120 rewrite > associative_plus in ⊢ (? ? (? % ?) ?);
121 rewrite > associative_plus in ⊢ (? ? % ?);
122 rewrite > associative_plus in ⊢ (? ? (? ? %) ?);
123 rewrite > associative_plus in ⊢ (? ? (? ? (? ? %)) ?);
124 rewrite > sym_plus in ⊢ (? ? (? ? (? ? (? ? %))) ?);
125 rewrite < associative_plus in ⊢ (? ? (? ? (? ? %)) ?);
126 rewrite < associative_plus in ⊢ (? ? (? ? %) ?);
127 rewrite < associative_plus in ⊢ (? ? (? ? (? % ?)) ?);
128 rewrite > H; clear H;
129 autobatch paramodulation.
134 match eqex (bl b) x0 with
135 [ true ⇒ mk_byte (xpred (bh b)) (xpred (bl b))
136 | false ⇒ mk_byte (bh b) (xpred (bl b))
140 ∀b. plusbyte (mk_byte x0 x0) b false = couple ? ? b false.
148 definition plusbytenc ≝
150 match plusbyte x y false with
151 [couple res _ ⇒ res].
153 definition plusbytec ≝
155 match plusbyte x y false with
158 lemma plusbytenc_O_x:
159 ∀x. plusbytenc (mk_byte x0 x0) x = x.
162 rewrite > plusbyte_O_x;
166 axiom eq_nat_of_byte_mod: ∀b. nat_of_byte b = nat_of_byte b \mod 256.
168 theorem plusbytenc_ok:
169 ∀b1,b2:byte. nat_of_byte (plusbytenc b1 b2) = (b1 + b2) \mod 256.
172 generalize in match (plusbyte_ok b1 b2 false);
173 elim (plusbyte b1 b2 false);
175 change with (nat_of_byte t = (b1 + b2) \mod 256);
176 rewrite < plus_n_O in H;
177 rewrite > H; clear H;
179 letin K ≝ (eq_nat_of_byte_mod t); clearbody K;
180 letin K' ≝ (eq_mod_times_n_m_m_O (nat_of_bool t1) 256 ?); clearbody K';
182 autobatch paramodulation.
187 lemma eq_eqbyte_x0_x0_byte_of_nat_S_false:
188 ∀b. b < 255 → eqbyte (mk_byte x0 x0) (byte_of_nat (S b)) = false.
191 cut (b < 15 ∨ b ≥ 15);
194 change in ⊢ (? ? (? ? %) ?) with (eqex x0 (exadecimal_of_nat (S b)));
195 rewrite > eq_eqex_S_x0_false;
196 [ elim (eqex (bh (mk_byte x0 x0))
197 (bh (mk_byte (exadecimal_of_nat (S b/16)) (exadecimal_of_nat (S b)))));simplify;
199 alias id "andb_sym" = "cic:/matita/nat/propr_div_mod_lt_le_totient1_aux/andb_sym.con".
206 change in ⊢ (? ? (? % ?) ?) with (eqex x0 (exadecimal_of_nat (S b/16)));
207 letin K ≝ (leq_m_n_to_eq_div_n_m_S (S b) 16 ? ?);
215 rewrite > eq_eqex_S_x0_false;
220 clear H2; clear a; clear H1; clear Hcut;
221 apply (le_times_to_le 16) [ autobatch | ] ;
222 rewrite > (div_mod (S b) 16) in H;[2:autobatch|]
223 rewrite > (div_mod 255 16) in H:(? ? %);[2:autobatch|]
224 lapply (le_to_le_plus_to_le ? ? ? ? ? H);
226 apply lt_mod_m_m;autobatch
227 |rewrite > sym_times;
228 rewrite > sym_times in ⊢ (? ? %); (* just to speed up qed *)
229 normalize in \vdash (? ? %);apply Hletin;
234 | elim (or_lt_le b 15);unfold ge;autobatch
238 lemma eq_bpred_S_a_a:
239 ∀a. a < 255 → bpred (byte_of_nat (S a)) = byte_of_nat a.
243 apply (bool_elim ? (eqex (bl (byte_of_nat (S a))) x0)); intros;
244 [ change with (mk_byte (xpred (bh (byte_of_nat (S a)))) (xpred (bl (byte_of_nat (S a))))
246 rewrite > (eqex_true_to_eq ? ? H1);
247 normalize in ⊢ (? ? (? ? %) ?);
249 change with (mk_byte (xpred (exadecimal_of_nat (S a/16))) xF =
250 mk_byte (exadecimal_of_nat (a/16)) (exadecimal_of_nat a));
254 change in ⊢ (? ? match ? % ? in bool return ? with [true\rArr ?|false\rArr ?] ?);
262 cut (a \mod 16 = 15 ∨ a \mod 16 < 15);
272 ∀x:byte.∀n.plusbytenc (byte_of_nat (x*n)) x = byte_of_nat (x * S n).
274 rewrite < byte_of_nat_nat_of_byte;
275 rewrite > (plusbytenc_ok (byte_of_nat (x*n)) x);
276 rewrite < times_n_Sm;
277 rewrite > nat_of_byte_byte_of_nat in ⊢ (? ? (? (? (? % ?) ?)) ?);
278 rewrite > eq_nat_of_byte_n_nat_of_byte_mod_n_256 in ⊢ (? ? ? %);
279 rewrite > mod_plus in ⊢ (? ? (? %) ?);
280 rewrite > mod_plus in ⊢ (? ? ? (? %));
281 rewrite < mod_mod in ⊢ (? ? (? (? (? % ?) ?)) ?); [2: autobatch | ];
282 rewrite > sym_plus in ⊢ (? ? (? (? % ?)) ?);
286 lemma eq_plusbytec_x0_x0_x_false:
287 ∀x.plusbytec (mk_byte x0 x0) x = false.