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

Simple diff for 'Feat/Opcode Circuits for DIV, REM, and REMU opcodes' #734

Draft
wants to merge 17 commits into
base: matthias/bg/div-rem-remu-opcodes-base
Choose a base branch
from

Conversation

matthiasgoergens
Copy link
Collaborator

This is just a simpler view of the diff for https://github.com/scroll-tech/ceno/pull/596/files

Copy link
Collaborator Author

@matthiasgoergens matthiasgoergens left a comment

Choose a reason for hiding this comment

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

Just a few comments. It's too long to review in one go.

ceno_zkvm/src/instructions/riscv/div.rs Show resolved Hide resolved
ceno_zkvm/src/instructions/riscv/div.rs Show resolved Hide resolved
ceno_zkvm/src/instructions/riscv/div.rs Show resolved Hide resolved
ceno_zkvm/src/instructions/riscv/div.rs Show resolved Hide resolved
ceno_zkvm/src/instructions/riscv/div.rs Show resolved Hide resolved
ceno_zkvm/src/instructions/riscv/div.rs Show resolved Hide resolved
ceno_zkvm/src/instructions/riscv/div.rs Show resolved Hide resolved
dividend_signed.assign_instance(instance, lkm, &dividend_v)?;
divisor_signed.assign_instance(instance, lkm, &divisor_v)?;

let negative_division_b = (dividend_s < 0) ^ (divisor_s < 0);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

What does the _b suffix stand for?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Meant as bool or bit as you prefer -- should I revise the names or add a comment to make this more clear?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Using a _bit suffix would be my preferred choice here, as it is still short but leads to less head scratching than just a b.

Thanks.

quotient_signed.assign_instance(instance, lkm, &quotient_v)?;
remainder_signed.assign_instance(instance, lkm, &remainder_v)?;

let remainder_pos_orientation = if negative_division_b {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

diff --git a/ceno_zkvm/src/instructions/riscv/div.rs b/ceno_zkvm/src/instructions/riscv/div.rs
index 4ed86da4..4d141149 100644
--- a/ceno_zkvm/src/instructions/riscv/div.rs
+++ b/ceno_zkvm/src/instructions/riscv/div.rs
@@ -436,16 +436,10 @@ impl<E: ExtensionField, I: RIVInstruction> Instruction<E> for ArithInstruction<E
                 quotient_signed.assign_instance(instance, lkm, &quotient_v)?;
                 remainder_signed.assign_instance(instance, lkm, &remainder_v)?;
 
-                let remainder_pos_orientation = if negative_division_b {
-                    -(remainder_s as i64)
-                } else {
-                    remainder_s as i64
-                };
-                let divisor_pos_orientation = if divisor_s < 0 {
-                    -(divisor_s as i64)
-                } else {
-                    divisor_s as i64
-                };
+                let neg_if = |b: bool, x: i32| if b { -x } else { x };
+
+                let remainder_pos_orientation = neg_if(negative_division_b, remainder_s) as i64;
+                let divisor_pos_orientation = neg_if(divisor_s < 0, divisor_s) as i64;
 
                 remainder_nonnegative.assign_instance(
                     instance,

This is a bit more compact.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sure, we can write it this way -- note that it's important here that the cast to i64 happens before negation, as one of the issues we run into is the edge case of negating the signed value -2^31.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks for thinking about the special case!

I was more thinking about replacing the twice repeated relatively long logic with something shorter to read:

Rust makes it easy to define small helper functions, so we might as well use them.

ceno_zkvm/src/instructions/riscv/div.rs Show resolved Hide resolved
Copy link
Collaborator Author

@matthiasgoergens matthiasgoergens left a comment

Choose a reason for hiding this comment

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

OK, wasn't much left below.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

All good in this file.

Copy link
Collaborator Author

@matthiasgoergens matthiasgoergens left a comment

Choose a reason for hiding this comment

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

See matthias/bg/div-rem-remu-opcodes-suggestions for more suggestions. (Though most of them are already in the review, too.)

@bgillesp
Copy link
Collaborator

Thanks Matthias! Gave a look in matthias/bg/div-rem-remu-opcodes-suggestions and didn't find any other changes that haven't been addressed already by various comments. Let me know if you see anything else! 🙏

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