libcoqtemplate.v 84 KB
Newer Older
Georgio Nicolas's avatar
Georgio Nicolas committed
1
2
3
Require Import Coq.Lists.List Coq.Bool.Bool Coq.Arith.PeanoNat
        Lia Coq.Strings.String.
        Import ListNotations.
Nadim Kobeissi's avatar
Coq  
Nadim Kobeissi committed
4

Georgio Nicolas's avatar
Georgio Nicolas committed
5
6
Inductive generator :=
| G.
Nadim Kobeissi's avatar
Coq  
Nadim Kobeissi committed
7

Georgio Nicolas's avatar
Georgio Nicolas committed
8
9
10
Inductive guard_status: Type :=
| guarded
| unguarded.
Nadim Kobeissi's avatar
Coq  
Nadim Kobeissi committed
11

Georgio Nicolas's avatar
Georgio Nicolas committed
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
Inductive constant : Type :=
| Nil
| cnstn : string -> constant
with primitive : Type :=
| ENC : value -> value -> primitive
| DEC : value -> value -> primitive
| AEAD_ENC : value -> value -> value -> primitive
| AEAD_DEC : value -> value -> value -> primitive
| PKE_ENC : value -> value -> primitive
| PKE_DEC : value -> value -> primitive
| HASH1 : value -> primitive
| HASH2 : value -> value -> primitive
| HASH3 : value -> value -> value -> primitive
| HASH4 : value -> value -> value -> value -> primitive
| HASH5 : value -> value -> value -> value -> value -> primitive
| MAC : value -> value -> primitive
| HKDF1: value -> value -> value -> primitive
| HKDF2: value -> value -> value -> primitive
| HKDF3: value -> value -> value -> primitive
| HKDF4: value -> value -> value -> primitive
| HKDF5: value -> value -> value -> primitive
| PW_HASH1 : value -> primitive
| PW_HASH2 : value -> value -> primitive
| PW_HASH3 : value -> value -> value -> primitive
| PW_HASH4 : value -> value -> value -> value -> primitive
| PW_HASH5 : value -> value -> value -> value -> value -> primitive
| SIGN: value -> value -> primitive
| SIGNVERIF: value -> value -> value -> primitive
| RINGSIGN: value -> value -> value -> value -> primitive
| RINGSIGNVERIF: value -> value -> value -> value -> value -> primitive
| BLIND: value -> value -> primitive
| UNBLIND: value -> value -> value -> primitive
| SHAMIR_SPLIT1: value -> primitive
| SHAMIR_SPLIT2: value -> primitive
| SHAMIR_SPLIT3: value -> primitive
| SHAMIR_JOIN: value -> value -> primitive
| CONCAT2 : value -> value -> primitive
| CONCAT3 : value -> value -> value -> primitive
| CONCAT4 : value -> value -> value -> value -> primitive
| CONCAT5 : value -> value -> value -> value -> value -> primitive
| SPLIT1: value -> primitive
| SPLIT2: value -> primitive
| SPLIT3: value -> primitive
| SPLIT4: value -> primitive
| SPLIT5: value -> primitive
  with equation : Type :=
| PUBKEY : generator -> value -> equation
| DH : generator -> value -> value -> equation
  with value : Type :=
| pass : constant -> value
| const : constant -> value 
| eq : equation -> value
| prim : primitive -> value
| default.
Nadim Kobeissi's avatar
Coq  
Nadim Kobeissi committed
66

Georgio Nicolas's avatar
Georgio Nicolas committed
67
68
69
Inductive qualifier : Type :=
| public
| private.
Nadim Kobeissi's avatar
Coq  
Nadim Kobeissi committed
70

Georgio Nicolas's avatar
Georgio Nicolas committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
Inductive declaration : Type :=
| assignment
| generation
| knowledge.

Inductive leak_status : Type :=
| leaked
| unleaked.

Inductive expression : Type :=
| EXP: declaration -> qualifier -> value -> leak_status -> expression.

