Fix integer conversions and log2

This commit is contained in:
Pratyush Mishra
2020-09-11 23:28:00 -07:00
parent 5e00793999
commit 40ce981801
4 changed files with 14 additions and 17 deletions

View File

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

View File

@@ -591,7 +591,7 @@ impl<F: Field> ToBytesGadget<F> for Boolean<F> {
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])
}

View File

@@ -3,6 +3,7 @@ macro_rules! make_uint {
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 @@ macro_rules! make_uint {
$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())),
}
}

View File

@@ -476,8 +476,8 @@ impl<F: PrimeField> TwoBitLookupGadget<F> for AllocatedFp<F> {
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 @@ impl<F: PrimeField> ThreeBitCondNegLookupGadget<F> for AllocatedFp<F> {
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 @@ impl<F: PrimeField> TwoBitLookupGadget<F> for FpVar<F> {
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 @@ impl<F: PrimeField> ThreeBitCondNegLookupGadget<F> for FpVar<F> {
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];