6.5610-project

Cryptography final project

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  69. 69
  70. 70
  71. 71
  72. 72
  73. 73
  74. 74
  75. 75
  76. 76
  77. 77
  78. 78
  79. 79
  80. 80
  81. 81
  82. 82
  83. 83
  84. 84
  85. 85
  86. 86
  87. 87
  88. 88
  89. 89
  90. 90
  91. 91
  92. 92
  93. 93
  94. 94
  95. 95
  96. 96
  97. 97
  98. 98
  99. 99
  100. 100
  101. 101
  102. 102
  103. 103
  104. 104
  105. 105
  106. 106
  107. 107
  108. 108
  109. 109
  110. 110
  111. 111
  112. 112
  113. 113
  114. 114
  115. 115
  116. 116
  117. 117
  118. 118
  119. 119
  120. 120
  121. 121
  122. 122
  123. 123
  124. 124
  125. 125
  126. 126
  127. 127
  128. 128
  129. 129
  130. 130
  131. 131
  132. 132
  133. 133
  134. 134
  135. 135
  136. 136
  137. 137
  138. 138
  139. 139
  140. 140
  141. 141
  142. 142
  143. 143
  144. 144
  145. 145
  146. 146
  147. 147
  148. 148
  149. 149
  150. 150
  151. 151
  152. 152
  153. 153
  154. 154
  155. 155
  156. 156
  157. 157
  158. 158
  159. 159
  160. 160
  161. 161
  162. 162
  163. 163
  164. 164
  165. 165
  166. 166
  167. 167
  168. 168
  169. 169
  170. 170
  171. 171
  172. 172
  173. 173
  174. 174
  175. 175
  176. 176
  177. 177
  178. 178
  179. 179
  180. 180
  181. 181
  182. 182
  183. 183
  184. 184
  185. 185
  186. 186
  187. 187
  188. 188
  189. 189
  190. 190
  191. 191
  192. 192
  193. 193
  194. 194
  195. 195
  196. 196
  197. 197
  198. 198
  199. 199
  200. 200
  201. 201
  202. 202
  203. 203
  204. 204
  205. 205
  206. 206
  207. 207
  208. 208
  209. 209
  210. 210
  211. 211
  212. 212
  213. 213
  214. 214
  215. 215
  216. 216
  217. 217
  218. 218
  219. 219
  220. 220
  221. 221
  222. 222
  223. 223
  224. 224
  225. 225
  226. 226
  227. 227
  228. 228
  229. 229
  230. 230
  231. 231
  232. 232
  233. 233
  234. 234
  235. 235
  236. 236
  237. 237
  238. 238
  239. 239
  240. 240
  241. 241
  242. 242
  243. 243
  244. 244
  245. 245
  246. 246
  247. 247
  248. 248
  249. 249
  250. 250
  251. 251
  252. 252
  253. 253
  254. 254
  255. 255
  256. 256
  257. 257
  258. 258
  259. 259
  260. 260
  261. 261
  262. 262
  263. 263
  264. 264
  265. 265
  266. 266
  267. 267
  268. 268
  269. 269
  270. 270
  271. 271
  272. 272
  273. 273
  274. 274
  275. 275
  276. 276
  277. 277
  278. 278
  279. 279
  280. 280
  281. 281
  282. 282
  283. 283
  284. 284
  285. 285
  286. 286
  287. 287
  288. 288
  289. 289
  290. 290
  291. 291
  292. 292
  293. 293
  294. 294
  295. 295
  296. 296
  297. 297
  298. 298
  299. 299
  300. 300
  301. 301
  302. 302
  303. 303
  304. 304
  305. 305
  306. 306
  307. 307
  308. 308
  309. 309
  310. 310
  311. 311
  312. 312
  313. 313
  314. 314
  315. 315
  316. 316
  317. 317
  318. 318
  319. 319
  320. 320
  321. 321
  322. 322
  323. 323
  324. 324
  325. 325
  326. 326
  327. 327
  328. 328
  329. 329
  330. 330
  331. 331
  332. 332
  333. 333
  334. 334
  335. 335
  336. 336
  337. 337
  338. 338
  339. 339
  340. 340
  341. 341
  342. 342
  343. 343
  344. 344
  345. 345
  346. 346
  347. 347
  348. 348
  349. 349
  350. 350
  351. 351
  352. 352
  353. 353
  354. 354
  355. 355
  356. 356
  357. 357
  358. 358
  359. 359
  360. 360
  361. 361
  362. 362
  363. 363
  364. 364
  365. 365
  366. 366
  367. 367
  368. 368
  369. 369
  370. 370
  371. 371
  372. 372
  373. 373
  374. 374
  375. 375
  376. 376
  377. 377
  378. 378
  379. 379
import Cert

/-! # Lean-side type checker

A reimplementation of `check` from `Dependent.lean` that works directly on
indexed `Cert.Node`s.  Shares the `EvalM` monad with `evalM`, so memoised
eval lookups carry over.

This is a *sanity oracle* — run it on a proof before dumping.  If `checkLean`
returns `true`, the dump will type-check in Lurk; if it returns `false`, the
proof is malformed and you'll get much better debugging info here than from
the Lurk verifier's eventual `assert failed`.

Mirrors the Lurk `check-cert` rule-by-rule so both must agree.
-/

namespace Cert

/-- Compare two **already-evaluated** type indices for equality, with universe
cumulativity.  Because the term table is hash-consed, normal-form equality
reduces to index equality. -/
partial def cumeqLean (a b : Nat) : EvalM Bool := do
  let ae  evalM a
  let be  evalM b
  let aNd  getNode ae
  let bNd  getNode be
  if aNd.tag == .typ && bNd.tag == .typ then
    return aNd.payload  bNd.payload
  else
    return ae == be

/-- The type-checker.  `env` is a list of *already-evaluated* type indices.
`t` is the term to check, `τ` is its expected type (also expected to be in
normal form).  Returns `true` iff well-typed.  Uses `evalM` whenever a sub-
expression must be normalised. -/
partial def checkLean (env : List Nat) (t τ : Nat) : EvalM Bool := do
  let nd  getNode t
  match nd.tag with
  | .var =>
    let x := nd.payload
    if h : x < env.length then
      let envType := env[x]
      let lifted  liftM (incrIdx (x + 1) envType)
      let liftedEv  evalM lifted
      cumeqLean liftedEv τ
    else return false
  | .lam =>
    let τNd  getNode τ
    if τNd.tag == .fn then
      let α := τNd.children[0]!
      let β := τNd.children[1]!
      let b := nd.children[0]!
      let αEv  evalM α
      let βEv  evalM β
      checkLean (αEv :: env) b βEv
    else return false
  | .app =>
    let f := nd.children[0]!
    let φ := nd.children[1]!
    let a := nd.children[2]!
    let φNd  getNode φ
    if φNd.tag == .fn then
      let α := φNd.children[0]!
      let β := φNd.children[1]!
      let αEv  evalM α
      let cf  checkLean env f φ
      let ca  checkLean env a αEv
      if cf && ca then
        let subRes  liftM (subIdx a β)
        let subEv  evalM subRes
        cumeqLean subEv τ
      else return false
    else return false
  | .fn =>
    let τNd  getNode τ
    if τNd.tag == .typ then
      let α := nd.children[0]!
      let β := nd.children[1]!
      let   checkLean env α τ
      if  then
        let αEv  evalM α
        checkLean (αEv :: env) β τ
      else return false
    else return false
  | .prod =>
    let τNd  getNode τ
    if τNd.tag == .typ then
      let α := nd.children[0]!
      let β := nd.children[1]!
      let   checkLean env α τ
      if  then
        -- β must check against (E[α] ⇨ 𝒰) (dependent product constraint).
        let αEv  evalM α
        let u0Idx  liftM (typ 0)
        let auxFn  liftM (fn αEv u0Idx)
        let auxFnEv  evalM auxFn
        checkLean env β auxFnEv
      else return false
    else return false
  | .sum =>
    let τNd  getNode τ
    if τNd.tag == .typ then
      let α := nd.children[0]!
      let β := nd.children[1]!
      let   checkLean env α τ
      if  then checkLean env β τ else return false
    else return false
  | .eq =>
    let τNd  getNode τ
    if τNd.tag == .typ then
      let a := nd.children[0]!
      let aPrime := nd.children[1]!
      let α := nd.children[2]!
      let αEv  evalM α
      let ca  checkLean env a αEv
      if ca then
        let cap  checkLean env aPrime αEv
        if cap then checkLean env α τ else return false
      else return false
    else return false
  | .fls =>
    let τNd  getNode τ
    return (τNd.tag == .typ)
  | .typ =>
    let τNd  getNode τ
    if τNd.tag == .typ then
      return (nd.payload + 1)  τNd.payload
    else return false
  | .opq =>
    -- An opaque leaf — black-box reference to a separately verified theorem.
    -- Look it up in the builder's theoremAlist; if found, compare its
    -- registered type to τ via cumeq.
    let b  liftM (m := BuilderM) get
    match b.theoremAlist.find? (fun (op, _, _, _) => op == t) with
    | some (_, ty, _, _) =>
      let tyEv  evalM ty
      cumeqLean tyEv τ
    | none => return false
  | _ =>
    -- Built-in constant.  Look up its tag in `dbtypeAlist` (threaded
    -- through via the EvalState pseudo-field — keep this simple by
    -- passing the list explicitly via the lifted reader below).
    return false  -- handled by the wrapper `checkLeanWithDbtypes` instead

/-- Convenience wrapper: build a proof, run `checkLean`, return `(ok?, builder, evalState)`.
The builder/state is returned so the caller can also dump if desired.

If any opaque theorems are registered in the builder, each one's body is
checked against its claimed type before the main proof is checked. -/
def runCheck (build : BuilderM (Nat × Nat)) : (Bool × Builder × EvalState) :=
  let ((termIdx, typeIdx), b1) := build.run {}
  let prog : EvalM Bool := do
    let bld  liftM (m := BuilderM) get
    let mut allOk := true
    for (_, ty, proof, _) in bld.theoremAlist do
      if allOk then
        let τEv  evalM ty
        let r  checkLean [] proof τEv
        if !r then allOk := false
    if !allOk then return false
    let τEv  evalM typeIdx
    checkLean [] termIdx τEv
  let ((ok, evalState), b2) := (prog.run {}).run b1
  (ok, b2, evalState)

/-- A more thorough checker that mirrors the Lurk verifier's `check-cert`,
including dbtype lookup for built-in constants.  Threads `dbtypes` through
every recursive call. -/
partial def checkLeanD (dbtypes : List (Nat × Nat))
    (env : List Nat) (t τ : Nat) : EvalM Bool := do
  let nd  getNode t
  match nd.tag with
  | .var =>
    let x := nd.payload
    if h : x < env.length then
      let envType := env[x]
      let lifted  liftM (incrIdx (x + 1) envType)
      let liftedEv  evalM lifted
      cumeqLean liftedEv τ
    else return false
  | .lam =>
    let τNd  getNode τ
    if τNd.tag == .fn then
      let α := τNd.children[0]!
      let β := τNd.children[1]!
      let b := nd.children[0]!
      let αEv  evalM α
      let βEv  evalM β
      checkLeanD dbtypes (αEv :: env) b βEv
    else return false
  | .app =>
    let f := nd.children[0]!
    let φ := nd.children[1]!
    let a := nd.children[2]!
    let φNd  getNode φ
    if φNd.tag == .fn then
      let α := φNd.children[0]!
      let β := φNd.children[1]!
      let αEv  evalM α
      let cf  checkLeanD dbtypes env f φ
      let ca  checkLeanD dbtypes env a αEv
      if cf && ca then
        let subRes  liftM (subIdx a β)
        let subEv  evalM subRes
        cumeqLean subEv τ
      else return false
    else return false
  | .fn =>
    let τNd  getNode τ
    if τNd.tag == .typ then
      let α := nd.children[0]!
      let β := nd.children[1]!
      let   checkLeanD dbtypes env α τ
      if  then
        let αEv  evalM α
        checkLeanD dbtypes (αEv :: env) β τ
      else return false
    else return false
  | .prod =>
    let τNd  getNode τ
    if τNd.tag == .typ then
      let α := nd.children[0]!
      let β := nd.children[1]!
      let   checkLeanD dbtypes env α τ
      if  then
        let αEv  evalM α
        let u0Idx  liftM (typ 0)
        let auxFn  liftM (fn αEv u0Idx)
        let auxFnEv  evalM auxFn
        checkLeanD dbtypes env β auxFnEv
      else return false
    else return false
  | .sum =>
    let τNd  getNode τ
    if τNd.tag == .typ then
      let α := nd.children[0]!
      let β := nd.children[1]!
      let   checkLeanD dbtypes env α τ
      if  then checkLeanD dbtypes env β τ else return false
    else return false
  | .eq =>
    let τNd  getNode τ
    if τNd.tag == .typ then
      let a := nd.children[0]!
      let aPrime := nd.children[1]!
      let α := nd.children[2]!
      let αEv  evalM α
      let ca  checkLeanD dbtypes env a αEv
      if ca then
        let cap  checkLeanD dbtypes env aPrime αEv
        if cap then checkLeanD dbtypes env α τ else return false
      else return false
    else return false
  | .fls =>
    let τNd  getNode τ
    return (τNd.tag == .typ)
  | .typ =>
    let τNd  getNode τ
    if τNd.tag == .typ then
      return (nd.payload + 1)  τNd.payload
    else return false
  | .opq =>
    -- Opaque reference: look up registered type in builder's theoremAlist.
    let b  liftM (m := BuilderM) get
    match b.theoremAlist.find? (fun (op, _, _, _) => op == t) with
    | some (_, ty, _, _) =>
      let tyEv  evalM ty
      cumeqLean tyEv τ
    | none => return false
  | _ =>
    -- Built-in constant: dbtype lookup.
    let tag := nd.tag.toNat
    match dbtypes.find? (·.1 == tag) with
    | some (_, dbtIdx) =>
      let dbtEv  evalM dbtIdx
      cumeqLean dbtEv τ
    | none => return false

/-- Run `checkLeanD` over a translator-produced build. -/
def runCheckWithDbtypes (build : BuilderM (Nat × Nat × List (Nat × Nat))) :
    (Bool × Builder × EvalState × List (Nat × Nat)) :=
  let ((termIdx, typeIdx, dbtypes), b1) := build.run {}
  let prog : EvalM Bool := do
    let τEv  evalM typeIdx
    checkLeanD dbtypes [] termIdx τEv
  let ((ok, evalState), b2) := (prog.run {}).run b1
  (ok, b2, evalState, dbtypes)

/-- Debug variant of `checkLeanD` that prints the first failure encountered. -/
partial def checkLeanDebug (dbtypes : List (Nat × Nat))
    (env : List Nat) (t τ : Nat) : EvalM (Bool × String) := do
  let nd  getNode t
  match nd.tag with
  | .var =>
    let x := nd.payload
    if h : x < env.length then
      let envType := env[x]
      let lifted  liftM (incrIdx (x + 1) envType)
      let liftedEv  evalM lifted
      let ok  cumeqLean liftedEv τ
      if ok then return (true, "")
      else return (false, s!"var {x}: incr'd env entry #{liftedEv} ≠ τ #{τ}")
    else return (false, s!"var {x} out of bounds (env size {env.length})")
  | .lam =>
    let τNd  getNode τ
    if τNd.tag == .fn then
      let α := τNd.children[0]!
      let β := τNd.children[1]!
      let b := nd.children[0]!
      let αEv  evalM α
      let βEv  evalM β
      checkLeanDebug dbtypes (αEv :: env) b βEv
    else return (false, s!"lam #{t} but τ #{τ} has tag {τNd.tag.toNat}, not fn")
  | .app =>
    let f := nd.children[0]!
    let φ := nd.children[1]!
    let a := nd.children[2]!
    let φNd  getNode φ
    if φNd.tag == .fn then
      let α := φNd.children[0]!
      let β := φNd.children[1]!
      let αEv  evalM α
      let (cf, why)  checkLeanDebug dbtypes env f φ
      if !cf then return (false, s!"app #{t}: f #{f} doesn't match φ #{φ}: {why}")
      let (ca, why)  checkLeanDebug dbtypes env a αEv
      if !ca then return (false, s!"app #{t}: a #{a} doesn't match α #{αEv}: {why}")
      let subRes  liftM (subIdx a β)
      let subEv  evalM subRes
      let ok  cumeqLean subEv τ
      if ok then return (true, "")
      else return (false, s!"app #{t}: sub-eval #{subEv} ≠ τ #{τ}")
    else return (false, s!"app #{t}: φ #{φ} has tag {φNd.tag.toNat}, not fn")
  | .opq =>
    -- Opaque reference: look up registered type in builder's theoremAlist.
    let b  liftM (m := BuilderM) get
    match b.theoremAlist.find? (fun (op, _, _, _) => op == t) with
    | some (_, ty, _, name) =>
      let tyEv  evalM ty
      let ok  cumeqLean tyEv τ
      if ok then return (true, "")
      else return (false, s!"opaque #{t} ({name}): registered type #{tyEv} ≠ τ #{τ}")
    | none => return (false, s!"opaque #{t}: not in theorem-alist")
  | _ =>
    -- Built-in constant: dbtype lookup.
    let tag := nd.tag.toNat
    match dbtypes.find? (·.1 == tag) with
    | some (_, dbtIdx) =>
      let dbtEv  evalM dbtIdx
      let ok  cumeqLean dbtEv τ
      if ok then return (true, "")
      else return (false, s!"built-in #{t} (tag {tag}): dbtype #{dbtEv} ≠ τ #{τ}")
    | none =>
      -- For non-built-in: fall back to type-position checker via checkLeanD
      let ok  checkLeanD dbtypes env t τ
      if ok then return (true, "")
      else return (false, s!"non-app/var/lam #{t} (tag {tag}): rejected by checkLeanD vs τ #{τ}")

def runCheckDebug (build : BuilderM (Nat × Nat × List (Nat × Nat))) :
    (Bool × String × Builder) :=
  let ((termIdx, typeIdx, dbtypes), b1) := build.run {}
  let prog : EvalM (Bool × String) := do
    -- Verify each registered theorem's body before the main term.
    let bld  liftM (m := BuilderM) get
    let mut firstFail : Option (Bool × String) := none
    for (_, ty, proof, name) in bld.theoremAlist do
      if firstFail.isNone then
        let τEv  evalM ty
        let res  checkLeanDebug dbtypes [] proof τEv
        if !res.1 then
          firstFail := some (false, s!"opaque theorem '{name}' body fails: {res.2}")
    match firstFail with
    | some r => return r
    | none =>
      let τEv  evalM typeIdx
      checkLeanDebug dbtypes [] termIdx τEv
  let (((ok, why), _evalState), b2) := (prog.run {}).run b1
  (ok, why, b2)

end Cert