Skip to content

Commit 3df9475

Browse files
committed
feat: Pass byte arrays by value to construct Value
Passing byte arrays by value should be faster and less annoying for the programmer, than passing references to byte arrays.
1 parent da16118 commit 3df9475

File tree

6 files changed

+62
-64
lines changed

6 files changed

+62
-64
lines changed

jets-bench/benches/elements/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ fn bench(c: &mut Criterion) {
774774
fn annex_hash() -> Arc<Value> {
775775
let ctx8 = SimplicityCtx8::with_len(511).value();
776776
let annex = if rand::random() {
777-
Value::right(Value::u256(&rand::random::<[u8; 32]>()))
777+
Value::right(Value::u256(rand::random::<[u8; 32]>()))
778778
} else {
779779
Value::left(Value::unit())
780780
};

jets-bench/src/data_structures.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -168,14 +168,14 @@ impl SimplicityEncode for SimplicityCtx8 {
168168
for (i, byte) in self.h.iter().flat_map(|x| x.to_be_bytes()).enumerate() {
169169
arr[i] = byte;
170170
}
171-
let mid_state = Value::u256(&arr);
171+
let mid_state = Value::u256(arr);
172172
Value::product(buf, Value::product(len, mid_state))
173173
}
174174
}
175175

176176
impl SimplicityEncode for elements::OutPoint {
177177
fn value(&self) -> Arc<Value> {
178-
let txid = Value::u256(self.txid.as_byte_array());
178+
let txid = Value::u256(self.txid.to_byte_array());
179179
let vout = Value::u32(self.vout);
180180
Value::product(txid, vout)
181181
}
@@ -185,12 +185,12 @@ impl SimplicityEncode for elements::confidential::Asset {
185185
fn value(&self) -> Arc<Value> {
186186
match self {
187187
elements::confidential::Asset::Explicit(a) => {
188-
Value::right(Value::u256(&a.into_inner().0))
188+
Value::right(Value::u256(a.into_inner().to_byte_array()))
189189
}
190190
elements::confidential::Asset::Confidential(gen) => {
191191
let ser = gen.serialize();
192192
let odd_gen = ser[0] & 1 == 1;
193-
let x_bytes: &[u8; 32] = (&ser[1..33]).try_into().unwrap();
193+
let x_bytes = (&ser[1..33]).try_into().unwrap();
194194
let x_pt = Value::u256(x_bytes);
195195
let y_pt = Value::u1(odd_gen as u8);
196196
Value::left(Value::product(y_pt, x_pt))
@@ -206,7 +206,7 @@ impl SimplicityEncode for elements::confidential::Value {
206206
elements::confidential::Value::Explicit(v) => Value::right(Value::u64(*v)),
207207
elements::confidential::Value::Confidential(v) => {
208208
let ser = v.serialize();
209-
let x_bytes: &[u8; 32] = (&ser[1..33]).try_into().unwrap();
209+
let x_bytes = (&ser[1..33]).try_into().unwrap();
210210
let x_pt = Value::u256(x_bytes);
211211
let y_pt = Value::u1((ser[0] & 1 == 1) as u8);
212212
Value::left(Value::product(y_pt, x_pt))
@@ -220,12 +220,12 @@ impl SimplicityEncode for elements::confidential::Nonce {
220220
fn value(&self) -> Arc<Value> {
221221
match self {
222222
elements::confidential::Nonce::Explicit(n) => {
223-
Value::right(Value::right(Value::u256(n)))
223+
Value::right(Value::right(Value::u256(*n)))
224224
}
225225
elements::confidential::Nonce::Confidential(n) => {
226226
let ser = n.serialize();
227-
let x_bytes: &[u8; 32] = (&ser[1..33]).try_into().unwrap();
228-
let x_pt = Value::u256(&x_bytes);
227+
let x_bytes = (&ser[1..33]).try_into().unwrap();
228+
let x_pt = Value::u256(x_bytes);
229229
let y_pt = Value::u1((ser[0] & 1 == 1) as u8);
230230
Value::right(Value::left(Value::product(y_pt, x_pt)))
231231
}
@@ -236,7 +236,7 @@ impl SimplicityEncode for elements::confidential::Nonce {
236236

237237
impl SimplicityEncode for SimplicityFe {
238238
fn value(&self) -> Arc<Value> {
239-
Value::u256(self.as_inner())
239+
Value::u256(*self.as_inner())
240240
}
241241
}
242242

@@ -252,8 +252,8 @@ impl SimplicityEncode for SimplicityGe {
252252
ser
253253
}
254254
};
255-
let x_bytes: &[u8; 32] = (&ser[1..33]).try_into().unwrap();
256-
let y_bytes: &[u8; 32] = (&ser[33..65]).try_into().unwrap();
255+
let x_bytes: [u8; 32] = (&ser[1..33]).try_into().unwrap();
256+
let y_bytes: [u8; 32] = (&ser[33..65]).try_into().unwrap();
257257
let x_pt = Value::u256(x_bytes);
258258
let y_pt = Value::u256(y_bytes);
259259
Value::product(x_pt, y_pt)
@@ -270,14 +270,14 @@ impl SimplicityEncode for SimplicityGej {
270270

271271
impl SimplicityEncode for SimplicityScalar {
272272
fn value(&self) -> Arc<Value> {
273-
Value::u256(&self.0)
273+
Value::u256(self.0)
274274
}
275275
}
276276

277277
impl SimplicityEncode for SimplicityPoint {
278278
fn value(&self) -> Arc<Value> {
279279
let ser = self.0.serialize(); // compressed
280-
let x_bytes: &[u8; 32] = (&ser[1..33]).try_into().unwrap();
280+
let x_bytes = (&ser[1..33]).try_into().unwrap();
281281
let y_pt = Value::u1((ser[0] & 1 == 1) as u8);
282282
let x_pt = Value::u256(x_bytes);
283283
Value::product(y_pt, x_pt)
@@ -412,6 +412,6 @@ pub fn genesis_pegin() -> Arc<Value> {
412412
Value::left(Value::unit())
413413
} else {
414414
let genesis_hash = rand::random::<[u8; 32]>();
415-
Value::right(Value::u256(&genesis_hash))
415+
Value::right(Value::u256(genesis_hash))
416416
}
417417
}

src/human_encoding/parse/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@ mod tests {
720720
0x7d, 0xf4, 0x90, 0x0d, 0x31, 0x05, 0x36, 0xc0,
721721
];
722722

723-
let signature = HashMap::from([(Arc::from("wit1"), Value::power_of_two(sig.as_ref()))]);
723+
let signature = HashMap::from([(Arc::from("wit1"), Value::u512(sig))]);
724724
assert_cmr_witness::<Elements>(
725725
"
726726
-- Witnesses
@@ -754,13 +754,13 @@ mod tests {
754754
("0b11111111", Value::u8(0b11111111)),
755755
(
756756
"0b00000001001000110100010101100111",
757-
Value::power_of_two(&[0b00000001, 0b00100011, 0b01000101, 0b01100111]),
757+
Value::power_of_two([0b00000001, 0b00100011, 0b01000101, 0b01100111]),
758758
),
759759
("0x0", Value::u4(0x0)),
760760
("0xf", Value::u4(0xf)),
761761
("0x00", Value::u8(0x00)),
762762
("0xff", Value::u8(0xff)),
763-
("0xdeadbeef", Value::power_of_two(&[0xde, 0xad, 0xbe, 0xef])),
763+
("0xdeadbeef", Value::power_of_two([0xde, 0xad, 0xbe, 0xef])),
764764
];
765765

766766
for (human, value) in human_values {

src/policy/satisfy.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ impl<Pk: ToXOnlyPubkey> Policy<Pk> {
105105
Policy::Key(ref key) => {
106106
let sig_wit = satisfier
107107
.lookup_tap_leaf_script_sig(key, &TapLeafHash::all_zeros())
108-
.map(|sig| Value::u512(sig.sig.as_ref()));
108+
.map(|sig| sig.sig.serialize())
109+
.map(Value::u512);
109110
super::serialize::key(inference_context, key, sig_wit)
110111
}
111112
Policy::After(n) => {
@@ -126,9 +127,7 @@ impl<Pk: ToXOnlyPubkey> Policy<Pk> {
126127
}
127128
}
128129
Policy::Sha256(ref hash) => {
129-
let preimage_wit = satisfier
130-
.lookup_sha256(hash)
131-
.map(|preimage| Value::u256(&preimage));
130+
let preimage_wit = satisfier.lookup_sha256(hash).map(Value::u256);
132131
super::serialize::sha256::<Pk, _, _>(inference_context, hash, preimage_wit)
133132
}
134133
Policy::And {

src/policy/serialize.rs

+34-32
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ use crate::types;
99
use crate::{Cmr, ConstructNode, ToXOnlyPubkey};
1010
use crate::{FailEntropy, Value};
1111

12+
use hashes::Hash;
13+
1214
use std::convert::TryFrom;
1315
use std::sync::Arc;
1416

@@ -54,7 +56,7 @@ where
5456
Pk: ToXOnlyPubkey,
5557
N: CoreConstructible + JetConstructible<Elements> + WitnessConstructible<W>,
5658
{
57-
let key_value = Value::u256(&key.to_x_only_pubkey().serialize());
59+
let key_value = Value::u256(key.to_x_only_pubkey().serialize());
5860
let const_key = N::const_word(inference_context, key_value);
5961
let sighash_all = N::jet(inference_context, Elements::SigAllHash);
6062
let pair_key_msg = N::pair(&const_key, &sighash_all).expect("consistent types");
@@ -118,7 +120,7 @@ where
118120
Pk: ToXOnlyPubkey,
119121
N: CoreConstructible + JetConstructible<Elements> + WitnessConstructible<W>,
120122
{
121-
let hash_value = Value::u256(Pk::to_sha256(hash).as_ref());
123+
let hash_value = Value::u256(Pk::to_sha256(hash).to_byte_array());
122124
let const_hash = N::const_word(inference_context, hash_value);
123125
let witness256 = N::witness(inference_context, witness);
124126
let computed_hash = compute_sha256(&witness256);
@@ -320,7 +322,7 @@ mod tests {
320322

321323
assert!(execute_successful(
322324
&commit,
323-
vec![Value::u512(signature.as_ref())],
325+
vec![Value::u512(signature.serialize())],
324326
&env
325327
));
326328
}
@@ -376,10 +378,10 @@ mod tests {
376378
let image = sha256::Hash::hash(&preimage);
377379
let (commit, env) = compile(Policy::Sha256(image));
378380

379-
let valid_witness = vec![Value::u256(&preimage)];
381+
let valid_witness = vec![Value::u256(preimage)];
380382
assert!(execute_successful(&commit, valid_witness, &env));
381383

382-
let invalid_witness = vec![Value::u256(&[0; 32])];
384+
let invalid_witness = vec![Value::u256([0; 32])];
383385
assert!(!execute_successful(&commit, invalid_witness, &env));
384386
}
385387

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

398-
let valid_witness = vec![Value::u256(&preimage0), Value::u256(&preimage1)];
400+
let valid_witness = vec![Value::u256(preimage0), Value::u256(preimage1)];
399401
assert!(execute_successful(&commit, valid_witness, &env));
400402

401-
let invalid_witness = vec![Value::u256(&preimage0), Value::u256(&[0; 32])];
403+
let invalid_witness = vec![Value::u256(preimage0), Value::u256([0; 32])];
402404
assert!(!execute_successful(&commit, invalid_witness, &env));
403405

404-
let invalid_witness = vec![Value::u256(&[0; 32]), Value::u256(&preimage1)];
406+
let invalid_witness = vec![Value::u256([0; 32]), Value::u256(preimage1)];
405407
assert!(!execute_successful(&commit, invalid_witness, &env));
406408
}
407409

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

418-
let valid_witness = vec![Value::u256(&preimage0)];
420+
let valid_witness = vec![Value::u256(preimage0)];
419421
assert!(execute_successful(&commit, valid_witness, &env));
420422

421-
let invalid_witness = vec![Value::u256(&[0; 32])];
423+
let invalid_witness = vec![Value::u256([0; 32])];
422424
assert!(!execute_successful(&commit, invalid_witness, &env));
423425
}
424426

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

437-
let valid_witness = vec![Value::u1(0), Value::u256(&preimage0), Value::u256(&[0; 32])];
439+
let valid_witness = vec![Value::u1(0), Value::u256(preimage0), Value::u256([0; 32])];
438440
assert!(execute_successful(&commit, valid_witness, &env));
439-
let valid_witness = vec![Value::u1(1), Value::u256(&[0; 32]), Value::u256(&preimage1)];
441+
let valid_witness = vec![Value::u1(1), Value::u256([0; 32]), Value::u256(preimage1)];
440442
assert!(execute_successful(&commit, valid_witness, &env));
441443

442-
let invalid_witness = vec![Value::u1(0), Value::u256(&[0; 32]), Value::u256(&preimage1)];
444+
let invalid_witness = vec![Value::u1(0), Value::u256([0; 32]), Value::u256(preimage1)];
443445
assert!(!execute_successful(&commit, invalid_witness, &env));
444-
let invalid_witness = vec![Value::u1(1), Value::u256(&preimage0), Value::u256(&[0; 32])];
446+
let invalid_witness = vec![Value::u1(1), Value::u256(preimage0), Value::u256([0; 32])];
445447
assert!(!execute_successful(&commit, invalid_witness, &env));
446448
}
447449

@@ -465,61 +467,61 @@ mod tests {
465467

466468
let valid_witness = vec![
467469
Value::u1(1),
468-
Value::u256(&preimage0),
470+
Value::u256(preimage0),
469471
Value::u1(1),
470-
Value::u256(&preimage1),
472+
Value::u256(preimage1),
471473
Value::u1(0),
472-
Value::u256(&[0; 32]),
474+
Value::u256([0; 32]),
473475
];
474476
assert!(execute_successful(&commit, valid_witness, &env));
475477

476478
let valid_witness = vec![
477479
Value::u1(1),
478-
Value::u256(&preimage0),
480+
Value::u256(preimage0),
479481
Value::u1(0),
480-
Value::u256(&[0; 32]),
482+
Value::u256([0; 32]),
481483
Value::u1(1),
482-
Value::u256(&preimage2),
484+
Value::u256(preimage2),
483485
];
484486
assert!(execute_successful(&commit, valid_witness, &env));
485487

486488
let valid_witness = vec![
487489
Value::u1(0),
488-
Value::u256(&[0; 32]),
490+
Value::u256([0; 32]),
489491
Value::u1(1),
490-
Value::u256(&preimage1),
492+
Value::u256(preimage1),
491493
Value::u1(1),
492-
Value::u256(&preimage2),
494+
Value::u256(preimage2),
493495
];
494496
assert!(execute_successful(&commit, valid_witness, &env));
495497

496498
let invalid_witness = vec![
497499
Value::u1(1),
498-
Value::u256(&preimage0),
500+
Value::u256(preimage0),
499501
Value::u1(1),
500-
Value::u256(&preimage1),
502+
Value::u256(preimage1),
501503
Value::u1(1),
502-
Value::u256(&preimage2),
504+
Value::u256(preimage2),
503505
];
504506
assert!(!execute_successful(&commit, invalid_witness, &env));
505507

506508
let invalid_witness = vec![
507509
Value::u1(1),
508-
Value::u256(&preimage1),
510+
Value::u256(preimage1),
509511
Value::u1(1),
510-
Value::u256(&preimage0),
512+
Value::u256(preimage0),
511513
Value::u1(0),
512-
Value::u256(&[0; 32]),
514+
Value::u256([0; 32]),
513515
];
514516
assert!(!execute_successful(&commit, invalid_witness, &env));
515517

516518
let invalid_witness = vec![
517519
Value::u1(1),
518-
Value::u256(&preimage0),
520+
Value::u256(preimage0),
519521
Value::u1(0),
520-
Value::u256(&[0; 32]),
522+
Value::u256([0; 32]),
521523
Value::u1(0),
522-
Value::u256(&[0; 32]),
524+
Value::u256([0; 32]),
523525
];
524526
assert!(!execute_successful(&commit, invalid_witness, &env));
525527
}

src/value.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -179,26 +179,23 @@ impl Value {
179179
}
180180

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

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

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

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

0 commit comments

Comments
 (0)