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
22 changes: 22 additions & 0 deletions spec/Spec/BlsHelpers.cry
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ bls_modular_inverse x = pow`{BlsFieldSize} x NEGATIVE_ONE BLS_MODULUS
bls_div : BlsFieldElement -> BlsFieldElement -> BlsFieldElement
bls_div x y = (x * (bls_modular_inverse y)) % BLS_MODULUS

/**
* Return x to power of [0, n-1].
* @see https://github.com/ethereum/consensus-specs/blob/dev/specs/deneb/polynomial-commitments.md#compute_powers
* NOTE: this Cryptol function requires n >= 1, which is not the same constraint on the Python function,
* whose constraint is n >= 0.
*/
compute_powers : {n} (fin n, width n <= 64, n >= 2) => BlsFieldElement -> [n]BlsFieldElement
compute_powers x = powers where
powers = [x] # [(powers@(i-1) * x) % BLS_MODULUS | i <- [1 .. n-1]]
b13decker marked this conversation as resolved.
Show resolved Hide resolved

/*
* ===============
* Unit test suite
Expand Down Expand Up @@ -70,3 +80,15 @@ property test_bls_modular_inverse x expectedY =
test_bls_div : BlsFieldElement -> BlsFieldElement -> BlsFieldElement -> Bit
property test_bls_div x y expected =
bls_div x y == expected

/**
* Unit tests for `bls_modular_inverse`.
b13decker marked this conversation as resolved.
Show resolved Hide resolved
* ```repl
* :prove test_compute_powers`{2} 13 [13, 13^^2]
* :prove test_compute_powers`{5} 13 [13, 13^^2, 13^^3, 13^^4, 13^^5]
* ```
* NOTE: we just chose these values at "random"
*/
test_compute_powers : {n} (fin n, width n <= 64, n >= 2) => BlsFieldElement -> [n]BlsFieldElement -> Bit
property test_compute_powers x expected =
join (compute_powers`{n} x) == join expected