forked from BlockstreamResearch/simplicity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenRustJets.hs
More file actions
466 lines (418 loc) · 15.7 KB
/
GenRustJets.hs
File metadata and controls
466 lines (418 loc) · 15.7 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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
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
301
302
303
304
305
306
307
308
309
310
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
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
{-# LANGUAGE OverloadedStrings #-}
module GenRustJets where
import Data.Char (toLower)
import Data.Foldable (toList)
import Data.Function (on)
import Data.Functor.Fixedpoint (Fix(..))
import Data.List (sortBy)
import Data.List.Split (chunksOf)
import Data.Maybe (fromMaybe)
import qualified Data.Map as Map
import Numeric (showHex)
import Prettyprinter ( Doc, (<+>), braces, comma, dquotes, fillSep, line, nest, parens, pretty, punctuate, semi, tupled, vsep
, SimpleDocStream, LayoutOptions(..), PageWidth(..), defaultLayoutOptions, layoutPretty
)
import Prettyprinter.Render.Text (renderIO)
import System.IO (IOMode(WriteMode), withFile)
import NameWrangler
import qualified Simplicity.Bitcoin.Jets as Bitcoin
import qualified Simplicity.Bitcoin.Term as Bitcoin
import qualified Simplicity.CoreJets as Core
import Simplicity.CoreJets (CoreJet)
import Simplicity.Digest
import qualified Simplicity.Elements.Jets as Elements
import qualified Simplicity.Elements.Term as Elements
import Simplicity.MerkleRoot
import Simplicity.Serialization
import Simplicity.Tree
import Simplicity.Ty
import Simplicity.Weight
x <-> y = x <> line <> y
nestBraces x = braces (nest 4 (line <> x) <> line)
data JetModule = CoreModule | BitcoinModule | ElementsModule
deriving Eq
data JetData x y = JetData { jetName :: String
, jetCMR :: CommitmentRoot x y
, jetModule :: JetModule
, jetCost :: Weight
}
sortJetName = sortBy (compare `on` name)
where
name (SomeArrow j) = jetName j
cJetName = lowerSnakeCase . jetName
coreJetData :: (TyC x, TyC y) => CoreJet x y -> JetData x y
coreJetData jet = JetData { jetName = mkName jet
, jetCMR = cmr
, jetModule = CoreModule
, jetCost = cost
}
where
cmr = Bitcoin.asJet (Bitcoin.CoreJet jet)
cost = Bitcoin.jetCost (Bitcoin.CoreJet jet)
elementsJetData :: (TyC x, TyC y) => Elements.JetType x y -> JetData x y
elementsJetData jet = JetData { jetName = mkName jet
, jetCMR = Elements.asJet jet
, jetModule = jetModule
, jetCost = Elements.jetCost jet
}
where
jetModule | Elements.CoreJet _ <- jet = CoreModule
| otherwise = ElementsModule
bitcoinJetData :: (TyC x, TyC y) => Bitcoin.JetType x y -> JetData x y
bitcoinJetData jet = JetData { jetName = mkName jet
, jetCMR = Bitcoin.asJet jet
, jetModule = jetModule
, jetCost = Bitcoin.jetCost jet
}
where
jetModule | Bitcoin.CoreJet _ <- jet = CoreModule
| otherwise = BitcoinModule
data Module = Module { moduleName :: Maybe String
, moduleCodes :: BinTree (SomeArrow JetData)
}
moduleJets :: Module -> [SomeArrow JetData]
moduleJets = sortJetName . toList . moduleCodes
rustModuleName = fromMaybe "Core" . moduleName
lowerRustModuleName = map toLower . rustModuleName
coreModule :: Module
coreModule = Module Nothing (someArrowMap coreJetData <$> (treeEvalBitStream Core.getJetBit))
-- Take Right is used to drop the (infinite) branch of constant word jets.
takeRight (Branch _ r) = r
elementsModule :: Module
elementsModule = Module (Just "Elements") (someArrowMap elementsJetData <$> takeRight (treeEvalBitStream Elements.getJetBit))
bitcoinModule :: Module
bitcoinModule = Module (Just "Bitcoin") (someArrowMap bitcoinJetData <$> takeRight (treeEvalBitStream Bitcoin.getJetBit))
data CompactTy = CTyOne
| CTyWord Int
| CTyMaybe CompactTy
| CTySum CompactTy CompactTy
| CTyProd CompactTy CompactTy
deriving (Eq, Ord)
compactTy :: Ty -> CompactTy
compactTy = memoCataTy go
where
go One = CTyOne
go (Sum CTyOne CTyOne) = CTyWord 1
go (Sum CTyOne y) = CTyMaybe y
go (Sum x y) = CTySum x y
go (Prod (CTyWord wx) (CTyWord wy)) | wx == wy = CTyWord (wx + wy)
go (Prod x y) = CTyProd x y
compactRustName :: CompactTy -> ShowS
compactRustName s = showString "b\"" . go s . showString "\""
where
go CTyOne = showString "1"
go (CTyWord 1) = showString "2"
go (CTyWord 32) = showString "i"
go (CTyWord 64) = showString "l"
go (CTyWord 256) = showString "h"
go (CTyWord n) | even n = let rec = go (CTyWord (n `div` 2)) in showString "*" . rec . rec
go (CTyMaybe x) = showString "+1" . go x
go (CTySum x y) = showString "+" . go x . go y
go (CTyProd x y) = showString "*" . go x . go y
showRustHash :: Hash256 -> Doc a
showRustHash h = fillSep $ ((<> comma) . format <$> chunksOf 2 str_h)
where
format x = pretty $ "0x" ++ x
str_h = padding ++ text
where
padding = replicate (64 - length text) '0'
text = showHex (integerHash256 h) ""
rustJetCmr :: Module -> Doc a
rustJetCmr mod = vsep $
[ nest 4 (vsep ("fn cmr(&self) -> Cmr {" :
-- Temporary if statement until Bitcoin Jets have weight costs assigned to them
-- See Haskell/Simplicity/Bitcoin/Jets.hs: jetCost (BitcoinJet jt) = error "Simplicity.Bitcoin.Jets.jetCost: :TODO: Implement jets for Bitcoin and benchmark them."
if Just "Bitcoin" == moduleName mod
then ["unimplemented!(\"Bitcoin jet CMRs weights have not yet been implemented.\")"]
else
[ nest 4 (vsep ("let bytes = match self {" :
map (<>comma)
[ nest 4 (vsep
[ pretty modname <> "::" <> pretty (jetName jet) <+> "=> ["
, showRustHash (commitmentRoot (jetCMR jet))
]) <-> "]"
| (SomeArrow jet) <- moduleJets mod
]))
, "};"
, mempty
, "Cmr(Midstate(bytes))"
]))
, "}"
]
where
modname = rustModuleName mod
rustJetTy fname getTy mod = vsep $
[ nest 4 (vsep (pretty ("fn "++fname++"(&self) -> TypeName {") :
[ nest 4 (vsep ("let name: &'static [u8] = match self {" :
map (<>comma)
[ pretty modname <> "::" <> pretty (jetName jet) <+> "=>" <+>
pretty (compactRustName (compactTy (getTy j)) "")
| j@(SomeArrow jet) <- moduleJets mod
]))
, "};"
, mempty
, "TypeName(name)"
]))
, "}"
]
where
modname = rustModuleName mod
rustJetSourceTy :: Module -> Doc a
rustJetSourceTy = rustJetTy "source_ty" (\(SomeArrow jet) -> unreflect (fst (reifyArrow jet)))
rustJetTargetTy :: Module -> Doc a
rustJetTargetTy = rustJetTy "target_ty" (\(SomeArrow jet) -> unreflect (snd (reifyArrow jet)))
rustJetPtr :: Module -> Doc a
rustJetPtr mod = vsep $
[ nest 4 (vsep ("fn c_jet_ptr(&self) -> &dyn Fn(&mut CFrameItem, CFrameItem, &Self::CJetEnvironment) -> bool {" :
if modname == "Bitcoin"
then ["unimplemented!(\"Bitcoin jets have not yet been implemented.\")"]
else [ nest 4 (vsep ("match self {" :
map (<>comma)
[ pretty modname <> "::" <> pretty (jetName jet) <+> "=>" <+>
pretty ("&simplicity_sys::c_jets::jets_wrapper::"++cJetName jet)
| SomeArrow jet <- moduleJets mod
]))
, "}"
]))
, "}"
]
where
modname = rustModuleName mod
rustJetEncode :: Module -> Doc a
rustJetEncode mod =
"fn encode<W: Write>(&self, w: &mut BitWriter<W>) -> std::io::Result<usize>" <+>
nestBraces ("let (n, len) = match self" <+>
nestBraces (vsep (foldMapWithPath item (moduleCodes mod))) <> semi <-> line <> "w.write_bits_be(n, len)")
where
item path (SomeArrow jet) = [pretty (rustModuleName mod ++ "::" ++ jetName jet) <+> "=>"
<+> tupled [pretty (code path 0 :: Int), pretty (length path)] <> comma]
code [] n = n
code (b : l) n = code l (2*n + if b then 1 else 0)
rustJetDecode :: Module -> Doc a
rustJetDecode mod =
"fn decode<I: Iterator<Item = u8>>(bits: &mut BitIter<I>) -> Result<Self, decode::Error>" <+>
nestBraces ("decode_bits!(bits," <+> braces (docTree (moduleCodes mod)) <> ")")
where
docTree Dead = mempty
docTree (Leaf (SomeArrow jet)) = pretty (rustModuleName mod ++ "::" ++ jetName jet)
docTree (Branch l r) = nest 4
( line <> ("0" <+> "=>" <+> braces (docTree l)) <> comma
<-> ("1" <+> "=>" <+> braces (docTree r))
) <> line
rustJetCost :: Module -> Doc a
rustJetCost mod = vsep $
[ nest 4 (vsep ("fn cost(&self) -> Cost {" :
if modname == "Bitcoin"
then ["unimplemented!(\"Unspecified cost of Bitcoin jets\")"]
else [ nest 4 (vsep ("match self {" :
map (<>comma)
[ pretty modname <> "::" <> pretty (jetName jet) <+> "=>" <+>
"Cost::from_milliweight(" <> (pretty . milliWeight $ jetCost jet) <> ")"
| SomeArrow jet <- moduleJets mod
]))
, "}"
]))
, "}"
]
where
modname = rustModuleName mod
rustJetImpl :: Module -> Doc a
rustJetImpl mod = vsep $
[ nest 4 (vsep $ punctuate line
["impl Jet for" <+> pretty modname <+> "{"
, env
, rustJetCmr mod
, rustJetSourceTy mod
, rustJetTargetTy mod
, rustJetEncode mod
, rustJetDecode mod
, rustJetPtr mod
, rustJetCost mod
])
, "}"
]
where
modname = rustModuleName mod
env = vsep
[ pretty $ "type Environment = "++env++";"
, pretty $ "type CJetEnvironment = "++cEnv++";"
, ""
, pretty $ "fn c_jet_env("++envArg++": &Self::Environment) -> &Self::CJetEnvironment {"
, pretty $ " "++envBody
, "}"
]
where
env | Nothing <- moduleName mod = "()"
| Just "Elements" == moduleName mod = "ElementsEnv<std::sync::Arc<elements::Transaction>>"
| Just name <- moduleName mod = name ++ "Env"
cEnv | Just "Elements" == moduleName mod = "CElementsTxEnv"
| otherwise = "()"
envArg | Just "Bitcoin" == moduleName mod = "_env"
| otherwise = "env"
envBody | Nothing == moduleName mod = "env"
| Just "Bitcoin" == moduleName mod = "unimplemented!(\"Unspecified CJetEnvironment for Bitcoin jets\")"
| otherwise = "env.c_tx_env()"
rustJetEnum :: Module -> Doc a
rustJetEnum mod = vsep
[ "/// The" <+> pretty (rustModuleName mod) <+> "jet family."
, "#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]"
, nest 4 $ vsep $
("pub enum" <+> pretty (rustModuleName mod) <+> "{") :
[ pretty (jetName jet) <> comma | (SomeArrow jet) <- moduleJets mod ]
, "}"
, ""
, nest 4 $ vsep $
("impl" <+> pretty (rustModuleName mod) <+> "{") :
("/// Array of all" <+> pretty (rustModuleName mod) <+> "jets.") :
[ nest 4 $ vsep $
("pub const ALL: [Self;" <+> pretty (length $ moduleJets mod) <> "] = [") :
[ "Self::" <> (pretty $ jetName jet) <> comma | (SomeArrow jet) <- moduleJets mod ]
, "];"
]
, "}"
]
rustJetDisplay :: Module -> Doc a
rustJetDisplay mod =
"impl fmt::Display for" <+> pretty modname <+>
nestBraces ("fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result" <+>
nestBraces ("match self" <+>
nestBraces (vsep (
map (<>comma)
[ pretty modname <> "::" <> pretty (jetName jet) <+> "=> f.write_str" <> (parens . dquotes . pretty $ cJetName jet)
| SomeArrow jet <- moduleJets mod
]))
)
)
where
modname = rustModuleName mod
rustJetFromStr :: Module -> Doc a
rustJetFromStr mod =
"impl str::FromStr for" <+> pretty modname <+>
nestBraces (vsep
[ "type Err = crate::Error;"
, mempty
, ("fn from_str(s: &str) -> Result<Self, Self::Err>" <+>
nestBraces ("match s" <+>
nestBraces (vsep (
map (<> comma)
([ dquotes (pretty (cJetName jet)) <+> "=> Ok" <> parens (pretty modname <> "::" <> pretty (jetName jet))
| SomeArrow jet <- moduleJets mod
] ++ [ "x => Err(crate::Error::InvalidJetName(x.to_owned()))" ]
)))
))
])
where
modname = rustModuleName mod
rustHeader :: Doc a
rustHeader = "/* This file has been automatically generated. */"
rustImports :: Module -> Doc a
rustImports mod = vsep (map (<> semi)
([ "use crate::jet::type_name::TypeName"
, "use crate::jet::Jet"
, "use crate::merkle::cmr::Cmr"
, "#[allow(unused_imports)]"
, "use crate::decode_bits"
, "use crate::{decode, BitIter, BitWriter}"
, "use crate::analysis::Cost"
, "use hashes::sha256::Midstate"
, "use simplicity_sys::CFrameItem"
, "use std::io::Write"
, "use std::{fmt, str}"
] ++ envImports))
where
envImports | Nothing == moduleName mod = []
| Just "Bitcoin" == moduleName mod = ["use crate::jet::bitcoin::BitcoinEnv"]
| Just name <- moduleName mod =
[ pretty $ "use crate::jet::"++map toLower name++"::"++name++"Env"
, pretty $ "use simplicity_sys::C"++name++"TxEnv"
]
rustJetDoc :: Module -> SimpleDocStream a
rustJetDoc mod = layoutPretty layoutOptions $ vsep (map (<> line)
[ rustHeader
, rustImports mod
, rustJetEnum mod
, rustJetImpl mod
, rustJetDisplay mod
, rustJetFromStr mod
])
rustFFIImports :: Doc a
rustFFIImports = vsep (map (<> semi)
[ "use crate::ffi::c_void"
, "use crate::{CElementsTxEnv, CFrameItem}"
])
rustFFISigs :: Module -> Doc a
rustFFISigs mod = vsep
[ nest 4 $ vsep $
"extern \"C\" {" :
(declaration <$> moduleJets mod)
, "}"
]
where
declaration (SomeArrow jet) = (<> semi) . vsep $ pretty <$>
[ linkName
, signature
]
where
linkName = "#[link_name = \"c_"++cJetName jet++"\"]"
signature = "pub fn "++cJetName jet++"(dst: *mut CFrameItem, src: *const CFrameItem, env: *const "++envType++") -> bool"
envType | CoreModule <- jetModule jet = "c_void"
| ElementsModule <- jetModule jet = "CElementsTxEnv"
rustFFIDoc :: Module -> SimpleDocStream a
rustFFIDoc mod = layoutPretty layoutOptions $ vsep (map (<> line)
[ rustHeader
, rustFFIImports
, rustFFISigs mod
])
rustWrapperImports :: Doc a
rustWrapperImports = vsep (map (<> semi)
[ "use crate::{CElementsTxEnv, CFrameItem}"
, "use super::elements_ffi"
])
rustWrappers :: Module -> Doc a
rustWrappers mod = vsep ((<> line) . wrapper <$> moduleJets mod)
where
wrapper (SomeArrow jet) = vsep
[ nest 4 $ vsep
[ pretty $ "pub fn "++cJetName jet++templateParam++"(dst: &mut CFrameItem, src: CFrameItem, "++envParam++") -> bool {"
, pretty $ "unsafe { "++lowerRustModuleName mod++"_ffi::"++cJetName jet++"(dst, &src, "++envArg++") }"
]
, "}"
]
where
templateParam | CoreModule <- jetModule jet = "<T>"
| otherwise = ""
envParam | CoreModule <- jetModule jet = "_env: &T"
| ElementsModule <- jetModule jet = "env: &CElementsTxEnv"
envArg | CoreModule <- jetModule jet = "std::ptr::null()"
| ElementsModule <- jetModule jet = "env"
rustWrapperDoc :: Module -> SimpleDocStream a
rustWrapperDoc mod = layoutPretty layoutOptions $ vsep (map (<> line)
[ rustHeader
, rustWrapperImports
, rustWrappers mod
])
cWrapperImports :: Doc a
cWrapperImports = vsep
[ "#include \"simplicity/elements/elementsJets.h\""
, "#include \"simplicity/simplicity_assert.h\""
, "#include \"wrapper.h\""
]
cWrappers :: Module -> Doc a
cWrappers mod = vsep (map wrapper $ moduleJets mod)
where
wrapper (SomeArrow jet) = pretty $ "WRAP_("++cJetName jet++")"
cWrapperDoc :: Module -> SimpleDocStream a
cWrapperDoc mod = layoutPretty layoutOptions $ vsep (map (<> line)
[ rustHeader -- also works for C
, cWrapperImports
, cWrappers mod
])
renderFile name doc = withFile name WriteMode (\h -> renderIO h doc)
main = do
renderFile "core.rs" (rustJetDoc coreModule)
renderFile "elements.rs" (rustJetDoc elementsModule)
renderFile "bitcoin.rs" (rustJetDoc bitcoinModule)
renderFile "jets_ffi.rs" (rustFFIDoc elementsModule)
renderFile "jets_wrapper.rs" (rustWrapperDoc elementsModule)
renderFile "jets_wrapper.c" (cWrapperDoc elementsModule)
layoutOptions = LayoutOptions { layoutPageWidth = AvailablePerLine 100 1 }