Skip to content

[EC] Fix mutable assignment in batch affine - followup #579#589

Merged
mratsim merged 1 commit intomasterfrom
batch-affine-mut
Mar 15, 2026
Merged

[EC] Fix mutable assignment in batch affine - followup #579#589
mratsim merged 1 commit intomasterfrom
batch-affine-mut

Conversation

@mratsim
Copy link
Owner

@mratsim mratsim commented Mar 15, 2026

Currently we have a failure constantine/constantine/math/elliptic/ec_shortweierstrass_batch_ops.nim(52, 3) Error: 'SecretBool(affs[0].y.mres.limbs[0])' cannot be assigned to

This is because after #579, the SecretWord myExpression creates an immutable expression

template zero(i: int): SecretBool =
when F is Fp:
SecretBool affs[i].y.mres.limbs[0]
else:
SecretBool affs[i].y.coords[0].mres.limbs[0]
affs[0].x = projs[0].z
zero(0) = affs[0].x.isZero()
affs[0].x.csetOne(zero(0))
for i in 1 ..< N:
# Skip zero z-coordinates (infinity points)
var z = projs[i].z
zero(i) = z.isZero()
z.csetOne(zero(i))

The strange thing is that, when compiling through nimble test or nimble test_parallel that do a simple exec, compilation works and test is run:

image

but compiling the code directly triggers a compiler error.

Summary by CodeRabbit

  • Refactor
    • Enhanced internal consistency and performance in cryptographic batch operations through improved type handling in elliptic curve computations.

@gemini-code-assist
Copy link

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a compilation error in the batch affine operations by refining the type handling for SecretBool and SecretWord. The changes ensure that expressions are correctly typed and cast when assigned or passed to functions, resolving an issue where an immutable SecretWord expression was implicitly assigned to a SecretBool type, leading to a compiler error.

Highlights

  • Type Correction for zero Template: The zero template's return type was changed from SecretBool to SecretWord to correctly handle the type of the expression isZero() which returns a SecretWord.
  • Explicit Type Casting: Introduced explicit SecretBool casts when passing the result of the zero template to functions like csetOne and csetZero, which expect a SecretBool argument.
  • Resolution of Compilation Error: This change resolves a compilation error related to immutable assignment, specifically 'SecretBool(...) cannot be assigned to', which occurred due to a type mismatch after a previous update.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • constantine/math/elliptic/ec_shortweierstrass_batch_ops.nim
    • Updated the zero template's return type from SecretBool to SecretWord in both batchAffine function overloads.
    • Modified assignments to zero(i) to explicitly cast the isZero() result to SecretWord.
    • Applied explicit SecretBool casts to the zero(i) and zero(0) template calls when used as arguments for csetOne and csetZero functions.
Activity
  • Identified a compilation failure: 'SecretBool(...) cannot be assigned to' in ec_shortweierstrass_batch_ops.nim.
  • Traced the error to an immutable expression created after changes introduced in [ec]: batchAffine does not use alloca anymore #579.
  • Noted that the error only occurred during direct compilation, not via nimble test or nimble test_parallel.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@coderabbitai
Copy link

coderabbitai bot commented Mar 15, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 81867b9a-239e-40fc-8c16-c31e0561ad91

📥 Commits

Reviewing files that changed from the base of the PR and between cedcf0d and b905691.

📒 Files selected for processing (1)
  • constantine/math/elliptic/ec_shortweierstrass_batch_ops.nim

📝 Walkthrough

Walkthrough

Type system refactoring in elliptic curve batch operations module replacing SecretBool with SecretWord for zero-tracking mechanism. The template zero(i: int) return type changed accordingly, with cascading updates to isZero checks, set operations, and batch inversion paths.

Changes

Cohort / File(s) Summary
Zero-tracking type refactoring
constantine/math/elliptic/ec_shortweierstrass_batch_ops.nim
Updated batch inversion logic to use SecretWord instead of SecretBool for zero-tracking; changed template zero(i: int) return type, adjusted isZero assignments, modified csetZero/csetOne calls with appropriate type conversions, and added explicit non-lazy path handling in loop branches.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐰 ✨ A Secret Word now tracks the zero,
Where once a Bool did dance so fair,
Through batch inversions we shall go,
With types aligned with crypto care!
Type safety blooms, the refactor's true. 🌟

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly references a specific fix for mutable assignment in batch affine operations and directly links to a follow-up issue, accurately summarizing the main change.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch batch-affine-mut
📝 Coding Plan
  • Generate coding plan for human review comments

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request addresses a compilation error related to assigning to an immutable expression, which was introduced in a previous change. The fix involves modifying a template to return a mutable l-value instead of a type-casted r-value, and then adding explicit type casts at the call sites to match the expected types. The changes are correct, consistent across the file, and effectively resolve the issue.

@mratsim
Copy link
Owner Author

mratsim commented Mar 15, 2026

@greptile

@mratsim mratsim merged commit 17eb597 into master Mar 15, 2026
17 checks passed
@mratsim mratsim deleted the batch-affine-mut branch March 15, 2026 09:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant