Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions jets-bench/src/data_structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use simplicity::{
/// length.
#[derive(Clone)]
pub struct SimplicityCtx8 {
buffer: [u8; 512],
buffer: [u8; 64],
h: [u32; 8],
length: usize,
}
Expand All @@ -31,7 +31,7 @@ impl Default for SimplicityCtx8 {
0x5be0cd19,
],
length: 0,
buffer: [0; 512],
buffer: [0; 64],
}
}
}
Expand All @@ -45,24 +45,26 @@ impl SimplicityCtx8 {
}
}

/// Read bytes from a Simplicity buffer of type (TWO^8)^<2^(n+1) as [`Value`].
/// Read bytes from a Simplicity buffer of type (TWO^8)^<2^n as [`Value`].
/// The notation X^<2 is notation for the type (S X)
/// The notation X^<(2*n) is notation for the type S (X^n) * X^<n
///
/// Cannot represent >= 2**16 bytes 0 <= n < 16 as simplicity consensus rule.
/// There is a precondition that 0 <= n <= 16.
///
/// # Panics:
///
/// Panics if the length of the slice is >= 2^(n + 1) bytes
pub fn var_len_buf_from_slice(v: &[u8], mut n: usize) -> Result<Value, EarlyEndOfStreamError> {
// Simplicity consensus rule for n < 16 while reading buffers.
assert!(n < 16);
assert!(v.len() < (1 << (n + 1)));
/// Panics if the length of the slice is >= 2^n bytes or if n > 16.
pub fn var_len_buf_from_slice(v: &[u8], n: usize) -> Result<Value, EarlyEndOfStreamError> {
// This precondition can probably be removed or relaxed, though
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It wasn't immediately obvious what this code was doing, so I had to read the doc comment, write it myself, then compare my code against this. It might've been clearer if while n > 0 was replaced with for i in (0..n).rev() (since the loop is just counting down to zero), plus that means you can get rid of (n - 1) (subtraction on usize always makes me do a double-take) and it changes two_two_n(n + 2) to two_two_n(i + 3) (where + 3 is obviously just a bytes-to-bits conversion).

That's all nit-picking though. Otherwise LGTM.

// we have no need to do so and it may require changing the type
// of 'n' to something larger.
assert!(n <= 16);
assert!(v.len() < 1 << n);
let mut iter = BitIter::new(v.iter().copied());
let mut res = None;
while n > 0 {
let ty = Final::two_two_n(n);
let v = if v.len() >= (1 << (n + 1)) {
for i in (0..n).rev() {
let ty = Final::two_two_n(i + 3);
let v = if v.len() & 1 << i != 0 {
let val = Value::from_compact_bits(&mut iter, &ty)?;
Value::some(val)
} else {
Expand All @@ -72,8 +74,8 @@ pub fn var_len_buf_from_slice(v: &[u8], mut n: usize) -> Result<Value, EarlyEndO
Some(prod) => Some(Value::product(prod, v)),
None => Some(v),
};
n -= 1;
}
assert_eq!(iter.next(), None);
Ok(res.unwrap_or(Value::unit()))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I'm going to actually recommend that you have precondition that 1 <= n, because 𝟙 is not actually a buffer type, and if people are passing 0 here it is probably a big mistake.

If you strongly disagree then you can keep the code as it is.

Copy link
Contributor

@roconnor-blockstream roconnor-blockstream Nov 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I take that back. Arguably 𝟙 is a buffer type for a degenerate empty buffer type. So you can leave the code as is.

}

Expand Down Expand Up @@ -157,8 +159,8 @@ pub trait SimplicityEncode {

impl SimplicityEncode for SimplicityCtx8 {
fn value(&self) -> Value {
let buf_len = self.length % 512;
let buf = var_len_buf_from_slice(&self.buffer[..buf_len], 8).unwrap();
let buf_len = self.length % 64;
let buf = var_len_buf_from_slice(&self.buffer[..buf_len], 6).unwrap();
let len = Value::u64(self.length as u64);
// convert to 32 byte array
let mut arr = [0u8; 32];
Expand Down