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

dev: leverage non-determinism in extract_limb_bits #953

Open
enitrat opened this issue Mar 7, 2025 · 0 comments
Open

dev: leverage non-determinism in extract_limb_bits #953

enitrat opened this issue Mar 7, 2025 · 0 comments
Assignees
Milestone

Comments

@enitrat
Copy link
Collaborator

enitrat commented Mar 7, 2025

Move extract_limb_bits to maths.cairo in cairo_core. Rename it to felt252_to_bits.

Using a hint, we can ask the prover to directly provide the bit decomposition of limb as an array of bits. The Cairo code then only needs to:

-Verify that the provided bit array is valid (i.e., contains only 0s and 1s).
-Reconstruct the original limb from the bit array and ensure it matches the input.

This approach eliminates the recursive calls and reduces the operation to a single pass for verification, significantly lowering the number of steps.

How It Works

Non-Deterministic Hint:
Inside the %{%} block, the prover computes the binary representation of limb and writes the bits directly into bits_ptr. It also sets len to the number of bits.
This is done efficiently in Python & Rust.

Verification:
The Cairo code:

  • iterates over the provided bits, verifying each is either 0 or 1 using range checks.
  • reconstructs the original limb by computing sum(bit[i] * 2^i) and checks that it equals the input limb.

The optimized version is still an O(n) verification steps (iteration on all bits), but these are simpler operations (multiplications and additions) and avoid recursion and heavy function calls. The heavy lifting is offloaded to the prover.

The logic is very similar to felt252_to_bytes_be

@github-project-automation github-project-automation bot moved this to Backlog in Keth Mar 7, 2025
@Eikix Eikix added this to the Optimization milestone Mar 10, 2025
@enitrat enitrat moved this from Backlog to Todo in Keth Mar 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Todo
Development

No branches or pull requests

3 participants