Skip to content

feat: add modular inverse for variable modulus #1507

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

ivokub
Copy link
Collaborator

@ivokub ivokub commented Jun 4, 2025

Description

This PR implements ModInverse for testing some ideas for more efficient ModExp. The problem with the approach right now is though that there may not always be an inverse in which case the solver would fail.

@yelhousni - maybe we could work past it by setting the inverse to be something 0 or 1 etc in case it doesn't exist?

Meanwhile - do not merge. I think in most cases we don't want to expose methods which would allow for liveness failure.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How has this been tested?

  • TestVariableInverse
  • Test B

Checklist:

  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • I did not modify files generated from templates
  • golangci-lint does not output errors locally
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

@ivokub ivokub requested review from yelhousni and Copilot June 4, 2025 12:22
@ivokub ivokub self-assigned this Jun 4, 2025
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds a new feature to compute the modular inverse with a variable modulus to support more efficient modular exponentiation. Key changes include:

  • Updating computeInverseHint to take an additional modulus parameter.
  • Modifying inverse and modular operations to use the variable modulus.
  • Adding tests to validate both successful and failing modular inversions.

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
std/math/emulated/hints.go Updated computeInverseHint to accept a variable modulus parameter.
std/math/emulated/field_ops.go Adjusted inverse to pass the variable modulus to computeInverseHint.
std/math/emulated/custommod.go Introduced ModInverse and standardized zero checks in modular operations.
std/math/emulated/custommod_test.go Added tests to validate modular inversion for both existent and non-existent cases.
Comments suppressed due to low confidence (1)

std/math/emulated/hints.go:43

  • Consider updating the function's documentation comment to mention the new modulus parameter and how it is used to form the hint inputs.
func (f *Field[T]) computeInverseHint(inLimbs []frontend.Variable, modulus *Element[T]) (inverseLimbs []frontend.Variable, err error) {

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.

1 participant