perf(bls12-381-gurvy): Optimize BLS12-381-GURVY Zr using fr.Element (reduce allocations)#45
Conversation
…duce allocations Signed-off-by: Ankit Basu <ankitbasu14@gmail.com>
|
Hi @adecaro I submitted a pr here as you instructed. Following its merge, I will submit a pr in the token-sdk repo with appropriate changes in the csp library 🙏 |
|
Hi @neetance , great effort 🙏 I have left comments on the PR and the issues raised by the linter need to be fixed. |
Signed-off-by: Ankit Basu <ankitbasu14@gmail.com>
|
Hi @adecaro, thanks for the review 🙏 |
|
@neetance , if you run |
Signed-off-by: Ankit Basu <ankitbasu14@gmail.com>
|
HI @neetance , great the PR passed here. Please, let's test this commit on the token-sdk. If everything goes well, we merge everything 🙏 |
|
@neetance , can you run the tests without the pool? Just to make sure we don't loose in performance. |
|
@adecaro sure I ran the tests in the other 2 executor modes and the results are fine, with improvements in allocs/op, and the rest of the metrics remaining same. Here are results: Serial:Unbounded:To make sure, I undid the changes using Serial:Unbounded:As we can see, the allocs/op is considerably higher for the second results that ran without the changes in the current mathlib library. Let me know if this is good 😄 |
|
hey, great effort @neetance , thanks for taking the time to run the experiments. Please, go ahead with a PR on the token-sdk 🙏 |
Summary
This PR optimizes the BLS12-381 GURVY driver by replacing the
big.Int-based scalar (Zr) representation withfr.Element.The goal is to eliminate repeated
big.Int <-> fr.Elementconversions in scalar arithmetic and reduce allocation overhead in cryptographic operations.Key Changes
Replace:
with:
Use
fr.Elementfor all scalar arithmeticEliminate repeated
SetBigInt / BigIntconversionsIntroduce
rawBigIntonly for special cases (e.g. GroupOrder)Optimize ModMul / ModAddMul / ModAddMul2 / ModAddMul3 using stack variables
Optimize MultiScalarMul to avoid per-scalar conversions
Use
sync.Poolforbig.Intonly where requiredBenchmark results
I ran the benchmark for the token-sdk csp proofs for the current mathlib library and the for the modified library locally and here are the results:
Before mathlib changes:
After the changes:
We can see that:
This is a considerable reduction in allocs/op as well as in B/op that we were aiming for
Observations
Testing
All tests pass:
Benchmarks run successfully
No functional changes
Let me know if this is good 🙏