Skip to content

fix: optimize zero constant detection and assignment#1545

Merged
gbotrel merged 4 commits intomasterfrom
fix/r1cs-zero-const-mul
Jul 16, 2025
Merged

fix: optimize zero constant detection and assignment#1545
gbotrel merged 4 commits intomasterfrom
fix/r1cs-zero-const-mul

Conversation

@ivokub
Copy link
Copy Markdown
Collaborator

@ivokub ivokub commented Jul 16, 2025

Description

Found in fuzzing test:

type C1_6314_8435483601975321392 struct {
	X0V frontend.Variable
	Y0V frontend.Variable `gnark:",public"`
}

func (circuit *C1_6314_8435483601975321392) Define(api frontend.API) error {
	x1 := api.IsZero(circuit.X0V) // depends on X0V
	x2 := api.And(0, x1)          // always 0
	x3 := api.Cmp(x2, 1)          // always -1
	api.AssertIsEqual(1, x3)      // always false
	return nil
}

type C2_6314_8435483601975321392 struct {
	X0V frontend.Variable
	Y0V frontend.Variable `gnark:",public"`
}

func (circuit *C2_6314_8435483601975321392) Define(api frontend.API) error {
	x1 := api.Cmp(circuit.X0V, circuit.X0V) // always 0
	x2 := api.Cmp(x1, -1)                   // always -1
	x3 := api.IsZero(x2)                    // always 0
	x4 := api.IsZero(circuit.X0V)           // depends on X0V
	x5 := api.IsZero(circuit.X0V)           // depends on X0v
	x6 := api.Select(x3, x4, x5)            // always returns x5
	x7 := api.And(0, x6)                    // always 0
	api.AssertIsLessOrEqual(1, x7)          // always fails
	return nil
}

Where previously R1CS compiler didn't fail for second circuit but did for SCS.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How has this been benchmarked?

Ran BW6 in BN254 Groth16 verifier circuit to benchmark compile time (as we now do big loop for constant value checking).

              │  old.txt   │           new.txt           │
              │   sec/op   │   sec/op    vs base         │
BW6InBN254-16   9.434 ± 2%   9.501 ± 1%  ~ (p=0.589 n=6)

              │   old.txt    │            new.txt            │
              │     B/op     │     B/op      vs base         │
BW6InBN254-16   21.28Gi ± 0%   21.28Gi ± 0%  ~ (p=1.000 n=6)

              │   old.txt   │           new.txt            │
              │  allocs/op  │  allocs/op   vs base         │
BW6InBN254-16   91.48M ± 0%   91.48M ± 0%  ~ (p=0.937 n=6)

Checklist:

  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • I did not modify files generated from templates
  • golangci-lint does not output errors locally
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

@ivokub ivokub requested a review from gbotrel July 16, 2025 09:50
@ivokub ivokub self-assigned this Jul 16, 2025
@ivokub ivokub added the src: fuzzing Issue found using a fuzzing tool label Jul 16, 2025
cursor[bot]

This comment was marked as outdated.

cursor[bot]

This comment was marked as outdated.

@gbotrel gbotrel merged commit eb2ae03 into master Jul 16, 2025
7 checks passed
@gbotrel gbotrel deleted the fix/r1cs-zero-const-mul branch July 16, 2025 15:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

src: fuzzing Issue found using a fuzzing tool

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants