Skip to content

Commit eb0bf15

Browse files
arena for sha256 and circuits
1 parent 723acbf commit eb0bf15

4 files changed

Lines changed: 69 additions & 452 deletions

File tree

src/libfuncs/circuit.rs

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use crate::{
99
libfuncs::r#struct::build_struct_value,
1010
metadata::{
1111
drop_overrides::DropOverridesMeta,
12-
realloc_bindings::ReallocBindingsMeta,
1312
runtime_bindings::{CircuitArithOperationType, RuntimeBindingsMeta},
1413
MetadataStorage,
1514
},
@@ -102,8 +101,6 @@ fn build_init_circuit_data<'ctx, 'this>(
102101
metadata: &mut MetadataStorage,
103102
info: &SignatureAndTypeConcreteLibfunc,
104103
) -> Result<()> {
105-
metadata.get_or_insert_with(|| ReallocBindingsMeta::new(context, helper));
106-
107104
let circuit_info = match registry.get_type(&info.ty)? {
108105
CoreTypeConcrete::Circuit(CircuitTypeConcrete::Circuit(info)) => &info.circuit_info,
109106
_ => return Err(SierraAssertError::BadTypeInfo.into()),
@@ -119,22 +116,21 @@ fn build_init_circuit_data<'ctx, 'this>(
119116

120117
// Calculate full capacity for array.
121118
let capacity = circuit_info.n_inputs;
122-
let u384_layout = get_integer_layout(384);
123-
let capacity_bytes = layout_repeat(&u384_layout, capacity)?
124-
.0
125-
.pad_to_align()
126-
.size();
127-
let capacity_bytes_value = entry.const_int(context, location, capacity_bytes, 64)?;
128-
129-
// Alloc memory for array.
130-
let ptr_ty = llvm::r#type::pointer(context, 0);
131-
let ptr = entry.append_op_result(llvm::zero(ptr_ty, location))?;
132-
let ptr = entry.append_op_result(ReallocBindingsMeta::realloc(
119+
let inputs_layout = layout_repeat(&get_integer_layout(384), capacity)?.0;
120+
let capacity_bytes_value =
121+
entry.const_int(context, location, inputs_layout.pad_to_align().size(), 64)?;
122+
123+
// Alloc memory for array from the arena.
124+
let align_value = entry.const_int(context, location, inputs_layout.align(), 64)?;
125+
let rtb = metadata.get_or_insert_with(RuntimeBindingsMeta::default);
126+
let ptr = rtb.arena_alloc(
133127
context,
134-
ptr,
135-
capacity_bytes_value,
128+
helper.module,
129+
entry,
136130
location,
137-
)?)?;
131+
capacity_bytes_value,
132+
align_value,
133+
)?;
138134

139135
// Create accumulator struct.
140136
let k0 = entry.const_int(context, location, 0, 64)?;
@@ -309,8 +305,6 @@ fn build_eval<'ctx, 'this>(
309305
metadata: &mut MetadataStorage,
310306
info: &SignatureAndTypeConcreteLibfunc,
311307
) -> Result<()> {
312-
metadata.get_or_insert_with(|| ReallocBindingsMeta::new(context, helper));
313-
314308
let circuit_info = match registry.get_type(&info.ty)? {
315309
CoreTypeConcrete::Circuit(CircuitTypeConcrete::Circuit(info)) => &info.circuit_info,
316310
_ => return Err(SierraAssertError::BadTypeInfo.into()),
@@ -373,21 +367,22 @@ fn build_eval<'ctx, 'this>(
373367

374368
// Calculate capacity for array.
375369
let outputs_capacity = circuit_info.values.len();
376-
let u384_integer_layout = get_integer_layout(384);
377-
let outputs_layout = layout_repeat(&u384_integer_layout, outputs_capacity)?.0;
378-
let outputs_capacity_bytes = outputs_layout.pad_to_align().size();
370+
let outputs_layout = layout_repeat(&get_integer_layout(384), outputs_capacity)?.0;
379371
let outputs_capacity_bytes_value =
380-
ok_block.const_int(context, location, outputs_capacity_bytes, 64)?;
372+
ok_block.const_int(context, location, outputs_layout.pad_to_align().size(), 64)?;
381373

382-
// Alloc memory for array.
383-
let ptr_ty = llvm::r#type::pointer(context, 0);
384-
let outputs_ptr = ok_block.append_op_result(llvm::zero(ptr_ty, location))?;
385-
let outputs_ptr = ok_block.append_op_result(ReallocBindingsMeta::realloc(
374+
// Alloc memory for array from the arena.
375+
let outputs_align_value =
376+
ok_block.const_int(context, location, outputs_layout.align(), 64)?;
377+
let rtb = metadata.get_or_insert_with(RuntimeBindingsMeta::default);
378+
let outputs_ptr = rtb.arena_alloc(
386379
context,
387-
outputs_ptr,
388-
outputs_capacity_bytes_value,
380+
helper.module,
381+
ok_block,
389382
location,
390-
)?)?;
383+
outputs_capacity_bytes_value,
384+
outputs_align_value,
385+
)?;
391386

392387
// Insert evaluated gates into the array.
393388
for (i, gate) in gates.into_iter().enumerate() {

src/starknet.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1863,14 +1863,24 @@ pub(crate) mod handler {
18631863
state: *mut [u32; 8],
18641864
block: &[u32; 16],
18651865
) {
1866-
let state_ref = unsafe { state.as_mut().unwrap() };
1866+
// Sha256StateHandle is Copy in corelib, so the input pointer may be
1867+
// aliased by other live copies. Mutate a fresh arena slot rather than
1868+
// the input buffer to keep aliased copies observing the prior state.
1869+
let new_state = unsafe {
1870+
crate::runtime::cairo_native__arena_alloc(
1871+
std::mem::size_of::<[u32; 8]>() as u64,
1872+
std::mem::align_of::<[u32; 8]>() as u64,
1873+
) as *mut [u32; 8]
1874+
};
1875+
unsafe { std::ptr::copy_nonoverlapping(state, new_state, 1) };
1876+
let state_ref = unsafe { new_state.as_mut().unwrap() };
18671877
let result = ptr.sha256_process_block(state_ref, block, gas);
18681878

18691879
*result_ptr = match result {
1870-
Ok(x) => SyscallResultAbi {
1880+
Ok(_) => SyscallResultAbi {
18711881
ok: ManuallyDrop::new(SyscallResultAbiOk {
18721882
tag: 0u8,
1873-
payload: ManuallyDrop::new(state),
1883+
payload: ManuallyDrop::new(new_state),
18741884
}),
18751885
},
18761886
Err(e) => Self::wrap_error(&e),

0 commit comments

Comments
 (0)