From ed6797b32992bb7e5cdaa25118e4fcc6d746ceef Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Wed, 11 Dec 2024 09:39:24 +0800 Subject: [PATCH 1/5] Introduce `Value::as_i32` To help make https://github.com/scroll-tech/ceno/pull/596 easier to read and reason about. --- ceno_zkvm/src/instructions/riscv/mul.rs | 4 +--- ceno_zkvm/src/uint.rs | 5 +++++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/ceno_zkvm/src/instructions/riscv/mul.rs b/ceno_zkvm/src/instructions/riscv/mul.rs index 58b410960..c588feadf 100644 --- a/ceno_zkvm/src/instructions/riscv/mul.rs +++ b/ceno_zkvm/src/instructions/riscv/mul.rs @@ -432,9 +432,7 @@ impl Signed { lkm, *val.as_u16_limbs().last().unwrap() as u64, )?; - let signed_val = val.as_u32() as i32; - - Ok(signed_val) + Ok(val.as_i32()) } pub fn expr(&self) -> Expression { diff --git a/ceno_zkvm/src/uint.rs b/ceno_zkvm/src/uint.rs index 193d34f13..f8e872a2e 100644 --- a/ceno_zkvm/src/uint.rs +++ b/ceno_zkvm/src/uint.rs @@ -684,6 +684,11 @@ impl<'a, T: Into + From + Copy + Default> Value<'a, T> { self.as_u64() as u32 } + /// Convert the limbs to a u32 value + pub fn as_i32(&self) -> i32 { + self.as_u32() as i32 + } + pub fn u16_fields(&self) -> Vec { self.limbs.iter().map(|v| F::from(*v as u64)).collect_vec() } From 856095e04372fb7186ddc358c991ffcd1f2069bd Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Thu, 12 Dec 2024 09:26:15 +0800 Subject: [PATCH 2/5] Fix doc comment --- ceno_zkvm/src/uint.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ceno_zkvm/src/uint.rs b/ceno_zkvm/src/uint.rs index f8e872a2e..5932eb078 100644 --- a/ceno_zkvm/src/uint.rs +++ b/ceno_zkvm/src/uint.rs @@ -684,7 +684,7 @@ impl<'a, T: Into + From + Copy + Default> Value<'a, T> { self.as_u64() as u32 } - /// Convert the limbs to a u32 value + /// Convert the limbs to an i32 value pub fn as_i32(&self) -> i32 { self.as_u32() as i32 } From 76a3cb711ef8b43d66dbd9daa71ac2c65a0a756f Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Thu, 12 Dec 2024 09:40:16 +0800 Subject: [PATCH 3/5] Fix and generialise --- ceno_zkvm/src/uint.rs | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/ceno_zkvm/src/uint.rs b/ceno_zkvm/src/uint.rs index 5932eb078..3c3cd7998 100644 --- a/ceno_zkvm/src/uint.rs +++ b/ceno_zkvm/src/uint.rs @@ -606,6 +606,24 @@ pub struct Value<'a, T: Into + From + Copy + Default> { pub limbs: Cow<'a, [u16]>, } +impl<'a, T: Into + From + Copy + Default> From<&'a Value<'a, T>> for &'a [u16] { + fn from(v: &'a Value<'a, T>) -> Self { + v.as_u16_limbs() + } +} + +impl<'a, T: Into + From + Copy + Default> From<&Value<'a, T>> for u64 { + fn from(v: &Value<'a, T>) -> Self { + v.as_u64() + } +} + +impl<'a, T: Into + From + Copy + Default> From<&Value<'a, T>> for u32 { + fn from(v: &Value<'a, T>) -> Self { + v.as_u32() + } +} + // TODO generalize to support non 16 bit limbs // TODO optimize api with fixed size array impl<'a, T: Into + From + Copy + Default> Value<'a, T> { @@ -616,10 +634,7 @@ impl<'a, T: Into + From + Copy + Default> Value<'a, T> { const LIMBS: usize = (Self::M + 15) / 16; pub fn new(val: T, lkm: &mut LkMultiplicity) -> Self { - let uint = Value:: { - val, - limbs: Cow::Owned(Self::split_to_u16(val)), - }; + let uint = Self::new_unchecked(val); Self::assert_u16(&uint.limbs, lkm); uint } From 8ea7d5c6c0f74f9c6255f1ac44c17086fffa560b Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Thu, 12 Dec 2024 09:42:38 +0800 Subject: [PATCH 4/5] Fix --- ceno_zkvm/src/uint.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/ceno_zkvm/src/uint.rs b/ceno_zkvm/src/uint.rs index 3c3cd7998..5a639a055 100644 --- a/ceno_zkvm/src/uint.rs +++ b/ceno_zkvm/src/uint.rs @@ -624,6 +624,12 @@ impl<'a, T: Into + From + Copy + Default> From<&Value<'a, T>> for u32 } } +impl<'a, T: Into + From + Copy + Default> From<&Value<'a, T>> for i32 { + fn from(v: &Value<'a, T>) -> Self { + v.as_i32() + } +} + // TODO generalize to support non 16 bit limbs // TODO optimize api with fixed size array impl<'a, T: Into + From + Copy + Default> Value<'a, T> { From 887f90618a78c7da53fea464c05e661833000423 Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Thu, 12 Dec 2024 09:43:22 +0800 Subject: [PATCH 5/5] Use --- ceno_zkvm/src/instructions/riscv/mul.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ceno_zkvm/src/instructions/riscv/mul.rs b/ceno_zkvm/src/instructions/riscv/mul.rs index c588feadf..16a08fe41 100644 --- a/ceno_zkvm/src/instructions/riscv/mul.rs +++ b/ceno_zkvm/src/instructions/riscv/mul.rs @@ -432,7 +432,7 @@ impl Signed { lkm, *val.as_u16_limbs().last().unwrap() as u64, )?; - Ok(val.as_i32()) + Ok(i32::from(val)) } pub fn expr(&self) -> Expression {