Skip to content

Commit

Permalink
better check in bounded int trim
Browse files Browse the repository at this point in the history
  • Loading branch information
FrancoGiachetta committed Jan 3, 2025
1 parent b3d25c8 commit 6abd6e0
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 27 deletions.
5 changes: 3 additions & 2 deletions src/libfuncs/bounded_int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -728,14 +728,14 @@ fn build_trim<'ctx, 'this>(
value.r#type(),
)?;
let trim_type = registry.get_type(&info.param_signatures()[0].ty)?;

let is_invalid = entry.cmpi(context, CmpiPredicate::Eq, value, trimmed_value, location)?;
let int_range = trim_type.integer_range(registry)?;

// There is no need to truncate the value type since we're only receiving power-of-two integers
// and constraining their range a single value from either the lower or upper limit. However,
// since we're returning a `BoundedInt` we need to offset its internal representation
// accordingly.
let value = if info.trimmed_value == BigInt::ZERO || trim_type.is_signed(registry)? {
let value = if info.trimmed_value == BigInt::ZERO || int_range.lower < BigInt::ZERO {
let offset = entry.const_int_from_type(
context,
location,
Expand All @@ -748,6 +748,7 @@ fn build_trim<'ctx, 'this>(
};

entry.append_operation(helper.cond_br(context, is_invalid, [0, 1], [&[], &[value]], location));

Ok(())
}

Expand Down
25 changes: 0 additions & 25 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,6 @@ pub trait TypeBuilder {
registry: &ProgramRegistry<CoreType, CoreLibfunc>,
) -> Result<bool, Self::Error>;

/// Return whether the type is signed, either directly or indirectly (ex. through
/// `Const`).
fn is_signed(
&self,
registry: &ProgramRegistry<CoreType, CoreLibfunc>,
) -> Result<bool, Self::Error>;

/// Return whether the type is a `felt252`, either directly or indirectly (ex. through
/// `NonZero<BoundedInt<>>`).
fn is_felt252(
Expand Down Expand Up @@ -889,24 +882,6 @@ impl TypeBuilder for CoreTypeConcrete {
})
}

fn is_signed(
&self,
registry: &ProgramRegistry<CoreType, CoreLibfunc>,
) -> Result<bool, Self::Error> {
Ok(match self {
CoreTypeConcrete::Sint8(_)
| CoreTypeConcrete::Sint16(_)
| CoreTypeConcrete::Sint32(_)
| CoreTypeConcrete::Sint64(_)
| CoreTypeConcrete::Sint128(_) => true,
CoreTypeConcrete::Const(info) => {
let inner_ty = registry.get_type(&info.inner_ty)?;
inner_ty.is_signed(registry)?
}
_ => false,
})
}

fn is_felt252(
&self,
registry: &ProgramRegistry<CoreType, CoreLibfunc>,
Expand Down

0 comments on commit 6abd6e0

Please sign in to comment.