Skip to content

feat(fuzz): Generate Match expressions and statements#9108

Merged
aakoshh merged 20 commits intomasterfrom
af/7926-fuzz-match
Jul 14, 2025
Merged

feat(fuzz): Generate Match expressions and statements#9108
aakoshh merged 20 commits intomasterfrom
af/7926-fuzz-match

Conversation

@aakoshh
Copy link
Contributor

@aakoshh aakoshh commented Jul 3, 2025

Description

Problem*

Resolves #7926

Summary*

Implement match statements in the AST generator.

Additional Context

We are generating the what a match statement looks like in the monomorphized AST, but doing so without having in mind a full match statement in Noir, rather, just doing a single layer.

For example take this program:

global G: (u32, u32) = (2, 3);
fn main() -> pub u8 {
  match G {
    (2, _) => 1, 
    (_, 3) => 2, 
    (4, _) => 3, 
    _ => 4
  }
}

It turns into the following AST:

global G$g0: (u32, u32) = (2, 3);
fn main$f0() -> pub u8 {
    {
        let internal variable$l0 = G$g0;
        match $0 {
            ($1, $2) => match $1 {
                2 => {
                    let _$l3 = internal_match_variable_1$l2;
                    1
                },
                4 => match $2 {
                    3 => {
                        let _$l4 = internal_match_variable_0$l1;
                        2
                    },
                    _ => {
                        let _$l5 = internal_match_variable_1$l2;
                        3
                    },
                },
                _ => match $2 {
                    3 => {
                        let _$l6 = internal_match_variable_0$l1;
                        2
                    },
                    _ => {
                        let _$l7 = internal variable$l0;
                        4
                    },
                },
            },
        }
    }
}

So, it's a series of simpler match statements, on one variable at a time. The AST fuzzer generates only one random match, which can be:

  • a bool variable against true, false, _
  • a numeric variable against some random values and _ (wanted to do ranges but the frontend doesn't parse them)
  • a tuple gets matched and individual fields introduced as local variables

I added support for variable names in the Match so that I can print a parseable AST.

The mono test which tests the AST roundtrip is currently not enabled. Example of the difference between how the generated AST is parsed back and printed are as follows:

Extra internal variables introduced by the compiler:

-                    match idx_b {
-                        3379574630 => if G_B {
-                            assert((idx_b <= (1329894387 - 3967336669)), "ETA")
-                            break
-                        } else {
-                            let e = (-((idx_b as Field) * (idx_b as Field)), (idx_b as Field))
-                        },
-                        3478020418 => (),
-                        _ => (),
+                    {
+                        let internal variable = idx_b
+                        match internal variable {
+                            3379574630 => if G_B {
+                                assert((idx_b <= (1329894387 - 3967336669)), "ETA")
+                                break
+                            } else {
+                                let e = (-((idx_b as Field) * (idx_b as Field)), (idx_b as Field))
+                            },
+                            3478020418 => (),
+                            _ => {
+                                let _ = internal variable
+                                ()
+                            },
+                        }

Never matched cases removed by the compiler:

-                match a {
-                    true => func_4((&mut "OF"), ctx_limit),
-                    true => func_4((&mut "FI"), ctx_limit),
-                    false => G_A,
-                    _ => func_4((&mut "SJ"), ctx_limit),
+                {
+                    let internal variable = a
+                    match internal variable {
+                        false => G_A,
+                        true => func_4((&mut "OF"), ctx_limit),
+                    }

Documentation*

Check one:

  • No documentation needed.
  • Documentation included in this PR.
  • [For Experimental Features] Documentation to be submitted in a separate PR.

PR Checklist*

  • I have tested the changes locally.
  • I have formatted the changes with Prettier and/or cargo fmt on default settings.

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'Test Suite Duration'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.20.

Benchmark suite Current: ce217e7 Previous: 913ee63 Ratio
test_report_zkpassport_noir-ecdsa_ 130 s 104 s 1.25
test_report_zkpassport_noir_rsa_ 2 s 1 s 2

This comment was automatically generated by workflow using github-action-benchmark.

CC: @TomAFrench

@aakoshh aakoshh added the Blocked PR is blocked on an issue label Jul 4, 2025
@aakoshh aakoshh marked this pull request as ready for review July 4, 2025 21:08
@aakoshh aakoshh requested a review from a team July 4, 2025 21:13
@aakoshh aakoshh requested a review from rkarabut July 10, 2025 09:39
@aakoshh aakoshh added Blocked PR is blocked on an issue and removed Blocked PR is blocked on an issue labels Jul 10, 2025
@aakoshh
Copy link
Contributor Author

aakoshh commented Jul 14, 2025

Latest test failures need #9195

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'Execution Time'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.20.

Benchmark suite Current: 327d0ce Previous: 11d86ef Ratio
private-kernel-inner 0.021 s 0.016 s 1.31

This comment was automatically generated by workflow using github-action-benchmark.

CC: @TomAFrench

@aakoshh aakoshh added this pull request to the merge queue Jul 14, 2025
Merged via the queue into master with commit 49c49ce Jul 14, 2025
118 checks passed
@aakoshh aakoshh deleted the af/7926-fuzz-match branch July 14, 2025 22:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Blocked PR is blocked on an issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Generate arbitrary Match expressions

2 participants