@@ -9,30 +9,30 @@ use hashes::sha256::Midstate;
9
9
10
10
use super :: { bip340_iv, compact_value, FailEntropy } ;
11
11
12
- /// Identity Merkle root (first pass)
12
+ /// Identity Merkle Root
13
13
///
14
14
/// A Merkle root that commits to a node's combinator, its witness data (if present),
15
15
/// and recursively its children. Used as input to the [`Ihr`] type which is probably
16
16
/// actually what you want.
17
17
#[ derive( Copy , Clone , PartialEq , Eq , Hash , PartialOrd , Ord ) ]
18
- pub struct FirstPassIhr ( Midstate ) ;
18
+ pub struct Imr ( Midstate ) ;
19
19
20
- impl_midstate_wrapper ! ( FirstPassIhr ) ;
20
+ impl_midstate_wrapper ! ( Imr ) ;
21
21
22
- /// Identity Merkle root
22
+ /// Identity Hash Root
23
23
///
24
- /// A Merkle root that commits to a node's combinator, its witness data (if present),
25
- /// its source and target types, and recursively its children .
24
+ /// A Merkle root that commits to a node's [`Imr`] (which recursively commits to its
25
+ /// childrens' [`Imr`]s) as well as its source and target types.
26
26
///
27
27
/// Uniquely identifies a program's structure in terms of combinators at redemption time.
28
28
#[ derive( Copy , Clone , PartialEq , Eq , Hash , PartialOrd , Ord ) ]
29
29
pub struct Ihr ( Midstate ) ;
30
30
31
31
impl_midstate_wrapper ! ( Ihr ) ;
32
32
33
- impl From < Cmr > for FirstPassIhr {
33
+ impl From < Cmr > for Imr {
34
34
fn from ( cmr : Cmr ) -> Self {
35
- FirstPassIhr :: from_byte_array ( cmr. to_byte_array ( ) )
35
+ Imr :: from_byte_array ( cmr. to_byte_array ( ) )
36
36
}
37
37
}
38
38
@@ -48,7 +48,7 @@ impl From<Tmr> for Ihr {
48
48
}
49
49
}
50
50
51
- impl FirstPassIhr {
51
+ impl Imr {
52
52
/// Produce a CMR for an iden combinator
53
53
pub const fn iden ( ) -> Self {
54
54
Self :: IDEN_IV
@@ -60,42 +60,42 @@ impl FirstPassIhr {
60
60
}
61
61
62
62
/// Produce a CMR for an injl combinator
63
- pub fn injl ( child : FirstPassIhr ) -> Self {
63
+ pub fn injl ( child : Self ) -> Self {
64
64
Self :: INJL_IV . update_1 ( child)
65
65
}
66
66
67
67
/// Produce a CMR for an injr combinator
68
- pub fn injr ( child : FirstPassIhr ) -> Self {
68
+ pub fn injr ( child : Self ) -> Self {
69
69
Self :: INJR_IV . update_1 ( child)
70
70
}
71
71
72
72
/// Produce a CMR for a take combinator
73
- pub fn take ( child : FirstPassIhr ) -> Self {
73
+ pub fn take ( child : Self ) -> Self {
74
74
Self :: TAKE_IV . update_1 ( child)
75
75
}
76
76
77
77
/// Produce a CMR for a drop combinator
78
- pub fn drop ( child : FirstPassIhr ) -> Self {
78
+ pub fn drop ( child : Self ) -> Self {
79
79
Self :: DROP_IV . update_1 ( child)
80
80
}
81
81
82
82
/// Produce a CMR for a comp combinator
83
- pub fn comp ( left : FirstPassIhr , right : FirstPassIhr ) -> Self {
83
+ pub fn comp ( left : Self , right : Self ) -> Self {
84
84
Self :: COMP_IV . update ( left, right)
85
85
}
86
86
87
87
/// Produce a CMR for a case combinator
88
- pub fn case ( left : FirstPassIhr , right : FirstPassIhr ) -> Self {
88
+ pub fn case ( left : Self , right : Self ) -> Self {
89
89
Self :: CASE_IV . update ( left, right)
90
90
}
91
91
92
92
/// Produce a CMR for a pair combinator
93
- pub fn pair ( left : FirstPassIhr , right : FirstPassIhr ) -> Self {
93
+ pub fn pair ( left : Self , right : Self ) -> Self {
94
94
Self :: PAIR_IV . update ( left, right)
95
95
}
96
96
97
97
/// Produce a CMR for a disconnect combinator
98
- pub fn disconnect ( left : FirstPassIhr , right : FirstPassIhr ) -> Self {
98
+ pub fn disconnect ( left : Self , right : Self ) -> Self {
99
99
Self :: DISCONNECT_IV . update ( left, right)
100
100
}
101
101
@@ -109,7 +109,7 @@ impl FirstPassIhr {
109
109
let mut engine = sha256:: HashEngine :: from_midstate ( Self :: WITNESS_IV . 0 , 0 ) ;
110
110
engine. input ( & value_hash[ ..] ) ;
111
111
engine. input ( ty. target . tmr ( ) . as_ref ( ) ) ;
112
- FirstPassIhr ( engine. midstate ( ) )
112
+ Self ( engine. midstate ( ) )
113
113
}
114
114
115
115
/// Produce an IHR for a fail combinator
@@ -131,95 +131,95 @@ impl FirstPassIhr {
131
131
}
132
132
133
133
#[ rustfmt:: skip]
134
- const IDEN_IV : FirstPassIhr = FirstPassIhr ( Midstate ( [
134
+ const IDEN_IV : Self = Self ( Midstate ( [
135
135
0x54 , 0x1a , 0x1a , 0x69 , 0xbd , 0x4b , 0xcb , 0xda ,
136
136
0x7f , 0x34 , 0x31 , 0x0e , 0x30 , 0x78 , 0xf7 , 0x26 ,
137
137
0x44 , 0x31 , 0x22 , 0xfb , 0xcc , 0x1c , 0xb5 , 0x36 ,
138
138
0x0c , 0x78 , 0x64 , 0xec , 0x0d , 0x32 , 0x3a , 0xc0 ,
139
139
] ) ) ;
140
140
141
141
#[ rustfmt:: skip]
142
- const UNIT_IV : FirstPassIhr = FirstPassIhr ( Midstate ( [
142
+ const UNIT_IV : Self = Self ( Midstate ( [
143
143
0xc4 , 0x0a , 0x10 , 0x26 , 0x3f , 0x74 , 0x36 , 0xb4 ,
144
144
0x16 , 0x0a , 0xcb , 0xef , 0x1c , 0x36 , 0xfb , 0xa4 ,
145
145
0xbe , 0x4d , 0x95 , 0xdf , 0x18 , 0x1a , 0x96 , 0x8a ,
146
146
0xfe , 0xab , 0x5e , 0xac , 0x24 , 0x7a , 0xdf , 0xf7 ,
147
147
] ) ) ;
148
148
149
149
#[ rustfmt:: skip]
150
- const INJL_IV : FirstPassIhr = FirstPassIhr ( Midstate ( [
150
+ const INJL_IV : Self = Self ( Midstate ( [
151
151
0x54 , 0xe9 , 0x1d , 0x18 , 0xd8 , 0xf8 , 0x1f , 0x6d ,
152
152
0x29 , 0x86 , 0xbb , 0x58 , 0x47 , 0x9a , 0x54 , 0xeb ,
153
153
0x63 , 0x0e , 0x95 , 0x23 , 0xb6 , 0x9e , 0xe8 , 0x53 ,
154
154
0x29 , 0x80 , 0xd0 , 0x55 , 0x58 , 0x19 , 0x4f , 0x15 ,
155
155
] ) ) ;
156
156
157
157
#[ rustfmt:: skip]
158
- const INJR_IV : FirstPassIhr = FirstPassIhr ( Midstate ( [
158
+ const INJR_IV : Self = Self ( Midstate ( [
159
159
0xd7 , 0x0f , 0xfd , 0xce , 0x97 , 0x77 , 0x7b , 0x4d ,
160
160
0xfe , 0x31 , 0xfd , 0x9f , 0xf5 , 0xd0 , 0x17 , 0xa6 ,
161
161
0x30 , 0x5d , 0x7e , 0xc6 , 0x0d , 0xf3 , 0xb1 , 0xbf ,
162
162
0x6d , 0x25 , 0xe8 , 0x16 , 0x33 , 0xde , 0xd4 , 0xbf ,
163
163
] ) ) ;
164
164
165
165
#[ rustfmt:: skip]
166
- const TAKE_IV : FirstPassIhr = FirstPassIhr ( Midstate ( [
166
+ const TAKE_IV : Self = Self ( Midstate ( [
167
167
0x50 , 0x5f , 0xc0 , 0x81 , 0xb5 , 0xba , 0x2a , 0xcd ,
168
168
0x09 , 0x50 , 0x67 , 0xc3 , 0xdf , 0xb8 , 0xea , 0x12 ,
169
169
0x6f , 0xa1 , 0x5d , 0x55 , 0xcb , 0x21 , 0x1e , 0x6a ,
170
170
0xed , 0x34 , 0xe8 , 0xd1 , 0xe3 , 0x7a , 0xf0 , 0xfa ,
171
171
] ) ) ;
172
172
173
173
#[ rustfmt:: skip]
174
- const DROP_IV : FirstPassIhr = FirstPassIhr ( Midstate ( [
174
+ const DROP_IV : Self = Self ( Midstate ( [
175
175
0x8a , 0x30 , 0x8d , 0x38 , 0xa1 , 0x13 , 0xa2 , 0x60 ,
176
176
0xb4 , 0xc7 , 0x14 , 0x5a , 0xbd , 0xc5 , 0x22 , 0x4d ,
177
177
0xeb , 0x70 , 0x13 , 0x79 , 0x59 , 0x0e , 0x0c , 0x8c ,
178
178
0x38 , 0x86 , 0x0b , 0xab , 0x12 , 0x71 , 0xa8 , 0xa8 ,
179
179
] ) ) ;
180
180
181
181
#[ rustfmt:: skip]
182
- const COMP_IV : FirstPassIhr = FirstPassIhr ( Midstate ( [
182
+ const COMP_IV : Self = Self ( Midstate ( [
183
183
0x57 , 0xec , 0x23 , 0xa2 , 0xa4 , 0x77 , 0x8e , 0x01 ,
184
184
0x58 , 0xa6 , 0x21 , 0x7a , 0xea , 0x3e , 0xf7 , 0x42 ,
185
185
0x8b , 0xa0 , 0x90 , 0x92 , 0x73 , 0xb9 , 0x73 , 0xfa ,
186
186
0x14 , 0x32 , 0xa9 , 0x27 , 0x84 , 0x3e , 0x92 , 0x7a ,
187
187
] ) ) ;
188
188
189
189
#[ rustfmt:: skip]
190
- const CASE_IV : FirstPassIhr = FirstPassIhr ( Midstate ( [
190
+ const CASE_IV : Self = Self ( Midstate ( [
191
191
0x29 , 0x5e , 0x2a , 0x6d , 0xc8 , 0xc5 , 0xce , 0x59 ,
192
192
0xe4 , 0xed , 0xcf , 0xe9 , 0xb4 , 0xd8 , 0xf7 , 0x64 ,
193
193
0x13 , 0x3a , 0xa5 , 0x51 , 0x4b , 0xd3 , 0xee , 0x8b ,
194
194
0x4b , 0x75 , 0xec , 0x8f , 0x4d , 0xeb , 0x08 , 0xbe ,
195
195
] ) ) ;
196
196
197
197
#[ rustfmt:: skip]
198
- const PAIR_IV : FirstPassIhr = FirstPassIhr ( Midstate ( [
198
+ const PAIR_IV : Self = Self ( Midstate ( [
199
199
0x7d , 0x5e , 0x6d , 0xac , 0x15 , 0xb1 , 0x42 , 0x8a ,
200
200
0x0d , 0x26 , 0x0c , 0x94 , 0x29 , 0xdb , 0xe8 , 0x89 ,
201
201
0x65 , 0x93 , 0xf3 , 0x1f , 0x70 , 0x86 , 0x27 , 0xee ,
202
202
0x75 , 0xb2 , 0x7e , 0xee , 0xfd , 0xd0 , 0x50 , 0x05 ,
203
203
] ) ) ;
204
204
205
205
#[ rustfmt:: skip]
206
- const DISCONNECT_IV : FirstPassIhr = FirstPassIhr ( Midstate ( [
206
+ const DISCONNECT_IV : Self = Self ( Midstate ( [
207
207
0x4e , 0xb7 , 0x99 , 0x5f , 0xb5 , 0xdd , 0xe5 , 0xd0 ,
208
208
0x85 , 0xf4 , 0x70 , 0x85 , 0xcd , 0x95 , 0x3d , 0x16 ,
209
209
0x84 , 0x54 , 0x11 , 0xed , 0xc6 , 0x89 , 0xe2 , 0x7a ,
210
210
0xf9 , 0xc3 , 0xde , 0xa2 , 0xfb , 0x12 , 0x25 , 0xd5 ,
211
211
] ) ) ;
212
212
213
213
#[ rustfmt:: skip]
214
- const WITNESS_IV : FirstPassIhr = FirstPassIhr ( Midstate ( [
214
+ const WITNESS_IV : Self = Self ( Midstate ( [
215
215
0xcb , 0x37 , 0xff , 0x70 , 0x01 , 0xc6 , 0x2d , 0x94 ,
216
216
0x42 , 0x4f , 0x98 , 0x7f , 0x30 , 0x23 , 0xb3 , 0x5e ,
217
217
0x30 , 0xd2 , 0x17 , 0x23 , 0x96 , 0x27 , 0x6f , 0x89 ,
218
218
0xd0 , 0x9f , 0x07 , 0xaa , 0x67 , 0xb6 , 0x21 , 0x96 ,
219
219
] ) ) ;
220
220
221
221
#[ rustfmt:: skip]
222
- const FAIL_IV : FirstPassIhr = FirstPassIhr ( Midstate ( [
222
+ const FAIL_IV : Self = Self ( Midstate ( [
223
223
0x22 , 0x83 , 0xc1 , 0x81 , 0x9e , 0x69 , 0x2f , 0x96 ,
224
224
0x85 , 0xfe , 0x95 , 0x40 , 0x76 , 0xc5 , 0x16 , 0x7c ,
225
225
0x03 , 0xbd , 0xe7 , 0xcc , 0xda , 0xab , 0x00 , 0x5e ,
@@ -228,11 +228,10 @@ impl FirstPassIhr {
228
228
}
229
229
230
230
impl Ihr {
231
- /// Do the second pass of the IHR computation. This must be called on the result
232
- /// of first pass.
233
- pub fn compute_pass2 ( first_pass : FirstPassIhr , ty : & FinalArrow ) -> Ihr {
231
+ /// Construct an IHR from its components: an IMR and the source and target types.
232
+ pub fn from_imr ( imr : Imr , ty : & FinalArrow ) -> Ihr {
234
233
let iv = Ihr ( bip340_iv ( b"Simplicity\x1f Identity" ) ) ;
235
- iv. update_1 ( Ihr ( first_pass . 0 ) )
234
+ iv. update_1 ( Ihr ( imr . 0 ) )
236
235
. update ( ty. source . tmr ( ) . into ( ) , ty. target . tmr ( ) . into ( ) )
237
236
}
238
237
}
@@ -244,38 +243,38 @@ mod tests {
244
243
#[ test]
245
244
#[ rustfmt:: skip] // wants to split up the check_iv lines below
246
245
fn ivs ( ) {
247
- fn check_iv ( target : FirstPassIhr , s : & ' static str ) {
246
+ fn check_iv ( target : Imr , s : & ' static str ) {
248
247
let name = s
249
248
. trim_start_matches ( "Simplicity\x1f " )
250
249
. trim_start_matches ( "Commitment\x1f " )
251
250
. trim_start_matches ( "Identity\x1f " ) ;
252
251
// Uncomment this if the IVs ever change
253
252
/*
254
- let target = FirstPassIhr (bip340_iv(s.as_bytes()));
253
+ let target = Imr (bip340_iv(s.as_bytes()));
255
254
println!(" #[rustfmt::skip]");
256
- println!(" const {}_IV: FirstPassIhr = FirstPassIhr (Midstate([", name.to_ascii_uppercase());
255
+ println!(" const {}_IV: Imr = Imr (Midstate([", name.to_ascii_uppercase());
257
256
print!(" "); for ch in &target.0[0..8] { print!(" 0x{:02x},", ch); }; println!();
258
257
print!(" "); for ch in &target.0[8..16] { print!(" 0x{:02x},", ch); }; println!();
259
258
print!(" "); for ch in &target.0[16..24] { print!(" 0x{:02x},", ch); }; println!();
260
259
print!(" "); for ch in &target.0[24..32] { print!(" 0x{:02x},", ch); }; println!();
261
260
println!(" ]));");
262
261
println!();
263
262
*/
264
- assert_eq ! ( target, FirstPassIhr ( bip340_iv( s. as_bytes( ) ) ) , "mismatch on IV for {}" , name) ;
263
+ assert_eq ! ( target, Imr ( bip340_iv( s. as_bytes( ) ) ) , "mismatch on IV for {}" , name) ;
265
264
}
266
265
267
266
// Note that these are the same as those for CMRs **except** for disconnect and witness.
268
- check_iv ( FirstPassIhr :: IDEN_IV , "Simplicity\x1f Commitment\x1f iden" ) ;
269
- check_iv ( FirstPassIhr :: UNIT_IV , "Simplicity\x1f Commitment\x1f unit" ) ;
270
- check_iv ( FirstPassIhr :: INJL_IV , "Simplicity\x1f Commitment\x1f injl" ) ;
271
- check_iv ( FirstPassIhr :: INJR_IV , "Simplicity\x1f Commitment\x1f injr" ) ;
272
- check_iv ( FirstPassIhr :: TAKE_IV , "Simplicity\x1f Commitment\x1f take" ) ;
273
- check_iv ( FirstPassIhr :: DROP_IV , "Simplicity\x1f Commitment\x1f drop" ) ;
274
- check_iv ( FirstPassIhr :: COMP_IV , "Simplicity\x1f Commitment\x1f comp" ) ;
275
- check_iv ( FirstPassIhr :: CASE_IV , "Simplicity\x1f Commitment\x1f case" ) ;
276
- check_iv ( FirstPassIhr :: PAIR_IV , "Simplicity\x1f Commitment\x1f pair" ) ;
277
- check_iv ( FirstPassIhr :: DISCONNECT_IV , "Simplicity\x1f Identity\x1f disconnect" ) ;
278
- check_iv ( FirstPassIhr :: WITNESS_IV , "Simplicity\x1f Identity\x1f witness" ) ;
279
- check_iv ( FirstPassIhr :: FAIL_IV , "Simplicity\x1f Commitment\x1f fail" ) ;
267
+ check_iv ( Imr :: IDEN_IV , "Simplicity\x1f Commitment\x1f iden" ) ;
268
+ check_iv ( Imr :: UNIT_IV , "Simplicity\x1f Commitment\x1f unit" ) ;
269
+ check_iv ( Imr :: INJL_IV , "Simplicity\x1f Commitment\x1f injl" ) ;
270
+ check_iv ( Imr :: INJR_IV , "Simplicity\x1f Commitment\x1f injr" ) ;
271
+ check_iv ( Imr :: TAKE_IV , "Simplicity\x1f Commitment\x1f take" ) ;
272
+ check_iv ( Imr :: DROP_IV , "Simplicity\x1f Commitment\x1f drop" ) ;
273
+ check_iv ( Imr :: COMP_IV , "Simplicity\x1f Commitment\x1f comp" ) ;
274
+ check_iv ( Imr :: CASE_IV , "Simplicity\x1f Commitment\x1f case" ) ;
275
+ check_iv ( Imr :: PAIR_IV , "Simplicity\x1f Commitment\x1f pair" ) ;
276
+ check_iv ( Imr :: DISCONNECT_IV , "Simplicity\x1f Identity\x1f disconnect" ) ;
277
+ check_iv ( Imr :: WITNESS_IV , "Simplicity\x1f Identity\x1f witness" ) ;
278
+ check_iv ( Imr :: FAIL_IV , "Simplicity\x1f Commitment\x1f fail" ) ;
280
279
}
281
280
}
0 commit comments