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

Add BLS Elliptic Curve support in Cryptol #14

Merged
merged 12 commits into from
Sep 24, 2024
Merged

Conversation

b13decker
Copy link
Collaborator

Add the elliptic curve operations needed to continue to implement the Deneb Python spec BLS helper functions.

(Note for reviewers: I've made an effort to make this PR more easily reviewed on a per-commit basis. Hope this helps).

This will be needed for the `bls_modular_inverse` function.
These two functions are hardcoded to work for the BLS_MODULUS. There is
currently no need to abstract them further.

These functions match the implementations found in the Python specs:
 - https://github.com/ethereum/consensus-specs/blob/dev/specs/deneb/polynomial-commitments.md#bls_modular_inverse
 - https://github.com/ethereum/consensus-specs/blob/dev/specs/deneb/polynomial-commitments.md#div
See https://github.com/ethereum/consensus-specs/blob/dev/specs/deneb/polynomial-commitments.md#compute_powers

Note that because of the way Cryptol's type system and comprehensions
work, we need to have the type constrain of `n >= 2`, whereas the Python
spec only constrains `n >= 0`.
We need to use a GCD algorithm because Cryptol cannot perform
expoentiation with negative exponents. This means we have an error in
the BlsHelpers::bls_modular_inverse function. I'll fix that next.
Have bls_modular_inverse and bls_div call the corresponding utility
functions with the hardcoded BLS_MODULUS. This fixes the previous error
in the bls_modular_inverse function.

This error was uncaught because the unit tests were wrong. The unit
tests have now been confirmed with the following site:
  https://planetcalc.com/3298/
I switched the API to use the Integer type for these utility functions
because it simplifies the code. The BlsHelper functions that call these
utility functions have been updated and now handle the conversion of
types (Integral <=> Integer) internally.

This also exposed an error in the modular_inverse when a negative
number was passed. A new test has been added to make sure we don't
regress.
These were redundant and missed during the previous commit redesign of
types.
And add note to the BlsHelpers::bls_modular_inverse about the inability
to use pow and why that code now looks different than the Python spec.
These are needed as the building blocks as we start to implement the
rest of the Deneb Python spec.
@b13decker b13decker added this to the Milestone 2 milestone Sep 18, 2024
@b13decker b13decker self-assigned this Sep 18, 2024
Copy link
Collaborator

@marsella marsella left a comment

Choose a reason for hiding this comment

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

I've only partly reviewed this but I'm going to give some feedback now since I think you started getting similar feedback in a separate channel.

I'm seeing some mixing of types that I think is introducing more potential bugs than you need. Some code uses bit vectors (integers mod 2^n for the width n), but they are actually holding integers mod p for p =\= 2^n, and also in other places they are converted to Integers.

Unless you are trying to do produce highly optimized implementations of these functions that minimize memory usage or something, I think you are better off using the Z type to represent your numbers. I've flagged several overflow concerns; these will all be handled by Cryptol if you use Z. The EC implementation in cryptol-specs uses that type if you would like to see it in practice.

spec/Common/Utils.cry Outdated Show resolved Hide resolved
spec/Spec/BlsHelpers.cry Show resolved Hide resolved
spec/Spec/BlsHelpers.cry Show resolved Hide resolved
spec/Spec/BlsHelpers.cry Outdated Show resolved Hide resolved
spec/Spec/BlsHelpers.cry Show resolved Hide resolved
spec/Spec/BlsHelpers.cry Show resolved Hide resolved
spec/Spec/BlsEC.cry Show resolved Hide resolved
spec/Spec/BlsEC.cry Show resolved Hide resolved
@b13decker
Copy link
Collaborator Author

I've only partly reviewed this but I'm going to give some feedback now since I think you started getting similar feedback in a separate channel.

I'm seeing some mixing of types that I think is introducing more potential bugs than you need. Some code uses bit vectors (integers mod 2^n for the width n), but they are actually holding integers mod p for p =\= 2^n, and also in other places they are converted to Integers.

Unless you are trying to do produce highly optimized implementations of these functions that minimize memory usage or something, I think you are better off using the Z type to represent your numbers. I've flagged several overflow concerns; these will all be handled by Cryptol if you use Z. The EC implementation in cryptol-specs uses that type if you would like to see it in practice.

Thanks for pointing out the Z type. That would have saved me a lot of trouble and time, but at least I know now. I think what I'm going to do is create an issue to refactor the code to use Z. Since I already have tests for each function, this shouldn't be bad. Thanks so much for this review!

@b13decker
Copy link
Collaborator Author

@marsella Captured all these issues here: #21

Thank you again for the thorough review!

Copy link
Collaborator

@marsella marsella left a comment

Choose a reason for hiding this comment

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

Thanks for making the follow-up issue.

@b13decker b13decker merged commit 569aed2 into main Sep 24, 2024
@b13decker b13decker deleted the spec/feature/ecc-bls branch September 24, 2024 20:41
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.

2 participants