Skip to content

Commit 9c854eb

Browse files
committed
Separate Jet env from Jet trait
1 parent 80f3821 commit 9c854eb

File tree

16 files changed

+1061
-970
lines changed

16 files changed

+1061
-970
lines changed

jets-bench/benches/elements/main.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use elements::confidential;
55
use rand::rngs::ThreadRng;
66
use simplicity::elements;
77
use simplicity::jet::elements::ElementsEnv;
8-
use simplicity::jet::{Elements, Jet};
8+
use simplicity::jet::{Elements, ElementsTxEnv, Jet, JetEnvironment};
99
use simplicity::types;
1010
use simplicity::types::Final;
1111
use simplicity::Value;
@@ -625,7 +625,7 @@ fn bench(c: &mut Criterion) {
625625
let (src, dst) = buffer.write(&src_ty, params, &mut rng);
626626
(dst, src, &env, buffer)
627627
},
628-
|(mut dst, src, env, _buffer)| jet.c_jet_ptr()(&mut dst, src, env.c_tx_env()),
628+
|(mut dst, src, env, _buffer)| ElementsTxEnv::c_jet_ptr(&jet)(&mut dst, src, env.c_tx_env()),
629629
BatchSize::SmallInput,
630630
)
631631
});
@@ -743,7 +743,7 @@ fn bench(c: &mut Criterion) {
743743
let (src, dst) = buffer.write(&src_ty, params, &mut rng);
744744
(dst, src, buffer)
745745
},
746-
|(mut dst, src, _buffer)| jet.c_jet_ptr()(&mut dst, src, env.c_tx_env()),
746+
|(mut dst, src, _buffer)| ElementsTxEnv::c_jet_ptr(&jet)(&mut dst, src, env.c_tx_env()),
747747
BatchSize::SmallInput,
748748
)
749749
});
@@ -806,7 +806,7 @@ fn bench(c: &mut Criterion) {
806806
let (src, dst) = buffer.write(&src_ty, params, &mut rng);
807807
(dst, src, buffer)
808808
},
809-
|(mut dst, src, _buffer)| jet.c_jet_ptr()(&mut dst, src, env.c_tx_env()),
809+
|(mut dst, src, _buffer)| ElementsTxEnv::c_jet_ptr(&jet)(&mut dst, src, env.c_tx_env()),
810810
BatchSize::SmallInput,
811811
)
812812
});
@@ -903,7 +903,7 @@ fn bench(c: &mut Criterion) {
903903
let (src, dst) = buffer.write(&src_ty, params, &mut rng);
904904
(dst, src, buffer)
905905
},
906-
|(mut dst, src, _buffer)| jet.c_jet_ptr()(&mut dst, src, env.c_tx_env()),
906+
|(mut dst, src, _buffer)| ElementsTxEnv::c_jet_ptr(&jet)(&mut dst, src, env.c_tx_env()),
907907
BatchSize::SmallInput,
908908
)
909909
});

jets-bench/src/input.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use simplicity::ffi::c_jets::frame_ffi::c_writeBit;
88
use simplicity::ffi::ffi::UWORD;
99
use simplicity::ffi::CFrameItem;
1010
use simplicity::hashes::Hash;
11-
use simplicity::jet::Elements;
11+
use simplicity::jet::{Elements, ElementsTxEnv, JetEnvironment};
1212
use simplicity::types::{self, CompleteBound};
1313
use simplicity::Value;
1414

