Skip to content

Commit b63ab1b

Browse files
FrancoGiachettaJulianGCalderongabrielbosio
authored
implement circuit_single_limb_less_than_guarantee_verify (#1212)
* implement circuit_single_limb_less_than_guarantee_verify * fmt * clippy * Fix comment Co-authored-by: Julian Gonzalez Calderon <gonzalezcalderonjulian@gmail.com> --------- Co-authored-by: Julian Gonzalez Calderon <gonzalezcalderonjulian@gmail.com> Co-authored-by: Gabriel Bosio <38794644+gabrielbosio@users.noreply.github.com>
1 parent a4be0f6 commit b63ab1b

File tree

1 file changed

+37
-4
lines changed

1 file changed

+37
-4
lines changed

src/libfuncs/circuit.rs

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,12 @@ pub fn build<'ctx, 'this>(
7171
CircuitConcreteLibfunc::IntoU96Guarantee(info) => {
7272
build_into_u96_guarantee(context, registry, entry, location, helper, metadata, info)
7373
}
74-
CircuitConcreteLibfunc::U96SingleLimbLessThanGuaranteeVerify(
75-
SignatureOnlyConcreteLibfunc { signature, .. },
76-
)
77-
| CircuitConcreteLibfunc::U96GuaranteeVerify(SignatureOnlyConcreteLibfunc { signature }) => {
74+
CircuitConcreteLibfunc::U96SingleLimbLessThanGuaranteeVerify(info) => {
75+
build_u96_single_limb_less_than_guarantee_verify(
76+
context, registry, entry, location, helper, metadata, info,
77+
)
78+
}
79+
CircuitConcreteLibfunc::U96GuaranteeVerify(SignatureOnlyConcreteLibfunc { signature }) => {
7880
super::build_noop::<1, true>(
7981
context,
8082
registry,
@@ -821,6 +823,37 @@ fn build_u96_limbs_less_than_guarantee_verify<'ctx, 'this>(
821823
Ok(())
822824
}
823825

826+
fn build_u96_single_limb_less_than_guarantee_verify<'ctx, 'this>(
827+
context: &'ctx Context,
828+
_registry: &ProgramRegistry<CoreType, CoreLibfunc>,
829+
entry: &'this Block<'ctx>,
830+
location: Location<'ctx>,
831+
helper: &LibfuncHelper<'ctx, 'this>,
832+
_metadata: &mut MetadataStorage,
833+
_info: &SignatureOnlyConcreteLibfunc,
834+
) -> Result<()> {
835+
let guarantee = entry.arg(0)?;
836+
837+
let u96_type = IntegerType::new(context, 96).into();
838+
// this libfunc will always receive gate and modulus with single limb
839+
let limb_struct_type = llvm::r#type::r#struct(context, &[u96_type; 1], false);
840+
841+
// extract gate and modulus from input value
842+
let gate = entry.extract_value(context, location, guarantee, limb_struct_type, 0)?;
843+
let modulus = entry.extract_value(context, location, guarantee, limb_struct_type, 1)?;
844+
845+
// extract the only limb from gate and modulus
846+
let gate_limb = entry.extract_value(context, location, gate, u96_type, 0)?;
847+
let modulus_limb = entry.extract_value(context, location, modulus, u96_type, 0)?;
848+
849+
// calcualte diff between limbs
850+
let diff = entry.append_op_result(arith::subi(modulus_limb, gate_limb, location))?;
851+
852+
entry.append_operation(helper.br(0, &[diff], location));
853+
854+
Ok(())
855+
}
856+
824857
/// Generate MLIR operations for the `get_circuit_output` libfunc.
825858
#[allow(clippy::too_many_arguments)]
826859
fn build_get_output<'ctx, 'this>(

0 commit comments

Comments
 (0)