simplicity-lang 0.9.0

General purpose library for processing Simplicity programs
Documentation
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
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
// SPDX-License-Identifier: CC0-1.0

use crate::merkle::midstate::MidstateExt;
use hashes::sha256::Midstate;

super::impl_mr_type! {
    /// Type Merkle root
    ///
    /// A Merkle root that commits to a type's primitive (unit, sum, product)
    /// and recursively its sub-types.
    ///
    /// Uniquely identifies a type.
    #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
    pub struct Tmr([u8; 32]);
}

impl Tmr {
    #[rustfmt::skip]
    const UNIT_IV: Midstate = Midstate::new([
        0x50, 0xb3, 0x8c, 0xd7, 0x64, 0x75, 0xff, 0x89,
        0x29, 0x28, 0x8b, 0xfc, 0xd0, 0xd9, 0xdf, 0x0e,
        0x4a, 0x24, 0x1c, 0x0a, 0x57, 0x08, 0x57, 0x2a,
        0xd2, 0x64, 0x19, 0x2a, 0x4f, 0xe6, 0x7b, 0xee,
    ], 64);

    #[rustfmt::skip]
    const SUM_IV: Midstate = Midstate::new([
        0x92, 0x0c, 0xfd, 0x83, 0xcf, 0x96, 0xbb, 0x32,
        0x73, 0x17, 0x36, 0x0f, 0x6d, 0x3a, 0xd7, 0x60,
        0x1e, 0xef, 0x0a, 0x16, 0xdd, 0x53, 0x14, 0x6c,
        0x2e, 0x5d, 0xe3, 0x5f, 0x51, 0xef, 0x8d, 0xa4,
    ], 64);

    #[rustfmt::skip]
    const PROD_IV: Midstate = Midstate::new([
        0xae, 0x99, 0x2a, 0x3e, 0xe0, 0x71, 0x3b, 0xf6,
        0x19, 0x5d, 0x3b, 0xac, 0x1a, 0xf5, 0x05, 0xa7,
        0x29, 0xc1, 0x4d, 0x47, 0x95, 0x58, 0xf0, 0xd6,
        0x29, 0x96, 0x30, 0xf7, 0xfa, 0x97, 0x2e, 0xf8,
    ], 64);