(* if qualifier = public -> can learn*)
(* if leak_status = leaked -> can learn*)
(* if sent over the wire -> can learn*)
(* when learning, need to check if attacker already knows equivalent value,
    but maybe this isn't necessary *)

(* 
Variable enc_dec :
  forall (k m : value), DEC k (ENC k m) = m. *)

(* It has no axioms attached to Hash *)

(* 

Variable pke_enc_correcntess : 
   PKE_DEC k (PKE_ENC((PUBKEY G k), m)) = m *)

(* Variable public : value -> Prop.
Variable encryption_constructor : value -> value -> value.
Variable hash_constructor : value -> value. *)

Inductive knows : value -> Type :=
(*equations*)
| public_key_generation (a ga: value) : knows a -> ga = (eq(PUBKEY G a)) -> knows ga
| dh_agreement_primitive (ga b a gab: value) (p: primitive): knows ga -> ga = 
  (eq(PUBKEY G a)) -> knows b -> b = prim p -> gab = (eq(DH G a b)) -> knows gab
| dh_agreement_constant (ga b a gab: value) (c: constant): knows ga -> ga = 
  eq(PUBKEY G a) -> knows b -> b = const c -> gab = (eq(DH G a b)) -> knows gab
(*core primitives*)    
      (* todo: assert*)
| concat2_constructor (a b out: value) : knows a -> knows b -> out = prim(CONCAT2 a b)
  -> knows out
| concat2_corerule_one (val a b : value) : knows val -> val = prim (CONCAT2 a b) -> knows a
| concat2_corerule_two (val a b : value) : knows val -> val = prim (CONCAT2 a b) -> knows b

| concat3_constructor (a b c out: value) : knows a -> knows b -> knows c
  -> out = prim(CONCAT3 a b c) -> knows out
| concat3_corerule_one (val a b c : value) : knows val -> val = prim (CONCAT3 a b c) -> knows a
| concat3_corerule_two (val a b c : value) : knows val -> val = prim (CONCAT3 a b c) -> knows b
| concat3_corerule_three (val a b c : value) : knows val -> val = prim (CONCAT3 a b c) -> knows c

| concat4_constructor (a b c d out: value) : knows a -> knows b -> knows c -> knows d
  -> out = prim(CONCAT4 a b c d) -> knows out
| concat4_corerule_one (val a b c d: value) : knows val -> val = prim (CONCAT4 a b c d) -> knows a
| concat4_corerule_two (val a b c d: value) : knows val -> val = prim (CONCAT4 a b c d) -> knows b
| concat4_corerule_three (val a b c d: value) : knows val -> val = prim (CONCAT4 a b c d) -> knows c
| concat4_corerule_four (val a b c d: value) : knows val -> val = prim (CONCAT4 a b c d) -> knows d

| concat5_constructor (a b c d e out: value) : knows a -> knows b -> knows c -> knows d -> knows e
  -> out = prim(CONCAT5 a b c d e) -> knows out
| concat5_corerule_one (val a b c d e: value) : knows val -> val = prim (CONCAT5 a b c d e) -> knows a
| concat5_corerule_two (val a b c d e: value) : knows val -> val = prim (CONCAT5 a b c d e) -> knows b
| concat5_corerule_three (val a b c d e: value) : knows val -> val = prim (CONCAT5 a b c d e) -> knows c
| concat5_corerule_four (val a b c d e: value) : knows val -> val = prim (CONCAT5 a b c d e) -> knows d
| concat5_corerule_fives (val a b c d e: value) : knows val -> val = prim (CONCAT5 a b c d e) -> knows e

(*hashing primitives*)
| hash1_constructor (a out: value) : knows a -> out = prim(HASH1 a) -> knows out
| hash2_constructor (a b out: value) : knows a -> knows b -> out = prim(HASH2 a b) -> knows out
| hash3_constructor (a b c out: value) : knows a -> knows b -> knows c -> out = prim(HASH3 a b c) -> knows out
| hash4_constructor (a b c d out: value) : knows a -> knows b -> knows c -> knows d -> out = prim(HASH4 a b c d) -> knows out
| hash5_constructor (a b c d e out: value) : knows a -> knows b -> knows c -> knows d -> knows e -> out = prim(HASH5 a b c d e) -> knows out
| mac_constructor (a b out: value) : knows a -> knows b -> out = prim(MAC a b) -> knows out
| hkdf1_constructor (salt ikm info out: value) : knows salt -> knows ikm -> knows info
  -> out = prim(HKDF1 salt ikm info) -> knows out
| hkdf2_constructor (salt ikm info out: value) : knows salt -> knows ikm -> knows info
  -> out = prim(HKDF2 salt ikm info) -> knows out
| hkdf3_constructor (salt ikm info out: value) : knows salt -> knows ikm -> knows info
  -> out = prim(HKDF3 salt ikm info) -> knows out
| hkdf4_constructor (salt ikm info out: value) : knows salt -> knows ikm -> knows info
  -> out = prim(HKDF4 salt ikm info) -> knows out
| hkdf5_constructor (salt ikm info out: value) : knows salt -> knows ikm -> knows info
  -> out = prim(HKDF5 salt ikm info) -> knows out
  |pw_hash1_constructor (a out: value) : knows a -> out = prim(PW_HASH1 a) -> knows out
  |pw_hash2_constructor (a b out: value) : knows a -> knows b -> out = prim(PW_HASH2 a b) -> knows out
  |pw_hash3_constructor (a b c out: value) : knows a -> knows b -> knows c -> out = prim(PW_HASH3 a b c) -> knows out
  |pw_hash4_constructor (a b c d out: value) : knows a -> knows b -> knows c -> knows d -> out = prim(PW_HASH4 a b c d) -> knows out
  |pw_hash5_constructor (a b c d e out: value) : knows a -> knows b -> knows c -> knows d -> knows e -> out = prim(PW_HASH5 a b c d e) -> knows out

(*encryption primitives*)
    (*symmetric encryption*)
| enc_constructor (k m out: value) : knows k -> knows m -> out = prim(ENC k m) -> knows out
| enc_decomposerule (k c m : value) : knows k -> knows c -> c = prim(ENC k m) -> knows m
| dec_constructor (k c m out: value) : knows k -> knows c -> c = prim(ENC k m)
  -> out = prim(DEC k c) -> knows out
| dec_rewriterule (c k m: value) : knows c -> c = prim(DEC k (prim(ENC k m))) -> knows m
    (*authenticated encryption with additional data*)
| aead_enc_constructor (k m ad out: value) : knows k -> knows m -> knows ad
   -> out = prim(AEAD_ENC k m ad) -> knows out
| aead_enc_decomposerule (k c ad m: value): knows k -> knows c -> c = prim(AEAD_ENC k m ad)
  -> knows ad -> knows m
    (*public key encryption*)
| pke_enc_constructor (gk m k out: value): knows gk -> gk = eq(PUBKEY G k) -> knows m
  -> out = prim(PKE_ENC gk m) -> knows out
| pke_enc_decomposerule (k c gk m: value): knows k -> knows gk -> gk = eq(PUBKEY G k)
  -> knows c -> c = prim(PKE_ENC gk m) -> knows m
| pke_dec_constructor(k c gk m out: value): knows k -> knows c -> c = prim(PKE_ENC gk m)
  -> out = prim(PKE_DEC k (prim(PKE_ENC gk m))) -> knows out
| pke_dec_rewriterule(c k m: value): knows c -> c = prim(PKE_DEC k (prim(PKE_ENC (eq(PUBKEY G k))m)))
  -> knows m

(*signature primitives*)
    (*classical digital signatures*)
| sign_constructor(k m out: value): knows k -> knows m -> out = prim(SIGN k m) -> knows out
| signverif_constructor(gk m s k out: value): knows gk -> gk = eq(PUBKEY G k) -> knows m -> knows s -> 
  s = prim(SIGN k m) -> out = prim(SIGNVERIF gk m s) -> knows out
| signverif_rewriterule(val k m s: value): knows val -> val = prim(SIGNVERIF (eq(PUBKEY G k)) m (prim(SIGN k m))) -> knows m

    (*ring signatures*)
| ringsign_constructor(ka gkb gkc m kb kc out: value): knows ka -> knows gkb -> gkb = eq(PUBKEY G kb) ->
    knows gkc -> gkc = eq(PUBKEY G kc) -> knows m -> out = prim(RINGSIGN ka gkb gkc m) -> knows out

      (*unsure about ringsignverif*)
      (*todo: ringsignverif_constructor*)
| ringsignverif_constructor(gka gkb gkc m s out ka kb kc: value): knows gka -> gka = eq(PUBKEY G ka)
  -> knows gkb -> gkb = eq(PUBKEY G kb) -> knows gkc -> gkc = eq(PUBKEY G kc) -> knows m
    -> knows s -> s = prim(RINGSIGN ka gkb gkc m) -> out = prim(RINGSIGNVERIF gka gkb gkc m s) -> knows out
| ringsignverif_rewriterule(gka gkb gkc m s ka kb kc: value): knows gka -> gka = eq(PUBKEY G ka) -> knows gkb -> gkb = eq(PUBKEY G kb)
  -> knows gkc -> gkc = eq(PUBKEY G kc) -> knows m -> knows s -> s = prim(RINGSIGN ka gkb gkc m) -> knows m

    (*blind signatures*)
| blind_constructor(k m out: value): knows k -> knows m -> out = prim(BLIND k m) -> knows out
| blind_decomposerule(k b m: value): knows k -> knows b -> b = prim(BLIND k m) -> knows m
| unblind_constructor(k m s a out: value): knows k -> knows m -> knows s -> 
  s = prim(SIGN a (prim(BLIND k m))) -> out = prim(UNBLIND k m s) -> knows out
| unblind_rewriterule(u k m a out: value): knows u ->
  u = prim(UNBLIND k m (prim(SIGN a (prim(BLIND k m))))) -> out = prim(SIGN a m) -> knows out

(*secret sharing*)
    (*unsure about shamir_split/shamir_join*)
| shamir_split1_constructor(k out: value): knows k -> out = prim(SHAMIR_SPLIT1 k) -> knows out
| shamir_split2_constructor(k out: value): knows k -> out = prim(SHAMIR_SPLIT2 k) -> knows out
| shamir_split3_constructor(k out: value): knows k -> out = prim(SHAMIR_SPLIT3 k) -> knows out
  (*todo: shamir_split_recomposerule*)
  (*todo: shamir_join_constructor*)
  (*todo: shamir_join_rebuildrule*).

Lemma concat2_rule : forall (a b val : value),
    knows val -> val = prim (CONCAT2 a b) ->
    (knows a * knows b)%type.
Proof using.
  intros a b val Hk Hv.
  pose proof (concat2_corerule_one _ _ _ Hk Hv).
  pose proof (concat2_corerule_two _ _ _ Hk Hv).
Nadim Kobeissi's avatar
Coq  
Nadim Kobeissi committed
227
228
229
  auto.
Qed.

Georgio Nicolas's avatar
Georgio Nicolas committed
230
231
232
233
Lemma concat2_cons_rule : forall  (a b val : value), knows a -> knows b -> val = prim (CONCAT2 a b) ->
                                               knows val.
Proof using.
  intros.  apply concat2_constructor with (a := a) (b := b); assumption.
Nadim Kobeissi's avatar
Coq  
Nadim Kobeissi committed
234
235
Qed.

Georgio Nicolas's avatar
Georgio Nicolas committed
236
237
238
239
240
241
Theorem insecure : forall (a c m k : value), knows c -> knows a -> k = prim (HASH1 a) ->
                                        c = prim (ENC k m) -> knows m.
Proof using.
  intros.
  pose proof (hash1_constructor _ _ H0 H1).
  apply enc_decomposerule with (k := k) (c := c); assumption.
Nadim Kobeissi's avatar
Coq  
Nadim Kobeissi committed
242
243
Qed.

Georgio Nicolas's avatar
Georgio Nicolas committed
244
  (* the task is to come up with natural deduction style rules for "knows"*)
Nadim Kobeissi's avatar
Coq  
Nadim Kobeissi committed
245

Georgio Nicolas's avatar
Georgio Nicolas committed
246
247
248
249
250
251
Fixpoint value_beq (v1 v2: value) : bool := 
  match v1, v2 with
    | const c1, const c2 => match c1, c2 with
        | cnstn a, cnstn a' => eqb a a'
        | Nil, Nil => true
        | _, _ => false
Nadim Kobeissi's avatar
Coq  
Nadim Kobeissi committed
252
      end
Georgio Nicolas's avatar
Georgio Nicolas committed
253
254
255
256
    | eq e1, eq e2 => match e1, e2 with
        | PUBKEY g1 k1, PUBKEY g2 k2 => value_beq k1 k2
        | DH _ exp1 exp2, DH _ exp1' exp2' => ((value_beq exp1 exp1') && (value_beq exp2 exp2')) || ((value_beq exp1 exp2') && (value_beq exp2 exp1'))
        | _, _ => false
Nadim Kobeissi's avatar
Coq  
Nadim Kobeissi committed
257
      end
Georgio Nicolas's avatar
Georgio Nicolas committed
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
    | prim p1, prim p2 => match p1, p2 with
        | ENC k1 m1, ENC k2 m2 => (value_beq k1 k2) && (value_beq m1 m2)
        | DEC k1 e1, DEC k2 e2 => (value_beq k1 k2) && (value_beq e1 e2)
        | AEAD_ENC k1 m1 ad1, AEAD_ENC k2 m2 ad2 => ((value_beq k1 k2) && (value_beq m1 m2)) && (value_beq ad1 ad2)
        | AEAD_DEC k1 e1 ad1, AEAD_DEC k2 e2 ad2 => ((value_beq k1 k2) && (value_beq e1 e2)) && (value_beq ad1 ad2)
        | PKE_ENC gk1 m1, PKE_ENC gk2 m2 => (value_beq gk1 gk2) && (value_beq m1 m2)
        | PKE_DEC k1 e1, PKE_DEC k2 e2 => (value_beq k1 k2) && (value_beq e1 e2)
        | HASH1 a1, HASH1 a2 => value_beq a1 a2
        | HASH2 a1 b1, HASH2 a2 b2 => (value_beq a1 a2) && (value_beq b1 b2)
        | HASH3 a1 b1 c1, HASH3 a2 b2 c2 => ((value_beq a1 a2) && (value_beq b1 b2)) && (value_beq c1 c2)
        | HASH4 a1 b1 c1 d1, HASH4 a2 b2 c2 d2 => ((value_beq a1 a2) && (value_beq b1 b2)) && ((value_beq c1 c2) && (value_beq d1 d2))
        | HASH5 a1 b1 c1 d1 e1, HASH5 a2 b2 c2 d2 e2 => (((value_beq a1 a2) && (value_beq b1 b2)) && ((value_beq c1 c2) && (value_beq d1 d2))) && (value_beq e1 e2)
        | MAC a1 b1, MAC a2 b2 => (value_beq a1 a2) && (value_beq b1 b2)
        | HKDF1 s1 k1 i1, HKDF1 s2 k2 i2 => ((value_beq s1 s2) && (value_beq k1 k2)) && (value_beq i1 i2)
        | HKDF2 s1 k1 i1, HKDF2 s2 k2 i2 => ((value_beq s1 s2) && (value_beq k1 k2)) && (value_beq i1 i2)
        | HKDF3 s1 k1 i1, HKDF3 s2 k2 i2 => ((value_beq s1 s2) && (value_beq k1 k2)) && (value_beq i1 i2)
        | HKDF4 s1 k1 i1, HKDF4 s2 k2 i2 => ((value_beq s1 s2) && (value_beq k1 k2)) && (value_beq i1 i2)
        | HKDF5 s1 k1 i1, HKDF5 s2 k2 i2 => ((value_beq s1 s2) && (value_beq k1 k2)) && (value_beq i1 i2)
        | PW_HASH1 a1, PW_HASH1 a2 => value_beq a1 a2
        | PW_HASH2 a1 b1, PW_HASH2 a2 b2 => (value_beq a1 a2) && (value_beq b1 b2)
        | PW_HASH3 a1 b1 c1, PW_HASH3 a2 b2 c2 => ((value_beq a1 a2) && (value_beq b1 b2)) && (value_beq c1 c2)
        | PW_HASH4 a1 b1 c1 d1, PW_HASH4 a2 b2 c2 d2 => ((value_beq a1 a2) && (value_beq b1 b2)) && ((value_beq c1 c2) && (value_beq d1 d2))
        | PW_HASH5 a1 b1 c1 d1 e1, PW_HASH5 a2 b2 c2 d2 e2 => (((value_beq a1 a2) && (value_beq b1 b2)) && ((value_beq c1 c2) && (value_beq d1 d2))) && (value_beq e1 e2)
        | SIGN k1 m1, SIGN k2 m2 => (value_beq k1 k2) && (value_beq m1 m2)
        | SIGNVERIF gk m s, SIGNVERIF gk' m' s' => ((value_beq gk gk') && (value_beq m m')) && (value_beq s s')
        | RINGSIGN ka1 gkb1 gkc1 m1, RINGSIGN ka2 gkb2 gkc2 m2 => ((value_beq ka1 ka2) && (value_beq gkb1 gkb2)) && (
          (value_beq gkc1 gkc2) && (value_beq m1 m2))
        | RINGSIGNVERIF gka1 gkb1 gkc1 m1 s1, RINGSIGNVERIF gka2 gkb2 gkc2 m2 s2 => 
          (((value_beq gka1 gka2) && (value_beq gkb1 gkb2)) && ((value_beq gkc1 gkc2) && (value_beq m1 m2))) && (value_beq s1 s2)
        | SHAMIR_SPLIT1 k1, SHAMIR_SPLIT1 k2 => value_beq k1 k2
        | SHAMIR_SPLIT2 k1, SHAMIR_SPLIT2 k2 => value_beq k1 k2
        | SHAMIR_SPLIT3 k1, SHAMIR_SPLIT3 k2 => value_beq k1 k2
        | SHAMIR_JOIN sa1 sb1, SHAMIR_JOIN sa2 sb2 => (value_beq sa1 sa2) && (value_beq sb1 sb2)
        | CONCAT2 a1 b1, CONCAT2 a2 b2 => (value_beq a1 a2) && (value_beq b1 b2)
        | CONCAT3 a1 b1 c1, CONCAT3 a2 b2 c2 => ((value_beq a1 a2) && (value_beq b1 b2)) && (value_beq c1 c2)
        | CONCAT4 a1 b1 c1 d1, CONCAT4 a2 b2 c2 d2 => ((value_beq a1 a2) && (value_beq b1 b2)) && ((value_beq c1 c2) && (value_beq d1 d2))
        | CONCAT5 a1 b1 c1 d1 e1, CONCAT5 a2 b2 c2 d2 e2 => (((value_beq a1 a2) && (value_beq b1 b2)) && ((value_beq c1 c2) && (value_beq d1 d2))) && (value_beq e1 e2)
        | SPLIT1 a1, SPLIT1 a2 => value_beq a1 a2
        | SPLIT2 a1, SPLIT2 a2 => value_beq a1 a2
        | SPLIT3 a1, SPLIT3 a2 => value_beq a1 a2
        | SPLIT4 a1, SPLIT4 a2 => value_beq a1 a2
        | SPLIT5 a1, SPLIT5 a2 => value_beq a1 a2
        | _, _ => false
Nadim Kobeissi's avatar
Coq  
Nadim Kobeissi committed
301
      end
Georgio Nicolas's avatar
Georgio Nicolas committed
302
303
304
305
    | pass a, pass b => match a, b with
        | cnstn a, cnstn b => eqb a b
        | Nil, Nil => true
        | _ , _ => false
Nadim Kobeissi's avatar
Coq  
Nadim Kobeissi committed
306
      end
Georgio Nicolas's avatar
Georgio Nicolas committed
307
308
    | default, default => true
    | _, _ => false
Nadim Kobeissi's avatar
Coq  
Nadim Kobeissi committed
309
310
  end.

Georgio Nicolas's avatar
Georgio Nicolas committed
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
Fixpoint has_nested_value (main v: value) : bool :=
    match main with
    | default => false
    | const _ => value_beq v main
    | pass _ => value_beq v main
    | eq e => match e with
        | PUBKEY _ exp => has_nested_value exp v
        | DH _ exp1 exp2 => (has_nested_value exp1 v) || (has_nested_value exp2 v)
      end
    | prim p => match p with
        | ENC k m => (has_nested_value k v) || (has_nested_value m v)
        | DEC k c => (has_nested_value k v) || (has_nested_value c v)
        | AEAD_ENC k m ad => ((has_nested_value k v) || (has_nested_value m v)) || (has_nested_value ad v)
        | AEAD_DEC k c ad => ((has_nested_value k v) || (has_nested_value c v)) || (has_nested_value ad v)
        | PKE_ENC gk m => (has_nested_value gk v) || (has_nested_value m v)
        | PKE_DEC k m => (has_nested_value k v) || (has_nested_value m v)
        | HASH1 a => has_nested_value a v
        | HASH2 a b => (has_nested_value a v) || (has_nested_value b v)
        | HASH3 a b c => ((has_nested_value a v) || (has_nested_value b v)) || (has_nested_value c v)
        | HASH4 a b c d => ((has_nested_value a v) || (has_nested_value b v)) || ((has_nested_value c v) || (has_nested_value d v))
        | HASH5 a b c d e => ((has_nested_value a v) || (has_nested_value b v)) || ((has_nested_value c v) || (has_nested_value d v)) || (has_nested_value e v)
        | MAC a b => (has_nested_value a v) || (has_nested_value b v)
        | HKDF1 s k i => ((has_nested_value s v) || (has_nested_value k v)) || (has_nested_value i v)
        | HKDF2 s k i => ((has_nested_value s v) || (has_nested_value k v)) || (has_nested_value i v)
        | HKDF3 s k i => ((has_nested_value s v) || (has_nested_value k v)) || (has_nested_value i v)
        | HKDF4 s k i => ((has_nested_value s v) || (has_nested_value k v)) || (has_nested_value i v)
        | HKDF5 s k i => ((has_nested_value s v) || (has_nested_value k v)) || (has_nested_value i v)
        | PW_HASH1 a => has_nested_value a v
        | PW_HASH2 a b => (has_nested_value a v) || (has_nested_value b v)
        | PW_HASH3 a b c => ((has_nested_value a v) || (has_nested_value b v)) || (has_nested_value c v)
        | PW_HASH4 a b c d => ((has_nested_value a v) || (has_nested_value b v)) || ((has_nested_value c v) || (has_nested_value d v))
        | PW_HASH5 a b c d e => ((has_nested_value a v) || (has_nested_value b v)) || ((has_nested_value c v) || (has_nested_value d v)) || (has_nested_value e v)
        | SIGN k m => (has_nested_value k v) || (has_nested_value m v)
        | SIGNVERIF k m s => ((has_nested_value k v) || (has_nested_value m v)) || (has_nested_value s v)
        | RINGSIGN ka gkb gkc m => ((has_nested_value ka v) || (has_nested_value gkb v)) || ((has_nested_value gkc v) || (has_nested_value m v))
        | RINGSIGNVERIF ka gkb gkc m s => ((has_nested_value ka v) || (has_nested_value gkb v)) || ((has_nested_value gkc v) || (has_nested_value m v)) || (has_nested_value s v)
        | BLIND k m => (has_nested_value k v) || (has_nested_value m v)
        | UNBLIND k m s => ((has_nested_value k v) || (has_nested_value m v)) || (has_nested_value s v)  
        | SHAMIR_SPLIT1 k => has_nested_value k v
        | SHAMIR_SPLIT2 k => has_nested_value k v
        | SHAMIR_SPLIT3 k => has_nested_value k v
        | SHAMIR_JOIN sa sb => (has_nested_value sa v) || (has_nested_value sb v)
        | CONCAT2 a b => (has_nested_value a v) || (has_nested_value b v)
        | CONCAT3 a b c => ((has_nested_value a v) || (has_nested_value b v)) || (has_nested_value c v)
        | CONCAT4 a b c d => ((has_nested_value a v) || (has_nested_value b v)) || ((has_nested_value c v) || (has_nested_value d v))
        | CONCAT5 a b c d e => ((has_nested_value a v) || (has_nested_value b v)) || ((has_nested_value c v) || (has_nested_value d v)) || (has_nested_value e v)
        | SPLIT1 a => has_nested_value a v
        | SPLIT2 a => has_nested_value a v
        | SPLIT3 a => has_nested_value a v
        | SPLIT4 a => has_nested_value a v
        | SPLIT5 a => has_nested_value a v
Nadim Kobeissi's avatar
Coq  
Nadim Kobeissi committed
362
363
      end
  end.
Georgio Nicolas's avatar
Georgio Nicolas committed
364
365
366
367
Definition is_primitive (v: value): bool :=
  match v with
    | prim p => true
    | _ => false
Nadim Kobeissi's avatar
Coq  
Nadim Kobeissi committed
368
369
  end.

Georgio Nicolas's avatar
Georgio Nicolas committed
370
371
372
373
Definition is_constant (v: value): bool :=
  match v with
    | const c => true
    | _ => false
Nadim Kobeissi's avatar
Coq  
Nadim Kobeissi committed
374
375
  end.

Georgio Nicolas's avatar
Georgio Nicolas committed
376
377
378
379
Definition is_equation (v: value): bool :=
  match v with
    | eq e => true
    | _ => false
Nadim Kobeissi's avatar
Coq  
Nadim Kobeissi committed
380
381
  end.

Georgio Nicolas's avatar
Georgio Nicolas committed
382
383
384
385
386
387
388
Fixpoint shallow_search (l: list value) (v: value) : bool :=
    match l with
    | [] => false
    | h :: t => match value_beq h v with
        | true => true
        | false => shallow_search t v
      end
Nadim Kobeissi's avatar
Coq  
Nadim Kobeissi committed
389
390
  end.

Georgio Nicolas's avatar
Georgio Nicolas committed
391
392
393
394
395
396
397
398
399
Definition deep_search (l : list value) (v : value) := filter (fun x => has_nested_value x v) l.
Fixpoint remove_value_list (l: list value) (v: value) : list value :=
  match l with
      | [] => []
      | h :: t => match value_beq h v with 
          | true => t
          | false => [h] ++ (remove_value_list t v)
        end
    end.
Nadim Kobeissi's avatar
Coq  
Nadim Kobeissi committed
400

Georgio Nicolas's avatar
Georgio Nicolas committed
401
402
403
404
Fixpoint merge_lists (l1 l2: list value) : list value :=
  match l1 with
      | [] => l2
      | h :: t => [h] ++ (merge_lists t (remove_value_list l2 h))
Nadim Kobeissi's avatar
Coq  
Nadim Kobeissi committed
405
406
  end.

Georgio Nicolas's avatar
Georgio Nicolas committed
407
408
Inductive principal : Type :=
| PRINCIPAL : string -> list expression -> principal.
Nadim Kobeissi's avatar
Coq  
Nadim Kobeissi committed
409

Georgio Nicolas's avatar
Georgio Nicolas committed
410
411
Inductive message : Type :=
| MSG : guard_status -> value -> message.
Nadim Kobeissi's avatar
Coq  
Nadim Kobeissi committed
412

Georgio Nicolas's avatar
Georgio Nicolas committed
413
414
415
Inductive block : Type :=
| pblock : principal -> block
| mblock : message -> block.
Nadim Kobeissi's avatar
Coq  
Nadim Kobeissi committed
416

Georgio Nicolas's avatar
Georgio Nicolas committed
417
418
Inductive state : Type :=
| STATE: list block -> state.
Nadim Kobeissi's avatar
Coq  
Nadim Kobeissi committed
419
420


Georgio Nicolas's avatar
Georgio Nicolas committed
421
422
423
424
425
426
427
Fixpoint absorb_expression (l: list expression) : list value :=
  match l with
    | [] => []  
    | h :: t => match h with
        | EXP dec qual val ls => match qual, ls with
            | private, not_leaked => absorb_expression t
            | _, _ => [val] ++ absorb_expression t
Nadim Kobeissi's avatar
Coq  
Nadim Kobeissi committed
428
429
430
431
          end
      end
  end.

Georgio Nicolas's avatar
Georgio Nicolas committed
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
Fixpoint has_password (v: value) : list value :=
  match v with
  | prim p => match p with
      | CONCAT2 a b => has_password a ++ has_password b
      | CONCAT3 a b c => has_password a ++ has_password b ++ has_password c
      | CONCAT4 a b c d => has_password a ++ has_password b ++ has_password c ++ has_password d
      | CONCAT5 a b c d e => has_password a ++ has_password b ++ has_password c ++ has_password d ++ has_password e
      | SPLIT1 a => has_password a
      | SPLIT2 a => has_password a
      | SPLIT3 a => has_password a
      | SPLIT4 a => has_password a
      | SPLIT5 a => has_password a
      | PW_HASH1 a => []
      | PW_HASH2 a b => []
      | PW_HASH3 a b c => []
      | PW_HASH4 a b c d => []
      | PW_HASH5 a b c d e => []
      | HASH1 a => has_password a
      | HASH2 a b => has_password a ++ has_password b
      | HASH3 a b c => has_password a ++ has_password b ++ has_password c
      | HASH4 a b c d => has_password a ++ has_password b ++ has_password c ++ has_password d
      | HASH5 a b c d e => has_password a ++ has_password b ++ has_password c ++ has_password d ++ has_password e
      | HKDF1 salt ikm info => has_password salt ++ has_password ikm ++ has_password info
      | HKDF2 salt ikm info => has_password salt ++ has_password ikm ++ has_password info
      | HKDF3 salt ikm info => has_password salt ++ has_password ikm ++ has_password info
      | HKDF4 salt ikm info => has_password salt ++ has_password ikm ++ has_password info
      | HKDF5 salt ikm info => has_password salt ++ has_password ikm ++ has_password info
      | AEAD_ENC k m ad => has_password k ++ has_password ad
      | AEAD_DEC k c ad => has_password k ++ has_password c ++  has_password ad
      | ENC k m => has_password k
      | DEC k c => has_password k ++ has_password c
      | MAC k m => has_password k
      | SIGN k m => has_password k
      | SIGNVERIF k m s => has_password k ++ has_password m ++ has_password s 
      | PKE_ENC gk m => has_password gk
      | PKE_DEC k c => has_password k ++ has_password c
      | SHAMIR_SPLIT1 k => has_password k
      | SHAMIR_SPLIT2 k => has_password k
      | SHAMIR_SPLIT3 k => has_password k
      | SHAMIR_JOIN sa sb => has_password sa ++ has_password sb
      | RINGSIGN ka gkb gkc m => has_password ka ++ has_password gkb ++ has_password gkc
      | RINGSIGNVERIF gka gkb gkc m s => has_password gka ++ has_password gkb ++ has_password gkc ++ has_password m ++ has_password s
      | BLIND k m => has_password k
      | UNBLIND k m s => has_password k ++ has_password m ++ has_password s
    end
  | eq e => match e with
      | PUBKEY _ exp => has_password exp
      | DH _ exp1 exp2 => has_password exp1 ++ has_password exp2
    end
  | const c => match c with
      | Nil => []
      | cnstn c => []
    end
    | pass a => [v]
    | default => []
end.

Fixpoint absorb_passwords_expression (l: list expression) : list value :=
  match l with
    | [] => []  
    | h :: t => match h with
        | EXP dec qual val ls => has_password val ++ absorb_passwords_expression t
