Skip to content

Commit dec0639

Browse files
committed
Merge #284: rename FirstPassIhr to Imr
296abdd run `cargo fmt` (Andrew Poelstra) 3d48b8e rename `FirstPassIhr` to `Imr` (Andrew Poelstra) Pull request description: In #270 we renamed IMR (Identity Merkle Root) to the IHR (Identity Hash Root). We did this via mass-rename which turned the `FirstPassImr` object into `FirstPassIhr`. The correct terminogy is now that `FirstPassImr` is simply `Imr`. This object continues to be a mostly-internal-only object which is used in service of computing the `Ihr`, which is what users are likely to interact with. Fixes #282 ACKs for top commit: uncomputable: ACK 296abdd That makes a lot of sense. I'm happy that we could get rid of `FirstPassImr`. In a way, we foreshadowed the need for `IHR` in the Rust code. Tree-SHA512: e6fee5a9dcd77f5eb28d70c5d29b5a8061bfb769f3d306e6781492609bbc6031c8b0922b7f32a36ec6f6b1a01fd21e7b993ec21264520f705e445d76b8ca9ee9
2 parents 4c6a841 + 296abdd commit dec0639

File tree

4 files changed

+96
-115
lines changed

4 files changed

+96
-115
lines changed

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ pub use crate::encode::{encode_natural, encode_value, encode_witness};
6161
pub use crate::merkle::{
6262
amr::Amr,
6363
cmr::Cmr,
64-
ihr::{FirstPassIhr, Ihr},
64+
ihr::{Ihr, Imr},
6565
tmr::Tmr,
6666
FailEntropy, HasCmr,
6767
};

src/merkle/ihr.rs

+49-50
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,30 @@ use hashes::sha256::Midstate;
99

1010
use super::{bip340_iv, compact_value, FailEntropy};
1111

12-
/// Identity Merkle root (first pass)
12+
/// Identity Merkle Root
1313
///
1414
/// A Merkle root that commits to a node's combinator, its witness data (if present),
1515
/// and recursively its children. Used as input to the [`Ihr`] type which is probably
1616
/// actually what you want.
1717
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
18-
pub struct FirstPassIhr(Midstate);
18+
pub struct Imr(Midstate);
1919

20-
impl_midstate_wrapper!(FirstPassIhr);
20+
impl_midstate_wrapper!(Imr);
2121

22-
/// Identity Merkle root
22+
/// Identity Hash Root
2323
///
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.
2626
///
2727
/// Uniquely identifies a program's structure in terms of combinators at redemption time.
2828
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
2929
pub struct Ihr(Midstate);
3030

3131
impl_midstate_wrapper!(Ihr);
3232

33-
impl From<Cmr> for FirstPassIhr {
33+
impl From<Cmr> for Imr {
3434
fn from(cmr: Cmr) -> Self {
35-
FirstPassIhr::from_byte_array(cmr.to_byte_array())
35+
Imr::from_byte_array(cmr.to_byte_array())
3636
}
3737
}
3838

@@ -48,7 +48,7 @@ impl From<Tmr> for Ihr {
4848
}
4949
}
5050

