-
Notifications
You must be signed in to change notification settings - Fork 12
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
Feat/Opcode Circuits for DIV, REM, and REMU opcodes #596
base: master
Are you sure you want to change the base?
Changes from 10 commits
4fb46a4
2812c12
6919ece
754a75d
b9be2fc
22bc22a
4c11b43
277fd77
36c2af4
3e7931b
7821de4
23e6421
eb0a117
92d44d5
4425c13
0158e01
54bf10c
92bce79
8317a28
0c3f5ed
41e3e19
137964a
0f71c30
124d623
833dbca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
mod div; | ||
mod is_lt; | ||
mod is_zero; | ||
mod signed; | ||
mod signed_ext; | ||
|
||
pub use div::DivConfig; | ||
pub use is_lt::{ | ||
AssertLTConfig, AssertSignedLtConfig, InnerLtConfig, IsLtConfig, SignedLtConfig, cal_lt_diff, | ||
}; | ||
pub use is_zero::{IsEqualConfig, IsZeroConfig}; | ||
pub use signed::Signed; | ||
pub use signed_ext::SignedExtendConfig; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use std::{fmt::Display, mem::MaybeUninit}; | ||
|
||
use ff_ext::ExtensionField; | ||
|
||
use crate::{ | ||
Value, | ||
circuit_builder::CircuitBuilder, | ||
error::ZKVMError, | ||
expression::Expression, | ||
instructions::riscv::constants::{BIT_WIDTH, UInt}, | ||
witness::LkMultiplicity, | ||
}; | ||
|
||
use super::SignedExtendConfig; | ||
|
||
/// Interprets a `UInt` value as a 2s-complement signed value. | ||
/// | ||
/// Uses 1 `WitIn` to represent the MSB of the value. | ||
bgillesp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pub struct Signed<E: ExtensionField> { | ||
pub is_negative: SignedExtendConfig<E>, | ||
val: Expression<E>, | ||
} | ||
|
||
impl<E: ExtensionField> Signed<E> { | ||
pub fn construct_circuit<NR: Into<String> + Display + Clone, N: FnOnce() -> NR>( | ||
cb: &mut CircuitBuilder<E>, | ||
name_fn: N, | ||
unsigned_val: &UInt<E>, | ||
) -> Result<Self, ZKVMError> { | ||
cb.namespace(name_fn, |cb| { | ||
let is_negative = unsigned_val.is_negative(cb)?; | ||
let val = unsigned_val.value() - (1u64 << BIT_WIDTH) * is_negative.expr(); | ||
bgillesp marked this conversation as resolved.
Show resolved
Hide resolved
bgillesp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Ok(Self { is_negative, val }) | ||
}) | ||
} | ||
|
||
pub fn assign_instance( | ||
&self, | ||
instance: &mut [MaybeUninit<E::BaseField>], | ||
lkm: &mut LkMultiplicity, | ||
val: &Value<u32>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will it be better to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this might be a nontrivial change to implement -- the |
||
) -> Result<i32, ZKVMError> { | ||
self.is_negative.assign_instance( | ||
instance, | ||
lkm, | ||
*val.as_u16_limbs().last().unwrap() as u64, | ||
)?; | ||
let signed_val = val.as_u32() as i32; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See #732 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just reviewed #732, will make the change here once it's merged into master. |
||
|
||
Ok(signed_val) | ||
} | ||
|
||
pub fn expr(&self) -> Expression<E> { | ||
self.val.clone() | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #731