GithubHelp home page GithubHelp logo

Comments (2)

fkrause98 avatar fkrause98 commented on July 21, 2024

There are some approaches for modexp, modexp has the following signature:

fn modexp(base: BigInt, exp: BigInt, modulo: Int) -> BigInt

Montgomery

The issue with the Montgomery algorithm is that we need to precompute a constant $R \in \mathbb{Z}$ such that $R \gt n$ and $gcd(R, n) = 1$ (where n is the modulo argument).
This is fine when we know $n$ beforehand, since we can just choose and hardcode $R$ but when $n$ is unknown this becomes problematic.
Problematic because we need to find a coprime, large number to an arbitrary $n$.
There are possible solutions to this though, if we split $n$ into two cases:

  1. If $n \equiv 1 \mod 2$, we could simply choose $R = 2^{\log_{2}(n)+1}$
  2. If $n \equiv 0 \mod 2$, this is not really clear but Montgomery Reduction With Even Modulus proposes a solution using The Chinese Remainder Theorem but of course we would have to go and implement it.

Maybe there are more solutions with Montgomery but I have not found them.

Barret

An alternative to Montgomery's is the Barret Reduction.
Barret still needs to precompute 2 constants: $k$ such that $2^{k} \gt n$ and $r = \frac{4^k}{n}$.
The election of $k$ can be done simply taking $k$ = $log_{2}(n)$ which can be done through shifts.
$r$ can also be computed with shifts and a division since $4^{k} = 2^{2k} = 1 \gg 2k$.
Also, as an advantage, the barret reduction does not need a special form like Montgomery's form
when operating.
The downside of this approach is that $0 \leq \text{base} \leq n^2$ as a precondition must hold.

Useful links:

from zksync_era_precompiles.

ilitteri avatar ilitteri commented on July 21, 2024

Closed as the research finished.

from zksync_era_precompiles.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.