Fix integer conversions and log2
Pratyush Mishra pratyushmishra@berkeley.edu
Fri, 11 Sep 2020 23:28:00 -0700
4 files changed,
14 insertions(+),
17 deletions(-)
M
crypto-primitives/src/merkle_tree/mod.rs
→
crypto-primitives/src/merkle_tree/mod.rs
@@ -258,12 +258,6 @@ }
impl algebra_core::Error for Error {} -/// Returns the log2 value of the given number. -#[inline] -fn log2(number: usize) -> usize { - algebra_core::log2(number) as usize -} - /// Returns the height of the tree, given the size of the tree. #[inline] fn tree_height(tree_size: usize) -> usize {@@ -271,7 +265,7 @@ if tree_size == 1 {
return 1; } - log2(tree_size) + algebra_core::log2(tree_size) as usize } /// Returns true iff the index represents the root.
M
r1cs-std/src/bits/boolean.rs
→
r1cs-std/src/bits/boolean.rs
@@ -591,7 +591,7 @@ #[tracing::instrument(target = "r1cs")]
fn to_bytes(&self) -> Result<Vec<UInt8<F>>, SynthesisError> { let mut bits = vec![self.clone()]; bits.extend(vec![Boolean::constant(false); 7]); - let value = self.value().map(|val| val as u8).ok(); + let value = self.value().map(u8::from).ok(); let byte = UInt8 { bits, value }; Ok(vec![byte]) }
M
r1cs-std/src/bits/uint.rs
→
r1cs-std/src/bits/uint.rs
@@ -3,6 +3,7 @@ ($name:ident, $size:expr, $native:ident, $mod_name:ident) => {
pub mod $mod_name { use algebra::{Field, FpParameters, PrimeField}; use core::borrow::Borrow; + use core::convert::TryFrom; use r1cs_core::{ lc, ConstraintSystemRef, LinearCombination, Namespace, SynthesisError, Variable,@@ -119,7 +120,9 @@ .collect();
$name { bits: new_bits, - value: self.value.map(|v| v.rotate_right(by as u32)), + value: self + .value + .map(|v| v.rotate_right(u32::try_from(by).unwrap())), } }
M
r1cs-std/src/fields/fp/mod.rs
→
r1cs-std/src/fields/fp/mod.rs
@@ -476,8 +476,8 @@ debug_assert_eq!(b.len(), 2);
debug_assert_eq!(c.len(), 4); if let Some(cs) = b.cs() { let result = Self::new_witness(cs.clone(), || { - let lsb = b[0].value()? as usize; - let msb = b[1].value()? as usize; + let lsb = usize::from(b[0].value()?); + let msb = usize::from(b[1].value()?); let index = lsb + (msb << 1); Ok(c[index]) })?;@@ -509,8 +509,8 @@ debug_assert_eq!(c.len(), 4);
if let Some(cs) = b.cs() { let result = Self::new_witness(cs.clone(), || { - let lsb = b[0].value()? as usize; - let msb = b[1].value()? as usize; + let lsb = usize::from(b[0].value()?); + let msb = usize::from(b[1].value()?); let index = lsb + (msb << 1); let intermediate = c[index];@@ -893,8 +893,8 @@ debug_assert_eq!(c.len(), 4);
if b.cs().is_some() { AllocatedFp::two_bit_lookup(b, c).map(Self::Var) } else { - let lsb = b[0].value()? as usize; - let msb = b[1].value()? as usize; + let lsb = usize::from(b[0].value()?); + let msb = usize::from(b[1].value()?); let index = lsb + (msb << 1); Ok(Self::Constant(c[index])) }@@ -916,8 +916,8 @@
if b.cs().or(b0b1.cs()).is_some() { AllocatedFp::three_bit_cond_neg_lookup(b, b0b1, c).map(Self::Var) } else { - let lsb = b[0].value()? as usize; - let msb = b[1].value()? as usize; + let lsb = usize::from(b[0].value()?); + let msb = usize::from(b[1].value()?); let index = lsb + (msb << 1); let intermediate = c[index];