Nadim Kobeissi's avatar
Coq  
Nadim Kobeissi committed
494
495
496
      end
  end.

Nadim Kobeissi's avatar
Nadim Kobeissi committed
497
Definition absorb_principal (p: principal) : list value :=
Nadim Kobeissi's avatar
Coq  
Nadim Kobeissi committed
498
  match p with
Georgio Nicolas's avatar
Georgio Nicolas committed
499
500
  | PRINCIPAL _ pk => merge_lists (absorb_expression pk) (absorb_passwords_expression pk)
end.
Nadim Kobeissi's avatar
Coq  
Nadim Kobeissi committed
501

Nadim Kobeissi's avatar
Nadim Kobeissi committed
502
Definition absorb_message (m: message) : value :=
Georgio Nicolas's avatar
Georgio Nicolas committed
503
504
505
506
  match m with
  | MSG _ val => val
end.

Nadim Kobeissi's avatar
Nadim Kobeissi committed
507
Definition absorb_block (b: block) : list value :=
Georgio Nicolas's avatar
Georgio Nicolas committed
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
  match b with
    | pblock p => absorb_principal p
    | mblock m => [absorb_message m]
end.

Fixpoint init_attacker (l: list block) : list value := 
  match l with
    | [] => []
    | h :: t => absorb_block h ++ init_attacker t
end.

Fixpoint gather_principal_expressions (l: list block) (pname: string) : list expression :=
  match l with
    | [] => []
    | h :: t => match h with
      | mblock _ => gather_principal_expressions t pname
      | pblock pb => match pb with
          | PRINCIPAL hpname hplist => match eqb pname hpname with
                  | true => hplist ++ gather_principal_expressions t pname
                  | false => gather_principal_expressions t pname
              end
          end
Nadim Kobeissi's avatar
Coq  
Nadim Kobeissi committed
530
531
532
      end
  end.

Georgio Nicolas's avatar
Georgio Nicolas committed
533
534
535
536
Fixpoint gather_principals (pnames: list string) (l: list block) : list principal :=
  match pnames with 
    | [] => []
    | p1 :: r => [PRINCIPAL p1 (gather_principal_expressions l p1)] ++ gather_principals r l
Nadim Kobeissi's avatar
Coq  
Nadim Kobeissi committed
537
538
  end.

Georgio Nicolas's avatar
Georgio Nicolas committed
539
540
541
542
543
Fixpoint get_values_expression_list (l: list expression) : list value :=
  match l with
    | [] => []
    | h :: t => match h with
      | EXP _ _ v _  => [v] ++ get_values_expression_list t
Nadim Kobeissi's avatar
Coq  
Nadim Kobeissi committed
544
545
546
      end
  end.

Georgio Nicolas's avatar
Georgio Nicolas committed
547
548
549
550
551
Fixpoint gather_principal_values (l: list principal) : list (list value) :=
  match l with
    | [] => []
    | h :: t => match h with
        | PRINCIPAL pname plist => [get_values_expression_list plist] ++ gather_principal_values t
Nadim Kobeissi's avatar
Coq  
Nadim Kobeissi committed
552
553
554
      end
  end.

Georgio Nicolas's avatar
Georgio Nicolas committed
555
556
557
558
559
Definition rewrite_diff (old new: value) : value :=
  match old, new with
    | const c1, const c2 => match value_beq old new with
        | true => old
        | false => new
Nadim Kobeissi's avatar
Coq  
Nadim Kobeissi committed
560
      end
Georgio Nicolas's avatar
Georgio Nicolas committed
561
562
563
    | pass a1, pass a2 => match value_beq old new with
        | true => old
        | false => new
Nadim Kobeissi's avatar
Coq  
Nadim Kobeissi committed
564
      end
Georgio Nicolas's avatar
Georgio Nicolas committed
565
566
567
568
569
570
    | prim p1, prim p2 => match p1, p2 with
        | ENC k1 m1, ENC k2 m2 => match value_beq k1 k2, value_beq m1 m2 with
            | true, true => old
            | false, true => prim(ENC k2 m1)
            | true, false => prim(ENC k1 m2)
            | false, false => new
Nadim Kobeissi's avatar
Coq  
Nadim Kobeissi committed
571
          end
Georgio Nicolas's avatar
Georgio Nicolas committed
572
573
574
575
576
        | DEC k1 c1, DEC k2 c2 => match value_beq k1 k2, value_beq c1 c2 with
            | true, true => old
            | false, true => prim(DEC k2 c1)
            | true, false => prim(DEC k1 c2)
            | false, false => new
Nadim Kobeissi's avatar
Coq  
Nadim Kobeissi committed
577
          end
Georgio Nicolas's avatar
Georgio Nicolas committed
578
579
580
581
582
583
584
585
586
587
        | AEAD_ENC k1 m1 ad1, AEAD_ENC k2 m2 ad2 =>
           match value_beq k1 k2, value_beq m1 m2, value_beq ad1 ad2 with
            | true, true, true => old
            | false, true, true => prim(AEAD_ENC k2 m1 ad1)
            | true, false, true => prim(AEAD_ENC k1 m2 ad1)
            | true, true, false => prim(AEAD_ENC k1 m1 ad2)
            | true, false, false => prim(AEAD_ENC k1 m2 ad2)
            | false, true, false => prim(AEAD_ENC k2 m1 ad1)
            | false, false, true => prim(AEAD_ENC k2 m2 ad1)
            | false, false, false => new
Nadim Kobeissi's avatar
Coq  
Nadim Kobeissi committed
588
          end
Georgio Nicolas's avatar
Georgio Nicolas committed
589
590
591
592
593
594
595
596
597
598
        | AEAD_DEC k1 c1 ad1, AEAD_DEC k2 c2 ad2 =>
           match value_beq k1 k2, value_beq c1 c2, value_beq ad1 ad2 with
            | true, true, true => old
            | false, true, true => prim(AEAD_DEC k2 c1 ad1)
            | true, false, true => prim(AEAD_DEC k1 c2 ad1)
            | true, true, false => prim(AEAD_DEC k1 c1 ad2)
            | true, false, false => prim(AEAD_DEC k1 c2 ad2)
            | false, true, false => prim(AEAD_DEC k2 c1 ad1)
            | false, false, true => prim(AEAD_DEC k2 c2 ad1)
            | false, false, false => new
Nadim Kobeissi's avatar
Coq  
Nadim Kobeissi committed
599
          end
