Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: apply range constraints to return values from unconstrained func…
…tions (#4217) # Description ## Problem\* Resolves <!-- Link to GitHub Issue --> ## Summary\* Consider the program ```rust unconstrained fn identity(x: u8) -> u8 { x } fn main(x: u8, y: pub Field) { assert(identity(x) as Field == 1000); } ``` From looking at the type information, this program should be unsatisfiable. A `u8` should not be able to contain the value `1000` so the assertion should always fail. If we look at the generated ACIR however we see that the return value of `identity()` doesn't need to be a valid `u8` and so can be set by the prover to be `1000`. ``` BLACKBOX::RANGE [(_0, num_bits: 8)] [ ] BRILLIG: inputs: [Single(Expression { mul_terms: [], linear_combinations: [(1, Witness(0))], q_c: 0 })] outputs: [Simple(Witness(2))] [Mov { destination: RegisterIndex(2), source: RegisterIndex(0) }, Const { destination: RegisterIndex(0), value: Value { inner: 0 } }, Const { destination: RegisterIndex(1), value: Value { inner: 0 } }, Mov { destination: RegisterIndex(2), source: RegisterIndex(2) }, Call { location: 7 }, Mov { destination: RegisterIndex(0), source: RegisterIndex(2) }, Stop, Mov { destination: RegisterIndex(3), source: RegisterIndex(2) }, Mov { destination: RegisterIndex(2), source: RegisterIndex(3) }, Return] EXPR [ (1, _2) -1000 ] ``` This PR modifies ACIR gen so that when inserting a call to a brillig function, we apply range constraints to the return values to ensure that the results provided by the prover must be valid for those types. ## Additional Context Note that we cannot enforce safety for all types which have validity conditions outside of the type system. e.g. If an unconstrained function returns a `U128` then this will not be constrained as its limbs are made up of `Field`s so no constraints will be applied allowing a potentially invalid value to be returned. ## Documentation\* Check one: - [x] No documentation needed. - [ ] Documentation included in this PR. - [ ] **[Exceptional Case]** Documentation to be submitted in a separate PR. # PR Checklist\* - [x] I have tested the changes locally. - [x] I have formatted the changes with [Prettier](https://prettier.io/) and/or `cargo fmt` on default settings.
- Loading branch information