refactor: optimize circuit inputs computation [skip-line-limit]#1310
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Warning Rate limit exceeded
⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. 📝 WalkthroughWalkthroughAdded a public Changes
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In
`@crates/zk-helpers/src/circuits/threshold/user_data_encryption/computation.rs`:
- Around line 402-433: The code centers ct0/ct1/pk0/pk1 after calling
reduce(&moduli) but calls e0.center(&moduli) without reduce, which confuses
readers because center() requires coefficients in [0, qi); update the code by
adding a concise clarifying comment immediately above the e0 =
CrtPolynomial::from_fhe_polynomial(&e0) / e0.center(&moduli) sequence stating
that error polynomials produced by from_fhe_polynomial() are guaranteed by the
FHE extraction routine to already have coefficients in the canonical [0,qi)
range (or reference the exact guarantee in utils.rs), or alternatively call
e0.reduce(&moduli)? before centering if no such guarantee exists; mention the
involved symbols (e0, from_fhe_polynomial, reduce, center, utils.rs) in the
comment for traceability.
🧹 Nitpick comments (5)
crates/zk-helpers/src/circuits/threshold/user_data_encryption/computation.rs (2)
455-465:expectandassert!will panic in non-test code — acceptable here as CRT invariant guards, but consider returning errors for resilience.The
expect("CRT requires exact division")on line 460 and theassert!on line 462 enforce mathematical invariants that must hold for correct CRT reconstruction. In ZK/crypto code, a violated invariant means the computation is fundamentally broken, so panicking is a defensible choice. That said, if this code ever runs in a context where graceful error propagation is preferred (e.g., a service), converting these toResult-based errors would be more resilient.
586-593: Nit:k1: k1can be shortened tok1.Rust allows field shorthand when the field name matches the variable name.
Proposed fix
- k1: k1, + k1,crates/zk-helpers/src/circuits/dkg/share_encryption/computation.rs (3)
349-351:_presetis actually used — drop the underscore prefix.The leading underscore conventionally signals an intentionally unused binding. Since
_presetis passed tobuild_pair_for_preseton line 351, the prefix is misleading. Rename topreset.Proposed fix
- fn compute(_preset: Self::Preset, data: &Self::Data) -> Result<Self, Self::Error> { + fn compute(preset: Self::Preset, data: &Self::Data) -> Result<Self, Self::Error> { let (_, dkg_params) = - build_pair_for_preset(_preset).map_err(|e| CircuitsErrors::Sample(e.to_string()))?; + build_pair_for_preset(preset).map_err(|e| CircuitsErrors::Sample(e.to_string()))?;
385-386:messageis not reduced tozkp_modulus, unlike every other polynomial stored inInputs.All other polynomials (
e1,u,e0_mod_q, and all CRT polynomials) are reduced modzkp_modulusbefore being stored.messageis skipped. While its coefficients are small enough that reduction is a no-op, the inconsistency may cause confusion or become a latent bug if plaintext encoding changes.Proposed fix — add reduce for consistency
e0_mod_q.reduce(&zkp_modulus); + message.reduce(&zkp_modulus);Also applies to: 541-543, 561-561
426-490: Per-modulus parallel block is nearly identical to the threshold/user_data_encryption counterpart.The
izip+par_bridge+map+sort_by_keypattern, including thee0_quotientcomputation,ct0i_hat/ct1i_hatassembly, anddecompose_residuecalls, is duplicated almost verbatim across both files. Consider extracting the shared CRT decomposition logic into a reusable helper (e.g., incrate::mathor a newcrate::crtmodule) to reduce maintenance burden. This can be deferred to a follow-up.
Summary by CodeRabbit
New Features
Refactor
Breaking Changes
Documentation