Georgio Nicolas's avatar
Georgio Nicolas committed
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
      | PKE_ENC gk1 m1, ENC gk2 m2 => match value_beq gk1 gk2, value_beq m1 m2 with
          | true, true => old
          | false, true => prim(PKE_ENC gk2 m1)
          | true, false => prim(PKE_ENC gk1 m2)
          | false, false => new
        end
      | PKE_DEC k1 c1, PKE_DEC k2 c2 => match value_beq k1 k2, value_beq c1 c2 with
          | true, true => old
          | false, true => prim(PKE_DEC k2 c1)
          | true, false => prim(PKE_DEC k1 c2)
          | false, false => new
        end
      | HASH1 a1, HASH1 a2 => match value_beq a1 a2 with
          | true => old
          | false => new
        end
      | HASH2 a1 b1, HASH2 a2 b2 => match value_beq a1 a2, value_beq b1 b2 with
          | true, true => old
          | false, true => prim(HASH2 a2 b1)
          | true, false => prim(HASH2 a1 b2)
          | false, false => new
        end
      | HASH3 a1 b1 c1, HASH3 a2 b2 c2 =>
        match value_beq a1 a2, value_beq b1 b2, value_beq c1 c2 with
         | true, true, true => old
         | false, true, true => prim(HASH3 a2 b1 c1)
         | true, false, true => prim(HASH3 a1 b2 c1)
         | true, true, false => prim(HASH3 a1 b1 c2)
         | true, false, false => prim(HASH3 a1 b2 c2)
         | false, true, false => prim(HASH3 a2 b1 c1)
         | false, false, true => prim(HASH3 a2 b2 c1)
         | false, false, false => new
       end
      | HASH4 a1 b1 c1 d1, HASH4 a2 b2 c2 d2 =>
        match value_beq a1 a2, value_beq b1 b2, value_beq c1 c2, value_beq d1 d2 with
         | true, true, true, true => old
         | false, true, true, true => prim(HASH4 a2 b1 c1 d1)
         | true, false, true, true => prim(HASH4 a1 b2 c1 d1)
         | true, true, false, true => prim(HASH4 a1 b1 c2 d1)
         | true, true, true, false => prim(HASH4 a1 b1 c1 d2)
         | false, false, true, true => prim(HASH4 a2 b2 c1 d1)
         | false, true, false, true => prim(HASH4 a2 b1 c2 d1)
         | false, true, true, false => prim(HASH4 a2 b1 c1 d2)
         | true, false, false, true => prim(HASH4 a1 b2 c2 d1)
         | true, false, true, false => prim(HASH4 a1 b2 c1 d2)
         | true, true, false, false => prim(HASH4 a1 b1 c2 d2)
         | true, false, false, false => prim(HASH4 a1 b2 c2 d2)
         | false, true, false, false => prim(HASH4 a2 b1 c2 d2)
         | false, false, true, false => prim(HASH4 a2 b2 c1 d2)
         | false, false, false, true => prim(HASH4 a2 b2 c2 d1)
         | false, false, false, false => new
       end
      | HASH5 a1 b1 c1 d1 e1, HASH5 a2 b2 c2 d2 e2=>
        match value_beq a1 a2, value_beq b1 b2, value_beq c1 c2, value_beq d1 d2, value_beq e1 e2 with
         | true, true, true, true, true => old
         | false, true, true, true, true => prim(HASH5 a2 b1 c1 d1 e1)
         | true, false, true, true, true => prim(HASH5 a1 b2 c1 d1 e1)
         | true, true, false, true, true => prim(HASH5 a1 b1 c2 d1 e1)
         | true, true, true, false, true => prim(HASH5 a1 b1 c1 d2 e1)
         | true, true, true, true, false => prim(HASH5 a1 b1 c1 d1 e2)
         | false, false, true, true, true => prim(HASH5 a2 b2 c1 d1 e1)
         | false, true, false, true, true => prim(HASH5 a2 b1 c2 d1 e1)
         | false, true, true, false, true => prim(HASH5 a2 b1 c1 d2 e1)
         | false, true, true, true, false => prim(HASH5 a2 b1 c1 d1 e2)
         | true, false, false, true, true => prim(HASH5 a1 b2 c2 d1 e1)
         | true, false, true, false, true => prim(HASH5 a1 b2 c1 d2 e1)
         | true, false, true, true, false => prim(HASH5 a1 b2 c1 d1 e2)
         | true, true, false, false, true => prim(HASH5 a1 b1 c2 d2 e1)
         | true, true, false, true, false => prim(HASH5 a1 b1 c2 d1 e2)
         | true, true, true, false, false => prim(HASH5 a1 b1 c1 d2 e2)
         | true, true, false, false, false => prim(HASH5 a1 b1 c2 d2 e2)
         | true, false, true, false, false => prim(HASH5 a1 b2 c1 d2 e2)
         | true, false, false, true, false => prim(HASH5 a1 b2 c2 d1 e2)
         | true, false, false, false, true => prim(HASH5 a1 b2 c2 d2 e1)
         | false, true, true, false, false => prim(HASH5 a2 b1 c1 d2 e2)
         | false, true, false, true, false => prim(HASH5 a2 b1 c2 d1 e2)
         | false, true, false, false, true => prim(HASH5 a2 b1 c2 d2 e1)
         | false, false, true, true, false => prim(HASH5 a2 b2 c1 d1 e2)
         | false, false, true, false, true => prim(HASH5 a2 b2 c1 d2 e1)
         | false, false, false, true, true => prim(HASH5 a2 b2 c2 d1 e1)
         | true, false, false, false, false => prim(HASH5 a1 b2 c2 d2 e2)
         | false, true, false, false, false => prim(HASH5 a2 b1 c2 d2 e2)
         | false, false, true, false, false => prim(HASH5 a2 b2 c1 d2 e2)
         | false, false, false, true, false => prim(HASH5 a2 b2 c2 d1 e2)
         | false, false, false, false, true => prim(HASH5 a2 b2 c2 d2 e1)
         | false, false, false, false, false => new
       end
        | MAC k1 m1, MAC k2 m2 => match value_beq k1 k2, value_beq m1 m2 with
            | true, true => old
            | false, true => prim(MAC k2 m1)
            | true, false => prim(MAC k1 m2)
            | false, false => new
          end
        | HKDF1 salt1 ikm1 info1, HKDF1 salt2 ikm2 info2 =>
          match value_beq salt1 salt2, value_beq ikm1 ikm2, value_beq info1 info2 with
           | true, true, true => old
           | false, true, true => prim(HKDF1 salt2 ikm1 info1)
           | true, false, true => prim(HKDF1 salt1 ikm2 info1)
           | true, true, false => prim(HKDF1 salt1 ikm1 info2)
           | true, false, false => prim(HKDF1 salt1 ikm2 info2)
           | false, true, false => prim(HKDF1 salt2 ikm1 info1)
           | false, false, true => prim(HKDF1 salt2 ikm2 info1)
           | false, false, false => new
         end
        | HKDF2 salt1 ikm1 info1, HKDF2 salt2 ikm2 info2 =>
          match value_beq salt1 salt2, value_beq ikm1 ikm2, value_beq info1 info2 with
           | true, true, true => old
           | false, true, true => prim(HKDF2 salt2 ikm1 info1)
           | true, false, true => prim(HKDF2 salt1 ikm2 info1)
           | true, true, false => prim(HKDF2 salt1 ikm1 info2)
           | true, false, false => prim(HKDF2 salt1 ikm2 info2)
           | false, true, false => prim(HKDF2 salt2 ikm1 info1)
           | false, false, true => prim(HKDF2 salt2 ikm2 info1)
           | false, false, false => new
         end
        | HKDF3 salt1 ikm1 info1, HKDF3 salt2 ikm2 info2 =>
          match value_beq salt1 salt2, value_beq ikm1 ikm2, value_beq info1 info2 with
           | true, true, true => old
           | false, true, true => prim(HKDF3 salt2 ikm1 info1)
           | true, false, true => prim(HKDF3 salt1 ikm2 info1)
           | true, true, false => prim(HKDF3 salt1 ikm1 info2)
           | true, false, false => prim(HKDF3 salt1 ikm2 info2)
           | false, true, false => prim(HKDF3 salt2 ikm1 info1)
           | false, false, true => prim(HKDF3 salt2 ikm2 info1)
           | false, false, false => new
         end
        | HKDF4 salt1 ikm1 info1, HKDF4 salt2 ikm2 info2 =>
          match value_beq salt1 salt2, value_beq ikm1 ikm2, value_beq info1 info2 with
           | true, true, true => old
           | false, true, true => prim(HKDF4 salt2 ikm1 info1)
           | true, false, true => prim(HKDF4 salt1 ikm2 info1)
           | true, true, false => prim(HKDF4 salt1 ikm1 info2)
           | true, false, false => prim(HKDF4 salt1 ikm2 info2)
           | false, true, false => prim(HKDF4 salt2 ikm1 info1)
           | false, false, true => prim(HKDF4 salt2 ikm2 info1)
           | false, false, false => new
         end
        | HKDF5 salt1 ikm1 info1, HKDF5 salt2 ikm2 info2 =>
          match value_beq salt1 salt2, value_beq ikm1 ikm2, value_beq info1 info2 with
           | true, true, true => old
           | false, true, true => prim(HKDF5 salt2 ikm1 info1)
           | true, false, true => prim(HKDF5 salt1 ikm2 info1)
           | true, true, false => prim(HKDF5 salt1 ikm1 info2)
           | true, false, false => prim(HKDF5 salt1 ikm2 info2)
           | false, true, false => prim(HKDF5 salt2 ikm1 info1)
           | false, false, true => prim(HKDF5 salt2 ikm2 info1)
           | false, false, false => new
         end
      | PW_HASH1 a1, PW_HASH1 a2 => match value_beq a1 a2 with
          | true => old
          | false => new
        end
      | PW_HASH2 a1 b1, PW_HASH2 a2 b2 => match value_beq a1 a2, value_beq b1 b2 with
          | true, true => old
          | false, true => prim(PW_HASH2 a2 b1)
          | true, false => prim(PW_HASH2 a1 b2)
          | false, false => new
        end
      | PW_HASH3 a1 b1 c1, PW_HASH3 a2 b2 c2 =>
        match value_beq a1 a2, value_beq b1 b2, value_beq c1 c2 with
         | true, true, true => old
         | false, true, true => prim(PW_HASH3 a2 b1 c1)
         | true, false, true => prim(PW_HASH3 a1 b2 c1)
         | true, true, false => prim(PW_HASH3 a1 b1 c2)
         | true, false, false => prim(PW_HASH3 a1 b2 c2)
         | false, true, false => prim(PW_HASH3 a2 b1 c1)
         | false, false, true => prim(PW_HASH3 a2 b2 c1)
         | false, false, false => new
       end
      | PW_HASH4 a1 b1 c1 d1, PW_HASH4 a2 b2 c2 d2 =>
        match value_beq a1 a2, value_beq b1 b2, value_beq c1 c2, value_beq d1 d2 with
         | true, true, true, true => old
         | false, true, true, true => prim(PW_HASH4 a2 b1 c1 d1)
         | true, false, true, true => prim(PW_HASH4 a1 b2 c1 d1)
         | true, true, false, true => prim(PW_HASH4 a1 b1 c2 d1)
         | true, true, true, false => prim(PW_HASH4 a1 b1 c1 d2)
         | false, false, true, true => prim(PW_HASH4 a2 b2 c1 d1)
         | false, true, false, true => prim(PW_HASH4 a2 b1 c2 d1)
         | false, true, true, false => prim(PW_HASH4 a2 b1 c1 d2)
         | true, false, false, true => prim(PW_HASH4 a1 b2 c2 d1)
         | true, false, true, false => prim(PW_HASH4 a1 b2 c1 d2)
         | true, true, false, false => prim(PW_HASH4 a1 b1 c2 d2)
         | true, false, false, false => prim(PW_HASH4 a1 b2 c2 d2)
         | false, true, false, false => prim(PW_HASH4 a2 b1 c2 d2)
         | false, false, true, false => prim(PW_HASH4 a2 b2 c1 d2)
         | false, false, false, true => prim(PW_HASH4 a2 b2 c2 d1)
         | false, false, false, false => new
       end
      | PW_HASH5 a1 b1 c1 d1 e1, PW_HASH5 a2 b2 c2 d2 e2=>
        match value_beq a1 a2, value_beq b1 b2, value_beq c1 c2, value_beq d1 d2, value_beq e1 e2 with
         | true, true, true, true, true => old
         | false, true, true, true, true => prim(PW_HASH5 a2 b1 c1 d1 e1)
         | true, false, true, true, true => prim(PW_HASH5 a1 b2 c1 d1 e1)
         | true, true, false, true, true => prim(PW_HASH5 a1 b1 c2 d1 e1)
         | true, true, true, false, true => prim(PW_HASH5 a1 b1 c1 d2 e1)
         | true, true, true, true, false => prim(PW_HASH5 a1 b1 c1 d1 e2)
         | false, false, true, true, true => prim(PW_HASH5 a2 b2 c1 d1 e1)
         | false, true, false, true, true => prim(PW_HASH5 a2 b1 c2 d1 e1)
         | false, true, true, false, true => prim(PW_HASH5 a2 b1 c1 d2 e1)
         | false, true, true, true, false => prim(PW_HASH5 a2 b1 c1 d1 e2)
         | true, false, false, true, true => prim(PW_HASH5 a1 b2 c2 d1 e1)
         | true, false, true, false, true => prim(PW_HASH5 a1 b2 c1 d2 e1)
         | true, false, true, true, false => prim(PW_HASH5 a1 b2 c1 d1 e2)
         | true, true, false, false, true => prim(PW_HASH5 a1 b1 c2 d2 e1)
         | true, true, false, true, false => prim(PW_HASH5 a1 b1 c2 d1 e2)
         | true, true, true, false, false => prim(PW_HASH5 a1 b1 c1 d2 e2)
         | true, true, false, false, false => prim(PW_HASH5 a1 b1 c2 d2 e2)
         | true, false, true, false, false => prim(PW_HASH5 a1 b2 c1 d2 e2)
         | true, false, false, true, false => prim(PW_HASH5 a1 b2 c2 d1 e2)
         | true, false, false, false, true => prim(PW_HASH5 a1 b2 c2 d2 e1)
         | false, true, true, false, false => prim(PW_HASH5 a2 b1 c1 d2 e2)
         | false, true, false, true, false => prim(PW_HASH5 a2 b1 c2 d1 e2)
         | false, true, false, false, true => prim(PW_HASH5 a2 b1 c2 d2 e1)
         | false, false, true, true, false => prim(PW_HASH5 a2 b2 c1 d1 e2)
         | false, false, true, false, true => prim(PW_HASH5 a2 b2 c1 d2 e1)
         | false, false, false, true, true => prim(PW_HASH5 a2 b2 c2 d1 e1)
         | true, false, false, false, false => prim(PW_HASH5 a1 b2 c2 d2 e2)
         | false, true, false, false, false => prim(PW_HASH5 a2 b1 c2 d2 e2)
         | false, false, true, false, false => prim(PW_HASH5 a2 b2 c1 d2 e2)
         | false, false, false, true, false => prim(PW_HASH5 a2 b2 c2 d1 e2)
         | false, false, false, false, true => prim(PW_HASH5 a2 b2 c2 d2 e1)
         | false, false, false, false, false => new
       end
      | SIGN k1 m1, SIGN k2 m2 => match value_beq k1 k2, value_beq m1 m2 with
          | true, true => old
          | false, true => prim(SIGN k2 m1)
          | true, false => prim(SIGN k1 m2)
          | false, false => new
        end
      | SIGNVERIF k1 m1 s1, SIGNVERIF k2 m2 s2 =>
         match value_beq k1 k2, value_beq m1 m2, value_beq s1 s2 with
          | true, true, true => old
          | false, true, true => prim(SIGNVERIF k2 m1 s1)
          | true, false, true => prim(SIGNVERIF k1 m2 s1)
          | true, true, false => prim(SIGNVERIF k1 m1 s2)
          | true, false, false => prim(SIGNVERIF k1 m2 s2)
          | false, true, false => prim(SIGNVERIF k2 m1 s1)
          | false, false, true => prim(SIGNVERIF k2 m2 s1)
          | false, false, false => new
        end
      | RINGSIGN ka1 gkb1 gkc1 m1, RINGSIGN ka2 gkb2 gkc2 m2 =>
        match value_beq ka1 ka2, value_beq gkb1 gkb2, value_beq gkc1 gkc2, value_beq m1 m2 with
          | true, true, true, true => old
          | false, true, true, true => prim(RINGSIGN ka2 gkb1 gkc1 m1)
          | true, false, true, true => prim(RINGSIGN ka1 gkb2 gkc1 m1)
          | true, true, false, true => prim(RINGSIGN ka1 gkb1 gkc2 m1)
          | true, true, true, false => prim(RINGSIGN ka1 gkb1 gkc1 m2)
          | false, false, true, true => prim(RINGSIGN ka2 gkb2 gkc1 m1)
          | false, true, false, true => prim(RINGSIGN ka2 gkb1 gkc2 m1)
          | false, true, true, false => prim(RINGSIGN ka2 gkb1 gkc1 m2)
          | true, false, false, true => prim(RINGSIGN ka1 gkb2 gkc2 m1)
          | true, false, true, false => prim(RINGSIGN ka1 gkb2 gkc1 m2)
          | true, true, false, false => prim(RINGSIGN ka1 gkb1 gkc2 m2)
          | true, false, false, false => prim(RINGSIGN ka1 gkb2 gkc2 m2)
          | false, true, false, false => prim(RINGSIGN ka2 gkb1 gkc2 m2)
          | false, false, true, false => prim(RINGSIGN ka2 gkb2 gkc1 m2)
          | false, false, false, true => prim(RINGSIGN ka2 gkb2 gkc2 m1)
          | false, false, false, false => new
        end
      | RINGSIGNVERIF gka1 gkb1 gkc1 m1 s1, RINGSIGNVERIF gka2 gkb2 gkc2 m2 s2=>
        match value_beq gka1 gka2, value_beq gkb1 gkb2, value_beq gkc1 gkc2, value_beq m1 m2, value_beq s1 s2 with
          | true, true, true, true, true => old
          | false, true, true, true, true => prim(RINGSIGNVERIF gka2 gkb1 gkc1 m1 s1)
          | true, false, true, true, true => prim(RINGSIGNVERIF gka1 gkb2 gkc1 m1 s1)
          | true, true, false, true, true => prim(RINGSIGNVERIF gka1 gkb1 gkc2 m1 s1)
          | true, true, true, false, true => prim(RINGSIGNVERIF gka1 gkb1 gkc1 m2 s1)
          | true, true, true, true, false => prim(RINGSIGNVERIF gka1 gkb1 gkc1 m1 s2)
          | false, false, true, true, true => prim(RINGSIGNVERIF gka2 gkb2 gkc1 m1 s1)
          | false, true, false, true, true => prim(RINGSIGNVERIF gka2 gkb1 gkc2 m1 s1)
          | false, true, true, false, true => prim(RINGSIGNVERIF gka2 gkb1 gkc1 m2 s1)
          | false, true, true, true, false => prim(RINGSIGNVERIF gka2 gkb1 gkc1 m1 s2)
          | true, false, false, true, true => prim(RINGSIGNVERIF gka1 gkb2 gkc2 m1 s1)
          | true, false, true, false, true => prim(RINGSIGNVERIF gka1 gkb2 gkc1 m2 s1)
          | true, false, true, true, false => prim(RINGSIGNVERIF gka1 gkb2 gkc1 m1 s2)
          | true, true, false, false, true => prim(RINGSIGNVERIF gka1 gkb1 gkc2 m2 s1)
          | true, true, false, true, false => prim(RINGSIGNVERIF gka1 gkb1 gkc2 m1 s2)
          | true, true, true, false, false => prim(RINGSIGNVERIF gka1 gkb1 gkc1 m2 s2)
          | true, true, false, false, false => prim(RINGSIGNVERIF gka1 gkb1 gkc2 m2 s2)
          | true, false, true, false, false => prim(RINGSIGNVERIF gka1 gkb2 gkc1 m2 s2)
          | true, false, false, true, false => prim(RINGSIGNVERIF gka1 gkb2 gkc2 m1 s2)
          | true, false, false, false, true => prim(RINGSIGNVERIF gka1 gkb2 gkc2 m2 s1)
          | false, true, true, false, false => prim(RINGSIGNVERIF gka2 gkb1 gkc1 m2 s2)
          | false, true, false, true, false => prim(RINGSIGNVERIF gka2 gkb1 gkc2 m1 s2)
          | false, true, false, false, true => prim(RINGSIGNVERIF gka2 gkb1 gkc2 m2 s1)
          | false, false, true, true, false => prim(RINGSIGNVERIF gka2 gkb2 gkc1 m1 s2)
          | false, false, true, false, true => prim(RINGSIGNVERIF gka2 gkb2 gkc1 m2 s1)
          | false, false, false, true, true => prim(RINGSIGNVERIF gka2 gkb2 gkc2 m1 s1)
          | true, false, false, false, false => prim(RINGSIGNVERIF gka1 gkb2 gkc2 m2 s2)
          | false, true, false, false, false => prim(RINGSIGNVERIF gka2 gkb1 gkc2 m2 s2)
          | false, false, true, false, false => prim(RINGSIGNVERIF gka2 gkb2 gkc1 m2 s2)
          | false, false, false, true, false => prim(RINGSIGNVERIF gka2 gkb2 gkc2 m1 s2)
          | false, false, false, false, true => prim(RINGSIGNVERIF gka2 gkb2 gkc2 m2 s1)
          | false, false, false, false, false => new
        end
      | BLIND k1 m1, BLIND k2 m2 => match value_beq k1 k2, value_beq m1 m2 with
          | true, true => old
          | false, true => prim(BLIND k2 m1)
          | true, false => prim(BLIND k1 m2)
          | false, false => new
        end
      | UNBLIND k1 m1 s1, UNBLIND k2 m2 s2 =>
        match value_beq k1 k2, value_beq m1 m2, value_beq s1 s2 with
          | true, true, true => old
          | false, true, true => prim(UNBLIND k2 m1 s1)
          | true, false, true => prim(UNBLIND k1 m2 s1)
          | true, true, false => prim(UNBLIND k1 m1 s2)
          | true, false, false => prim(UNBLIND k1 m2 s2)
          | false, true, false => prim(UNBLIND k2 m1 s1)
          | false, false, true => prim(UNBLIND k2 m2 s1)
          | false, false, false => new
        end
      | SHAMIR_SPLIT1 a1, SHAMIR_SPLIT1 a2 => match value_beq a1 a2 with
          | true => old
          | false => new
        end
      | SHAMIR_SPLIT2 a1, SHAMIR_SPLIT2 a2 => match value_beq a1 a2 with
          | true => old
          | false => new
        end
      | SHAMIR_SPLIT3 a1, SHAMIR_SPLIT3 a2 => match value_beq a1 a2 with
          | true => old
          | false => new
        end
      | SHAMIR_JOIN sa1 sb1, SHAMIR_JOIN sa2 sb2 => match value_beq sa1 sa2, value_beq sb1 sb2 with
          | true, true => old
          | false, true => prim(SHAMIR_JOIN sa2 sb1)
          | true, false => prim(SHAMIR_JOIN sa1 sb2)
          | false, false => new
        end
      | CONCAT2 a1 b1, CONCAT2 a2 b2 => match value_beq a1 a2, value_beq b1 b2 with
          | true, true => old
          | false, true => prim(CONCAT2 a2 b1)
          | true, false => prim(CONCAT2 a1 b2)
          | false, false => new
        end
      | CONCAT3 a1 b1 c1, CONCAT3 a2 b2 c2 =>
        match value_beq a1 a2, value_beq b1 b2, value_beq c1 c2 with
          | true, true, true => old
          | false, true, true => prim(CONCAT3 a2 b1 c1)
          | true, false, true => prim(CONCAT3 a1 b2 c1)
          | true, true, false => prim(CONCAT3 a1 b1 c2)
          | true, false, false => prim(CONCAT3 a1 b2 c2)
          | false, true, false => prim(CONCAT3 a2 b1 c1)
          | false, false, true => prim(CONCAT3 a2 b2 c1)
          | false, false, false => new
        end
      | CONCAT4 a1 b1 c1 d1, CONCAT4 a2 b2 c2 d2 =>
        match value_beq a1 a2, value_beq b1 b2, value_beq c1 c2, value_beq d1 d2 with
          | true, true, true, true => old
          | false, true, true, true => prim(CONCAT4 a2 b1 c1 d1)
          | true, false, true, true => prim(CONCAT4 a1 b2 c1 d1)
          | true, true, false, true => prim(CONCAT4 a1 b1 c2 d1)
          | true, true, true, false => prim(CONCAT4 a1 b1 c1 d2)
          | false, false, true, true => prim(CONCAT4 a2 b2 c1 d1)
          | false, true, false, true => prim(CONCAT4 a2 b1 c2 d1)
          | false, true, true, false => prim(CONCAT4 a2 b1 c1 d2)
          | true, false, false, true => prim(CONCAT4 a1 b2 c2 d1)
          | true, false, true, false => prim(CONCAT4 a1 b2 c1 d2)
          | true, true, false, false => prim(CONCAT4 a1 b1 c2 d2)
          | true, false, false, false => prim(CONCAT4 a1 b2 c2 d2)
          | false, true, false, false => prim(CONCAT4 a2 b1 c2 d2)
          | false, false, true, false => prim(CONCAT4 a2 b2 c1 d2)
          | false, false, false, true => prim(CONCAT4 a2 b2 c2 d1)
          | false, false, false, false => new
        end
      | CONCAT5 a1 b1 c1 d1 e1, CONCAT5 a2 b2 c2 d2 e2=>
        match value_beq a1 a2, value_beq b1 b2, value_beq c1 c2, value_beq d1 d2, value_beq e1 e2 with
          | true, true, true, true, true => old
          | false, true, true, true, true => prim(CONCAT5 a2 b1 c1 d1 e1)
          | true, false, true, true, true => prim(CONCAT5 a1 b2 c1 d1 e1)
          | true, true, false, true, true => prim(CONCAT5 a1 b1 c2 d1 e1)
          | true, true, true, false, true => prim(CONCAT5 a1 b1 c1 d2 e1)
          | true, true, true, true, false => prim(CONCAT5 a1 b1 c1 d1 e2)
          | false, false, true, true, true => prim(CONCAT5 a2 b2 c1 d1 e1)
          | false, true, false, true, true => prim(CONCAT5 a2 b1 c2 d1 e1)
          | false, true, true, false, true => prim(CONCAT5 a2 b1 c1 d2 e1)
          | false, true, true, true, false => prim(CONCAT5 a2 b1 c1 d1 e2)
          | true, false, false, true, true => prim(CONCAT5 a1 b2 c2 d1 e1)
          | true, false, true, false, true => prim(CONCAT5 a1 b2 c1 d2 e1)
          | true, false, true, true, false => prim(CONCAT5 a1 b2 c1 d1 e2)
          | true, true, false, false, true => prim(CONCAT5 a1 b1 c2 d2 e1)
          | true, true, false, true, false => prim(CONCAT5 a1 b1 c2 d1 e2)
          | true, true, true, false, false => prim(CONCAT5 a1 b1 c1 d2 e2)
          | true, true, false, false, false => prim(CONCAT5 a1 b1 c2 d2 e2)
          | true, false, true, false, false => prim(CONCAT5 a1 b2 c1 d2 e2)
          | true, false, false, true, false => prim(CONCAT5 a1 b2 c2 d1 e2)
          | true, false, false, false, true => prim(CONCAT5 a1 b2 c2 d2 e1)
          | false, true, true, false, false => prim(CONCAT5 a2 b1 c1 d2 e2)
          | false, true, false, true, false => prim(CONCAT5 a2 b1 c2 d1 e2)
          | false, true, false, false, true => prim(CONCAT5 a2 b1 c2 d2 e1)
          | false, false, true, true, false => prim(CONCAT5 a2 b2 c1 d1 e2)
          | false, false, true, false, true => prim(CONCAT5 a2 b2 c1 d2 e1)
          | false, false, false, true, true => prim(CONCAT5 a2 b2 c2 d1 e1)
          | true, false, false, false, false => prim(CONCAT5 a1 b2 c2 d2 e2)
          | false, true, false, false, false => prim(CONCAT5 a2 b1 c2 d2 e2)
          | false, false, true, false, false => prim(CONCAT5 a2 b2 c1 d2 e2)
          | false, false, false, true, false => prim(CONCAT5 a2 b2 c2 d1 e2)
          | false, false, false, false, true => prim(CONCAT5 a2 b2 c2 d2 e1)
          | false, false, false, false, false => new
        end
      | SPLIT1 a1, SPLIT1 a2 => match value_beq a1 a2 with