Skip to content

Commit

Permalink
Save circuit output in struct representation, instead of integer
Browse files Browse the repository at this point in the history
It is ultimately returned in struct representation, so now it is
converted sooner (useful for guarantee representation)
  • Loading branch information
JulianGCalderon committed Feb 4, 2025
1 parent ef706f8 commit 5e5ce0c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
26 changes: 11 additions & 15 deletions src/libfuncs/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
error::{Result, SierraAssertError},
libfuncs::r#struct::build_struct_value,
metadata::MetadataStorage,
types::TypeBuilder,
types::{circuit::build_u384_struct_type, TypeBuilder},
utils::{get_integer_layout, layout_repeat, BlockExt, ProgramRegistryExt},
};
use cairo_lang_sierra::{
Expand Down Expand Up @@ -401,9 +401,15 @@ fn build_eval<'ctx, 'this>(
circuit_info.mul_offsets.len() * MOD_BUILTIN_INSTANCE_SIZE,
)?;

// convert circuit output from integer representation to struct representation
let gates = gates
.into_iter()
.map(|value| u384_integer_to_struct(context, ok_block, location, value))
.collect::<Result<Vec<_>>>()?;

let n_gates = circuit_info.values.len();
let gates_array = ok_block.append_op_result(llvm::undef(
llvm::r#type::array(IntegerType::new(context, 384).into(), n_gates as u32),
llvm::r#type::array(build_u384_struct_type(context), n_gates as u32),
location,
))?;
let gates_array = ok_block.insert_values(context, location, gates_array, &gates)?;
Expand Down Expand Up @@ -813,14 +819,13 @@ fn build_get_output<'ctx, 'this>(
llvm::r#type::array(IntegerType::new(context, 384).into(), n_gates as u32),
0,
)?;
let output_integer = entry.extract_value(
let output_struct = entry.extract_value(
context,
location,
output_gates,
IntegerType::new(context, 384).into(),
build_u384_struct_type(context),
output_idx,
)?;
let output_struct = u384_integer_to_struct(context, entry, location, output_integer)?;

let guarantee_type_id = &info.branch_signatures()[0].vars[1].ty;
let guarantee_type = registry.build_type(context, helper, metadata, guarantee_type_id)?;
Expand Down Expand Up @@ -907,16 +912,7 @@ fn u384_integer_to_struct<'a>(
block.trunci(limb, u96_type, location)?
};

let struct_type = llvm::r#type::r#struct(
context,
&[
IntegerType::new(context, 96).into(),
IntegerType::new(context, 96).into(),
IntegerType::new(context, 96).into(),
IntegerType::new(context, 96).into(),
],
false,
);
let struct_type = build_u384_struct_type(context);
let struct_value = block.append_op_result(llvm::undef(struct_type, location))?;

block.insert_values(
Expand Down
2 changes: 1 addition & 1 deletion src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ mod bounded_int;
mod r#box;
mod builtin_costs;
mod bytes31;
mod circuit;
pub mod circuit;
mod coupon;
mod ec_op;
mod ec_point;
Expand Down
17 changes: 15 additions & 2 deletions src/types/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ pub fn build_circuit_outputs<'ctx>(
Ok(llvm::r#type::r#struct(
context,
&[
llvm::r#type::array(IntegerType::new(context, 384).into(), n_gates as u32),
llvm::r#type::array(IntegerType::new(context, 96).into(), 4),
llvm::r#type::array(build_u384_struct_type(context), n_gates as u32),
build_u384_struct_type(context),
],
false,
))
Expand Down Expand Up @@ -286,3 +286,16 @@ pub fn layout(
CircuitTypeConcrete::CircuitPartialOutputs(_) => Ok(Layout::new::<()>()),
}
}

pub fn build_u384_struct_type(context: &Context) -> Type<'_> {
llvm::r#type::r#struct(
context,
&[
IntegerType::new(context, 96).into(),
IntegerType::new(context, 96).into(),
IntegerType::new(context, 96).into(),
IntegerType::new(context, 96).into(),
],
false,
)
}

0 comments on commit 5e5ce0c

Please sign in to comment.