mirror of
https://github.com/arnaucube/ark-r1cs-std.git
synced 2026-01-10 16:01:28 +01:00
Fix integer conversions and log2
This commit is contained in:
@@ -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.
|
||||||
|
|||||||
@@ -591,7 +591,7 @@ impl<F: Field> ToBytesGadget<F> for Boolean<F> {
|
|||||||
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])
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -476,8 +476,8 @@ impl<F: PrimeField> TwoBitLookupGadget<F> for AllocatedFp<F> {
|
|||||||
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 lsb = usize::from(b[0].value()?);
|
||||||
let msb = b[1].value()? as usize;
|
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<F: PrimeField> ThreeBitCondNegLookupGadget<F> for AllocatedFp<F> {
|
|||||||
|
|
||||||
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 lsb = usize::from(b[0].value()?);
|
||||||
let msb = b[1].value()? as usize;
|
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<F: PrimeField> TwoBitLookupGadget<F> for FpVar<F> {
|
|||||||
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 lsb = usize::from(b[0].value()?);
|
||||||
let msb = b[1].value()? as usize;
|
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<F: PrimeField> ThreeBitCondNegLookupGadget<F> for FpVar<F> {
|
|||||||
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 lsb = usize::from(b[0].value()?);
|
||||||
let msb = b[1].value()? as usize;
|
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];
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user