-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.lean
More file actions
136 lines (112 loc) · 4.76 KB
/
Main.lean
File metadata and controls
136 lines (112 loc) · 4.76 KB
1
2
3
4
5
6
7
8
9
10
11
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
66
67
68
69
70
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
import FraudProof.DataStructures.Hash
import FraudProof.DataStructures.BTree
import FraudProof.DataStructures.MTree
import Batteries.Lean.IO.Process -- easy run process
import Mathlib.Control.Combinators -- mapM and stuff
------------------------------------------------------------
-- Relative path to python hash function.
-- def python : String
-- := "/home/mceresa/Research/WIP/LeanPOC/FraudProof/PY/venv/bin/python"
section PythonRelPath
open System
variable (projectRoot : System.FilePath)
def relPydir : System.FilePath
:= projectRoot.join $ mkFilePath ["PY"]
def relPython : System.FilePath
:= (relPydir projectRoot).join $ mkFilePath ["venv","bin","python"]
def relPyHash : System.FilePath
:= (relPydir projectRoot).join $ mkFilePath ["hashing.py"]
end PythonRelPath
-- File path definition plus some basic checks
def fileExistsOrError (fp : System.FilePath) : IO System.FilePath :=
Monad.cond fp.pathExists (return fp) ( throw $ IO.userError s!"File {fp.toString} not found :-(" )
def pythonFileP : IO System.FilePath := relPython <$> IO.currentDir
def hashFileP : IO System.FilePath := relPyHash <$> IO.currentDir
------------------------------------------------------------
-- Running pyHash script
def externalHashing' (str : String) : IO (Option String) :=
do
let python <- pythonFileP
let pyHash <- hashFileP
let res <- IO.Process.runCmdWithInput' python.toString (Array.mkArray2 pyHash.toString str)
if res.stderr != ""
then return none
else return (some res.stdout)
section PyHash
variable (python pyhash : String)
def externalHashing (str : String) : IO String :=
IO.Process.runCmdWithInput python (Array.mkArray2 pyhash str)
def combHashing (str1 str2 : String) : IO String :=
externalHashing python pyhash (str1.append str2)
-- Hash instances (flagged as unsafe)
unsafe instance ecck : Hash String String where
mhash str := match unsafeIO $ externalHashing python pyhash str with
| .error _ => ""
| .ok h => h
unsafe def unsafeExtComb (str1 str2 : String) : String
:= match unsafeIO (combHashing python pyhash str1 str2) with
| .error _ => ""
| .ok hash => hash
unsafe instance ecckMagma : HashMagma String where
comb := unsafeExtComb python pyhash
------------------------------------------------------------
-- * Laws
-- We only need laws to prove theorems.
--
-- All laws are assumed. Outside this work.
-- unsafe instance collfree : CollResistant String String where
-- noCollisions := sorry
-- unsafe instance magmaLaw : SLawFulHash String where
-- neqLeft := sorry
-- neqRight := sorry
------------------------------------------------------------
------------------------------------------------------------
-- * Small Demo with elements
-- .
-- Some elements, we can IO them.
-- unsafe def elemsHashes : List String := elements.map (ecck.mhash python pyhash)
-- unsafe def elemsTree : BTree String :=
-- match elemsHashes.fromList with
-- | none => sorry -- it's unsafe, I don't case about stuff anymore.
-- | some t => t
-- -- Get Merkle Tree
-- unsafe def elemsMTree : MTree String := hash_BTree elemsTree
------------------------------------------------------------
end PyHash
--------------------
def elements : List String := ["esto", "es", "una", "demo"]
-- Validity Function
def strAlphaNum : String -> Bool := flip String.all Char.isAlphanum
-- Helper
def someOrFail {α : Type}(ov : Option α)(errmsg : String) : IO α
:= match ov with
| none => throw $ IO.userError errmsg
| some v => return v
------------------------------------------------------------
-- Main entry point.
def main : IO Unit :=
do
IO.println "Hello Human?"
IO.println "This s a simple demo of our humble piece of software"
--------------------
-- Getting IO Hash functions.
--
let python <- pythonFileP >>= fileExistsOrError
let pyHash <- hashFileP >>= fileExistsOrError
let hash := externalHashing python.toString pyHash.toString
let magmaHash := combHashing python.toString pyHash.toString
--
IO.println s!"Elements of the tree :{elements}"
let encs <- Monad.mapM hash elements
IO.println s!"Hashed elements:{encs}"
let btree := encs.fromList
IO.println "Generated a balanced tree. We assumed both players know the tree."
let t <- someOrFail btree "Empty Tree" >>= hashM hash magmaHash
IO.println s!"And presents the following MTree root hash {t.hash}"
--------------------
-- Adding an extra elem that is not |strAlphanum| valid.
let inV := "notValid!" :: elements
let htree <- List.fromList <$> Monad.mapM hash inV
let t <- someOrFail htree "Empty Tree" >>= hashM hash magmaHash
IO.println s!"Propose {t.hash}"
------------------------------------------------------------