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

Witgen: Can't reason about range constraints on expressions #2119

Open
georgwiese opened this issue Nov 19, 2024 · 1 comment
Open

Witgen: Can't reason about range constraints on expressions #2119

georgwiese opened this issue Nov 19, 2024 · 1 comment

Comments

@georgwiese
Copy link
Collaborator

Consider this example:

namespace main(4);
    // Two bit-constrained witness columns
    // In practice, bit1 is always one (but this is not constrained)
    col witness bit1(i) query Query::Hint(1);
    col witness bit2;
    bit1 * (bit1 - 1) = 0;
    bit2 * (bit2 - 1) = 0;

    // Constrain their sum to be binary as well.
    // This ensures that at most one of the two bits is set.
    // Therefore, bit2 is always zero.
    let bit_sum = bit1 + bit2;
    bit_sum * (bit_sum - 1) = 0;

    // Some witness that depends on bit2.
    col witness foo;
    foo = bit2 * 42 + (1 - bit2) * 43;

Given that bit1 = 1 is set via a hint, the witness should be fully determined:

  • Because bit2 and bit_sum are binary, bit2 must be zero
  • bit2 uniquely determines foo

However, because bit_sum is just an expression, the binary range constraint is not stored with it, and witgen never figures out bit2.

To work around this, we can materialize bit_sum. The following works:

    let bit_sum;
    bit_sum * (bit_sum - 1) = 0;
@georgwiese
Copy link
Collaborator Author

The following works

This is true after #2120.

github-merge-queue bot pushed a commit that referenced this issue Nov 20, 2024
When writing #2119, I came across another issue in witgen. When running
this file on `main`:

```rs
namespace main(4);

    // Two bit-constrained witness columns
    // In practice, bit1 is always one (but this is not constrained)
    col witness bit1(i) query Query::Hint(1);
    col witness bit2;
    bit1 * (bit1 - 1) = 0;
    bit2 * (bit2 - 1) = 0;

    // Constrain their sum to be binary as well.
    // This ensures that at most one of the two bits is set.
    // Therefore, bit2 is always zero.
    let bit_sum;
    bit_sum = bit1 + bit2;
    bit_sum * (bit_sum - 1) = 0;

    // Some witness that depends on bit2.
    col witness foo;
    foo = bit2 * 42 + (1 - bit2) * 43;
```

I'm getting
```
thread 'main' panicked at /Users/georg/coding/powdr/executor/src/witgen/global_constraints.rs:258:17:
assertion failed: known_constraints.insert(p, RangeConstraint::from_max_bit(0)).is_none()
```

This is because when it sees the binary range constraint `bit_sum *
(bit_sum - 1) = 0`, it already figured out that `bit_sum` can be at most
`1` because of `bit_sum = bit1 + bit2`.

This PR fixes it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant