Skip to content
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

Barret reduction for modexp #170

Draft
wants to merge 59 commits into
base: main
Choose a base branch
from
Draft

Conversation

fkrause98
Copy link
Contributor

@fkrause98 fkrause98 commented Oct 23, 2023

Pending tasks:

  • Finish barret_reduction implementation.
  • Precalculate needed constants.
  • Do not hardcode addresses.
  • Add the necessary scratch buffers.
  • Integrate into modexp respecting the following:
    1. Make sure this reduction is done AFTER the first loop so the base is inside the [0, n**2) range.
    2. Make sure the reduction is not used if the modulo is a power of 2, which can done "easily" through
    ands.

Useful references:

ilitteri and others added 30 commits September 22, 2023 13:11
* Add `bigUIntShl` implementation

* Add constants

* Fix compilation
* Add `bigUIntShr` implementation

* Add constants
* Implement bigUIntBitOr

* Fix bigUIntBitOr

* Fix bigUIntBitOr

Co-authored-by: Ivan Litteri <[email protected]>

* Fix missing closing brackets

---------

Co-authored-by: Ivan Litteri <[email protected]>
* Implement bigUIntCondSelect

* Fix missing curly braces

Co-authored-by: Ivan Litteri <[email protected]>

* Implement Big UInt Right Shift (#137)

* Add `bigUIntShr` implementation

* Add constants

* Implement Big UInt Bitwise Or for modexp (#135)

* Implement bigUIntBitOr

* Fix bigUIntBitOr

* Fix bigUIntBitOr

Co-authored-by: Ivan Litteri <[email protected]>

* Fix missing closing brackets

---------

Co-authored-by: Ivan Litteri <[email protected]>

---------

Co-authored-by: Ivan Litteri <[email protected]>
* First implementation of mul operation for bigints

* Fix multiplication for big integers

* Fix some merge issues

* Improve comments and function docs

* Delete whitespaces
* First substraction draft

* Fix compile problems

* Working implementation

* Updated code

* Updated code

* Update subtract implementation

* Remove console_log

* Add docs for function

* Update function docs

* Remove tests from from ModExp.yul

* Fix typo

* Restore horrible whitespaces to avoid an ugly merge conflict

* Update precompiles/Modexp.yul

Co-authored-by: Ivan Litteri <[email protected]>

* Revert "Update precompiles/Modexp.yul"

This reverts commit 582bc41.

---------

Co-authored-by: Joaquín P. Centeno <[email protected]>
Co-authored-by: Ivan Litteri <[email protected]>
* Make Big UInt API functions naming consistent

* Refactor `bigUIntAdd` variable names

* Refactor `bigUIntMul` variable names

* Refactor `subLimbsWithBorrow`

* Refactor `bigUintSubtractionWithBorrow`

* Refactor `bigUIntAdd`

* Fix `bigUIntSubWithBorrow`

* Format `storeLimbValueAtOffset`

* Refactor `bigUIntBitOr`

Made it consistent with the rest of the code convention and naming

* Refactor `bigUIntCondSelect`

Made it consistent with the rest of the code convention and naming

* Reorder `overflowingSubWithBorrow`

* Move comment to modexp API Docs section
* Division draft

* Non working draft

* Fix compile errors

* Use proper pointers for quotient and remainder

* Add fix note, some more changes

* Add comment

* Implement `big_uint_bit_size`

* Increase pointer to prevent it from steping over console_log

* WIP divrem

* Fix loop and zero initializer

* Push test cases

* Add other test case

* Add docs and tests for `big_uint_inplace_or_1`

* Fix bug related to bit shifting

* Fix borrow return in big uint sub function

* Delete playground file used for debugging

* Fix sub with borrow function

* Add playground again to check more big integer division tests

* Remove playground used for testing

* Write documentation for new shift functions

* Improve naming and documentation for new helper functions

* Rename bigUIntOrWith1 to bigUintInPlaceOrWith1

* Add tmp buffer parameters to bigUIntDivRem. Improve docs.

* Simplify subLimbsWithBorrow

Co-authored-by: Ivan Litteri <[email protected]>

* Remove `mul` call from `bigUIntInPlaceOrWith1`

Co-authored-by: Ivan Litteri <[email protected]>

* Remove multiplications from copyBigUint

Co-authored-by: Ivan Litteri <[email protected]>

* Optimize bigUIntBitSize loop

Co-authored-by: Ivan Litteri <[email protected]>

* Simplify zeroWithLimbSizeAt

---------

Co-authored-by: Francisco Krause Arnim <[email protected]>
Co-authored-by: IAvecilla <[email protected]>
Co-authored-by: Ivan Litteri <[email protected]>
* Division draft

* Non working draft

* Fix compile errors

* Use proper pointers for quotient and remainder

* Add fix note, some more changes

* Add comment

* Implement `big_uint_bit_size`

* Increase pointer to prevent it from steping over console_log

* WIP divrem

* Fix loop and zero initializer

* Push test cases

* Add other test case

* Add docs and tests for `big_uint_inplace_or_1`

* Fix bug related to bit shifting

* Fix borrow return in big uint sub function

* Delete playground file used for debugging

* Fix sub with borrow function

* Add playground again to check more big integer division tests

* Remove playground used for testing

* Write documentation for new shift functions

* Improve naming and documentation for new helper functions

* Rename bigUIntOrWith1 to bigUintInPlaceOrWith1

* Add tmp buffer parameters to bigUIntDivRem. Improve docs.

* Add big uint mul mod skeleton

* Remove wrong comment

* Update algorithm comment

* Add limb size doubling and divide by two for mul mod operation

* Functions to duplicate and halve limb size work in place

* Use camelCase

* Remove console_log

* Add docs

* Update doc

---------

Co-authored-by: Francisco Krause Arnim <[email protected]>
Co-authored-by: Joaquín P. Centeno <[email protected]>
* Add free memory pointer function

* Update precompiles/Modexp.yul

Co-authored-by: Ivan Litteri <[email protected]>

* Update precompiles/Modexp.yul

Co-authored-by: Ivan Litteri <[email protected]>

---------

Co-authored-by: Ivan Litteri <[email protected]>
* WIP: modexp skeleton

* Use of mul mod function for big integers

* imlement aux function to check if big uint is larger than 1

* minor fix

* Restore modexp from target branch

* Fix mul mod

* Finish modexp implementation

* Remove playground used for debugging

* Update modexp with final state of modular exponentiation function

* Fix merge issue

* Change all names to camel case

* fix typo

Co-authored-by: Francisco Krause Arnim <[email protected]>

---------

Co-authored-by: IAvecilla <[email protected]>
Co-authored-by: Ivan Litteri <[email protected]>
Co-authored-by: Francisco Krause Arnim <[email protected]>
@fkrause98 fkrause98 marked this pull request as draft October 23, 2023 21:21
Base automatically changed from modexp_reimplementation to main October 30, 2023 23:21
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.

4 participants