8000 Fix expacc by plafer · Pull Request #1676 · 0xMiden/miden-vm · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix expacc #1676

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

Merged
merged 5 commits into from
Mar 4, 2025
Merged

Fix expacc #1676

merged 5 commits into from
Mar 4, 2025

Conversation

plafer
Copy link
Collaborator
@plafer plafer commented Feb 28, 2025

Fixes the docs and edge case of the EXPACC instruction. Specifically,

  • docs: the exp and base variables were reversed (in base^exp)
    • this mistake was also everywhere in the code
  • The case where base = 0 would fail with integer underflow

@plafer plafer added bug Something isn't working documentation Improvements or additions to documentation labels Feb 28, 2025
Copy link
Collaborator
@Al-Kindi-0 Al-Kindi-0 left a comment

Choose a reason for hiding this comment

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

Looks good to me, thank you for fixing this!

The `EXPACC` operation pops top $4$ elements from the top of the stack, performs a single round of exponent aggregation, and pushes the resulting $4$ values onto the stack. The diagram below illustrates this graphically.
The `EXPACC` operation computes one round of the expression $base^{exp}$. It is expected that `Expacc` is called at least `num_exp_bits` times, where `num_exp_bits` is the number of bits required to represent `exp`.

It pops top $4$ elements from the top of the stack, performs a single round of exponent aggregation, and pushes the resulting $4$ values onto the stack. The diagram below illustrates this graphically.
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: feels like some redundancy with the word " top" in "It pops top $4$ elements from the top of the stack"

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ah yes, removed the first "top"


![expacc](../../assets/design/stack/field_operations/EXPACC.png)

Expacc is based on the observation that the exponentiation of a number can be computed by repeatedly squaring the base and multiplying the accumulator by the base when the least significant bit of the exponent is 1.
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: the usage of base to signify the original base and the accumulated base holding the intermediate results is a bit confusing

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good point. Renamed base -> base_acc, and acc -> result_acc (to disambiguate with the new base_acc).

Even with this change though, I find the English version of the algorithm written here still hard to read, so hopefully the example is enough to make things more clear.


![expacc](../../assets/design/stack/field_operations/EXPACC.png)

Expacc is based on the observation that the exponentiation of a number can be computed by repeatedly squaring the base and multiplying the accumulator by the base when the least significant bit of the exponent is 1.

For example, take $b^5 = (b^2)^2 \cdot b$. Over the course of 3 iterations ($5$ is $101$ in binary), the algorithm will compute $b$, $b^2$ and $b^4$ (placed in `base`). Hence, we want to multiply `base` in `acc` when $base = b$ and when $base = b^4$, which occurs on the first and third iterations (corresponding to the $1$ bits in the binary representation of 5).
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: same as the previous remark with respect to base

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

fixed as above (and in the implementation of op_expacc)

8000
@@ -208,7 +214,7 @@ The `acc` in the next frame is the product of `val` and `acc` in the current fra
s_2' - s_2 * h_0 = 0 \text{ | degree} = 2
$$

Copy link
Collaborator

Choose a reason for hiding this comment

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

some of the variable names in the remaining constraints should be updated as well

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I left those as-is, since this is the convention that the rest of the file adopts (i.e. use "real names" in the diagram, but then use "stack indices" in the constraints description).

@plafer plafer force-pushed the plafer-fix-expacc branch from 91e8a02 to fcbd022 Compare March 3, 2025 16:04
Copy link
Contributor
@bobbinth bobbinth left a comment

Choose a reason for hiding this comment

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

Looks good! Thank you for fixing this!

@bobbinth bobbinth merged commit 0db3e00 into next Mar 4, 2025
9 checks passed
@bobbinth bobbinth deleted the plafer-fix-expacc branch March 4, 2025 01:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0