PageRenderTime 26ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/CryptoHashes.v

https://gitlab.com/oytunistrator/ledgertheory
Coq | 292 lines | 245 code | 40 blank | 7 comment | 0 complexity | 4981e77c188062eb7b3259dfe48bf042 MD5 | raw file
  1. (** Copyright (c) 2015 Bill White **)
  2. (** Distributed under the MIT/X11 software license **)
  3. (** See http://www.opensource.org/licenses/mit-license.php **)
  4. (** CryptoHashes : Idealized representation of cryptographic hashing
  5. using inductive types enclosed in a module. The module
  6. simulates the 'trapdoor' property by disallowing writing a destructor
  7. function via a match. **)
  8. Require Export Addrs.
  9. Module Type HashValsType.
  10. Parameter hashval : Type.
  11. Parameter hashnat : nat -> hashval.
  12. Parameter hashaddr : addr -> hashval.
  13. Parameter hashpair : hashval -> hashval -> hashval.
  14. Parameter hashval_eq_dec : forall (h1 h2:hashval), { h1 = h2 } + { h1 <> h2 }.
  15. Axiom hashnatinj : forall m n, hashnat m = hashnat n -> m = n.
  16. Axiom hashaddrinj : forall alpha beta, hashaddr alpha = hashaddr beta -> alpha = beta.
  17. Axiom hashpairinj : forall h1a h1b h2a h2b, hashpair h1a h1b = hashpair h2a h2b -> h1a = h2a /\ h1b = h2b.
  18. Axiom hashnataddrdiscr : forall m alpha, hashnat m <> hashaddr alpha.
  19. Axiom hashnatpairdiscr : forall m h1 h2, hashnat m <> hashpair h1 h2.
  20. Axiom hashaddrpairdiscr : forall alpha h1 h2, hashaddr alpha <> hashpair h1 h2.
  21. Axiom hashval_ind : forall p:hashval -> Prop,
  22. (forall n, p (hashnat n)) ->
  23. (forall alpha, p (hashaddr alpha)) ->
  24. (forall h1, p h1 -> forall h2, p h2 -> p (hashpair h1 h2)) ->
  25. forall h, p h.
  26. End HashValsType.
  27. Module HashVals : HashValsType.
  28. Inductive hashval' : Type :=
  29. | hashnat' : nat -> hashval'
  30. | hashaddr' : addr -> hashval'
  31. | hashpair' : hashval' -> hashval' -> hashval'.
  32. Definition hashval := hashval'.
  33. Definition hashnat := hashnat'.
  34. Definition hashaddr := hashaddr'.
  35. Definition hashpair := hashpair'.
  36. Fixpoint hashval_eq_dec (h1 h2:hashval) : { h1 = h2 } + { h1 <> h2 }.
  37. destruct h1 as [n1|alpha1|h1a h1b]; destruct h2 as [n2|alpha2|h2a h2b]; try (right; discriminate).
  38. - destruct (eq_nat_dec n1 n2).
  39. + left. congruence.
  40. + right. congruence.
  41. - destruct (addr_eq_dec alpha1 alpha2).
  42. + left. congruence.
  43. + right. congruence.
  44. - destruct (hashval_eq_dec h1a h2a).
  45. + destruct (hashval_eq_dec h1b h2b).
  46. * left. congruence.
  47. * right. congruence.
  48. + right. congruence.
  49. Defined.
  50. Lemma hashnatinj : forall m n, hashnat m = hashnat n -> m = n.
  51. intros m n H. inversion H. reflexivity.
  52. Qed.
  53. Lemma hashaddrinj : forall alpha beta, hashaddr alpha = hashaddr beta -> alpha = beta.
  54. intros alpha beta H. inversion H. reflexivity.
  55. Qed.
  56. Lemma hashpairinj : forall h1a h1b h2a h2b, hashpair h1a h1b = hashpair h2a h2b -> h1a = h2a /\ h1b = h2b.
  57. intros h1a h1b h2a h2b H1. inversion H1. tauto.
  58. Qed.
  59. Lemma hashnataddrdiscr : forall m alpha, hashnat m <> hashaddr alpha.
  60. discriminate.
  61. Qed.
  62. Lemma hashnatpairdiscr : forall m h1 h2, hashnat m <> hashpair h1 h2.
  63. discriminate.
  64. Qed.
  65. Lemma hashaddrpairdiscr : forall alpha h1 h2, hashaddr alpha <> hashpair h1 h2.
  66. discriminate.
  67. Qed.
  68. Lemma hashval_ind : forall p:hashval -> Prop,
  69. (forall n, p (hashnat n)) ->
  70. (forall alpha, p (hashaddr alpha)) ->
  71. (forall h1, p h1 -> forall h2, p h2 -> p (hashpair h1 h2)) ->
  72. forall h, p h.
  73. exact hashval'_ind.
  74. Qed.
  75. End HashVals.
  76. Export HashVals.
  77. Lemma hashpair_neq_L h1 h2 : hashpair h1 h2 <> h1.
  78. revert h1 h2. apply (hashval_ind (fun h1 => forall h2, hashpair h1 h2 <> h1)).
  79. - intros n h2 H. symmetry in H. revert H. apply hashnatpairdiscr.
  80. - intros alpha h2 H. symmetry in H. revert H. apply hashaddrpairdiscr.
  81. - intros h1a IHa h1b IHb h2 H. apply hashpairinj in H. destruct H as [H _].
  82. revert H. apply IHa.
  83. Qed.
  84. Lemma hashpair_neq_R h1 h2 : hashpair h1 h2 <> h2.
  85. revert h2 h1. apply (hashval_ind (fun h2 => forall h1, hashpair h1 h2 <> h2)).
  86. - intros n h1 H. symmetry in H. revert H. apply hashnatpairdiscr.
  87. - intros alpha h1 H. symmetry in H. revert H. apply hashaddrpairdiscr.
  88. - intros h2a IHa h2b IHb h1 H. apply hashpairinj in H. destruct H as [_ H].
  89. revert H. apply IHb.
  90. Qed.
  91. Fixpoint hashlist (hl:list hashval) : hashval :=
  92. match hl with
  93. | h::hr => hashpair h (hashlist hr)
  94. | nil => hashnat 0
  95. end.
  96. Lemma hashlistinj : forall hl1 hl2, hashlist hl1 = hashlist hl2 -> hl1 = hl2.
  97. intros hl1. induction hl1 as [|h1 hr1 IH]; intros [|h2 hr2].
  98. - reflexivity.
  99. - simpl. intros H. exfalso. revert H. apply hashnatpairdiscr.
  100. - simpl. intros H. exfalso. symmetry in H. revert H. apply hashnatpairdiscr.
  101. - simpl. intros H3. apply hashpairinj in H3. destruct H3 as [H4 H5].
  102. subst h2. f_equal.
  103. apply IH. exact H5.
  104. Qed.
  105. Lemma hashmapinj {X} (f : X -> hashval) (l l': list X) :
  106. (forall x y, f x = f y -> x = y) ->
  107. map f l = map f l' -> l = l'.
  108. intros H1. revert l'. induction l as [|x l IH]; intros [|y l'].
  109. - tauto.
  110. - discriminate.
  111. - discriminate.
  112. - intros H2. inversion H2. f_equal.
  113. + revert H0. apply H1.
  114. + revert H3. apply IH.
  115. Qed.
  116. Definition hashopair (h1 h2:option hashval) : option hashval :=
  117. match h1,h2 with
  118. | None,None => None
  119. | Some h1,None => Some (hashpair (hashnat 0) h1)
  120. | None,Some h2 => Some (hashpair (hashnat 1) h2)
  121. | Some h1,Some h2 => Some (hashlist (hashnat 2::h1::h2::nil))
  122. end.
  123. Lemma hashopairinj h1a h1b h2a h2b : hashopair h1a h1b = hashopair h2a h2b -> h1a = h2a /\ h1b = h2b.
  124. destruct h1a as [h1a|]; destruct h2a as [h2a|];
  125. destruct h1b as [h1b|]; destruct h2b as [h2b|]; simpl; intros H; try (discriminate H).
  126. - inversion H. apply hashpairinj in H1. destruct H1 as [H2 H3]. apply hashpairinj in H3.
  127. destruct H3 as [H4 H5]. apply hashpairinj in H5. destruct H5 as [H6 H7].
  128. split; congruence.
  129. - inversion H. apply hashpairinj in H1. destruct H1 as [H2 H3]. apply hashnatinj in H2. discriminate H2.
  130. - inversion H. apply hashpairinj in H1. destruct H1 as [H2 H3]. apply hashnatinj in H2. discriminate H2.
  131. - inversion H. apply hashpairinj in H1. destruct H1 as [H2 H3].
  132. split; congruence.
  133. - inversion H. apply hashpairinj in H1. destruct H1 as [H2 H3]. apply hashnatinj in H2. discriminate H2.
  134. - inversion H. apply hashpairinj in H1. destruct H1 as [H2 H3]. apply hashnatinj in H2. discriminate H2.
  135. - inversion H. apply hashpairinj in H1. destruct H1 as [H2 H3]. apply hashnatinj in H2. discriminate H2.
  136. - inversion H. apply hashpairinj in H1. destruct H1 as [H2 H3]. apply hashnatinj in H2. discriminate H2.
  137. - inversion H. apply hashpairinj in H1. destruct H1 as [H2 H3].
  138. split; congruence.
  139. - split; reflexivity.
  140. Qed.
  141. Lemma hashopair_None_1 (h1 h2:option hashval) : hashopair h1 h2 = None -> h1 = None.
  142. destruct h1 as [h1|].
  143. - simpl. destruct h2; discriminate.
  144. - tauto.
  145. Qed.
  146. Lemma hashopair_None_2 (h1 h2:option hashval) : hashopair h1 h2 = None -> h2 = None.
  147. destruct h2 as [h2|].
  148. - destruct h1; discriminate.
  149. - tauto.
  150. Qed.
  151. Definition hashopair1 (h1:hashval) (h2:option hashval) : hashval :=
  152. match h2 with
  153. | Some h2 => hashlist (hashnat 2::h1::h2::nil)
  154. | None => hashpair (hashnat 0) h1
  155. end.
  156. Definition hashopair2 (h1:option hashval) (h2:hashval) : hashval :=
  157. match h1 with
  158. | Some h1 => hashlist (hashnat 2::h1::h2::nil)
  159. | None => hashpair (hashnat 1) h2
  160. end.
  161. Fixpoint ohashlist (hl:list hashval) : option hashval :=
  162. match hl with
  163. | nil => None
  164. | h::hr =>
  165. match ohashlist hr with
  166. | None => Some(hashpair (hashnat 3) h)
  167. | Some k => Some(hashpair (hashnat 4) (hashpair h k))
  168. end
  169. end.
  170. Lemma ohashlistinj : forall hl1 hl2, ohashlist hl1 = ohashlist hl2 -> hl1 = hl2.
  171. intros hl1. induction hl1 as [|h1 hr1 IH]; intros [|h2 hr2] H1.
  172. - reflexivity.
  173. - exfalso. simpl in H1. destruct (ohashlist hr2); discriminate H1.
  174. - exfalso. simpl in H1. destruct (ohashlist hr1); discriminate H1.
  175. - simpl in H1.
  176. destruct (ohashlist hr1) as [k1|] eqn:E1; destruct (ohashlist hr2) as [k2|] eqn:E2.
  177. + inversion H1.
  178. apply hashpairinj in H0. destruct H0 as [_ H0].
  179. apply hashpairinj in H0. destruct H0 as [H0 H2].
  180. subst h2. f_equal. apply IH. congruence.
  181. + exfalso. inversion H1.
  182. apply hashpairinj in H0. destruct H0 as [H0 _].
  183. apply hashnatinj in H0. omega.
  184. + exfalso. inversion H1.
  185. apply hashpairinj in H0. destruct H0 as [H0 _].
  186. apply hashnatinj in H0. omega.
  187. + inversion H1.
  188. apply hashpairinj in H0. destruct H0 as [_ H0].
  189. subst h2. f_equal. apply IH. congruence.
  190. Qed.
  191. Inductive subh (h:hashval) : hashval -> Prop :=
  192. | subh_L h' : subh h (hashpair h h')
  193. | subh_R h' : subh h (hashpair h' h)
  194. | subh_PL h1 h2 : subh h h1 -> subh h (hashpair h1 h2)
  195. | subh_PR h1 h2 : subh h h2 -> subh h (hashpair h1 h2)
  196. .
  197. Lemma subh_hashlist h hl : In h hl -> subh h (hashlist hl).
  198. induction hl.
  199. - simpl; tauto.
  200. - intros [H1|H1]; simpl.
  201. + subst a. apply subh_L.
  202. + apply subh_PR. now apply IHhl.
  203. Qed.
  204. Lemma subh_tra h1 h2 h3 : subh h1 h2 -> subh h2 h3 -> subh h1 h3.
  205. intros H. revert h3. induction H as [h2|h2|h2 h3 H1 IH1|h2 h3 H1 IH1].
  206. - intros h3 H. induction H as [h3|h3|h3 h4 H2 IH2|h3 h4 H2 IH2].
  207. + apply subh_PL. apply subh_L.
  208. + apply subh_PR. apply subh_L.
  209. + now apply subh_PL.
  210. + now apply subh_PR.
  211. - intros h3 H. induction H as [h3|h3|h3 h4 H2 IH2|h3 h4 H2 IH2].
  212. + apply subh_PL. apply subh_R.
  213. + apply subh_PR. apply subh_R.
  214. + now apply subh_PL.
  215. + now apply subh_PR.
  216. - intros h4 H. induction H as [h4|h4|h4 h5 H2 IH2|h4 h5 H2 IH2].
  217. + apply IH1. apply subh_PL. apply subh_L.
  218. + apply IH1. apply subh_PR. apply subh_L.
  219. + now apply subh_PL.
  220. + now apply subh_PR.
  221. - intros h4 H. induction H as [h4|h4|h4 h5 H2 IH2|h4 h5 H2 IH2].
  222. + apply IH1. apply subh_PL. apply subh_R.
  223. + apply IH1. apply subh_PR. apply subh_R.
  224. + now apply subh_PL.
  225. + now apply subh_PR.
  226. Qed.
  227. Lemma subh_irrefl h : ~ subh h h.
  228. revert h. apply hashval_ind.
  229. - intros n H. inversion H.
  230. + symmetry in H1. revert H1. apply hashnatpairdiscr.
  231. + symmetry in H1. revert H1. apply hashnatpairdiscr.
  232. + symmetry in H0. revert H0. apply hashnatpairdiscr.
  233. + symmetry in H0. revert H0. apply hashnatpairdiscr.
  234. - intros alpha H. inversion H.
  235. + symmetry in H1. revert H1. apply hashaddrpairdiscr.
  236. + symmetry in H1. revert H1. apply hashaddrpairdiscr.
  237. + symmetry in H0. revert H0. apply hashaddrpairdiscr.
  238. + symmetry in H0. revert H0. apply hashaddrpairdiscr.
  239. - intros h1 IH1 h2 IH2 H. inversion H.
  240. + revert H1. apply hashpair_neq_L.
  241. + revert H1. apply hashpair_neq_R.
  242. + apply IH1. apply subh_tra with (h2 := (hashpair h1 h2)).
  243. * apply subh_L.
  244. * apply hashpairinj in H0. destruct H0 as [H0a H0b]. congruence.
  245. + apply IH2. apply subh_tra with (h2 := (hashpair h1 h2)).
  246. * apply subh_R.
  247. * apply hashpairinj in H0. destruct H0 as [H0a H0b]. congruence.
  248. Qed.
  249. Lemma subh_asym h h' : subh h h' -> ~ subh h' h.
  250. intros H1 H2. apply (subh_irrefl h).
  251. now apply subh_tra with (h2 := h').
  252. Qed.