51-
impl FirstPassIhr {
51+
impl Imr {
5252
/// Produce a CMR for an iden combinator
5353
pub const fn iden() -> Self {
5454
Self::IDEN_IV
@@ -60,42 +60,42 @@ impl FirstPassIhr {
6060
}
6161

6262
/// Produce a CMR for an injl combinator
63-
pub fn injl(child: FirstPassIhr) -> Self {
63+
pub fn injl(child: Self) -> Self {
6464
Self::INJL_IV.update_1(child)
6565
}
6666

6767
/// Produce a CMR for an injr combinator
68-
pub fn injr(child: FirstPassIhr) -> Self {
68+
pub fn injr(child: Self) -> Self {
6969
Self::INJR_IV.update_1(child)
7070
}
7171

7272
/// Produce a CMR for a take combinator
73-
pub fn take(child: FirstPassIhr) -> Self {
73+
pub fn take(child: Self) -> Self {
7474
Self::TAKE_IV.update_1(child)
7575
}
7676

7777
/// Produce a CMR for a drop combinator
78-
pub fn drop(child: FirstPassIhr) -> Self {
78+
pub fn drop(child: Self) -> Self {
7979
Self::DROP_IV.update_1(child)
8080
}
8181

8282
/// 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 {
8484
Self::COMP_IV.update(left, right)
8585
}
8686

8787
/// 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 {
8989
Self::CASE_IV.update(left, right)
9090
}
9191

9292
/// 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 {
9494
Self::PAIR_IV.update(left, right)
9595
}
9696

9797
/// 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 {
9999
Self::DISCONNECT_IV.update(left, right)
100100
}
101101

@@ -109,7 +109,7 @@ impl FirstPassIhr {
109109
let mut engine = sha256::HashEngine::from_midstate(Self::WITNESS_IV.0, 0);
110110
engine.input(&value_hash[..]);
111111
engine.input(ty.target.tmr().as_ref());
112-
FirstPassIhr(engine.midstate())
112+
Self(engine.midstate())
113113
}
114114

115115
/// Produce an IHR for a fail combinator
@@ -131,95 +131,95 @@ impl FirstPassIhr {
131131
}
132132

133133
#[rustfmt::skip]
134-
const IDEN_IV: FirstPassIhr = FirstPassIhr(Midstate([
134+
const IDEN_IV: Self = Self(Midstate([
135135
0x54, 0x1a, 0x1a, 0x69, 0xbd, 0x4b, 0xcb, 0xda,
136136
0x7f, 0x34, 0x31, 0x0e, 0x30, 0x78, 0xf7, 0x26,
137137
0x44, 0x31, 0x22, 0xfb, 0xcc, 0x1c, 0xb5, 0x36,
138138
0x0c, 0x78, 0x64, 0xec, 0x0d, 0x32, 0x3a, 0xc0,
139139
]));
140140

141141
#[rustfmt::skip]
142-
const UNIT_IV: FirstPassIhr = FirstPassIhr(Midstate([
142+
const UNIT_IV: Self = Self(Midstate([
143143
0xc4, 0x0a, 0x10, 0x26, 0x3f, 0x74, 0x36, 0xb4,
144144
0x16, 0x0a, 0xcb, 0xef, 0x1c, 0x36, 0xfb, 0xa4,
145145
0xbe, 0x4d, 0x95, 0xdf, 0x18, 0x1a, 0x96, 0x8a,
146146
0xfe, 0xab, 0x5e, 0xac, 0x24, 0x7a, 0xdf, 0xf7,
147147
]));
148148

149149
#[rustfmt::skip]
150-
const INJL_IV: FirstPassIhr = FirstPassIhr(Midstate([
150+
const INJL_IV: Self = Self(Midstate([
151151
0x54, 0xe9, 0x1d, 0x18, 0xd8, 0xf8, 0x1f, 0x6d,
152152
0x29, 0x86, 0xbb, 0x58, 0x47, 0x9a, 0x54, 0xeb,
153153
0x63, 0x0e, 0x95, 0x23, 0xb6, 0x9e, 0xe8, 0x53,
154154
0x29, 0x80, 0xd0, 0x55, 0x58, 0x19, 0x4f, 0x15,
155155
]));
156156

157157
#[rustfmt::skip]
158-
const INJR_IV: FirstPassIhr = FirstPassIhr(Midstate([
158+
const INJR_IV: Self = Self(Midstate([
159159
0xd7, 0x0f, 0xfd, 0xce, 0x97, 0x77, 0x7b, 0x4d,
160160
0xfe, 0x31, 0xfd, 0x9f, 0xf5, 0xd0, 0x17, 0xa6,
161161
0x30, 0x5d, 0x7e, 0xc6, 0x0d, 0xf3, 0xb1, 0xbf,
162162
0x6d, 0x25, 0xe8, 0x16, 0x33, 0xde, 0xd4, 0xbf,
163163
]));
164164

165165
#[rustfmt::skip]
166-
const TAKE_IV: FirstPassIhr = FirstPassIhr(Midstate([
166+
const TAKE_IV: Self = Self(Midstate([
167167
0x50, 0x5f, 0xc0, 0x81, 0xb5, 0xba, 0x2a, 0xcd,
168168
0x09, 0x50, 0x67, 0xc3, 0xdf, 0xb8, 0xea, 0x12,
169169
0x6f, 0xa1, 0x5d, 0x55, 0xcb, 0x21, 0x1e, 0x6a,
170170
0xed, 0x34, 0xe8, 0xd1, 0xe3, 0x7a, 0xf0, 0xfa,
171171
]));
172172

173173
#[rustfmt::skip]
174-
const DROP_IV: FirstPassIhr = FirstPassIhr(Midstate([
174+
const DROP_IV: Self = Self(Midstate([
175175
0x8a, 0x30, 0x8d, 0x38, 0xa1, 0x13, 0xa2, 0x60,
176176
0xb4, 0xc7, 0x14, 0x5a, 0xbd, 0xc5, 0x22, 0x4d,
177177
0xeb, 0x70, 0x13, 0x79, 0x59, 0x0e, 0x0c, 0x8c,
178178
0x38, 0x86, 0x0b, 0xab, 0x12, 0x71, 0xa8, 0xa8,
179179
]));
180180

181181
#[rustfmt::skip]
182-
const COMP_IV: FirstPassIhr = FirstPassIhr(Midstate([
182+
const COMP_IV: Self = Self(Midstate([
183183
0x57, 0xec, 0x23, 0xa2, 0xa4, 0x77, 0x8e, 0x01,
184184
0x58, 0xa6, 0x21, 0x7a, 0xea, 0x3e, 0xf7, 0x42,
185185
0x8b, 0xa0, 0x90, 0x92, 0x73, 0xb9, 0x73, 0xfa,
186186
0x14, 0x32, 0xa9, 0x27, 0x84, 0x3e, 0x92, 0x7a,
187187
]));
188188

189189
#[rustfmt::skip]
190-
const CASE_IV: FirstPassIhr = FirstPassIhr(Midstate([
190+
const CASE_IV: Self = Self(Midstate([
191191
0x29, 0x5e, 0x2a, 0x6d, 0xc8, 0xc5, 0xce, 0x59,
192192
0xe4, 0xed, 0xcf, 0xe9, 0xb4, 0xd8, 0xf7, 0x64,
193193
0x13, 0x3a, 0xa5, 0x51, 0x4b, 0xd3, 0xee, 0x8b,
194194
0x4b, 0x75, 0xec, 0x8f, 0x4d, 0xeb, 0x08, 0xbe,
195195
]));
196196

197197
#[rustfmt::skip]
198-
const PAIR_IV: FirstPassIhr = FirstPassIhr(Midstate([
198+
const PAIR_IV: Self = Self(Midstate([
199199
0x7d, 0x5e, 0x6d, 0xac, 0x15, 0xb1, 0x42, 0x8a,
200200
0x0d, 0x26, 0x0c, 0x94, 0x29, 0xdb, 0xe8, 0x89,
201201
0x65, 0x93, 0xf3, 0x1f, 0x70, 0x86, 0x27, 0xee,
202202
0x75, 0xb2, 0x7e, 0xee, 0xfd, 0xd0, 0x50, 0x05,
203203
]));
204204

205205
#[rustfmt::skip]
206-
const DISCONNECT_IV: FirstPassIhr = FirstPassIhr(Midstate([
206+
const DISCONNECT_IV: Self = Self(Midstate([
207207
0x4e, 0xb7, 0x99, 0x5f, 0xb5, 0xdd, 0xe5, 0xd0,
208208
0x85, 0xf4, 0x70, 0x85, 0xcd, 0x95, 0x3d, 0x16,
209209
0x84, 0x54, 0x11, 0xed, 0xc6, 0x89, 0xe2, 0x7a,
210210
0xf9, 0xc3, 0xde, 0xa2, 0xfb, 0x12, 0x25, 0xd5,
211211
]));
212212

213213
#[rustfmt::skip]
214-
const WITNESS_IV: FirstPassIhr = FirstPassIhr(Midstate([
214+
const WITNESS_IV: Self = Self(Midstate([
215215
0xcb, 0x37, 0xff, 0x70, 0x01, 0xc6, 0x2d, 0x94,
216216
0x42, 0x4f, 0x98, 0x7f, 0x30, 0x23, 0xb3, 0x5e,
217217
0x30, 0xd2, 0x17, 0x23, 0x96, 0x27, 0x6f, 0x89,
218218
0xd0, 0x9f, 0x07, 0xaa, 0x67, 0xb6, 0x21, 0x96,
219219
]));
220220

221221
#[rustfmt::skip]
222-
const FAIL_IV: FirstPassIhr = FirstPassIhr(Midstate([
222+
const FAIL_IV: Self = Self(Midstate([
223223
0x22, 0x83, 0xc1, 0x81, 0x9e, 0x69, 0x2f, 0x96,
224224
0x85, 0xfe, 0x95, 0x40, 0x76, 0xc5, 0x16, 0x7c,
225225
0x03, 0xbd, 0xe7, 0xcc, 0xda, 0xab, 0x00, 0x5e,
@@ -228,11 +228,10 @@ impl FirstPassIhr {
228228
}
229229

230230
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 {
234233
let iv = Ihr(bip340_iv(b"Simplicity\x1fIdentity"));
235-
iv.update_1(Ihr(first_pass.0))
234+
iv.update_1(Ihr(imr.0))
236235
.update(ty.source.tmr().into(), ty.target.tmr().into())
237236
}
238237
}
@@ -244,38 +243,38 @@ mod tests {
244243
#[test]
245244
#[rustfmt::skip] // wants to split up the check_iv lines below
246245
fn ivs() {
247-
fn check_iv(target: FirstPassIhr, s: &'static str) {
246+
fn check_iv(target: Imr, s: &'static str) {
248247
let name = s
249248
.trim_start_matches("Simplicity\x1f")
250249
.trim_start_matches("Commitment\x1f")
251250
.trim_start_matches("Identity\x1f");
252251
// Uncomment this if the IVs ever change
253252
/*
254-
let target = FirstPassIhr(bip340_iv(s.as_bytes()));
253+
let target = Imr(bip340_iv(s.as_bytes()));
255254
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());
257256
print!(" "); for ch in &target.0[0..8] { print!(" 0x{:02x},", ch); }; println!();
258257
print!(" "); for ch in &target.0[8..16] { print!(" 0x{:02x},", ch); }; println!();
259258
print!(" "); for ch in &target.0[16..24] { print!(" 0x{:02x},", ch); }; println!();
260259
print!(" "); for ch in &target.0[24..32] { print!(" 0x{:02x},", ch); }; println!();
261260
println!(" ]));");
262261
println!();
263262
*/
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);
265264
}
266265

267266
// Note that these are the same as those for CMRs **except** for disconnect and witness.
268-
check_iv(FirstPassIhr::IDEN_IV, "Simplicity\x1fCommitment\x1fiden");
269-
check_iv(FirstPassIhr::UNIT_IV, "Simplicity\x1fCommitment\x1funit");
270-
check_iv(FirstPassIhr::INJL_IV, "Simplicity\x1fCommitment\x1finjl");
271-
check_iv(FirstPassIhr::INJR_IV, "Simplicity\x1fCommitment\x1finjr");
272-
check_iv(FirstPassIhr::TAKE_IV, "Simplicity\x1fCommitment\x1ftake");
273-
check_iv(FirstPassIhr::DROP_IV, "Simplicity\x1fCommitment\x1fdrop");
274-
check_iv(FirstPassIhr::COMP_IV, "Simplicity\x1fCommitment\x1fcomp");
275-
check_iv(FirstPassIhr::CASE_IV, "Simplicity\x1fCommitment\x1fcase");
276-
check_iv(FirstPassIhr::PAIR_IV, "Simplicity\x1fCommitment\x1fpair");
277-
check_iv(FirstPassIhr::DISCONNECT_IV, "Simplicity\x1fIdentity\x1fdisconnect");
278-
check_iv(FirstPassIhr::WITNESS_IV, "Simplicity\x1fIdentity\x1fwitness");
279-
check_iv(FirstPassIhr::FAIL_IV, "Simplicity\x1fCommitment\x1ffail");
267+
check_iv(Imr::IDEN_IV, "Simplicity\x1fCommitment\x1fiden");
268+
check_iv(Imr::UNIT_IV, "Simplicity\x1fCommitment\x1funit");
269+
check_iv(Imr::INJL_IV, "Simplicity\x1fCommitment\x1finjl");
270+
check_iv(Imr::INJR_IV, "Simplicity\x1fCommitment\x1finjr");
271+
check_iv(Imr::TAKE_IV, "Simplicity\x1fCommitment\x1ftake");
272+
check_iv(Imr::DROP_IV, "Simplicity\x1fCommitment\x1fdrop");
273+
check_iv(Imr::COMP_IV, "Simplicity\x1fCommitment\x1fcomp");
274+
check_iv(Imr::CASE_IV, "Simplicity\x1fCommitment\x1fcase");
275+
check_iv(Imr::PAIR_IV, "Simplicity\x1fCommitment\x1fpair");
276+
check_iv(Imr::DISCONNECT_IV, "Simplicity\x1fIdentity\x1fdisconnect");
277+
check_iv(Imr::WITNESS_IV, "Simplicity\x1fIdentity\x1fwitness");
278+
check_iv(Imr::FAIL_IV, "Simplicity\x1fCommitment\x1ffail");
280279
}
281280
}

0 commit comments

Comments
 (0)