@@ -8,10 +8,9 @@ use simplicity::{
8
8
bitcoin, elements,
9
9
hashes:: Hash ,
10
10
hex:: FromHex ,
11
- types:: { self , Type } ,
11
+ types:: Final ,
12
12
BitIter , Error , Value ,
13
13
} ;
14
- use std:: sync:: Arc ;
15
14
16
15
/// Engine to compute SHA256 hash function.
17
16
/// We can't use hashes::sha256::HashEngine because it does not accept
@@ -55,21 +54,19 @@ impl SimplicityCtx8 {
55
54
/// # Panics:
56
55
///
57
56
/// Panics if the length of the slice is >= 2^(n + 1) bytes
58
- pub fn var_len_buf_from_slice ( v : & [ u8 ] , mut n : usize ) -> Result < Arc < Value > , Error > {
57
+ pub fn var_len_buf_from_slice ( v : & [ u8 ] , mut n : usize ) -> Result < Value , Error > {
59
58
// Simplicity consensus rule for n < 16 while reading buffers.
60
59
assert ! ( n < 16 ) ;
61
60
assert ! ( v. len( ) < ( 1 << ( n + 1 ) ) ) ;
62
61
let mut iter = BitIter :: new ( v. iter ( ) . copied ( ) ) ;
63
- let ctx = types:: Context :: new ( ) ;
64
- let types = Type :: powers_of_two ( & ctx, n) ; // size n + 1
65
62
let mut res = None ;
66
63
while n > 0 {
64
+ let ty = Final :: two_two_n ( n) ;
67
65
let v = if v. len ( ) >= ( 1 << ( n + 1 ) ) {
68
- let ty = & types[ n] ;
69
- let val = iter. read_value ( & ty. final_data ( ) . unwrap ( ) ) ?;
70
- Value :: right ( val)
66
+ let val = iter. read_value ( & ty) ?;
67
+ Value :: some ( val)
71
68
} else {
72
- Value :: left ( Value :: unit ( ) )
69
+ Value :: none ( ty )
73
70
} ;
74
71
res = match res {
75
72
Some ( prod) => Some ( Value :: product ( prod, v) ) ,
@@ -155,11 +152,11 @@ pub struct SimplicityPoint(pub bitcoin::secp256k1::PublicKey);
155
152
/// Trait defining how to encode a data structure into a Simplicity value
156
153
/// This is then used to write these vales into the bit machine.
157
154
pub trait SimplicityEncode {
158
- fn value ( & self ) -> Arc < Value > ;
155
+ fn value ( & self ) -> Value ;
159
156
}
160
157
161
158
impl SimplicityEncode for SimplicityCtx8 {
162
- fn value ( & self ) -> Arc < Value > {
159
+ fn value ( & self ) -> Value {
163
160
let buf_len = self . length % 512 ;
164
161
let buf = var_len_buf_from_slice ( & self . buffer [ ..buf_len] , 8 ) . unwrap ( ) ;
165
162
let len = Value :: u64 ( self . length as u64 ) ;
@@ -174,74 +171,80 @@ impl SimplicityEncode for SimplicityCtx8 {
174
171
}
175
172
176
173
impl SimplicityEncode for elements:: OutPoint {
177
- fn value ( & self ) -> Arc < Value > {
174
+ fn value ( & self ) -> Value {
178
175
let txid = Value :: u256 ( self . txid . to_byte_array ( ) ) ;
179
176
let vout = Value :: u32 ( self . vout ) ;
180
177
Value :: product ( txid, vout)
181
178
}
182
179
}
183
180
184
181
impl SimplicityEncode for elements:: confidential:: Asset {
185
- fn value ( & self ) -> Arc < Value > {
182
+ fn value ( & self ) -> Value {
186
183
match self {
187
184
elements:: confidential:: Asset :: Explicit ( a) => {
188
- Value :: right ( Value :: u256 ( a. into_inner ( ) . to_byte_array ( ) ) )
185
+ let left = Final :: product ( Final :: u1 ( ) , Final :: u256 ( ) ) ;
186
+ Value :: right ( left, Value :: u256 ( a. into_inner ( ) . to_byte_array ( ) ) )
189
187
}
190
188
elements:: confidential:: Asset :: Confidential ( gen) => {
191
189
let ser = gen. serialize ( ) ;
192
190
let odd_gen = ser[ 0 ] & 1 == 1 ;
193
191
let x_bytes = ( & ser[ 1 ..33 ] ) . try_into ( ) . unwrap ( ) ;
194
192
let x_pt = Value :: u256 ( x_bytes) ;
195
193
let y_pt = Value :: u1 ( odd_gen as u8 ) ;
196
- Value :: left ( Value :: product ( y_pt, x_pt) )
194
+ Value :: left ( Value :: product ( y_pt, x_pt) , Final :: u256 ( ) )
197
195
}
198
196
elements:: confidential:: Asset :: Null => panic ! ( "Tried to encode Null asset" ) ,
199
197
}
200
198
}
201
199
}
202
200
203
201
impl SimplicityEncode for elements:: confidential:: Value {
204
- fn value ( & self ) -> Arc < Value > {
202
+ fn value ( & self ) -> Value {
205
203
match self {
206
- elements:: confidential:: Value :: Explicit ( v) => Value :: right ( Value :: u64 ( * v) ) ,
204
+ elements:: confidential:: Value :: Explicit ( v) => {
205
+ let left = Final :: product ( Final :: u1 ( ) , Final :: u256 ( ) ) ;
206
+ Value :: right ( left, Value :: u64 ( * v) )
207
+ } ,
207
208
elements:: confidential:: Value :: Confidential ( v) => {
208
209
let ser = v. serialize ( ) ;
209
210
let x_bytes = ( & ser[ 1 ..33 ] ) . try_into ( ) . unwrap ( ) ;
210
211
let x_pt = Value :: u256 ( x_bytes) ;
211
212
let y_pt = Value :: u1 ( ( ser[ 0 ] & 1 == 1 ) as u8 ) ;
212
- Value :: left ( Value :: product ( y_pt, x_pt) )
213
+ Value :: left ( Value :: product ( y_pt, x_pt) , Final :: u64 ( ) )
213
214
}
214
215
elements:: confidential:: Value :: Null => panic ! ( "Tried to encode Null value" ) ,
215
216
}
216
217
}
217
218
}
218
219
219
220
impl SimplicityEncode for elements:: confidential:: Nonce {
220
- fn value ( & self ) -> Arc < Value > {
221
+ fn value ( & self ) -> Value {
222
+ let ty_l = Final :: product ( Final :: u1 ( ) , Final :: u256 ( ) ) ;
223
+ let ty_r = Final :: u256 ( ) ;
221
224
match self {
222
225
elements:: confidential:: Nonce :: Explicit ( n) => {
223
- Value :: right ( Value :: right ( Value :: u256 ( * n) ) )
226
+ Value :: some ( Value :: right ( ty_l , Value :: u256 ( * n) ) )
224
227
}
225
228
elements:: confidential:: Nonce :: Confidential ( n) => {
226
229
let ser = n. serialize ( ) ;
227
230
let x_bytes = ( & ser[ 1 ..33 ] ) . try_into ( ) . unwrap ( ) ;
228
231
let x_pt = Value :: u256 ( x_bytes) ;
229
232
let y_pt = Value :: u1 ( ( ser[ 0 ] & 1 == 1 ) as u8 ) ;
230
- Value :: right ( Value :: left ( Value :: product ( y_pt, x_pt) ) )
233
+ Value :: some ( Value :: left ( Value :: product ( y_pt, x_pt) , ty_r ) )
231
234
}
232
- elements:: confidential:: Nonce :: Null => Value :: left ( Value :: unit ( ) ) ,
235
+ elements:: confidential:: Nonce :: Null => Value :: none ( Final :: sum ( ty_l , ty_r ) ) ,
233
236
}
234
237
}
235
238
}
236
239
237
240
impl SimplicityEncode for SimplicityFe {
238
- fn value ( & self ) -> Arc < Value > {
241
+ fn value ( & self ) -> Value {
239
242
Value :: u256 ( * self . as_inner ( ) )
240
243
}
241
244
}
242
245
243
246
impl SimplicityEncode for SimplicityGe {
244
- fn value ( & self ) -> Arc < Value > {
247
+ fn value ( & self ) -> Value {
245
248
let ser = match & self {
246
249
SimplicityGe :: ValidPoint ( p) => p. serialize_uncompressed ( ) ,
247
250
SimplicityGe :: InvalidPoint ( x, y) => {
@@ -261,21 +264,21 @@ impl SimplicityEncode for SimplicityGe {
261
264
}
262
265
263
266
impl SimplicityEncode for SimplicityGej {
264
- fn value ( & self ) -> Arc < Value > {
267
+ fn value ( & self ) -> Value {
265
268
let ge = self . ge . value ( ) ;
266
269
let z = self . z . value ( ) ;
267
270
Value :: product ( ge, z)
268
271
}
269
272
}
270
273
271
274
impl SimplicityEncode for SimplicityScalar {
272
- fn value ( & self ) -> Arc < Value > {
275
+ fn value ( & self ) -> Value {
273
276
Value :: u256 ( self . 0 )
274
277
}
275
278
}
276
279
277
280
impl SimplicityEncode for SimplicityPoint {
278
- fn value ( & self ) -> Arc < Value > {
281
+ fn value ( & self ) -> Value {
279
282
let ser = self . 0 . serialize ( ) ; // compressed
280
283
let x_bytes = ( & ser[ 1 ..33 ] ) . try_into ( ) . unwrap ( ) ;
281
284
let y_pt = Value :: u1 ( ( ser[ 0 ] & 1 == 1 ) as u8 ) ;
@@ -407,11 +410,11 @@ impl BenchSample for SimplicityPoint {
407
410
}
408
411
409
412
// Sample genesis pegin with 50% probability
410
- pub fn genesis_pegin ( ) -> Arc < Value > {
413
+ pub fn genesis_pegin ( ) -> Value {
411
414
if rand:: random ( ) {
412
- Value :: left ( Value :: unit ( ) )
415
+ Value :: none ( Final :: two_two_n ( 8 ) )
413
416
} else {
414
417
let genesis_hash = rand:: random :: < [ u8 ; 32 ] > ( ) ;
415
- Value :: right ( Value :: u256 ( genesis_hash) )
418
+ Value :: some ( Value :: u256 ( genesis_hash) )
416
419
}
417
420
}
0 commit comments