@@ -296,7 +296,6 @@ impl FlatValue {
296296
fn call_jet(&self, jet: Elements, dest_bits: usize) -> Self {
297297
use core::{mem, ptr};
298298
use simplicity::ffi::c_jets::uword_width;
299-
use simplicity::jet::Jet as _;
300299

301300
assert!(dest_bits <= 8 * MAX_VALUE_BYTES);
302301
let mut ret = Self::zero_n_bits(dest_bits);
@@ -354,10 +353,11 @@ impl FlatValue {
354353

355354
// We can assert this because in our sampling code jets should never
356355
// fail. In the benchmarking code they might.
357-
assert!(jet.c_jet_ptr()(
356+
357+
assert!(ElementsTxEnv::c_jet_ptr(&jet)(
358358
&mut dst_write_frame,
359359
src_read_frame,
360-
Elements::c_jet_env(&env)
360+
env.c_tx_env()
361361
));
362362
// The write frame winds up as an array of usizes with all bytes in
363363
// reverse order. (The bytes of the usizes are in reverse order due
@@ -1366,8 +1366,12 @@ impl InputSample for DivMod12864Input {
13661366
for (bit1, bit2) in sample_1.bit_iter().zip(sample_2.bit_iter()) {
13671367
match (bit1, bit2) {
13681368
(false, false) | (true, true) => {} // both equal
1369-
(true, false) => return FlatValue::product(&[sample_2, UniformBits.sample(0, 64), sample_1]),
1370-
(false, true) => return FlatValue::product(&[sample_1, UniformBits.sample(0, 64), sample_2]),
1369+
(true, false) => {
1370+
return FlatValue::product(&[sample_2, UniformBits.sample(0, 64), sample_1])
1371+
}
1372+
(false, true) => {
1373+
return FlatValue::product(&[sample_1, UniformBits.sample(0, 64), sample_2])
1374+
}
13711375
}
13721376
}
13731377
unreachable!("if we get here, two uniform 63-bit samples were exactly equal")
@@ -1428,7 +1432,7 @@ mod tests {
14281432

14291433
let (src, mut dst) = buffer.write(&src_ty, &params, &mut rand::thread_rng());
14301434

1431-
jet.c_jet_ptr()(&mut dst, src, env.c_tx_env());
1435+
ElementsTxEnv::c_jet_ptr(&jet)(&mut dst, src, env.c_tx_env());
14321436
}
14331437

14341438
#[test]

src/bit_machine/mod.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use std::fmt;
1515
use std::sync::Arc;
1616

1717
use crate::analysis;
18+
use crate::jet::JetEnvironment;
1819
use crate::jet::{Jet, JetFailed};
1920
use crate::node::{self, RedeemNode};
2021
use crate::types::Final;
@@ -60,9 +61,9 @@ impl BitMachine {
6061
}
6162

6263
#[cfg(test)]
63-
pub fn test_exec<J: Jet>(
64-
program: Arc<crate::node::ConstructNode<J>>,
65-
env: &J::Environment,
64+
pub fn test_exec<JE: JetEnvironment>(
65+
program: Arc<crate::node::ConstructNode<JE::Jet>>,
66+
env: &JE,
6667
) -> Result<Value, ExecutionError> {
6768
use crate::node::SimpleFinalizer;
6869

@@ -220,10 +221,10 @@ impl BitMachine {
220221
/// ## Precondition
221222
///
222223
/// The Bit Machine is constructed via [`Self::for_program()`] to ensure enough space.
223-
pub fn exec<J: Jet>(
224+
pub fn exec<JE: JetEnvironment>(
224225
&mut self,
225-
program: &RedeemNode<J>,
226-
env: &J::Environment,
226+
program: &RedeemNode<JE::Jet>,
227+
env: &JE,
227228
) -> Result<Value, ExecutionError> {
228229
self.exec_with_tracker(program, env, &mut NoTracker)
229230
}
@@ -236,22 +237,22 @@ impl BitMachine {
236237
/// ## Precondition
237238
///
238239
/// The Bit Machine is constructed via [`Self::for_program()`] to ensure enough space.
239-
pub fn exec_with_tracker<J: Jet, T: ExecTracker<J>>(
240+
pub fn exec_with_tracker<JE: JetEnvironment, T: ExecTracker<JE::Jet>>(
240241
&mut self,
241-
program: &RedeemNode<J>,
242-
env: &J::Environment,
242+
program: &RedeemNode<JE::Jet>,
243+
env: &JE,
243244
tracker: &mut T,
244245
) -> Result<Value, ExecutionError> {
245-
enum CallStack<'a, J: Jet> {
246-
Goto(&'a RedeemNode<J>),
246+
enum CallStack<'a, JE: JetEnvironment> {
247+
Goto(&'a RedeemNode<JE::Jet>),
247248
MoveWriteFrameToRead,
248249
DropReadFrame,
249250
CopyFwd(usize),
250251
Back(usize),
251252
}
252253

253254
// Not used, but useful for debugging, so keep it around
254-
impl<J: Jet> fmt::Debug for CallStack<'_, J> {
255+
impl<JE: JetEnvironment> fmt::Debug for CallStack<'_, JE> {
255256
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
256257
match self {
257258
CallStack::Goto(ins) => write!(f, "goto {}", ins.inner()),
@@ -268,7 +269,7 @@ impl BitMachine {
268269
}
269270

270271
let mut ip = program;
271-
let mut call_stack = vec![];
272+
let mut call_stack: Vec<CallStack<'_, JE>> = vec![];
272273

273274
let output_width = ip.arrow().target.bit_width();
274275
if output_width > 0 {
@@ -435,7 +436,7 @@ impl BitMachine {
435436
}
436437
}
437438

438-
fn exec_jet<J: Jet>(&mut self, jet: J, env: &J::Environment) -> Result<(), JetFailed> {
439+
fn exec_jet<JE: JetEnvironment>(&mut self, jet: JE::Jet, env: &JE) -> Result<(), JetFailed> {
439440
use crate::ffi::c_jets::frame_ffi::{c_readBit, c_writeBit, CFrameItem};
440441
use crate::ffi::c_jets::uword_width;
441442
use crate::ffi::ffi::UWORD;
@@ -524,8 +525,8 @@ impl BitMachine {
524525
let (input_read_frame, _input_buffer) = unsafe { get_input_frame(self, input_width) };
525526
let (mut output_write_frame, output_buffer) = unsafe { get_output_frame(output_width) };
526527

527-
let jet_fn = jet.c_jet_ptr();
528-
let c_env = J::c_jet_env(env);
528+
let jet_fn = JE::c_jet_ptr(&jet);
529+
let c_env = env.c_jet_env();
529530
let success = jet_fn(&mut output_write_frame, input_read_frame, c_env);
530531

531532
if !success {
@@ -598,6 +599,7 @@ impl From<JetFailed> for ExecutionError {
598599
mod tests {
599600
use super::*;
600601

602+
use crate::jet::CoreEnv;
601603
#[cfg(feature = "elements")]
602604
use crate::jet::{elements::ElementsEnv, Elements};
603605
#[cfg(feature = "elements")]
@@ -697,7 +699,7 @@ mod tests {
697699
for _ in 0..100 {
698700
bomb = Node::pair(&bomb, &bomb).unwrap();
699701
}
700-
let _ = bomb.finalize_pruned(&());
702+
let _ = bomb.finalize_pruned(&CoreEnv::new());
701703
});
702704
}
703705
}

src/human_encoding/mod.rs

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -224,19 +224,19 @@ impl<J: Jet> Forest<J> {
224224
#[cfg(test)]
225225
mod tests {
226226
use crate::human_encoding::Forest;
227-
use crate::jet::{Core, Jet};
227+
use crate::jet::{CoreEnv, JetEnvironment};
228228
use crate::types;
229229
use crate::{BitMachine, Value};
230230
use std::collections::HashMap;
231231
use std::sync::Arc;
232232

233-
fn assert_finalize_ok<J: Jet>(
233+
fn assert_finalize_ok<JE: JetEnvironment>(
234234
s: &str,
235235
witness: &HashMap<Arc<str>, Value>,
236-
env: &J::Environment,
236+
env: &JE,
237237
) {
238238
types::Context::with_context(|ctx| {
239-
let program = Forest::<J>::parse(s)
239+
let program = Forest::<JE::Jet>::parse(s)
240240
.expect("Failed to parse human encoding")
241241
.to_witness_node(&ctx, witness)
242242
.expect("Forest is missing expected root")
@@ -247,14 +247,14 @@ mod tests {
247247
});
248248
}
249249

250-
fn assert_finalize_err<J: Jet>(
250+
fn assert_finalize_err<JE: JetEnvironment>(
251251
s: &str,
252252
witness: &HashMap<Arc<str>, Value>,
253-
env: &J::Environment,
253+
env: &JE,
254254
err_msg: &'static str,
255255
) {
256256
types::Context::with_context(|ctx| {
257-
let program = match Forest::<J>::parse(s)
257+
let program = match Forest::<JE::Jet>::parse(s)
258258
.expect("Failed to parse human encoding")
259259
.to_witness_node(&ctx, witness)
260260
.expect("Forest is missing expected root")
@@ -290,19 +290,24 @@ mod tests {
290290
(Arc::from("a"), Value::u8(0x00)),
291291
(Arc::from("b"), Value::u8(0x01)),
292292
]);
293-
assert_finalize_ok::<Core>(s, &a_less_than_b, &());
293+
assert_finalize_ok::<CoreEnv>(s, &a_less_than_b, &CoreEnv::new());
294294

295295
let b_greater_equal_a = HashMap::from([
296296
(Arc::from("a"), Value::u8(0x01)),
297297
(Arc::from("b"), Value::u8(0x01)),
298298
]);
299-
assert_finalize_err::<Core>(s, &b_greater_equal_a, &(), "Jet failed during execution");
299+
assert_finalize_err::<CoreEnv>(
300+
s,
301+
&b_greater_equal_a,
302+
&CoreEnv::new(),
303+
"Jet failed during execution",
304+
);
300305
}
301306

302307
#[test]
303308
fn executed_witness_without_value() {
304309
let witness = HashMap::from([(Arc::from("wit1"), Value::u32(1337))]);
305-
assert_finalize_err::<Core>(
310+
assert_finalize_err::<CoreEnv>(
306311
"
307312
wit1 := witness : 1 -> 2^32
308313
wit2 := witness : 1 -> 2^32
@@ -311,7 +316,7 @@ mod tests {
311316
main := comp wits_are_equal jet_verify : 1 -> 1
312317
",
313318
&witness,
314-
&(),
319+
&CoreEnv::new(),
315320
"Jet failed during execution",
316321
);
317322
}
@@ -326,42 +331,47 @@ mod tests {
326331
main := comp input comp process jet_verify : 1 -> 1
327332
";
328333
let wit2_is_pruned = HashMap::from([(Arc::from("wit1"), Value::u1(0))]);
329-
assert_finalize_ok::<Core>(s, &wit2_is_pruned, &());
334+
assert_finalize_ok::<CoreEnv>(s, &wit2_is_pruned, &CoreEnv::new());
330335

331336
let wit2_is_missing = HashMap::from([(Arc::from("wit1"), Value::u1(1))]);
332-
assert_finalize_err::<Core>(s, &wit2_is_missing, &(), "Jet failed during execution");
337+
assert_finalize_err::<CoreEnv>(
338+
s,
339+
&wit2_is_missing,
340+
&CoreEnv::new(),
341+
"Jet failed during execution",
342+
);
333343

334344
let wit2_is_present = HashMap::from([
335345
(Arc::from("wit1"), Value::u1(1)),
336346
(Arc::from("wit2"), Value::u64(u64::MAX)),
337347
]);
338-
assert_finalize_ok::<Core>(s, &wit2_is_present, &());
348+
assert_finalize_ok::<CoreEnv>(s, &wit2_is_present, &CoreEnv::new());
339349
}
340350

341351
#[test]
342352
fn executed_hole_with_value() {
343353
let empty = HashMap::new();
344-
assert_finalize_ok::<Core>(
354+
assert_finalize_ok::<CoreEnv>(
345355
"
346356
id1 := iden : 2^256 * 1 -> 2^256 * 1
347357
main := comp (disconnect id1 ?hole) unit
348358
hole := unit
349359
",
350360
&empty,
351-
&(),
361+
&CoreEnv::new(),
352362
);
353363
}
354364

355365
#[test]
356366
fn executed_hole_without_value() {
357367
let empty = HashMap::new();
358-
assert_finalize_err::<Core>(
368+
assert_finalize_err::<CoreEnv>(
359369
"
360370
wit1 := witness
361371
main := comp wit1 comp disconnect iden ?dis2 unit
362372
",
363373
&empty,
364-
&(),
374+
&CoreEnv::new(),
365375
"disconnect node had one child (redeem time); must have two",
366376
);
367377
}
@@ -374,9 +384,14 @@ mod tests {
374384
main := comp wit2 jet_verify : 1 -> 1
375385
";
376386
let wit1_populated = HashMap::from([(Arc::from("wit1"), Value::u1(1))]);
377-
assert_finalize_err::<Core>(s, &wit1_populated, &(), "Jet failed during execution");
387+
assert_finalize_err::<CoreEnv>(
388+
s,
389+
&wit1_populated,
390+
&CoreEnv::new(),
391+
"Jet failed during execution",
392+
);
378393

379394
let wit2_populated = HashMap::from([(Arc::from("wit2"), Value::u1(1))]);
380-
assert_finalize_ok::<Core>(s, &wit2_populated, &());
395+
assert_finalize_ok::<CoreEnv>(s, &wit2_populated, &CoreEnv::new());
381396
}
382397
}

0 commit comments

Comments
 (0)