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

ZIR-194: Fix and test BigInt type inference #45

Merged
merged 40 commits into from
Oct 21, 2024
Merged

Conversation

tzerrell
Copy link
Member

This fixes a number of problems in the type inference for BigInts in calculating the number of coefficients, their maximum positive & negative values, and their minimum bits. It also adds lit tests verifying these properties. For the lit tests, I manually computed the output types without looking at the algorithms to give some redundancy.

Also adds a verifier to the BigInt type enforcing:

  • No overflow (i.e. maxPos + maxNeg < BabyBear)
  • BigInts with a minimum value must be positive (i.e., at least one of maxNeg and minBits must be zero)

Makes the design decision that minBits should only be set if we know the number is positive, i.e. so that it is at least 2^minBits. Negative numbers don't count, even if we know they are large in absolute value.

I also stuck a rename into this PR since this forces a rebuild of all the BigInt files anyway -- the existing nondet_invmod vs inv vs ModularInvOp vs NondetInvModOp was confusing even me, and since we're in integers there's no real need to specify it's a modular inverse: here's no other sensible multiplicative inverse. The new names are nondet_inv (nondet) and inv (checked) (and corresponding variants, e.g. NondetInvOp and InvOp).

This could also be divided up as 3 PRs (one for BigInt type inference, one for the BigInt type verifier, and one for the renames of the inversion operations). I thought in this case it made more sense to unify them (in particular because that lets me make a single companion PR on the risc0 repo), but if we want to split them it would be straightforward as the commits are fairly clearly separated.

@tzerrell tzerrell requested review from shkoo and pdg744 October 10, 2024 23:00
@tzerrell tzerrell self-assigned this Oct 10, 2024
@github-actions github-actions bot changed the title Fix and test BigInt type inference ZIR-194: Fix and test BigInt type inference Oct 10, 2024
Copy link

@pdg744 pdg744 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 walking me through the details!
Might want to add a couple notes to help clarify:

  • where to look to cross-reference the test logic & the algorithm that encodes that logic
  • the subtleties around normalization we discussed

Copy link
Contributor

@jacobdweightman jacobdweightman left a comment

Choose a reason for hiding this comment

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

Awesome work. I love the new tests for the dialect, they're basically executable documentation! If you haven't already, it would probably be good to create an issue to track the Circom tests you turned off 😄

@tzerrell
Copy link
Member Author

I'm going to wait for risc0/risc0#2413 to be approved before landing this, as otherwise we won't be able to bootstrap bigint into risc0 while this is landed and risc0/risc0#2413 is outstanding.

@tzerrell
Copy link
Member Author

There's an unsigned commit snuck in the middle, so I'm rebasing to get everything signed.

tzerrell added a commit to risc0/risc0 that referenced this pull request Oct 21, 2024
Make a number of fixes to BigInt dialect type inference. The bulk of the
work is in the corresponding zirgen PR: risc0/zirgen#45.
@tzerrell tzerrell merged commit ee85492 into main Oct 21, 2024
8 checks passed
@tzerrell tzerrell deleted the tzerrell/bigint-checks branch October 21, 2024 21:12
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.

3 participants