Browse Source

Fix integer conversions and log2

master
Pratyush Mishra 4 years ago
parent
commit
40ce981801
4 changed files with 14 additions and 17 deletions
  1. +1
    -7
      crypto-primitives/src/merkle_tree/mod.rs
  2. +1
    -1
      r1cs-std/src/bits/boolean.rs
  3. +4
    -1
      r1cs-std/src/bits/uint.rs
  4. +8
    -8
      r1cs-std/src/fields/fp/mod.rs

+ 1
- 7
crypto-primitives/src/merkle_tree/mod.rs

@ -258,12 +258,6 @@ impl core::fmt::Display for Error {
impl algebra_core::Error for Error {} 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. /// Returns the height of the tree, given the size of the tree.
#[inline] #[inline]
fn tree_height(tree_size: usize) -> usize { fn tree_height(tree_size: usize) -> usize {
@ -271,7 +265,7 @@ fn tree_height(tree_size: usize) -> usize {
return 1; return 1;
} }
log2(tree_size)
algebra_core::log2(tree_size) as usize
} }
/// Returns true iff the index represents the root. /// Returns true iff the index represents the root.

+ 1
- 1
r1cs-std/src/bits/boolean.rs

@ -591,7 +591,7 @@ impl ToBytesGadget for Boolean {
fn to_bytes(&self) -> Result<Vec<UInt8<F>>, SynthesisError> { fn to_bytes(&self) -> Result<Vec<UInt8<F>>, SynthesisError> {
let mut bits = vec![self.clone()]; let mut bits = vec![self.clone()];
bits.extend(vec![Boolean::constant(false); 7]); 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 }; let byte = UInt8 { bits, value };
Ok(vec![byte]) Ok(vec![byte])
} }

+ 4
- 1
r1cs-std/src/bits/uint.rs

@ -3,6 +3,7 @@ macro_rules! make_uint {
pub mod $mod_name { pub mod $mod_name {
use algebra::{Field, FpParameters, PrimeField}; use algebra::{Field, FpParameters, PrimeField};
use core::borrow::Borrow; use core::borrow::Borrow;
use core::convert::TryFrom;
use r1cs_core::{ use r1cs_core::{
lc, ConstraintSystemRef, LinearCombination, Namespace, SynthesisError, Variable, lc, ConstraintSystemRef, LinearCombination, Namespace, SynthesisError, Variable,
@ -119,7 +120,9 @@ macro_rules! make_uint {
$name { $name {
bits: new_bits, 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())),
} }
} }

+ 8
- 8
r1cs-std/src/fields/fp/mod.rs

@ -476,8 +476,8 @@ impl TwoBitLookupGadget for AllocatedFp {
debug_assert_eq!(c.len(), 4); debug_assert_eq!(c.len(), 4);
if let Some(cs) = b.cs() { if let Some(cs) = b.cs() {
let result = Self::new_witness(cs.clone(), || { 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 index = lsb + (msb << 1);
Ok(c[index]) Ok(c[index])
})?; })?;
@ -509,8 +509,8 @@ impl ThreeBitCondNegLookupGadget for AllocatedFp {
if let Some(cs) = b.cs() { if let Some(cs) = b.cs() {
let result = Self::new_witness(cs.clone(), || { 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 index = lsb + (msb << 1);
let intermediate = c[index]; let intermediate = c[index];
@ -893,8 +893,8 @@ impl TwoBitLookupGadget for FpVar {
if b.cs().is_some() { if b.cs().is_some() {
AllocatedFp::two_bit_lookup(b, c).map(Self::Var) AllocatedFp::two_bit_lookup(b, c).map(Self::Var)
} else { } 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 index = lsb + (msb << 1);
Ok(Self::Constant(c[index])) Ok(Self::Constant(c[index]))
} }
@ -916,8 +916,8 @@ impl ThreeBitCondNegLookupGadget for FpVar {
if b.cs().or(b0b1.cs()).is_some() { if b.cs().or(b0b1.cs()).is_some() {
AllocatedFp::three_bit_cond_neg_lookup(b, b0b1, c).map(Self::Var) AllocatedFp::three_bit_cond_neg_lookup(b, b0b1, c).map(Self::Var)
} else { } 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 index = lsb + (msb << 1);
let intermediate = c[index]; let intermediate = c[index];

Loading…
Cancel
Save