-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: decompose
Instruction::Constrain
into multiple more basic con…
…straints (#3892) # Description ## Problem\* Some experimentation related to #3782. ## Summary\* If we switch to an unary constrain instruction then we will want to be able to decompose an assertion on a composite type to perform assertions directly on its fields. Otherwise we have to create a predicate for each field and then collect these all together before constraining that. For example consider this program which simulates an unary constrain ``` fn main(x: [Field; 2], y: [Field; 2]) { let equal = x == y; assert(equal); } ``` Currently this produces: ``` acir fn main f0 { b0(v0: [Field; 2], v1: [Field; 2]): v66 = array_get v0, index Field 0 v67 = array_get v1, index Field 0 v68 = eq v66, v67 v69 = array_get v0, index Field 1 v70 = array_get v1, index Field 1 v71 = eq v69, v70 v72 = mul v68, v71 constrain v72 == u1 1 return } Compiled ACIR for main (unoptimized): current witness index : 10 public parameters indices : [] return value indices : [] EXPR [ (-1, _1) (1, _3) (-1, _5) 0 ] BRILLIG: inputs: [Single(Expression { mul_terms: [], linear_combinations: [(1, Witness(5))], q_c: 0 })] outputs: [Simple(Witness(6))] [JumpIfNot { condition: RegisterIndex(0), location: 3 }, Const { destination: RegisterIndex(1), value: Value { inner: 1 } }, BinaryFieldOp { destination: RegisterIndex(0), op: Div, lhs: RegisterIndex(1), rhs: RegisterIndex(0) }, Stop] EXPR [ (1, _5, _6) (1, _7) -1 ] EXPR [ (1, _5, _7) 0 ] EXPR [ (-1, _2) (1, _4) (-1, _8) 0 ] BRILLIG: inputs: [Single(Expression { mul_terms: [], linear_combinations: [(1, Witness(8))], q_c: 0 })] outputs: [Simple(Witness(9))] [JumpIfNot { condition: RegisterIndex(0), location: 3 }, Const { destination: RegisterIndex(1), value: Value { inner: 1 } }, BinaryFieldOp { destination: RegisterIndex(0), op: Div, lhs: RegisterIndex(1), rhs: RegisterIndex(0) }, Stop] EXPR [ (1, _8, _9) (1, _10) -1 ] EXPR [ (1, _8, _10) 0 ] EXPR [ (1, _7, _10) -1 ] ``` After this PR we can recover the current optimisations for `assert(x == y)` ``` acir fn main f0 { b0(v0: [Field; 2], v1: [Field; 2]): v66 = array_get v0, index Field 0 v67 = array_get v1, index Field 0 v68 = array_get v0, index Field 1 v69 = array_get v1, index Field 1 constrain v66 == v67 constrain v68 == v69 return } Compiled ACIR for main (unoptimized): current witness index : 4 public parameters indices : [] return value indices : [] EXPR [ (1, _1) (-1, _3) 0 ] EXPR [ (1, _2) (-1, _4) 0 ] ``` ## Additional Context ## Documentation\* Check one: - [ ] No documentation needed. - [ ] Documentation included in this PR. - [ ] **[Exceptional Case]** Documentation to be submitted in a separate PR. # PR Checklist\* - [ ] I have tested the changes locally. - [ ] I have formatted the changes with [Prettier](https://prettier.io/) and/or `cargo fmt` on default settings. --------- Co-authored-by: jfecher <[email protected]>
- Loading branch information
1 parent
857ff97
commit 51cf9d3
Showing
3 changed files
with
222 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters