Skip to content

perf(csp): optimize range proof prover with native gnark arithmetic - #1679

Merged
adecaro merged 1 commit into
LFDT-Panurus:mainfrom
neetance:perf/optimize-csp-prover-native-arithmetic
May 7, 2026
Merged

perf(csp): optimize range proof prover with native gnark arithmetic#1679
adecaro merged 1 commit into
LFDT-Panurus:mainfrom
neetance:perf/optimize-csp-prover-native-arithmetic

Conversation

@neetance

@neetance neetance commented May 6, 2026

Copy link
Copy Markdown
Contributor

closes #1675

Description

Currently, the CSP Range Proof generation (Prove function in rp.go) heavily relies on mathlib.Zr scalar operations for building linear forms, computing inner products, and generating blinded witnesses. Because mathlib wraps gnark-crypto, every scalar multiplication or addition executed in these O(n) loops requires big.Int object allocations and round-trip conversions.

This PR bypasses the mathlib.Zr abstraction during the heaviest cryptographic loops in the Prover, dispatching directly to the underlying gnark-crypto Montgomery form arithmetic for supported curves (BN254 and BLS12-381).

Changes

  1. Introduced Native Prover Primitives (rp_native.go):
    • nativeRPBuildLF: Constructs the aggregated linear form natively.
    • nativeRPInnerProduct: Computes inner products natively.
    • nativeRPBlindWitness: Computes the blinded witness and evaluations using native field arithmetic.
  2. Updated Prover Logic (rp.go): Modified the CSP prover to check for curve compatibility via math.DispatchCurve and route the heavy $O(n)$ allocations to the new native functions.
  3. Scoped Strictly to the Prover: Kept the verifier and csp.go entirely untouched to preserve current validation stability.

Benchmarks

I ran the TestParallelBFProver for the csp range proof process and got the following results:

  • Before:

go test -v ./token/core/zkatdlog/nogh/v1/crypto/rp/csp/... -run TestParallelBFProver -count=1
=== RUN   TestParallelBFProver
=== RUN   TestParallelBFProver/Setup(bits_32,_curve_BN254,_#i_2,_#o_2)_with_16_workers
Metric           Value     Description
------           -----     -----------
Workers          16        
Total Ops        553       (Low Sample Size)
Duration         1.015s    (Good Duration)
Real Throughput  544.58/s  Observed Ops/sec (Wall Clock)
Pure Throughput  555.92/s  Theoretical Max (Low Overhead)

Latency Distribution:
 Min           15.956174ms  
 P50 (Median)  28.54867ms   
 Average       28.781021ms  
 P95           35.493159ms  
 P99           38.997791ms  
 P99.9         43.633265ms  
 Max           47.084761ms  (Stable Tail)

Stability Metrics:
 Std Dev  3.892847ms  
 IQR      5.09145ms   Interquartile Range
 Jitter   4.209978ms  Avg delta per worker
 CV       13.53%      Moderate Variance (10-20%)

System Health & Reliability:
 Error Rate          0.0000%          (100% Success) (0 errors)
 Memory              1293899 B/op     Allocated bytes per operation
 Allocs              14151 allocs/op  Allocations per operation
 Alloc Rate          658.73 MB/s      Memory pressure on system
 GC Overhead         8.67%            (Severe GC Thrashing)
 GC Pause            87.991197ms      Total Stop-The-World time
 GC Cycles           201              Full garbage collection cycles
 Goroutines Created  0                Net goroutines above baseline during recording

Latency Heatmap (Dynamic Range):
Range                     Freq  Distribution Graph
 15.956174ms-16.843267ms  2      (0.4%)
 18.768151ms-19.811578ms  3     █ (0.5%)
 19.811578ms-20.913015ms  1      (0.2%)
 20.913015ms-22.075687ms  9     ███ (1.6%)
 22.075687ms-23.302999ms  19    ████████ (3.4%)
 23.302999ms-24.598544ms  39    █████████████████ (7.1%)
 24.598544ms-25.966115ms  60    ██████████████████████████ (10.8%)
 25.966115ms-27.409717ms  77    █████████████████████████████████ (13.9%)
 27.409717ms-28.933578ms  91    ████████████████████████████████████████ (16.5%)
 28.933578ms-30.542158ms  80    ███████████████████████████████████ (14.5%)
 30.542158ms-32.240168ms  81    ███████████████████████████████████ (14.6%)
 32.240168ms-34.032581ms  40    █████████████████ (7.2%)
 34.032581ms-35.924643ms  29    ████████████ (5.2%)
 35.924643ms-37.921896ms  11    ████ (2.0%)
 37.921896ms-40.030187ms  8     ███ (1.4%)
 40.030187ms-42.255691ms  2      (0.4%)
 44.604922ms-47.084761ms  1      (0.2%)

--- Analysis & Recommendations ---
[WARN] Low sample size (553). Results may not be statistically significant. Run for longer.
[INFO] High Allocations (14151/op). This will trigger frequent GC cycles and increase Max Latency.
----------------------------------
--- PASS: TestParallelBFProver (4.18s)
    --- PASS: TestParallelBFProver/Setup(bits_32,_curve_BN254,_#i_2,_#o_2)_with_16_workers (4.18s)
PASS
ok      github.com/hyperledger-labs/fabric-token-sdk/token/core/zkatdlog/nogh/v1/crypto/rp/csp  4.207s
  • After:

go test -v ./token/core/zkatdlog/nogh/v1/crypto/rp/csp/... -run TestParallelBFProver -count=1
=== RUN   TestParallelBFProver
=== RUN   TestParallelBFProver/Setup(bits_32,_curve_BN254,_#i_2,_#o_2)_with_16_workers
Metric           Value     Description
------           -----     -----------
Workers          16        
Total Ops        559       (Low Sample Size)
Duration         1.016s    (Good Duration)
Real Throughput  550.20/s  Observed Ops/sec (Wall Clock)
Pure Throughput  562.28/s  Theoretical Max (Low Overhead)

Latency Distribution:
 Min           18.039946ms  
 P50 (Median)  28.389763ms  
 Average       28.455781ms  
 P95           34.418849ms  
 P99           37.613913ms  
 P99.9         38.956758ms  
 Max           39.794254ms  (Stable Tail)

Stability Metrics:
 Std Dev  3.334132ms  
 IQR      4.167685ms  Interquartile Range
 Jitter   3.722971ms  Avg delta per worker
 CV       11.72%      Moderate Variance (10-20%)

System Health & Reliability:
 Error Rate          0.0000%          (100% Success) (0 errors)
 Memory              1214220 B/op     Allocated bytes per operation
 Allocs              13166 allocs/op  Allocations per operation
 Alloc Rate          626.96 MB/s      Memory pressure on system
 GC Overhead         8.42%            (Severe GC Thrashing)
 GC Pause            85.517627ms      Total Stop-The-World time
 GC Cycles           201              Full garbage collection cycles
 Goroutines Created  50               Net goroutines above baseline during recording

Latency Heatmap (Dynamic Range):
Range                     Freq  Distribution Graph
 18.039946ms-18.767848ms  1      (0.2%)
 18.767848ms-19.525121ms  1      (0.2%)
 19.525121ms-20.31295ms   1      (0.2%)
 20.31295ms-21.132567ms   7     ███ (1.3%)
 21.132567ms-21.985255ms  6     ██ (1.1%)
 21.985255ms-22.872349ms  8     ███ (1.4%)
 22.872349ms-23.795236ms  21    █████████ (3.8%)
 23.795236ms-24.755362ms  21    █████████ (3.8%)
 24.755362ms-25.754228ms  43    ████████████████████ (7.7%)
 25.754228ms-26.793398ms  62    █████████████████████████████ (11.1%)
 26.793398ms-27.874498ms  69    ████████████████████████████████ (12.3%)
 27.874498ms-28.999219ms  85    ████████████████████████████████████████ (15.2%)
 28.999219ms-30.169323ms  73    ██████████████████████████████████ (13.1%)
 30.169323ms-31.38664ms   66    ███████████████████████████████ (11.8%)
 31.38664ms-32.653074ms   46    █████████████████████ (8.2%)
 32.653074ms-33.970609ms  20    █████████ (3.6%)
 33.970609ms-35.341306ms  14    ██████ (2.5%)
 35.341306ms-36.767309ms  7     ███ (1.3%)
 36.767309ms-38.250851ms  6     ██ (1.1%)
 38.250851ms-39.794254ms  2      (0.4%)

--- Analysis & Recommendations ---
[WARN] Low sample size (559). Results may not be statistically significant. Run for longer.
[INFO] High Allocations (13166/op). This will trigger frequent GC cycles and increase Max Latency.
----------------------------------
--- PASS: TestParallelBFProver (4.19s)
    --- PASS: TestParallelBFProver/Setup(bits_32,_curve_BN254,_#i_2,_#o_2)_with_16_workers (4.19s)
PASS
ok      github.com/hyperledger-labs/fabric-token-sdk/token/core/zkatdlog/nogh/v1/crypto/rp/csp  4.216s

We can observe that the allocs/op number went down from 14151 to 13166, a total of 985 allocs(~7% decrease)
the other metrics remain stable with no visible regression

Let me know if this is good 🙏

Signed-off-by: Ankit Basu <ankitbasu14@gmail.com>
@adecaro
adecaro self-requested a review May 7, 2026 05:32
@adecaro adecaro self-assigned this May 7, 2026
@adecaro adecaro added this to the Q2/26 milestone May 7, 2026

@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.

Great effort @neetance , thanks a lot 🙏

@adecaro
adecaro merged commit b5aaea3 into LFDT-Panurus:main May 7, 2026
93 of 94 checks passed
SurbhiAgarwal1 pushed a commit to SurbhiAgarwal1/fabric-token-sdk that referenced this pull request May 13, 2026
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.

perf: Optimize CSP Range Proof Generation with Native Gnark Arithmetic

2 participants