    /// The TMRs of the types TWO^(2^n) for small values of n
    #[rustfmt::skip]
    pub const TWO_TWO_N: [Tmr; 32] = [
        Tmr([
            0x88, 0x5a, 0x22, 0xde, 0x3e, 0xdb, 0x3f, 0x40,
            0xdb, 0x06, 0x09, 0xc2, 0x40, 0x23, 0x30, 0x3f,
            0xec, 0x67, 0x87, 0xbe, 0x4d, 0x49, 0x21, 0xe4,
            0x5e, 0xe9, 0xd9, 0x83, 0xac, 0xb5, 0xd8, 0x9d,
        ]),
        Tmr([
            0x55, 0x22, 0xba, 0x1c, 0x92, 0x89, 0x4d, 0xf2,
            0x57, 0x23, 0xcf, 0xeb, 0x5a, 0xf2, 0x59, 0xf4,
            0xc5, 0x5a, 0x5c, 0xfc, 0x82, 0x00, 0xd5, 0x9b,
            0xad, 0x09, 0x64, 0x99, 0x7f, 0x9b, 0x2b, 0x04,
        ]),
        Tmr([
            0xfa, 0x28, 0xba, 0x9f, 0x0f, 0x07, 0xb4, 0xfa,
            0xe4, 0xdd, 0x86, 0x23, 0x61, 0x41, 0x12, 0xff,
            0x3b, 0xaf, 0xab, 0xe2, 0x9c, 0x41, 0x5d, 0x2e,
            0x0c, 0x1e, 0xd3, 0xc4, 0x13, 0x51, 0x28, 0xe0,
        ]),
        Tmr([
            0xd3, 0x43, 0xe4, 0xd5, 0xfa, 0xa4, 0xce, 0xa4,
            0x4b, 0xb7, 0x11, 0xef, 0x4b, 0x1b, 0x96, 0xb7,
            0xe0, 0xfb, 0x26, 0xcf, 0xa8, 0x5a, 0x96, 0x04,
            0x39, 0xba, 0x84, 0x58, 0xc0, 0xb6, 0xc5, 0xc3,
        ]),
        Tmr([
            0x62, 0x8c, 0x36, 0x1f, 0x09, 0xcb, 0x2e, 0x5b,
            0xaa, 0xd9, 0xc6, 0xb7, 0x26, 0xa9, 0xfa, 0x95,
            0xd1, 0xda, 0x67, 0x79, 0x7e, 0xe2, 0x4c, 0xcf,
            0x01, 0x41, 0x0c, 0x97, 0x93, 0x39, 0x95, 0x00,
        ]),
        Tmr([
            0xb1, 0xa9, 0xf9, 0x68, 0x78, 0x33, 0xdc, 0x44,
            0xbc, 0x8f, 0x17, 0xd3, 0xcb, 0xc2, 0x55, 0xb7,
            0xaa, 0x49, 0x9a, 0x78, 0xfe, 0xc8, 0x5c, 0xc9,
            0x8a, 0x0c, 0x33, 0x5c, 0x86, 0xed, 0x26, 0xe9,
        ]),
        Tmr([
            0xfd, 0x75, 0xbc, 0xd8, 0xcb, 0x55, 0xa5, 0xe4,
            0x24, 0xe0, 0x92, 0x88, 0xac, 0x8e, 0xb4, 0x29,
            0x42, 0x53, 0x4e, 0xc1, 0x09, 0x9b, 0x72, 0xbe,
            0xce, 0x0e, 0xa2, 0xc5, 0x47, 0x9b, 0xf3, 0xe7,
        ]),
        Tmr([
            0xe3, 0x94, 0xd2, 0xa0, 0x51, 0x6d, 0x73, 0x8d,
            0x6e, 0x04, 0x59, 0xb9, 0x1b, 0xb0, 0xdd, 0x47,
            0x1d, 0x0d, 0xaa, 0x77, 0x96, 0x71, 0xa5, 0x31,
            0x3b, 0x62, 0x14, 0x0e, 0xc7, 0x0a, 0x22, 0x6e,
        ]),
        Tmr([
            0xe4, 0xc4, 0x72, 0x8d, 0x87, 0x67, 0x49, 0xbe,
            0xbe, 0x4d, 0x62, 0x97, 0xdf, 0xad, 0xd4, 0x2c,
            0xe2, 0x68, 0x44, 0x50, 0x0d, 0x96, 0xc5, 0xe9,
            0x14, 0x61, 0x57, 0x50, 0x8e, 0xb9, 0x09, 0x8f,
        ]),
        Tmr([
            0x1a, 0xe5, 0x45, 0xb7, 0xc3, 0xfd, 0x85, 0xfe,
            0x81, 0x6c, 0x70, 0xea, 0x4c, 0x78, 0xb7, 0x41,
            0x9d, 0x8b, 0xe3, 0xc1, 0xfb, 0x3f, 0x72, 0x23,
            0xfa, 0x7d, 0x8d, 0x50, 0x36, 0x75, 0x51, 0x8f,
        ]),
        Tmr([
            0x6a, 0x9c, 0x72, 0x40, 0xe5, 0xd7, 0x1d, 0x5a,
            0x83, 0x30, 0xb3, 0xb3, 0xb6, 0x65, 0x0b, 0xc3,
            0x66, 0x6c, 0x3f, 0xef, 0x2e, 0x80, 0x3e, 0xd8,
            0x64, 0xf1, 0x2f, 0xcd, 0x8f, 0xea, 0xc1, 0xca,
        ]),
        Tmr([
            0x0a, 0x2a, 0x58, 0x7a, 0xeb, 0xc9, 0x6b, 0x0e,
            0x3e, 0x26, 0x01, 0x38, 0x2e, 0xb3, 0x2a, 0x9f,
            0x1e, 0x29, 0xc8, 0x50, 0x1c, 0xbd, 0xd8, 0x0e,
            0x87, 0x05, 0xc0, 0xe7, 0xd3, 0xf5, 0xb3, 0x4f,
        ]),
        Tmr([
            0xb3, 0xed, 0xb6, 0xfd, 0x83, 0x8e, 0xc4, 0x05,
            0x7b, 0x76, 0x8c, 0x86, 0xb0, 0xe8, 0xa9, 0x6d,
            0x1c, 0xe3, 0xf1, 0x69, 0x54, 0x31, 0x4d, 0xcd,
            0x3d, 0xbf, 0x72, 0x85, 0xeb, 0xa5, 0x63, 0xa9,
        ]),
        Tmr([
            0xd0, 0x47, 0x4c, 0x0c, 0x1e, 0x2c, 0x09, 0xd9,
            0xc5, 0xf0, 0x50, 0x98, 0x93, 0x57, 0xce, 0x71,
            0xab, 0x5d, 0x90, 0x4e, 0x8e, 0x2e, 0x82, 0x87,
            0x42, 0x86, 0xc0, 0xc2, 0x12, 0xde, 0x3e, 0x71,
        ]),
        Tmr([
            0x37, 0x92, 0x46, 0xdc, 0x86, 0x6d, 0x37, 0x0d,
            0xbf, 0xc9, 0x70, 0x4f, 0xbc, 0x57, 0xcd, 0x7a,
            0x6e, 0xb5, 0xed, 0xe7, 0x97, 0xa0, 0x0c, 0x4a,
            0x72, 0xb3, 0xb4, 0xa5, 0xd9, 0xf0, 0x9f, 0xe1,
        ]),
        Tmr([
            0x8a, 0x59, 0xe8, 0x3f, 0x43, 0xe2, 0xa1, 0xf9,
            0x07, 0xd3, 0xa2, 0x96, 0x67, 0x53, 0x33, 0x6a,
            0x1d, 0xa2, 0x0d, 0x23, 0xfe, 0x63, 0x63, 0x28,
            0x58, 0xf9, 0x4e, 0xc0, 0x7d, 0x19, 0x13, 0xd2,
        ]),
        Tmr([
            0x0f, 0x51, 0x95, 0x68, 0x9f, 0x55, 0x6d, 0x00,
            0xbe, 0x92, 0xfc, 0x2d, 0xc5, 0x7b, 0xcb, 0xb1,
            0x25, 0x62, 0x0f, 0xfc, 0x00, 0x96, 0x3c, 0x09,
            0x14, 0xb1, 0x96, 0x15, 0x1a, 0x77, 0x84, 0x1c,
        ]),
        Tmr([
            0x6d, 0x2a, 0x47, 0x99, 0xa1, 0x6e, 0xb6, 0x78,
            0x4e, 0x49, 0x66, 0xb8, 0x1d, 0xa6, 0x27, 0xd3,
            0x5a, 0xa7, 0x08, 0xfd, 0x97, 0x25, 0xd6, 0x04,
            0x77, 0x7b, 0x14, 0x3e, 0x28, 0xd4, 0xac, 0xf0,
        ]),
        Tmr([
            0x2a, 0x07, 0x5c, 0x50, 0xca, 0x10, 0xe5, 0x00,
            0x92, 0x77, 0x86, 0xca, 0x86, 0xa5, 0x2b, 0xb7,
            0xe5, 0x2d, 0xdb, 0x16, 0x42, 0xe3, 0x77, 0x45,
            0xfb, 0x44, 0x14, 0x91, 0x39, 0xb3, 0x2d, 0x67,
        ]),
        Tmr([
            0xf5, 0x67, 0xc8, 0x1b, 0x82, 0x5d, 0xc9, 0x00,
            0xe0, 0xbc, 0x0c, 0x59, 0xce, 0xa6, 0xaa, 0x1f,
            0x1f, 0x34, 0x28, 0x3f, 0xcb, 0xee, 0xff, 0x73,
            0x2d, 0xb8, 0xe2, 0x17, 0x57, 0x6c, 0x22, 0x55,
        ]),
        Tmr([
            0x0a, 0xee, 0x10, 0x39, 0xd3, 0xc8, 0xaf, 0x91,
            0x41, 0xbc, 0xb9, 0xfd, 0xf3, 0xf8, 0x24, 0x1e,
            0xf9, 0xfd, 0x4b, 0x85, 0x85, 0x3a, 0x79, 0xba,
            0xbc, 0xfb, 0x2a, 0xb4, 0xdb, 0x8e, 0xf3, 0x8c,
        ]),
        Tmr([
            0x30, 0xaa, 0xe9, 0xb8, 0xf2, 0xc6, 0xfa, 0xf0,
            0x7a, 0x62, 0x07, 0x2f, 0x55, 0xb2, 0x96, 0x98,
            0x5d, 0xd7, 0x3b, 0xc1, 0x04, 0x42, 0xc8, 0x02,
            0x79, 0x32, 0xff, 0xf4, 0x1f, 0x2a, 0x2d, 0x72,
        ]),
        Tmr([
            0xbc, 0x14, 0x6c, 0xb5, 0xae, 0x67, 0xe0, 0x0a,
            0x6d, 0x8e, 0x3f, 0xd0, 0x56, 0xfb, 0x7a, 0x72,
            0x82, 0x04, 0x84, 0xc6, 0x1a, 0xcf, 0x17, 0xfe,
            0xcc, 0xf1, 0x30, 0xcb, 0xa6, 0x22, 0xd2, 0x7f,
        ]),
        Tmr([
            0xa8, 0x67, 0xba, 0xcf, 0xf8, 0xd1, 0xc2, 0x5c,
            0x9e, 0x82, 0x4f, 0x1b, 0x63, 0xa4, 0x9c, 0x17,
            0x67, 0x9b, 0xba, 0x8a, 0xd8, 0x76, 0x99, 0xa0,
            0xcd, 0xf7, 0x7d, 0xef, 0x0a, 0x1b, 0x75, 0x11,
        ]),
        Tmr([
            0xc1, 0x28, 0xdc, 0x7e, 0x8d, 0x79, 0x4a, 0x7a,
            0x48, 0x08, 0x14, 0x95, 0xa6, 0xeb, 0x2b, 0x2d,
            0x3c, 0xb6, 0x6b, 0xc0, 0xe0, 0x33, 0x27, 0x3b,
            0xf9, 0xdd, 0xbc, 0x98, 0x6c, 0x22, 0x68, 0x92,
        ]),
        Tmr([
            0x19, 0x9f, 0xfd, 0xe4, 0x7d, 0x27, 0x91, 0xa3,
            0x58, 0xf2, 0x3a, 0xe5, 0x08, 0x25, 0x16, 0xc6,
            0x5e, 0x57, 0xf5, 0x5a, 0xe6, 0x6d, 0xdf, 0x44,
            0x65, 0xba, 0xab, 0x4b, 0xfa, 0x3c, 0x3a, 0x5f,
        ]),
        Tmr([
            0x40, 0x38, 0x09, 0x5d, 0xbb, 0x32, 0xe2, 0x7e,
            0x6e, 0x31, 0x66, 0x52, 0x7b, 0xce, 0xdd, 0xa8,
            0x4e, 0x8b, 0x21, 0xf6, 0xd4, 0x2f, 0x7e, 0x56,
            0xea, 0xff, 0x21, 0x85, 0x6a, 0xd2, 0x04, 0xa2,
        ]),
        Tmr([
            0xc3, 0xa0, 0xa6, 0xf8, 0x7e, 0x6c, 0x3f, 0x02,
            0xbf, 0x98, 0x05, 0x1c, 0xe4, 0x5d, 0x6d, 0x69,
            0x55, 0x38, 0x24, 0x78, 0xf1, 0x0f, 0xa0, 0x6a,
            0xdd, 0x51, 0x30, 0xe5, 0x99, 0x17, 0xf8, 0xc7,
        ]),
        Tmr([
            0x7c, 0x25, 0xbf, 0x44, 0x7a, 0x45, 0xba, 0x13,
            0x23, 0xe8, 0xdb, 0x8b, 0x81, 0x78, 0x87, 0xa8,
            0x85, 0x8b, 0xee, 0x25, 0x6e, 0x5c, 0xa3, 0x52,
            0x8e, 0x79, 0xcf, 0xbc, 0x2d, 0x41, 0x9c, 0xe3,
        ]),
        Tmr([
            0x75, 0xa7, 0x7a, 0x6c, 0x7b, 0x6d, 0xe9, 0x1d,
            0x23, 0xc1, 0x00, 0x4d, 0x8e, 0xb0, 0xb3, 0x5d,
            0x9c, 0xdf, 0x6f, 0xa1, 0xdf, 0xc7, 0xca, 0xbd,
            0x04, 0x66, 0x92, 0xf1, 0xa2, 0x43, 0x14, 0x17,
        ]),
        Tmr([
            0x9f, 0xc2, 0x4a, 0xb6, 0x99, 0x70, 0xf8, 0x66,
            0x6d, 0x7b, 0x1e, 0x52, 0x5a, 0x3a, 0x8e, 0x79,
            0x43, 0x99, 0x76, 0xe7, 0x79, 0x2b, 0xfe, 0x1a,
            0x1b, 0x0d, 0x0d, 0x84, 0xff, 0x03, 0x84, 0x34,
        ]),
        Tmr([
            0x64, 0x25, 0x1a, 0xb9, 0x70, 0x77, 0x17, 0x0e,
            0x19, 0x91, 0x15, 0x76, 0x5e, 0x93, 0x43, 0xe7,
            0x1b, 0x59, 0x0e, 0x41, 0x78, 0x88, 0xf2, 0xa8,
            0x36, 0x43, 0x11, 0xd8, 0x65, 0x40, 0xed, 0xfa,
        ]),
    ];

    /// The TMRs of the types (TWO^8)^<(2^(N + 1)) for small values of n
    #[rustfmt::skip]
    pub const BUFFER8_TWO_N_PLUS_ONE: [Tmr; 16] = [
        Tmr([
            0x66, 0x8c, 0x79, 0xa6, 0xf0, 0xe0, 0x86, 0xa6,
            0x2d, 0x73, 0xf0, 0xc6, 0x52, 0xb1, 0x5c, 0x56,
            0xb1, 0xd3, 0x53, 0x68, 0xe1, 0xff, 0xad, 0x76,
            0xc9, 0xd4, 0xdb, 0xae, 0xb5, 0x2d, 0xe6, 0x2f,
        ]),
        Tmr([
            0x9f, 0x91, 0xf5, 0x19, 0x9b, 0xb8, 0x16, 0x7a,
            0x3b, 0x6c, 0xf8, 0xd0, 0x39, 0x75, 0x6e, 0x1b,
            0x33, 0x18, 0x42, 0x6e, 0xef, 0x3a, 0x9b, 0xa0,
            0x90, 0x2c, 0x91, 0xdc, 0x5f, 0x7e, 0x63, 0x39,
        ]),
        Tmr([
            0x16, 0x98, 0x45, 0xb0, 0xe2, 0xf0, 0x4a, 0x83,
            0x76, 0xfe, 0x91, 0x14, 0xbd, 0x35, 0x3a, 0x29,
            0xe9, 0xb7, 0x80, 0x42, 0xb7, 0x70, 0x1b, 0xa1,
            0x6d, 0x02, 0xc4, 0x62, 0x18, 0xef, 0x8b, 0xdf,
        ]),
        Tmr([
            0xbd, 0x59, 0x57, 0x8c, 0x45, 0x78, 0xfe, 0xc4,
            0xef, 0x2a, 0x29, 0xc1, 0xad, 0xea, 0x4c, 0xe1,
            0x30, 0x6c, 0x1a, 0x2b, 0x10, 0xd6, 0x18, 0xbc,
            0x58, 0x44, 0x67, 0x84, 0x17, 0x97, 0x3b, 0x8e,
        ]),
        Tmr([
            0x88, 0xd1, 0x8c, 0x11, 0x5f, 0x86, 0x54, 0x40,
            0x5f, 0xd7, 0x1d, 0xe8, 0x7e, 0x6f, 0xf0, 0x80,
            0x6c, 0x5d, 0xa7, 0x97, 0xca, 0xaf, 0x48, 0xe2,
            0x47, 0xea, 0xcc, 0x43, 0x99, 0x96, 0x89, 0x1a,
        ]),
        Tmr([
            0x90, 0x65, 0x22, 0xe1, 0x6d, 0xf3, 0x6f, 0x83,
            0xa4, 0x50, 0x32, 0x8a, 0xf8, 0x4e, 0x2f, 0x3e,
            0x52, 0xf6, 0xd6, 0x49, 0xa7, 0x88, 0xe3, 0xb2,
            0x1c, 0xca, 0xe3, 0xcd, 0x9a, 0x24, 0x66, 0x77,
        ]),
        Tmr([
            0x3a, 0xba, 0x4c, 0xce, 0x0e, 0x46, 0xc5, 0x54,
            0xb4, 0x4f, 0x70, 0x21, 0x3d, 0x4d, 0x8b, 0xd8,
            0xe8, 0xf1, 0x7e, 0x2b, 0xd1, 0x7b, 0xdf, 0xb7,
            0x15, 0x0a, 0xdf, 0xfd, 0x9a, 0xdf, 0x80, 0x9d,
        ]),
        Tmr([
            0x7d, 0x81, 0x2a, 0xf0, 0x44, 0xcb, 0x04, 0xd2,
            0xaf, 0x4f, 0xc8, 0xe9, 0xb5, 0x30, 0x66, 0x03,
            0x9b, 0xd5, 0x89, 0x2a, 0x56, 0xa5, 0xd5, 0x64,
            0x1c, 0x5c, 0x58, 0xc8, 0x6e, 0xed, 0x8a, 0x35,
        ]),
        Tmr([
            0x23, 0x0e, 0xa9, 0x70, 0x03, 0x3e, 0x47, 0xc5,
            0x61, 0x68, 0x1e, 0xe7, 0x20, 0x58, 0xb9, 0xc0,
            0x75, 0x5b, 0x8d, 0x87, 0x22, 0x80, 0x18, 0x4f,
            0x2e, 0x3f, 0xe3, 0x93, 0xc0, 0x47, 0x8f, 0x1a,
        ]),
        Tmr([
            0x6e, 0x90, 0x88, 0x04, 0x20, 0x46, 0x2b, 0x84,
            0xf2, 0x16, 0x30, 0x2a, 0x09, 0x56, 0x9c, 0xc8,
            0xd4, 0x47, 0x54, 0x4a, 0xcd, 0x8a, 0xbc, 0x53,
            0xd8, 0xa7, 0xcf, 0x21, 0x9b, 0x44, 0x9c, 0x95,
        ]),
        Tmr([
            0x7a, 0xd1, 0x5d, 0xed, 0x85, 0x02, 0xa6, 0xf1,
            0x1e, 0xb8, 0xa4, 0x9b, 0x25, 0xc8, 0xd7, 0x62,
            0xa0, 0xfe, 0x00, 0x4f, 0xb5, 0x7d, 0x73, 0x43,
            0xbd, 0xf4, 0x90, 0xed, 0x7e, 0xd2, 0xf4, 0x07,
        ]),
        Tmr([
            0xaa, 0x30, 0x30, 0x15, 0x71, 0xcf, 0x8d, 0x2d,
            0x64, 0xe9, 0x65, 0x4b, 0x0c, 0xbd, 0xe0, 0x54,
            0x01, 0x77, 0xab, 0xc2, 0xf2, 0xef, 0xfb, 0x7f,
            0x05, 0x2e, 0x27, 0xa8, 0xcc, 0xcc, 0xee, 0x62,
        ]),
        Tmr([
            0x28, 0x6d, 0x4d, 0x57, 0x6d, 0xb8, 0xe8, 0x45,
            0x00, 0x9c, 0x74, 0x84, 0xe0, 0x0a, 0x9f, 0xa3,
            0xf3, 0x18, 0xa8, 0x21, 0x9e, 0x38, 0x66, 0x2c,
            0xda, 0x11, 0x75, 0x78, 0x22, 0xe4, 0x53, 0xbb,
        ]),
        Tmr([
            0x10, 0x56, 0xa7, 0x17, 0xdc, 0x0c, 0x86, 0xb7,
            0x49, 0x97, 0x5e, 0x9c, 0x36, 0x4a, 0xb5, 0xa6,
            0x64, 0x77, 0xc4, 0x2d, 0xe1, 0x6e, 0x5b, 0x05,
            0x29, 0x81, 0x4f, 0x4c, 0xba, 0x95, 0x7c, 0xb3,
        ]),
        Tmr([
            0x94, 0x59, 0x0e, 0xf5, 0x66, 0x10, 0xaa, 0x7f,
            0xec, 0x46, 0x5b, 0x72, 0x32, 0xab, 0xa3, 0xf4,
            0x7c, 0x94, 0x2b, 0x8e, 0x8d, 0xc8, 0x76, 0x82,
            0x51, 0xd3, 0x70, 0x2c, 0x99, 0xb9, 0xef, 0xef,
        ]),
        Tmr([
            0xd0, 0x7f, 0x71, 0xb7, 0xcd, 0x48, 0xb8, 0x50,
            0xad, 0x09, 0x06, 0x8b, 0x9b, 0x48, 0xe0, 0xf8,
            0x5f, 0x84, 0xc0, 0x1b, 0x34, 0x58, 0x6a, 0x51,
            0x34, 0x61, 0xfa, 0xf7, 0x6d, 0x54, 0x34, 0x64,
        ]),
    ];

    /// The TMR of the `Ctx8` type used by the SHA256 jets.
    #[rustfmt::skip]
    pub const CTX8: Tmr = Tmr([
        0x8d, 0x5c, 0xe5, 0xf6, 0xeb, 0xfc, 0x70, 0x61,
        0xe7, 0x91, 0x16, 0xbd, 0xf6, 0x6f, 0xb4, 0xe4,
        0xc6, 0xca, 0x32, 0xa0, 0x1c, 0xda, 0x48, 0xf6,
        0xed, 0xc1, 0x0a, 0xee, 0xfd, 0x31, 0x0e, 0x8c,
    ]);

    /// The TMR for the unit type
    pub const fn unit() -> Tmr {
        Self::from_byte_array(Self::UNIT_IV.to_parts().0)
    }

    /// The TMR for the successor of a type.
    ///
    /// Given a type `X`, we define its successor `S X` as `1 + X`.
    /// In Rust notation this would be `Option<X>`.
    pub fn successor(self) -> Tmr {
        Self::SUM_IV
            .zz_update_2x32(Tmr::unit(), self)
            .into_merkle_root()
    }

    /// The TMR for the sum of two types, whose TMRs are given
    pub fn sum(tmr1: Tmr, tmr2: Tmr) -> Tmr {
        Self::SUM_IV
            .update_64(&super::concat(tmr1, tmr2))
            .into_merkle_root()
    }

