GithubHelp home page GithubHelp logo

arithmetic-operations-for / integers-modulo-n-big-endian Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 1.0 3.93 MB

:mermaid: Modular arithmetic for JavaScript

Home Page: https://arithmetic-operations-for.github.io/integers-modulo-n-big-endian

License: GNU Affero General Public License v3.0

JavaScript 61.57% Python 34.40% Shell 4.02%
algorithms agpl javascript biginteger big-endian integer modulo prime modular arithmetic

integers-modulo-n-big-endian's People

Watchers

 avatar

Forkers

lgtm-migrator

integers-modulo-n-big-endian's Issues

Barrett reduction

We want to compute x mod n. Let q = floor(x/n) then x - qn = x mod n and 0 <= x - qn < n. This requires one multiplication and one subtraction once q is known. Using multiprecision floating point arithmetic, we could precompute 1/n and compute q with a single multiplication. We just need to figure exactly how much precision is required. Let r be the radix used to represent integers, Barrett reduction approximates 1/n with m/r^k for some k (m can be precomputed as m = floor(r^k/n)). Then dividing by n amounts to multiplying by m then dividing by r^k (a shift by k places using representation in base r). Provided we choose k appropriately, this will give the correct result modulo n with a result between 0 and 2n-1 (this happens because of the rounding down of m/r^k and can be fixed with a single subtraction).

The error of our approximation of 1/n is e = 1/n - m/r^k, so as long as xe < 1, hence e < 1/x, we are fine (since that error is rounded down to zero). This gives the bound required on k for the reduction to work: we have m = floor(r^k/n) and e = 1/n - m/r^k < 1/x, hence

1/n - m/r^k < 1/x
m/r^k > 1/n - 1/x
m/r^k > (x - n) / xn
r^k < m xn / (x - n)

For instance if n = r^l with l <= k then r^k/n is an integer and m = r^k/n thus

1 < x / (x - n)

Hence n != 0, x > n. This applies for all cases where n divides r^k and does not restrict k other than k >= 0. But the reduction is useless in that case.

What if n does not divide r^k? Then m = floor(r^k/n) > r^k/n - 1. Thus we can guarantee that r^k < mxn / (x - n) if

r^k < r^k x / (x - n) - xn / (x - n)
1 < x / (x - n) - xn / (x - n) / r^k
xn / (x - n) / r^k < x / (x - n) - 1 = (x - (x - n)) / (x - n) = n / (x - n)
r^k > x
k > log x / log r

Which means that k must be at least the number of words in x.
Note that we divide by x-n so x > n.

See https://en.wikipedia.org/wiki/Barrett_reduction

Simplify Montgomery#pow API

The exponent for Montgomery#pow should only take binary representation of exponent as input. Consumers of the library are supposed to provide their own binary conversion.

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.