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

Introduce PhantomData for SignedExtendConfig #518

Closed
wants to merge 1 commit into from
Closed
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
24 changes: 15 additions & 9 deletions ceno_zkvm/src/gadgets/signed_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,33 @@ use crate::{
witness::LkMultiplicity,
};
use ff_ext::ExtensionField;
use std::mem::MaybeUninit;
use std::{marker::PhantomData, mem::MaybeUninit};

pub struct SignedExtendConfig {
pub struct SignedExtendConfig<E> {
/// most significant bit
msb: WitIn,
/// number of bits contained in the value
n_bits: usize,

_marker: PhantomData<E>,
}

impl SignedExtendConfig {
pub fn construct_limb<E: ExtensionField>(
impl<E: ExtensionField> SignedExtendConfig<E> {
pub fn construct_limb(
cb: &mut CircuitBuilder<E>,
val: Expression<E>,
) -> Result<Self, ZKVMError> {
Self::construct_circuit(cb, 16, val)
}

pub fn construct_byte<E: ExtensionField>(
pub fn construct_byte(
cb: &mut CircuitBuilder<E>,
val: Expression<E>,
) -> Result<Self, ZKVMError> {
Self::construct_circuit(cb, 8, val)
}

fn construct_circuit<E: ExtensionField>(
fn construct_circuit(
cb: &mut CircuitBuilder<E>,
n_bits: usize,
val: Expression<E>, // it's assumed that val is within [0, 2^N_BITS)
Expand All @@ -56,11 +58,15 @@ impl SignedExtendConfig {
2 * val - (msb.expr() << n_bits),
)?;

Ok(SignedExtendConfig { msb, n_bits })
Ok(SignedExtendConfig {
msb,
n_bits,
_marker: PhantomData,
})
}

/// Get the signed extended value
pub fn signed_extended_value<E: ExtensionField>(&self, val: Expression<E>) -> UInt<E> {
pub fn signed_extended_value(&self, val: Expression<E>) -> UInt<E> {
assert_eq!(UInt::<E>::LIMB_BITS, 16);

let limb0 = match self.n_bits {
Expand All @@ -71,7 +77,7 @@ impl SignedExtendConfig {
UInt::from_exprs_unchecked(vec![limb0, self.msb.expr() * 0xffff])
}

pub fn assign_instance<E: ExtensionField>(
pub fn assign_instance(
&self,
instance: &mut [MaybeUninit<E::BaseField>],
lk_multiplicity: &mut LkMultiplicity,
Expand Down
4 changes: 2 additions & 2 deletions ceno_zkvm/src/instructions/riscv/memory/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub struct LoadConfig<E: ExtensionField> {
memory_read: UInt<E>,
target_limb: Option<WitIn>,
target_limb_bytes: Option<Vec<WitIn>>,
signed_extend_config: Option<SignedExtendConfig>,
signed_extend_config: Option<SignedExtendConfig<E>>,
}

pub struct LoadInstruction<E, I>(PhantomData<(E, I)>);
Expand Down Expand Up @@ -249,7 +249,7 @@ impl<E: ExtensionField, I: RIVInstruction> Instruction<E> for LoadInstruction<E,
_ => 0,
};
if let Some(signed_ext_config) = config.signed_extend_config.as_ref() {
signed_ext_config.assign_instance::<E>(instance, lk_multiplicity, val)?;
signed_ext_config.assign_instance(instance, lk_multiplicity, val)?;
}

Ok(())
Expand Down
Loading