    /// The TMR for the sum of two types, whose TMRs are given
    ///
    /// This function is slow but can be used in a const context. If you do not
    /// need to run in a const context, use [`Self::sum`] instead.
    pub const fn sum_unoptimized(tmr1: Tmr, tmr2: Tmr) -> Tmr {
        Self::from_byte_array(
            Self::SUM_IV
                .update_2x32_unoptimized(tmr1.as_byte_array(), tmr2.as_byte_array())
                .to_parts()
                .0,
        )
    }

    /// The TMR for the product of two types, whose TMRs are given
    pub fn product(tmr1: Tmr, tmr2: Tmr) -> Tmr {
        Self::PROD_IV.zz_update_2x32(tmr1, tmr2).into_merkle_root()
    }

    /// The TMR for the product of two types, whose TMRs are given
    ///
    /// This function is slow but can be used in a const context. If you do not
    /// need to run in a const context, use [`Self::product`] instead.
    pub const fn product_unoptimized(tmr1: Tmr, tmr2: Tmr) -> Tmr {
        Self::from_byte_array(
            Self::PROD_IV
                .update_2x32_unoptimized(tmr1.as_byte_array(), tmr2.as_byte_array())
                .to_parts()
                .0,
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use crate::jet::Jet;
    use crate::merkle::bip340_iv;
    use crate::types::{self, Final};

    #[test]
    fn ivs() {
        fn check_iv(target: Midstate, s: &'static str) {
            let name = s.trim_start_matches("Simplicity\x1fType\x1f");
            // Uncomment this if the IVs ever change
            /*
            let target = Tmr(bip340_iv(s.as_bytes()));
            println!("    #[rustfmt::skip]");
            println!("    const {}_IV: Midstate = Midstate::new([", name.to_ascii_uppercase());
            print!("       "); for ch in &target.0[0..8] { print!(" 0x{:02x},", ch); }; println!();
            print!("       "); for ch in &target.0[8..16] { print!(" 0x{:02x},", ch); }; println!();
            print!("       "); for ch in &target.0[16..24] { print!(" 0x{:02x},", ch); }; println!();
            print!("       "); for ch in &target.0[24..32] { print!(" 0x{:02x},", ch); }; println!();
            println!("    ], 64);");
            println!();
            */
            assert_eq!(
                target,
                bip340_iv(s.as_bytes()),
                "mismatch on IV for {}",
                name
            );
        }

        check_iv(Tmr::UNIT_IV, "Simplicity\x1fType\x1funit");
        check_iv(Tmr::SUM_IV, "Simplicity\x1fType\x1fsum");
        check_iv(Tmr::PROD_IV, "Simplicity\x1fType\x1fprod");
    }

    #[test]
    #[allow(clippy::needless_range_loop)]
    fn const_powers_of_2() {
        fn check_pow(target: Tmr, i: usize, expected_tmrs: &[Tmr]) {
            // Uncomment this if the IVs ever change
            /*
            let target = tmrs[i];
            println!("    Tmr([");
            print!("       "); for ch in &target.0[0..8] { print!(" 0x{:02x},", ch); }; println!();
            print!("       "); for ch in &target.0[8..16] { print!(" 0x{:02x},", ch); }; println!();
            print!("       "); for ch in &target.0[16..24] { print!(" 0x{:02x},", ch); }; println!();
            print!("       "); for ch in &target.0[24..32] { print!(" 0x{:02x},", ch); }; println!();
            println!("    ]),");
            */
            assert_eq!(target, expected_tmrs[i], "mismatch on TMR for TWO^(2^{i})");
        }

        let n = Tmr::TWO_TWO_N.len();
        types::Context::with_context(|ctx| {
            let tmrs = types::Type::powers_of_two(&ctx, n)
                .iter()
                .filter_map(types::Type::tmr)
                .collect::<Vec<Tmr>>();
            debug_assert_eq!(tmrs.len(), n);
            for i in 0..n {
                check_pow(Tmr::TWO_TWO_N[i], i, &tmrs);
            }
        });
    }

    #[test]
    fn direct_constructors() {
        let unit = Tmr::unit();
        let bit1 = Tmr::sum(unit, unit);
        let bit2 = Tmr::sum_unoptimized(unit, unit);
        assert_eq!(bit1, bit2);
        assert_eq!(bit1, Tmr::TWO_TWO_N[0]);

        let dbit1 = Tmr::product(bit1, bit1);
        let dbit2 = Tmr::product_unoptimized(bit1, bit1);
        assert_eq!(dbit1, dbit2);
        assert_eq!(dbit1, Tmr::TWO_TWO_N[1]);
    }

    #[test]
    fn const_buffer8() {
        use std::sync::Arc;

        let two_8_tmr = Tmr::TWO_TWO_N[3];
        let buf_0_tmr = two_8_tmr.successor();

        let two_8 = Final::two_two_n_fixed::<3>();
        let buf_0 = Arc::clone(&two_8).successor();

        assert_eq!(buf_0, Final::buffer8_two_n_plus_one(0).unwrap());
        assert_eq!(two_8.tmr(), two_8_tmr);
        assert_eq!(buf_0.tmr(), buf_0_tmr);
        assert_eq!(buf_0_tmr, Tmr::BUFFER8_TWO_N_PLUS_ONE[0]);

        let mut tmr = buf_0_tmr;
        let mut fin = buf_0;
        assert_eq!(Tmr::BUFFER8_TWO_N_PLUS_ONE.len(), 16);
        for n in 1..Tmr::BUFFER8_TWO_N_PLUS_ONE.len() {
            tmr = Tmr::product(Tmr::TWO_TWO_N[n + 3].successor(), tmr);
            fin = Final::product(Final::two_two_n(n + 3).unwrap().successor(), fin);

            assert_eq!(fin.tmr(), tmr);
            assert_eq!(fin, Final::buffer8_two_n_plus_one(n).unwrap());
            assert_eq!(tmr, Tmr::BUFFER8_TWO_N_PLUS_ONE[n]);

            // For the case of n == 5, i.e. a 63-byte buffer (63 = 2^(n + 1) - 1),
            // we can check against the SHA256 Ctx8 type to make sure that our
            // computations are actually correct.
            if n == 5 {
                let extra_ctx_stuff = Final::product(
                    Final::two_two_n_fixed::<6>(), // 2^64
                    Final::two_two_n_fixed::<8>(), // 2^256
                );
                let synthetic_ctx_ty = Final::product(Arc::clone(&fin), extra_ctx_stuff);
                let actual_ctx_ty = crate::jet::Core::Sha256Ctx8Init.target_ty().to_final();
                assert_eq!(synthetic_ctx_ty, actual_ctx_ty,);
                assert_eq!(synthetic_ctx_ty, Final::ctx8());
                assert_eq!(synthetic_ctx_ty.tmr(), Tmr::CTX8);
            }
        }
    }
}