Skip to content

Commit

Permalink
Added high level tests for constrain + verify.
Browse files Browse the repository at this point in the history
commit-id:81cdc190
  • Loading branch information
orizi committed Jun 24, 2024
1 parent ec35a69 commit 71bc49e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
39 changes: 39 additions & 0 deletions corelib/src/circuit.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,45 @@ impl U384IntoCircuitInputValue of IntoCircuitInputValue<u384> {
}
}

impl CircuitPendingInputIntoCircuitInputValue of IntoCircuitInputValue<CircuitPendingInput> {
fn into_circuit_input_value(self: CircuitPendingInput) -> [U96Guarantee; 4] {
self.value
}
}

/// A type that contains a pending input for a circuit.
pub struct CircuitPendingInput {
value: [U96Guarantee; 4],
}
impl CircuitPendingInputDestruct of Destruct<CircuitPendingInput> {
fn destruct(self: CircuitPendingInput) nopanic {
let [_val0, _val1, _val2, _val3] = self.value;
}
}

impl Felt252sTryIntoCircuitPendingInput of TryInto<[felt252; 4], CircuitPendingInput> {
fn try_into(self: [felt252; 4]) -> Option<CircuitPendingInput> {
let [val0, val1, val2, val3] = self;
Option::Some(
CircuitPendingInput {
value: [val0.try_into()?, val1.try_into()?, val2.try_into()?, val3.try_into()?,]
}
)
}
}

impl Felt252TryIntoU96Guarantee of TryInto<felt252, U96Guarantee> {
fn try_into(self: felt252) -> Option<U96Guarantee> {
match core::internal::bounded_int_felt252_constrain::<0x1000000000000000000000000>(self) {
core::internal::ConstraitFelt252Result::Under(guarantee) => Option::Some(guarantee),

Check warning on line 329 in corelib/src/circuit.cairo

View workflow job for this annotation

GitHub Actions / typos

"Constrait" should be "Constraints" or "Constraint".
core::internal::ConstraitFelt252Result::Over(guarantee) => {

Check warning on line 330 in corelib/src/circuit.cairo

View workflow job for this annotation

GitHub Actions / typos

"Constrait" should be "Constraints" or "Constraint".
core::internal::bounded_int_verify_guarantee(guarantee);
Option::None
}
}
}
}

/// A trait for evaluating a circuit.
#[generate_trait]
pub impl EvalCircuitImpl<C> of EvalCircuitTrait<C> {
Expand Down
15 changes: 14 additions & 1 deletion corelib/src/test/circuit_test.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use core::circuit::{
RangeCheck96, AddMod, MulMod, u96, CircuitElement, CircuitInput, circuit_add, circuit_sub,
circuit_mul, circuit_inverse, EvalCircuitTrait, u384, CircuitOutputsTrait, CircuitModulus,
FillInputResultTrait, CircuitInputs,
FillInputResultTrait, CircuitInputs, CircuitPendingInput,
};

use core::test::test_utils::assert_eq;
Expand Down Expand Up @@ -100,3 +100,16 @@ fn test_fill_inputs_loop() {

circuit_inputs.done();
}

fn test_into_u96_guarantee() {
let input_opt: Option<CircuitPendingInput> = [1, 2, 3, 4].try_into();
assert!(input_opt.is_some());
let input_opt: Option<CircuitPendingInput> = [0x1000000000000000000000001, 2, 3, 4].try_into();
assert!(input_opt.is_none());
let input_opt: Option<CircuitPendingInput> = [1, 0x1000000000000000000000002, 3, 4].try_into();
assert!(input_opt.is_none());
let input_opt: Option<CircuitPendingInput> = [1, 2, 0x1000000000000000000000003, 4].try_into();
assert!(input_opt.is_none());
let input_opt: Option<CircuitPendingInput> = [1, 2, 3, 0x1000000000000000000000004].try_into();
assert!(input_opt.is_none());
}

0 comments on commit 71bc49e

Please sign in to comment.