Skip to content

perf(csp): reduce allocations using in-place scalar operations#1593

Merged
adecaro merged 3 commits into
LFDT-Panurus:mainfrom
neetance:perf/csp-allocation-reduction
Apr 24, 2026
Merged

perf(csp): reduce allocations using in-place scalar operations#1593
adecaro merged 3 commits into
LFDT-Panurus:mainfrom
neetance:perf/csp-allocation-reduction

Conversation

@neetance

Copy link
Copy Markdown
Contributor

Summary

This PR optimizes the CSP (Compressed Sigma Protocol) prover and verifier by reducing allocation overhead in scalar operations.

The changes are based on the mathlib optimizations (fr.Element-based Zr) and introduce in-place arithmetic and scratch reuse across the CSP proof methods.


Changes

  • Replace allocation-heavy operations:

    • ModAddMul -> ModAddMul2InPlace / ModAddMul3InPlace
    • ModMul -> ModMulInPlace
  • Introduce scratch variable reuse in verifier:

    • avoid repeated Zr allocations inside loops
  • Optimize folding steps in prover:

    • eliminate temporary scalar allocations during vector reduction
  • Optimize sVector computation:

    • use in-place updates instead of allocating intermediate values

Performance Improvement

  • Significant reduction in the allocs/op metric (by 13%)
  • Lower GC pressure in CSP proof generation and verification
  • Execution time remains stable (no regressions observed)

The actual benchmark results given in the mathlib pr


Testing

  • All tests pass:
    go test ./token/core/zkatdlog/nogh/v1/...

  • Benchmarks executed successfully

  • No functional changes


Note

I modified the go.mod file to replace the actual mathlib library url with my current mathlib commit, which can be changed back to normal once the mathlib pr is merged

Let me know if this looks good
Thanks 🙏

VRight []*mathlib.Zr // cross scalar <f_R, wit_L>

Curve *mathlib.Curve
Left []*mathlib.G1

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

why removing the comments on all these fields?

challenges[i] = c
cSq := v.Curve.ModMul(c, c, v.Curve.GroupOrder)

// Update value: v' = c·v + VLeft[i] + c²·VRight[i]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

why removing these comments?

@adecaro

adecaro commented Apr 24, 2026

Copy link
Copy Markdown
Contributor

@neetance , fantastic, thanks. Please, just restore the code comments unless they are not appropriate anymore, but I don't think so. Thanks 🙏

@adecaro adecaro self-assigned this Apr 24, 2026
@adecaro adecaro added this to the Q2/26 milestone Apr 24, 2026
@adecaro adecaro force-pushed the perf/csp-allocation-reduction branch from 0011298 to eaf7667 Compare April 24, 2026 14:29
@neetance neetance force-pushed the perf/csp-allocation-reduction branch from eaf7667 to c0dd3e6 Compare April 24, 2026 15:43
@adecaro adecaro self-requested a review April 24, 2026 16:34
@adecaro adecaro force-pushed the perf/csp-allocation-reduction branch from c0dd3e6 to 3a791b6 Compare April 24, 2026 16:34

@adecaro adecaro left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@adecaro

adecaro commented Apr 24, 2026

Copy link
Copy Markdown
Contributor

Hi @neetance , I have merged your PR on mathlib and created the tag v0.1.0. Please, update the go.mod of the token-sdk and we are good to merge this. Thanks a lot for this great effort 🙏

@neetance neetance force-pushed the perf/csp-allocation-reduction branch from 3a791b6 to 0dec2d4 Compare April 24, 2026 17:17
Signed-off-by: Ankit Basu <ankitbasu14@gmail.com>
Signed-off-by: Ankit Basu <ankitbasu14@gmail.com>
Signed-off-by: Ankit Basu <ankitbasu14@gmail.com>
@adecaro adecaro force-pushed the perf/csp-allocation-reduction branch from 0dec2d4 to 68e4117 Compare April 24, 2026 17:40
@adecaro

adecaro commented Apr 24, 2026

Copy link
Copy Markdown
Contributor

@neetance , would you like to work on porting token/core/zkatdlog/nogh/v1/crypto/math/math.go#BatchInverse to further improve its efficiency? What do you think?

@adecaro adecaro merged commit 2293ed0 into LFDT-Panurus:main Apr 24, 2026
57 checks passed
@neetance

Copy link
Copy Markdown
Contributor Author

@adecaro yes sure sounds good to me 😄
Will analyze the code in detail and figure out where we can make some optimizations and open an issue once I come up with a solution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants