Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Value refactor #237

Merged
Merged
Show file tree
Hide file tree
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
Empty file.
12 changes: 6 additions & 6 deletions jets-bench/benches/elements/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -755,30 +755,30 @@ fn bench(c: &mut Criterion) {
let ctx8 = SimplicityCtx8::with_len(511).value();
let genesis_pegin = genesis_pegin();
let outpoint = elements::OutPoint::sample().value();
Value::prod(ctx8, Value::prod(genesis_pegin, outpoint))
Value::product(ctx8, Value::product(genesis_pegin, outpoint))
}

fn asset_amount_hash() -> Arc<Value> {
let ctx8 = SimplicityCtx8::with_len(511).value();
let asset = confidential::Asset::sample().value();
let amount = confidential::Value::sample().value();
Value::prod(ctx8, Value::prod(asset, amount))
Value::product(ctx8, Value::product(asset, amount))
}

fn nonce_hash() -> Arc<Value> {
let ctx8 = SimplicityCtx8::with_len(511).value();
let nonce = confidential::Nonce::sample().value();
Value::prod(ctx8, nonce)
Value::product(ctx8, nonce)
}

fn annex_hash() -> Arc<Value> {
let ctx8 = SimplicityCtx8::with_len(511).value();
let annex = if rand::random() {
Value::sum_r(Value::u256_from_slice(&rand::random::<[u8; 32]>()))
Value::right(Value::u256(&rand::random::<[u8; 32]>()))
} else {
Value::sum_l(Value::unit())
Value::left(Value::unit())
};
Value::prod(ctx8, annex)
Value::product(ctx8, annex)
}
let arr: [(Elements, Arc<dyn Fn() -> Arc<Value>>); 4] = [
(Elements::OutpointHash, Arc::new(&outpoint_hash)),
Expand Down
72 changes: 39 additions & 33 deletions jets-bench/src/data_structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ pub fn var_len_buf_from_slice(v: &[u8], mut n: usize) -> Result<Arc<Value>, Erro
let v = if v.len() >= (1 << (n + 1)) {
let ty = &types[n];
let val = iter.read_value(&ty.final_data().unwrap())?;
Value::sum_r(val)
Value::right(val)
} else {
Value::sum_l(Value::unit())
Value::left(Value::unit())
};
res = match res {
Some(prod) => Some(Value::prod(prod, v)),
Some(prod) => Some(Value::product(prod, v)),
None => Some(v),
};
n -= 1;
Expand Down Expand Up @@ -164,35 +164,36 @@ impl SimplicityEncode for SimplicityCtx8 {
let buf = var_len_buf_from_slice(&self.buffer[..buf_len], 8).unwrap();
let len = Value::u64(self.length as u64);
// convert to 32 byte array
let arr = self
.h
.iter()
.flat_map(|x| x.to_be_bytes())
.collect::<Vec<u8>>();
let mid_state = Value::u256_from_slice(&arr);
Value::prod(buf, Value::prod(len, mid_state))
let mut arr = [0u8; 32];
for (i, byte) in self.h.iter().flat_map(|x| x.to_be_bytes()).enumerate() {
arr[i] = byte;
}
let mid_state = Value::u256(&arr);
Value::product(buf, Value::product(len, mid_state))
}
}

impl SimplicityEncode for elements::OutPoint {
fn value(&self) -> Arc<Value> {
let txid = Value::u256_from_slice(&self.txid[..]);
let txid = Value::u256(self.txid.as_byte_array());
let vout = Value::u32(self.vout);
Value::prod(txid, vout)
Value::product(txid, vout)
}
}

impl SimplicityEncode for elements::confidential::Asset {
fn value(&self) -> Arc<Value> {
match self {
elements::confidential::Asset::Explicit(a) => {
Value::sum_r(Value::u256_from_slice(&a.into_inner()[..]))
Value::right(Value::u256(&a.into_inner().0))
}
elements::confidential::Asset::Confidential(gen) => {
let odd_gen = gen.serialize()[0] & 1 == 1;
let x_pt = Value::u256_from_slice(&gen.serialize()[1..]);
let ser = gen.serialize();
let odd_gen = ser[0] & 1 == 1;
let x_bytes: &[u8; 32] = (&ser[1..33]).try_into().unwrap();
let x_pt = Value::u256(x_bytes);
let y_pt = Value::u1(odd_gen as u8);
Value::sum_l(Value::prod(y_pt, x_pt))
Value::left(Value::product(y_pt, x_pt))
}
elements::confidential::Asset::Null => panic!("Tried to encode Null asset"),
}
Expand All @@ -202,12 +203,13 @@ impl SimplicityEncode for elements::confidential::Asset {
impl SimplicityEncode for elements::confidential::Value {
fn value(&self) -> Arc<Value> {
match self {
elements::confidential::Value::Explicit(v) => Value::sum_r(Value::u64(*v)),
elements::confidential::Value::Explicit(v) => Value::right(Value::u64(*v)),
elements::confidential::Value::Confidential(v) => {
let ser = v.serialize();
let x_pt = Value::u256_from_slice(&ser[1..]);
let x_bytes: &[u8; 32] = (&ser[1..33]).try_into().unwrap();
let x_pt = Value::u256(x_bytes);
let y_pt = Value::u1((ser[0] & 1 == 1) as u8);
Value::sum_l(Value::prod(y_pt, x_pt))
Value::left(Value::product(y_pt, x_pt))
}
elements::confidential::Value::Null => panic!("Tried to encode Null value"),
}
Expand All @@ -218,22 +220,23 @@ impl SimplicityEncode for elements::confidential::Nonce {
fn value(&self) -> Arc<Value> {
match self {
elements::confidential::Nonce::Explicit(n) => {
Value::sum_r(Value::sum_r(Value::u256_from_slice(&n[..])))
Value::right(Value::right(Value::u256(n)))
}
elements::confidential::Nonce::Confidential(n) => {
let ser = n.serialize();
let x_pt = Value::u256_from_slice(&ser[1..]);
let x_bytes: &[u8; 32] = (&ser[1..33]).try_into().unwrap();
let x_pt = Value::u256(&x_bytes);
let y_pt = Value::u1((ser[0] & 1 == 1) as u8);
Value::sum_r(Value::sum_l(Value::prod(y_pt, x_pt)))
Value::right(Value::left(Value::product(y_pt, x_pt)))
}
elements::confidential::Nonce::Null => Value::sum_l(Value::unit()),
elements::confidential::Nonce::Null => Value::left(Value::unit()),
}
}
}

impl SimplicityEncode for SimplicityFe {
fn value(&self) -> Arc<Value> {
Value::u256_from_slice(self.as_inner())
Value::u256(self.as_inner())
}
}

Expand All @@ -249,32 +252,35 @@ impl SimplicityEncode for SimplicityGe {
ser
}
};
let x_pt = Value::u256_from_slice(&ser[1..33]);
let y_pt = Value::u256_from_slice(&ser[33..]);
Value::prod(x_pt, y_pt)
let x_bytes: &[u8; 32] = (&ser[1..33]).try_into().unwrap();
let y_bytes: &[u8; 32] = (&ser[33..65]).try_into().unwrap();
let x_pt = Value::u256(x_bytes);
let y_pt = Value::u256(y_bytes);
Value::product(x_pt, y_pt)
}
}

impl SimplicityEncode for SimplicityGej {
fn value(&self) -> Arc<Value> {
let ge = self.ge.value();
let z = self.z.value();
Value::prod(ge, z)
Value::product(ge, z)
}
}

impl SimplicityEncode for SimplicityScalar {
fn value(&self) -> Arc<Value> {
Value::u256_from_slice(&self.0[..])
Value::u256(&self.0)
}
}

impl SimplicityEncode for SimplicityPoint {
fn value(&self) -> Arc<Value> {
let ser = self.0.serialize(); // compressed
let x_bytes: &[u8; 32] = (&ser[1..33]).try_into().unwrap();
let y_pt = Value::u1((ser[0] & 1 == 1) as u8);
let x_pt = Value::u256_from_slice(&ser[1..]);
Value::prod(y_pt, x_pt)
let x_pt = Value::u256(x_bytes);
Value::product(y_pt, x_pt)
}
}

Expand Down Expand Up @@ -403,9 +409,9 @@ impl BenchSample for SimplicityPoint {
// Sample genesis pegin with 50% probability
pub fn genesis_pegin() -> Arc<Value> {
if rand::random() {
Value::sum_l(Value::unit())
Value::left(Value::unit())
} else {
let genesis_hash = rand::random::<[u8; 32]>();
Value::sum_r(Value::u256_from_slice(&genesis_hash[..]))
Value::right(Value::u256(&genesis_hash))
}
}
6 changes: 3 additions & 3 deletions jets-bench/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,16 @@ pub fn random_value(ty: &types::Final, rng: &mut ThreadRng) -> Arc<Value> {
},
StackItem::LeftSum => {
let left = value_stack.pop().unwrap();
value_stack.push(Value::sum_l(left));
value_stack.push(Value::left(left));
}
StackItem::RightSum => {
let right = value_stack.pop().unwrap();
value_stack.push(Value::sum_r(right));
value_stack.push(Value::right(right));
}
StackItem::Product => {
let right = value_stack.pop().unwrap();
let left = value_stack.pop().unwrap();
value_stack.push(Value::prod(left, right));
value_stack.push(Value::product(left, right));
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/bit_encoding/bititer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,16 +252,16 @@ impl<I: Iterator<Item = u8>> BitIter<I> {
},
State::DoSumL => {
let val = result_stack.pop().unwrap();
result_stack.push(Value::sum_l(val));
result_stack.push(Value::left(val));
}
State::DoSumR => {
let val = result_stack.pop().unwrap();
result_stack.push(Value::sum_r(val));
result_stack.push(Value::right(val));
}
State::DoProduct => {
let val_r = result_stack.pop().unwrap();
let val_l = result_stack.pop().unwrap();
result_stack.push(Value::prod(val_l, val_r));
result_stack.push(Value::product(val_l, val_r));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/bit_encoding/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ pub fn decode_power_of_2<I: Iterator<Item = bool>>(
let right = stack.pop().unwrap();
let left = stack.pop().unwrap();
stack.push(StackElem {
value: Value::prod(left.value, right.value),
value: Value::product(left.value, right.value),
width: left.width * 2,
});
}
Expand Down
6 changes: 3 additions & 3 deletions src/bit_encoding/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,15 +291,15 @@ pub fn encode_value<W: io::Write>(value: &Value, w: &mut BitWriter<W>) -> io::Re

match value {
Value::Unit => {}
Value::SumL(left) => {
Value::Left(left) => {
w.write_bit(false)?;
encode_value(left, w)?;
}
Value::SumR(right) => {
Value::Right(right) => {
w.write_bit(true)?;
encode_value(right, w)?;
}
Value::Prod(left, right) => {
Value::Product(left, right) => {
encode_value(left, w)?;
encode_value(right, w)?;
}
Expand Down
6 changes: 3 additions & 3 deletions src/bit_machine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,9 @@ impl BitMachine {
for val in val.pre_order_iter::<NoSharing>() {
match val {
Value::Unit => {}
Value::SumL(..) => self.write_bit(false),
Value::SumR(..) => self.write_bit(true),
Value::Prod(..) => {}
Value::Left(..) => self.write_bit(false),
Value::Right(..) => self.write_bit(true),
Value::Product(..) => {}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/human_encoding/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ impl<'a> fmt::Display for DisplayWord<'a> {
f.write_str("0b")?;
for comb in self.0.pre_order_iter::<NoSharing>() {
match comb {
Value::SumL(..) => f.write_str("0")?,
Value::SumR(..) => f.write_str("1")?,
Value::Left(..) => f.write_str("0")?,
Value::Right(..) => f.write_str("1")?,
_ => {}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/jet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ mod tests {
.unwrap();
assert_eq!(
BitMachine::test_exec(two_words, &()).expect("executing"),
Value::prod(
Value::product(
Value::u1(0), // carry bit
Value::u32(2 + 16), // result
),
Expand All @@ -128,7 +128,7 @@ mod tests {
.unwrap();
assert_eq!(
BitMachine::test_exec(two_words, &()).expect("executing"),
Value::prod(Value::u32(2), Value::u16(16)),
Value::product(Value::u32(2), Value::u16(16)),
);
}
}
2 changes: 1 addition & 1 deletion src/merkle/cmr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ mod tests {
#[test]
fn fixed_const_word_cmr() {
// Checked against C implementation
let bit0 = Value::sum_l(Value::unit());
let bit0 = Value::left(Value::unit());
#[rustfmt::skip]
assert_eq!(
Cmr::const_word(&bit0),
Expand Down
6 changes: 3 additions & 3 deletions src/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,15 +190,15 @@ pub trait CoreConstructible: Sized {
for data in value.post_order_iter::<NoSharing>() {
match data.node {
Value::Unit => stack.push(Self::unit(inference_context)),
Value::SumL(..) => {
Value::Left(..) => {
let child = stack.pop().unwrap();
stack.push(Self::injl(&child));
}
Value::SumR(..) => {
Value::Right(..) => {
let child = stack.pop().unwrap();
stack.push(Self::injr(&child));
}
Value::Prod(..) => {
Value::Product(..) => {
let right = stack.pop().unwrap();
let left = stack.pop().unwrap();
stack.push(
Expand Down
4 changes: 2 additions & 2 deletions src/policy/satisfy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl<Pk: ToXOnlyPubkey> Policy<Pk> {
Policy::Key(ref key) => {
let sig_wit = satisfier
.lookup_tap_leaf_script_sig(key, &TapLeafHash::all_zeros())
.map(|sig| Value::u512_from_slice(sig.sig.as_ref()));
.map(|sig| Value::u512(sig.sig.as_ref()));
super::serialize::key(inference_context, key, sig_wit)
}
Policy::After(n) => {
Expand All @@ -128,7 +128,7 @@ impl<Pk: ToXOnlyPubkey> Policy<Pk> {
Policy::Sha256(ref hash) => {
let preimage_wit = satisfier
.lookup_sha256(hash)
.map(|preimage| Value::u256_from_slice(preimage.as_ref()));
.map(|preimage| Value::u256(&preimage));
super::serialize::sha256::<Pk, _, _>(inference_context, hash, preimage_wit)
}
Policy::And {
Expand Down
Loading
Loading