Skip to content
This repository has been archived by the owner on Nov 9, 2023. It is now read-only.

Implement Modular Inverse Algorithm. #12

Merged
merged 25 commits into from
May 28, 2019

Conversation

CPerezz
Copy link
Contributor

@CPerezz CPerezz commented May 28, 2019

Implemented all of the necessary functions and tools to implement the Kalinski Modular Inverse Algorithm for FieldElement.

This consists on the following implementations:

Everything is tested, commented and documented.

The idea is to Implement at some point Savas & Koç Montgomery Modular Inverse algorithm version and benchmark it vs #9

Now we can continue with Point Addition algorithms that need the inverse operator to be performed. All ready to proceed with #8

CPerezz added 25 commits May 16, 2019 17:58
- We need num type in order to use
` num::Integer::is_even()` function
for the implementation of `is_even()`
function for `FieldElement`
- Added use of `num::Integer` trait to use implement
`is_even()` function.
- Derive `PartialOrd` for `FieldElement` struct.
- Implement `is_even()` for `FieldElement`.
- Implement first part of `inverse` for `FieldElement`
to be concise, `PhaseI` described on the paper:
https://ieeexplore.ieee.org/document/863048
- Phase II takes care about acomplishing
the last step to complete the
`AlmMontInv()` function specified on the
paper: IEEE TRANSACTIONS ON COMPUTERS, VOL. 49, NO. 7, JULY 2000 763
The Montgomery Modular Inverse - Revisited
E. Sava¸s, C¸. K. Ko¸c
- This returns `(a^-1) * 2^n (mod p)`
- Implementation of McIvor, Corinne & Mcloone, Maire & Mccanny, J.V.. (2004).
  Improved Montgomery modular inverse algorithm.
  Electronics Letters. 40. 1110 - 1112. 10.1049/el:20045610.
- Stopped until `two_pow_k()` func is implemented.
-Perform the operation `2^k` returning the
result as a `FieldElement`.

Note: Input `k` has to relay over the range:
0..253 otherways, it will fail.
- Corrected the implementation for the
`two_pow_k()` function substracting
`52^i` consistently on each slot.
- Implemented tests for all of the edge-
cases of the function and all are passing.
- The implementation done, might be enough
performant, anyways, we can explore different
implementations once #9 is solved.
- Finnished implementation according to the
algorithm that appears on:
The Montgomery Modular Inverse - Revisited
E. Sava¸s, C¸. K. Ko¸: https://ieeexplore.ieee.org/document/863048

- Need for tests and a probable refactor
of some wrong parts of the code.
- Implemented PartialOrd as the derivation
was not doing the expected.
- Implemented Ord for `FieldElement` since
the derivation behaviour wasn't the expected.
- Implement tests for `PartialOrd` &
`Ord` implementations.
- Fix some Phase I nitpicks.
- We need to perform 2-3 MontgomeryProds in order
to perform the inverseMod operation.
- Implemented division by two for odd and even
`FieldElements` with carrying.
- Implemented tests for `half()` which pass successfully.
- Implement `to_montgomery()` conversion function.
- Implement `montgomery_mul()` for `FieldElement`.
- Refactor last part of the algorithm with Montgomery mul
function.
- Implemented `from_montgomery()` function
in oder to be able to convert back numbers
that live on the Montgomery domain.
- Tying now the `inverse()` Phase I with `p =
FieldElement::minus_one()` as is the biggest
value we can give (since p = 0 (mod l)).
- Value of `p` has been proven to be working
by writting `p` (which equals `FIELD_L` not as
`FieldElement::zero()` but on it's normal conversion.
This solves some of the issues of #10 .

- Added `INV_RR = (2^253)^2 (mod l)` on
the u64 backend on `constants.rs`.
- Implemented `montgomery_mul()` & to/from
Montgomery conversions.

Still needs to be reviewed and tested to see if
the reduction with the new modulo is working
as expected.

- Added more comment-docs on `constants.rs` file.
- `ConditionallyNeg` and `ConditionallySelectable`
aren't used at the moment so can ve removed.

- Also removed `Eq` trait import.
- Increased the max range to `260` since on functions
like `inverse()` we need to exponenciate to the
Montgomery modulus, so `2^260`.
- Tests are failing atm. We should review the montgomery
conversions and try to review it by parts.
- The tools mentioned on #11 have been implemented
altogether with `montgomery_mul()`.

- Implemented tests for both functions which are
passing correctly.
- Fixed the loop on Phase II where the number
of iterations should start from 0 instead
of 1.
- Two definitely hasn't to be on the montgomery
domain.

Last part of the algorithm has to be reviewed.

**Phase I && Phase II have been tested and are
working as expected to. They are correctly
implemented and giving the results needed.**

This is a big advance in order to close #9
**Refecences:**
B. S. Kaliski Jr. - The  Montgomery  inverse  and  its  applica-tions.
IEEE Transactions on Computers, 44(8):1064–1065, August-1995

After this commit, #9 can be closed.

-Implemented Kalinski Modular Inverse
algorithm.
- Implemented tests for the algorithm
which are passing successfully.
- Improved the docs of the inverse function.

At this point, Implement the Savas & Koç
modification of this algorithm to increase
the performance of the operation will be great.
So maybe we can implement the algorithm and then
benchmark coth solutions to see which performs
better with our `FieldElement` definition.

Tools for the second algorithm implementation
were builded on 54152f2 and allowed us to close #11
@CPerezz CPerezz added enhancement New feature or request review needed This PR, MR or Issue status needs to be reviewed. labels May 28, 2019
@CPerezz CPerezz requested review from ZER0 and LukePearson1 May 28, 2019 11:15
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request review needed This PR, MR or Issue status needs to be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants