author | wenzelm |
Fri, 12 Jan 2018 14:43:06 +0100 | |
changeset 67407 | dbaa38bd223a |
parent 66453 | cc19f7ca2ed6 |
child 72515 | c7038c397ae3 |
permissions | -rw-r--r-- |
41561 | 1 |
(* Title: HOL/SPARK/Examples/RIPEMD-160/RMD.thy |
2 |
Author: Fabian Immler, TU Muenchen |
|
3 |
||
4 |
Verification of the RIPEMD-160 hash function |
|
5 |
*) |
|
6 |
||
7 |
theory RMD |
|
66453
cc19f7ca2ed6
session-qualified theory imports: isabelle imports -U -i -d '~~/src/Benchmarks' -a;
wenzelm
parents:
62390
diff
changeset
|
8 |
imports "HOL-Word.Word" |
41561 | 9 |
begin |
10 |
||
11 |
||
67407 | 12 |
\<comment> \<open>all operations are defined on 32-bit words\<close> |
41561 | 13 |
|
41587 | 14 |
type_synonym word32 = "32 word" |
15 |
type_synonym byte = "8 word" |
|
16 |
type_synonym perm = "nat \<Rightarrow> nat" |
|
17 |
type_synonym chain = "word32 * word32 * word32 * word32 * word32" |
|
18 |
type_synonym block = "nat \<Rightarrow> word32" |
|
19 |
type_synonym message = "nat \<Rightarrow> block" |
|
41561 | 20 |
|
67407 | 21 |
\<comment> \<open>nonlinear functions at bit level\<close> |
41561 | 22 |
|
23 |
definition f::"[nat, word32, word32, word32] => word32" |
|
24 |
where |
|
25 |
"f j x y z = |
|
26 |
(if ( 0 <= j & j <= 15) then x XOR y XOR z |
|
27 |
else if (16 <= j & j <= 31) then (x AND y) OR (NOT x AND z) |
|
28 |
else if (32 <= j & j <= 47) then (x OR NOT y) XOR z |
|
29 |
else if (48 <= j & j <= 63) then (x AND z) OR (y AND NOT z) |
|
30 |
else if (64 <= j & j <= 79) then x XOR (y OR NOT z) |
|
31 |
else 0)" |
|
32 |
||
33 |
||
67407 | 34 |
\<comment> \<open>added constants (hexadecimal)\<close> |
41561 | 35 |
|
36 |
definition K::"nat => word32" |
|
37 |
where |
|
38 |
"K j = |
|
39 |
(if ( 0 <= j & j <= 15) then 0x00000000 |
|
40 |
else if (16 <= j & j <= 31) then 0x5A827999 |
|
41 |
else if (32 <= j & j <= 47) then 0x6ED9EBA1 |
|
42 |
else if (48 <= j & j <= 63) then 0x8F1BBCDC |
|
43 |
else if (64 <= j & j <= 79) then 0xA953FD4E |
|
44 |
else 0)" |
|
45 |
||
46 |
definition K'::"nat => word32" |
|
47 |
where |
|
48 |
"K' j = |
|
49 |
(if ( 0 <= j & j <= 15) then 0x50A28BE6 |
|
50 |
else if (16 <= j & j <= 31) then 0x5C4DD124 |
|
51 |
else if (32 <= j & j <= 47) then 0x6D703EF3 |
|
52 |
else if (48 <= j & j <= 63) then 0x7A6D76E9 |
|
53 |
else if (64 <= j & j <= 79) then 0x00000000 |
|
54 |
else 0)" |
|
55 |
||
56 |
||
67407 | 57 |
\<comment> \<open>selection of message word\<close> |
41561 | 58 |
|
59 |
definition r_list :: "nat list" |
|
60 |
where "r_list = [ |
|
61 |
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, |
|
62 |
7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8, |
|
63 |
3, 10, 14, 4, 9, 15, 8, 1, 2, 7, 0, 6, 13, 11, 5, 12, |
|
64 |
1, 9, 11, 10, 0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2, |
|
65 |
4, 0, 5, 9, 7, 12, 2, 10, 14, 1, 3, 8, 11, 6, 15, 13 |
|
66 |
]" |
|
67 |
||
68 |
definition r'_list :: "nat list" |
|
69 |
where "r'_list = [ |
|
70 |
5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12, |
|
71 |
6, 11, 3, 7, 0, 13, 5, 10, 14, 15, 8, 12, 4, 9, 1, 2, |
|
72 |
15, 5, 1, 3, 7, 14, 6, 9, 11, 8, 12, 2, 10, 0, 4, 13, |
|
73 |
8, 6, 4, 1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14, |
|
74 |
12, 15, 10, 4, 1, 5, 8, 7, 6, 2, 13, 14, 0, 3, 9, 11 |
|
75 |
]" |
|
76 |
||
77 |
definition r :: perm |
|
78 |
where "r j = r_list ! j" |
|
79 |
||
80 |
definition r' :: perm |
|
81 |
where "r' j = r'_list ! j" |
|
82 |
||
83 |
||
67407 | 84 |
\<comment> \<open>amount for rotate left (rol)\<close> |
41561 | 85 |
|
86 |
definition s_list :: "nat list" |
|
87 |
where "s_list = [ |
|
88 |
11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8, |
|
89 |
7, 6, 8, 13, 11, 9, 7, 15, 7, 12, 15, 9, 11, 7, 13, 12, |
|
90 |
11, 13, 6, 7, 14, 9, 13, 15, 14, 8, 13, 6, 5, 12, 7, 5, |
|
91 |
11, 12, 14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5, 12, |
|
92 |
9, 15, 5, 11, 6, 8, 13, 12, 5, 12, 13, 14, 11, 8, 5, 6 |
|
93 |
]" |
|
94 |
||
95 |
definition s'_list :: "nat list" |
|
96 |
where "s'_list = [ |
|
97 |
8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14, 12, 6, |
|
98 |
9, 13, 15, 7, 12, 8, 9, 11, 7, 7, 12, 7, 6, 15, 13, 11, |
|
99 |
9, 7, 15, 11, 8, 6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5, |
|
100 |
15, 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5, 15, 8, |
|
101 |
8, 5, 12, 9, 12, 5, 14, 6, 8, 13, 6, 5, 15, 13, 11, 11 |
|
102 |
]" |
|
103 |
||
104 |
definition s :: perm |
|
105 |
where "s j = s_list ! j" |
|
106 |
||
107 |
definition s' :: perm |
|
108 |
where "s' j = s'_list ! j" |
|
109 |
||
110 |
||
67407 | 111 |
\<comment> \<open>Initial value (hexadecimal)\<close> |
41561 | 112 |
|
113 |
definition h0_0::word32 where "h0_0 = 0x67452301" |
|
114 |
definition h1_0::word32 where "h1_0 = 0xEFCDAB89" |
|
115 |
definition h2_0::word32 where "h2_0 = 0x98BADCFE" |
|
116 |
definition h3_0::word32 where "h3_0 = 0x10325476" |
|
117 |
definition h4_0::word32 where "h4_0 = 0xC3D2E1F0" |
|
118 |
definition h_0::chain where "h_0 = (h0_0, h1_0, h2_0, h3_0, h4_0)" |
|
119 |
||
120 |
||
121 |
definition step_l :: |
|
122 |
"[ block, |
|
123 |
chain, |
|
124 |
nat |
|
125 |
] => chain" |
|
126 |
where |
|
127 |
"step_l X c j = |
|
128 |
(let (A, B, C, D, E) = c in |
|
67407 | 129 |
(\<comment> \<open>\<open>A:\<close>\<close> E, |
130 |
\<comment> \<open>\<open>B:\<close>\<close> word_rotl (s j) (A + f j B C D + X (r j) + K j) + E, |
|
131 |
\<comment> \<open>\<open>C:\<close>\<close> B, |
|
132 |
\<comment> \<open>\<open>D:\<close>\<close> word_rotl 10 C, |
|
133 |
\<comment> \<open>\<open>E:\<close>\<close> D))" |
|
41561 | 134 |
|
135 |
definition step_r :: |
|
136 |
"[ block, |
|
137 |
chain, |
|
138 |
nat |
|
139 |
] \<Rightarrow> chain" |
|
140 |
where |
|
141 |
"step_r X c' j = |
|
142 |
(let (A', B', C', D', E') = c' in |
|
67407 | 143 |
(\<comment> \<open>\<open>A':\<close>\<close> E', |
144 |
\<comment> \<open>\<open>B':\<close>\<close> word_rotl (s' j) (A' + f (79 - j) B' C' D' + X (r' j) + K' j) + E', |
|
145 |
\<comment> \<open>\<open>C':\<close>\<close> B', |
|
146 |
\<comment> \<open>\<open>D':\<close>\<close> word_rotl 10 C', |
|
147 |
\<comment> \<open>\<open>E':\<close>\<close> D'))" |
|
41561 | 148 |
|
149 |
definition step_both :: |
|
150 |
"[ block, chain * chain, nat ] \<Rightarrow> chain * chain" |
|
151 |
where |
|
152 |
"step_both X cc j = (case cc of (c, c') \<Rightarrow> |
|
153 |
(step_l X c j, step_r X c' j))" |
|
154 |
||
155 |
definition steps::"[ block, chain * chain, nat] \<Rightarrow> chain * chain" |
|
156 |
where "steps X cc i = foldl (step_both X) cc [0..<i]" |
|
157 |
||
158 |
definition round::"[ block, chain ] \<Rightarrow> chain" |
|
159 |
where "round X h = |
|
160 |
(let (h0, h1, h2, h3, h4) = h in |
|
161 |
let ((A, B, C, D, E), (A', B', C', D', E')) = steps X (h, h) 80 in |
|
67407 | 162 |
(\<comment> \<open>\<open>h0:\<close>\<close> h1 + C + D', |
163 |
\<comment> \<open>\<open>h1:\<close>\<close> h2 + D + E', |
|
164 |
\<comment> \<open>\<open>h2:\<close>\<close> h3 + E + A', |
|
165 |
\<comment> \<open>\<open>h3:\<close>\<close> h4 + A + B', |
|
166 |
\<comment> \<open>\<open>h4:\<close>\<close> h0 + B + C'))" |
|
41561 | 167 |
|
168 |
definition rmd_body::"[ message, chain, nat ] => chain" |
|
169 |
where |
|
170 |
"rmd_body X h i = round (X i) h" |
|
171 |
||
172 |
definition rounds::"message \<Rightarrow> chain \<Rightarrow> nat \<Rightarrow> chain" |
|
173 |
where |
|
174 |
"rounds X h i = foldl (rmd_body X) h_0 [0..<i]" |
|
175 |
||
176 |
definition rmd :: "message \<Rightarrow> nat \<Rightarrow> chain" |
|
177 |
where |
|
178 |
"rmd X len = rounds X h_0 len" |
|
179 |
||
62390 | 180 |
end |