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

[WIP] Feat/Opcode Circuits for DIV, REM, and REMU opcodes #596

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ceno_zkvm/src/gadgets/is_lt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::{

use super::SignedExtendConfig;

// TODO rename to AssertLtConfig (LT -> Lt) to fit naming conventions
#[derive(Debug, Clone)]
pub struct AssertLTConfig(InnerLtConfig);

Expand Down
2 changes: 2 additions & 0 deletions ceno_zkvm/src/gadgets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ mod div;
mod is_lt;
mod is_zero;
mod signed_ext;
mod signed;

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_ext::SignedExtendConfig;
pub use signed::Signed;
57 changes: 57 additions & 0 deletions ceno_zkvm/src/gadgets/signed.rs
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.
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();

Ok(Self { is_negative, val })
})
}

pub fn assign_instance(
&self,
instance: &mut [MaybeUninit<E::BaseField>],
lkm: &mut LkMultiplicity,
val: &Value<u32>,
) -> 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;

Ok(signed_val)
}

pub fn expr(&self) -> Expression<E> {
self.val.clone()
}
}
9 changes: 7 additions & 2 deletions ceno_zkvm/src/gadgets/signed_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@ use crate::{
use ff_ext::ExtensionField;
use std::{marker::PhantomData, mem::MaybeUninit};

/// Extract the most significant bit from an expression previously constrained
/// to an 8- or 16-bit length.
///
/// Uses 1 `WitIn` value to store the bit, one `assert_bit` constraint, and one
/// `u8` or `u16` table lookup.
#[derive(Debug)]
pub struct SignedExtendConfig<E> {
/// most significant bit
/// Most significant bit
msb: WitIn,
/// number of bits contained in the value
/// Number of bits of the represented value
n_bits: usize,

_marker: PhantomData<E>,
Expand Down
Loading