Skip to content

Commit

Permalink
Merge #240: Value fixup
Browse files Browse the repository at this point in the history
5c6a26f refactor: Rename power_of_two -> from_byte_array (Christian Lewe)
3df9475 feat: Pass byte arrays by value to construct Value (Christian Lewe)

Pull request description:

  Fixes outstanding issues of #237 which I failed to address before merging.

ACKs for top commit:
  apoelstra:
    ACK 5c6a26f nice\!

Tree-SHA512: 9ab3acc83b5040c9c5a839682d4eb06e7b23bd91fb23a62cdec27433a34477d5b49c3b932822b180ea254022513ca458a1ca15d8f22873da767f719cd2cbcba2
  • Loading branch information
uncomputable committed Jul 29, 2024
2 parents da16118 + 5c6a26f commit d01ca91
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 66 deletions.
2 changes: 1 addition & 1 deletion jets-bench/benches/elements/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ fn bench(c: &mut Criterion) {
fn annex_hash() -> Arc<Value> {
let ctx8 = SimplicityCtx8::with_len(511).value();
let annex = if rand::random() {
Value::right(Value::u256(&rand::random::<[u8; 32]>()))
Value::right(Value::u256(rand::random::<[u8; 32]>()))
} else {
Value::left(Value::unit())
};
Expand Down
28 changes: 14 additions & 14 deletions jets-bench/src/data_structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,14 @@ impl SimplicityEncode for SimplicityCtx8 {
for (i, byte) in self.h.iter().flat_map(|x| x.to_be_bytes()).enumerate() {
arr[i] = byte;
}
let mid_state = Value::u256(&arr);
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(self.txid.as_byte_array());
let txid = Value::u256(self.txid.to_byte_array());
let vout = Value::u32(self.vout);
Value::product(txid, vout)
}
Expand All @@ -185,12 +185,12 @@ impl SimplicityEncode for elements::confidential::Asset {
fn value(&self) -> Arc<Value> {
match self {
elements::confidential::Asset::Explicit(a) => {
Value::right(Value::u256(&a.into_inner().0))
Value::right(Value::u256(a.into_inner().to_byte_array()))
}
elements::confidential::Asset::Confidential(gen) => {
let ser = gen.serialize();
let odd_gen = ser[0] & 1 == 1;
let x_bytes: &[u8; 32] = (&ser[1..33]).try_into().unwrap();
let x_bytes = (&ser[1..33]).try_into().unwrap();
let x_pt = Value::u256(x_bytes);
let y_pt = Value::u1(odd_gen as u8);
Value::left(Value::product(y_pt, x_pt))
Expand All @@ -206,7 +206,7 @@ impl SimplicityEncode for elements::confidential::Value {
elements::confidential::Value::Explicit(v) => Value::right(Value::u64(*v)),
elements::confidential::Value::Confidential(v) => {
let ser = v.serialize();
let x_bytes: &[u8; 32] = (&ser[1..33]).try_into().unwrap();
let x_bytes = (&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::left(Value::product(y_pt, x_pt))
Expand All @@ -220,12 +220,12 @@ impl SimplicityEncode for elements::confidential::Nonce {
fn value(&self) -> Arc<Value> {
match self {
elements::confidential::Nonce::Explicit(n) => {
Value::right(Value::right(Value::u256(n)))
Value::right(Value::right(Value::u256(*n)))
}
elements::confidential::Nonce::Confidential(n) => {
let ser = n.serialize();
let x_bytes: &[u8; 32] = (&ser[1..33]).try_into().unwrap();
let x_pt = Value::u256(&x_bytes);
let x_bytes = (&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::right(Value::left(Value::product(y_pt, x_pt)))
}
Expand All @@ -236,7 +236,7 @@ impl SimplicityEncode for elements::confidential::Nonce {

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

Expand All @@ -252,8 +252,8 @@ impl SimplicityEncode for SimplicityGe {
ser
}
};
let x_bytes: &[u8; 32] = (&ser[1..33]).try_into().unwrap();
let y_bytes: &[u8; 32] = (&ser[33..65]).try_into().unwrap();
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)
Expand All @@ -270,14 +270,14 @@ impl SimplicityEncode for SimplicityGej {

impl SimplicityEncode for SimplicityScalar {
fn value(&self) -> Arc<Value> {
Value::u256(&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 x_bytes = (&ser[1..33]).try_into().unwrap();
let y_pt = Value::u1((ser[0] & 1 == 1) as u8);
let x_pt = Value::u256(x_bytes);
Value::product(y_pt, x_pt)
Expand Down Expand Up @@ -412,6 +412,6 @@ pub fn genesis_pegin() -> Arc<Value> {
Value::left(Value::unit())
} else {
let genesis_hash = rand::random::<[u8; 32]>();
Value::right(Value::u256(&genesis_hash))
Value::right(Value::u256(genesis_hash))
}
}
9 changes: 6 additions & 3 deletions src/human_encoding/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ mod tests {
0x7d, 0xf4, 0x90, 0x0d, 0x31, 0x05, 0x36, 0xc0,
];

let signature = HashMap::from([(Arc::from("wit1"), Value::power_of_two(sig.as_ref()))]);
let signature = HashMap::from([(Arc::from("wit1"), Value::u512(sig))]);
assert_cmr_witness::<Elements>(
"
-- Witnesses
Expand Down Expand Up @@ -754,13 +754,16 @@ mod tests {
("0b11111111", Value::u8(0b11111111)),
(
"0b00000001001000110100010101100111",
Value::power_of_two(&[0b00000001, 0b00100011, 0b01000101, 0b01100111]),
Value::from_byte_array([0b00000001, 0b00100011, 0b01000101, 0b01100111]),
),
("0x0", Value::u4(0x0)),
("0xf", Value::u4(0xf)),
("0x00", Value::u8(0x00)),
("0xff", Value::u8(0xff)),
("0xdeadbeef", Value::power_of_two(&[0xde, 0xad, 0xbe, 0xef])),
(
"0xdeadbeef",
Value::from_byte_array([0xde, 0xad, 0xbe, 0xef]),
),
];

for (human, value) in human_values {
Expand Down
7 changes: 3 additions & 4 deletions src/policy/satisfy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ 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(sig.sig.as_ref()));
.map(|sig| sig.sig.serialize())
.map(Value::u512);
super::serialize::key(inference_context, key, sig_wit)
}
Policy::After(n) => {
Expand All @@ -126,9 +127,7 @@ impl<Pk: ToXOnlyPubkey> Policy<Pk> {
}
}
Policy::Sha256(ref hash) => {
let preimage_wit = satisfier
.lookup_sha256(hash)
.map(|preimage| Value::u256(&preimage));
let preimage_wit = satisfier.lookup_sha256(hash).map(Value::u256);
super::serialize::sha256::<Pk, _, _>(inference_context, hash, preimage_wit)
}
Policy::And {
Expand Down
66 changes: 34 additions & 32 deletions src/policy/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use crate::types;
use crate::{Cmr, ConstructNode, ToXOnlyPubkey};
use crate::{FailEntropy, Value};

use hashes::Hash;

use std::convert::TryFrom;
use std::sync::Arc;

Expand Down Expand Up @@ -54,7 +56,7 @@ where
Pk: ToXOnlyPubkey,
N: CoreConstructible + JetConstructible<Elements> + WitnessConstructible<W>,
{
let key_value = Value::u256(&key.to_x_only_pubkey().serialize());
let key_value = Value::u256(key.to_x_only_pubkey().serialize());
let const_key = N::const_word(inference_context, key_value);
let sighash_all = N::jet(inference_context, Elements::SigAllHash);
let pair_key_msg = N::pair(&const_key, &sighash_all).expect("consistent types");
Expand Down Expand Up @@ -118,7 +120,7 @@ where
Pk: ToXOnlyPubkey,
N: CoreConstructible + JetConstructible<Elements> + WitnessConstructible<W>,
{
let hash_value = Value::u256(Pk::to_sha256(hash).as_ref());
let hash_value = Value::u256(Pk::to_sha256(hash).to_byte_array());
let const_hash = N::const_word(inference_context, hash_value);
let witness256 = N::witness(inference_context, witness);
let computed_hash = compute_sha256(&witness256);
Expand Down Expand Up @@ -320,7 +322,7 @@ mod tests {

assert!(execute_successful(
&commit,
vec![Value::u512(signature.as_ref())],
vec![Value::u512(signature.serialize())],
&env
));
}
Expand Down Expand Up @@ -376,10 +378,10 @@ mod tests {
let image = sha256::Hash::hash(&preimage);
let (commit, env) = compile(Policy::Sha256(image));

let valid_witness = vec![Value::u256(&preimage)];
let valid_witness = vec![Value::u256(preimage)];
assert!(execute_successful(&commit, valid_witness, &env));

let invalid_witness = vec![Value::u256(&[0; 32])];
let invalid_witness = vec![Value::u256([0; 32])];
assert!(!execute_successful(&commit, invalid_witness, &env));
}

Expand All @@ -395,13 +397,13 @@ mod tests {
right: Arc::new(Policy::Sha256(image1)),
});

let valid_witness = vec![Value::u256(&preimage0), Value::u256(&preimage1)];
let valid_witness = vec![Value::u256(preimage0), Value::u256(preimage1)];
assert!(execute_successful(&commit, valid_witness, &env));

let invalid_witness = vec![Value::u256(&preimage0), Value::u256(&[0; 32])];
let invalid_witness = vec![Value::u256(preimage0), Value::u256([0; 32])];
assert!(!execute_successful(&commit, invalid_witness, &env));

let invalid_witness = vec![Value::u256(&[0; 32]), Value::u256(&preimage1)];
let invalid_witness = vec![Value::u256([0; 32]), Value::u256(preimage1)];
assert!(!execute_successful(&commit, invalid_witness, &env));
}

Expand All @@ -415,10 +417,10 @@ mod tests {
right: Arc::new(Policy::Trivial),
});

let valid_witness = vec![Value::u256(&preimage0)];
let valid_witness = vec![Value::u256(preimage0)];
assert!(execute_successful(&commit, valid_witness, &env));

let invalid_witness = vec![Value::u256(&[0; 32])];
let invalid_witness = vec![Value::u256([0; 32])];
assert!(!execute_successful(&commit, invalid_witness, &env));
}

Expand All @@ -434,14 +436,14 @@ mod tests {
right: Arc::new(Policy::Sha256(image1)),
});

let valid_witness = vec![Value::u1(0), Value::u256(&preimage0), Value::u256(&[0; 32])];
let valid_witness = vec![Value::u1(0), Value::u256(preimage0), Value::u256([0; 32])];
assert!(execute_successful(&commit, valid_witness, &env));
let valid_witness = vec![Value::u1(1), Value::u256(&[0; 32]), Value::u256(&preimage1)];
let valid_witness = vec![Value::u1(1), Value::u256([0; 32]), Value::u256(preimage1)];
assert!(execute_successful(&commit, valid_witness, &env));

let invalid_witness = vec![Value::u1(0), Value::u256(&[0; 32]), Value::u256(&preimage1)];
let invalid_witness = vec![Value::u1(0), Value::u256([0; 32]), Value::u256(preimage1)];
assert!(!execute_successful(&commit, invalid_witness, &env));
let invalid_witness = vec![Value::u1(1), Value::u256(&preimage0), Value::u256(&[0; 32])];
let invalid_witness = vec![Value::u1(1), Value::u256(preimage0), Value::u256([0; 32])];
assert!(!execute_successful(&commit, invalid_witness, &env));
}

Expand All @@ -465,61 +467,61 @@ mod tests {

let valid_witness = vec![
Value::u1(1),
Value::u256(&preimage0),
Value::u256(preimage0),
Value::u1(1),
Value::u256(&preimage1),
Value::u256(preimage1),
Value::u1(0),
Value::u256(&[0; 32]),
Value::u256([0; 32]),
];
assert!(execute_successful(&commit, valid_witness, &env));

let valid_witness = vec![
Value::u1(1),
Value::u256(&preimage0),
Value::u256(preimage0),
Value::u1(0),
Value::u256(&[0; 32]),
Value::u256([0; 32]),
Value::u1(1),
Value::u256(&preimage2),
Value::u256(preimage2),
];
assert!(execute_successful(&commit, valid_witness, &env));

let valid_witness = vec![
Value::u1(0),
Value::u256(&[0; 32]),
Value::u256([0; 32]),
Value::u1(1),
Value::u256(&preimage1),
Value::u256(preimage1),
Value::u1(1),
Value::u256(&preimage2),
Value::u256(preimage2),
];
assert!(execute_successful(&commit, valid_witness, &env));

let invalid_witness = vec![
Value::u1(1),
Value::u256(&preimage0),
Value::u256(preimage0),
Value::u1(1),
Value::u256(&preimage1),
Value::u256(preimage1),
Value::u1(1),
Value::u256(&preimage2),
Value::u256(preimage2),
];
assert!(!execute_successful(&commit, invalid_witness, &env));

let invalid_witness = vec![
Value::u1(1),
Value::u256(&preimage1),
Value::u256(preimage1),
Value::u1(1),
Value::u256(&preimage0),
Value::u256(preimage0),
Value::u1(0),
Value::u256(&[0; 32]),
Value::u256([0; 32]),
];
assert!(!execute_successful(&commit, invalid_witness, &env));

let invalid_witness = vec![
Value::u1(1),
Value::u256(&preimage0),
Value::u256(preimage0),
Value::u1(0),
Value::u256(&[0; 32]),
Value::u256([0; 32]),
Value::u1(0),
Value::u256(&[0; 32]),
Value::u256([0; 32]),
];
assert!(!execute_successful(&commit, invalid_witness, &env));
}
Expand Down
21 changes: 9 additions & 12 deletions src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,26 +179,23 @@ impl Value {
}

/// Create a value from 32 bytes.
pub fn u256(bytes: &[u8; 32]) -> Arc<Self> {
Value::power_of_two(bytes)
pub fn u256(bytes: [u8; 32]) -> Arc<Self> {
Value::from_byte_array(bytes)
}

/// Create a value from 64 bytes.
pub fn u512(bytes: &[u8; 64]) -> Arc<Self> {
Value::power_of_two(bytes)
pub fn u512(bytes: [u8; 64]) -> Arc<Self> {
Value::from_byte_array(bytes)
}

/// Create a value from a byte slice.
/// Create a value from a byte array.
///
/// ## Panics
///
/// The length of the slice is not a power of two.
pub fn power_of_two(v: &[u8]) -> Arc<Self> {
assert!(
v.len().is_power_of_two(),
"Slice length must be a power of two"
);
let mut values: VecDeque<_> = v.iter().map(|b| Value::u8(*b)).collect();
/// The array length is not a power of two.
pub fn from_byte_array<const N: usize>(bytes: [u8; N]) -> Arc<Self> {
assert!(N.is_power_of_two(), "Array length must be a power of two");
let mut values: VecDeque<_> = bytes.into_iter().map(Value::u8).collect();

while values.len() > 1 {
let mut alt_values = VecDeque::with_capacity(values.len() / 2);
Expand Down

0 comments on commit d01ca91

Please sign in to comment.