Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bit_shift_comptime panics in debug mode #2745

Closed
TomAFrench opened this issue Sep 18, 2023 · 2 comments
Closed

bit_shift_comptime panics in debug mode #2745

TomAFrench opened this issue Sep 18, 2023 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@TomAFrench
Copy link
Member

I'm getting a panic on these lines when compiling bit_shift_comptime in debug mode.

if max_rhs_bits != 0 {
max_q_bits = max_bit_size - max_rhs_bits + 1;
}

@TomAFrench
Copy link
Member Author

TomAFrench commented Sep 18, 2023

The minimal reproduction is

fn main() {
    let a: u1 = 1 >> 1;
    assert(a == 0);
}

This gets compiled down to

acir fn main f0 {
  b0(v0: u64):
    v10 = div u1 1, Field 2
    constrain v10 == u1 0
    return 
}

We should be able to compile this down to an empty circuit. The issue is that when we try and simplify 1 >> 1 we assume that rhs conforms to the same max number of bits as lhs. This means that when we truncate the rhs down to bit_size then 2 gets truncated down to zero.

Type::Numeric(NumericType::Unsigned { bit_size }) => {
let function = self.operator.get_u128_function();
let lhs = truncate(lhs.try_into_u128()?, *bit_size);
let rhs = truncate(rhs.try_into_u128()?, *bit_size);
// The divisor is being truncated into the type of the operand, which can potentially
// lead to the rhs being zero.
// If the rhs of a division is zero, attempting to evaluate the divison will cause a compiler panic.
// Thus, we do not evaluate the division in this method, as we want to avoid triggering a panic,
// and the operation should be handled by ACIR generation.
if matches!(self.operator, BinaryOp::Div) && rhs == 0 {
return None;
}
let result = function(lhs, rhs);
truncate(result, *bit_size).into()
}

The compiler then thinks we're performing a division by zero so defers to ACIR generation where we get the final panic.

@Savio-Sou Savio-Sou added the bug Something isn't working label Sep 19, 2023
@guipublic
Copy link
Contributor

I cannot reproduce this issue, so I close it.

@github-project-automation github-project-automation bot moved this from 📋 Backlog to ✅ Done in Noir Nov 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Archived in project
Development

No branches or pull requests

3 participants