Skip to content

Commit 4944378

Browse files
add comments
1 parent 17903b6 commit 4944378

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

program.cairo

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use core::circuit::{
2+
CircuitData, CircuitElement, CircuitInput, circuit_add, circuit_sub, circuit_mul, circuit_inverse,
3+
EvalCircuitResult, EvalCircuitTrait, u384, CircuitOutputsTrait, CircuitModulus, into_u96_guarantee, U96Guarantee,
4+
CircuitInputs, AddInputResultTrait, AddInputResult, IntoCircuitInputValue, add_circuit_input
5+
};
6+
fn main() -> u384 {
7+
let in1 = CircuitElement::<CircuitInput<0>> {};
8+
let in2 = CircuitElement::<CircuitInput<1>> {};
9+
let add1 = circuit_add(in1, in2);
10+
let mul1 = circuit_mul(add1, in1);
11+
let mul2 = circuit_mul(mul1, add1);
12+
let inv1 = circuit_inverse(mul2);
13+
let sub1 = circuit_sub(inv1, in2);
14+
let sub2 = circuit_sub(sub1, mul2);
15+
let inv2 = circuit_inverse(sub2);
16+
let add2 = circuit_add(inv2, inv2);
17+
18+
let modulus = TryInto::<_, CircuitModulus>::try_into([17, 14, 14, 14]).unwrap();
19+
20+
let outputs = (add2,)
21+
.new_inputs()
22+
.next([9, 2, 9, 3])
23+
.next([5, 7, 0, 8])
24+
.done()
25+
.eval(modulus)
26+
.unwrap();
27+
28+
outputs.get_output(add2)
29+
}

src/libfuncs/circuit.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,11 @@ fn build_eval<'ctx, 'this>(
398398
location,
399399
outputs_ptr,
400400
&[GepIndex::Const(
401-
outputs_prefix_layout.size() as i32 + elem_stride.size() as i32 * i as i32,
401+
// The offset is calculated as the prefix, which is the 4
402+
// bytes from the reference counter plus the extra padding.
403+
// Then, we need to add the element stride time the current
404+
// index.
405+
outputs_prefix_layout.size() as i32 + elem_stride.pad_to_align().size() as i32 * i as i32,
402406
)],
403407
IntegerType::new(context, 384).into(),
404408
)?;
@@ -942,12 +946,16 @@ fn build_get_output<'ctx, 'this>(
942946
)?;
943947

944948
let circuit_output_prefix_offset = calc_circuit_output_prefix_layout().size() as i32;
945-
let elem_stride = get_integer_layout(384).size() as i32;
949+
let elem_stride = get_integer_layout(384).pad_to_align().size() as i32;
946950
let output_integer_ptr = entry.gep(
947951
context,
948952
location,
949953
circuit_ptr,
950954
&[GepIndex::Const(
955+
// The offset is calculated as the prefix, which is the 4
956+
// bytes from the reference counter plus the extra padding.
957+
// Then, we need to add the element stride time the current
958+
// index.
951959
circuit_output_prefix_offset + elem_stride * output_idx as i32,
952960
)],
953961
u384_type,

src/metadata/trace_dump.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,6 @@ pub mod trace_dump_runtime {
566566
else {
567567
panic!("generic arg should be a Circuit");
568568
};
569-
570569
let u96_layout = get_integer_layout(96);
571570

572571
let n_outputs = circuit.circuit_info.values.len();
@@ -586,7 +585,7 @@ pub mod trace_dump_runtime {
586585
// get gate values
587586
for i in 0..n_outputs {
588587
let gate_ptr = value_ptr
589-
.byte_add(outputs_prefix_layout.size() + gate_stride.size() * i);
588+
.byte_add(outputs_prefix_layout.size() + gate_stride.pad_to_align().size() * i);
590589
values.push(u384_struct_to_bigint(gate_ptr, 4));
591590
}
592591

0 commit comments

Comments
 (0)