mirror of
https://github.com/arnaucube/ark-r1cs-std.git
synced 2026-01-23 12:13:48 +01:00
Adds Bowe-Hopwood hash
This commit is contained in:
committed by
Pratyush Mishra
parent
19856bd9b6
commit
581f3df55f
@@ -1,7 +1,6 @@
|
||||
use algebra::{BitIterator, Field, FpParameters, PrimeField};
|
||||
|
||||
use crate::prelude::*;
|
||||
use crate::Assignment;
|
||||
use crate::{prelude::*, Assignment};
|
||||
use r1cs_core::{ConstraintSystem, LinearCombination, SynthesisError, Variable};
|
||||
use std::borrow::Borrow;
|
||||
|
||||
@@ -346,7 +345,11 @@ impl Boolean {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn lc<ConstraintF: Field>(&self, one: Variable, coeff: ConstraintF) -> LinearCombination<ConstraintF> {
|
||||
pub fn lc<ConstraintF: Field>(
|
||||
&self,
|
||||
one: Variable,
|
||||
coeff: ConstraintF,
|
||||
) -> LinearCombination<ConstraintF> {
|
||||
match *self {
|
||||
Boolean::Constant(c) => {
|
||||
if c {
|
||||
@@ -396,7 +399,11 @@ impl Boolean {
|
||||
}
|
||||
|
||||
/// Perform XOR over two boolean operands
|
||||
pub fn xor<'a, ConstraintF, CS>(cs: CS, a: &'a Self, b: &'a Self) -> Result<Self, SynthesisError>
|
||||
pub fn xor<'a, ConstraintF, CS>(
|
||||
cs: CS,
|
||||
a: &'a Self,
|
||||
b: &'a Self,
|
||||
) -> Result<Self, SynthesisError>
|
||||
where
|
||||
ConstraintF: Field,
|
||||
CS: ConstraintSystem<ConstraintF>,
|
||||
@@ -441,7 +448,11 @@ impl Boolean {
|
||||
}
|
||||
|
||||
/// Perform AND over two boolean operands
|
||||
pub fn and<'a, ConstraintF, CS>(cs: CS, a: &'a Self, b: &'a Self) -> Result<Self, SynthesisError>
|
||||
pub fn and<'a, ConstraintF, CS>(
|
||||
cs: CS,
|
||||
a: &'a Self,
|
||||
b: &'a Self,
|
||||
) -> Result<Self, SynthesisError>
|
||||
where
|
||||
ConstraintF: Field,
|
||||
CS: ConstraintSystem<ConstraintF>,
|
||||
@@ -629,7 +640,10 @@ impl From<AllocatedBit> for Boolean {
|
||||
}
|
||||
|
||||
impl<ConstraintF: Field> AllocGadget<bool, ConstraintF> for Boolean {
|
||||
fn alloc<F, T, CS: ConstraintSystem<ConstraintF>>(cs: CS, value_gen: F) -> Result<Self, SynthesisError>
|
||||
fn alloc<F, T, CS: ConstraintSystem<ConstraintF>>(
|
||||
cs: CS,
|
||||
value_gen: F,
|
||||
) -> Result<Self, SynthesisError>
|
||||
where
|
||||
F: FnOnce() -> Result<T, SynthesisError>,
|
||||
T: Borrow<bool>,
|
||||
@@ -713,7 +727,10 @@ impl<ConstraintF: Field> ConditionalEqGadget<ConstraintF> for Boolean {
|
||||
}
|
||||
|
||||
impl<ConstraintF: Field> ToBytesGadget<ConstraintF> for Boolean {
|
||||
fn to_bytes<CS: ConstraintSystem<ConstraintF>>(&self, _cs: CS) -> Result<Vec<UInt8>, SynthesisError> {
|
||||
fn to_bytes<CS: ConstraintSystem<ConstraintF>>(
|
||||
&self,
|
||||
_cs: CS,
|
||||
) -> Result<Vec<UInt8>, SynthesisError> {
|
||||
let mut bits = vec![Boolean::constant(false); 7];
|
||||
bits.push(*self);
|
||||
bits.reverse();
|
||||
@@ -734,15 +751,11 @@ impl<ConstraintF: Field> ToBytesGadget<ConstraintF> for Boolean {
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::{AllocatedBit, Boolean};
|
||||
use crate::{
|
||||
test_constraint_system::TestConstraintSystem,
|
||||
prelude::*
|
||||
};
|
||||
use algebra::{fields::bls12_381::Fr, BitIterator, Field, PrimeField};
|
||||
use algebra::UniformRand;
|
||||
use rand::SeedableRng;
|
||||
use rand_xorshift::XorShiftRng;
|
||||
use crate::{prelude::*, test_constraint_system::TestConstraintSystem};
|
||||
use algebra::{fields::bls12_381::Fr, BitIterator, Field, PrimeField, UniformRand};
|
||||
use r1cs_core::ConstraintSystem;
|
||||
use rand::SeedableRng;
|
||||
use rand_xorshift::XorShiftRng;
|
||||
use std::str::FromStr;
|
||||
|
||||
#[test]
|
||||
@@ -1775,8 +1788,8 @@ use rand_xorshift::XorShiftRng;
|
||||
// let mut bits = vec![];
|
||||
// for (i, b) in BitIterator::new(r).skip(1).enumerate() {
|
||||
// bits.push(Boolean::from(
|
||||
// AllocatedBit::alloc(cs.ns(|| format!("bit_gadget {}", i)),
|
||||
// Some(b)) .unwrap(),
|
||||
// AllocatedBit::alloc(cs.ns(|| format!("bit_gadget {}",
|
||||
// i)), Some(b)) .unwrap(),
|
||||
// ));
|
||||
// }
|
||||
|
||||
|
||||
@@ -7,7 +7,10 @@ pub mod uint32;
|
||||
pub mod uint8;
|
||||
|
||||
pub trait ToBitsGadget<ConstraintF: Field> {
|
||||
fn to_bits<CS: ConstraintSystem<ConstraintF>>(&self, cs: CS) -> Result<Vec<Boolean>, SynthesisError>;
|
||||
fn to_bits<CS: ConstraintSystem<ConstraintF>>(
|
||||
&self,
|
||||
cs: CS,
|
||||
) -> Result<Vec<Boolean>, SynthesisError>;
|
||||
|
||||
/// Additionally checks if the produced list of booleans is 'valid'.
|
||||
fn to_bits_strict<CS: ConstraintSystem<ConstraintF>>(
|
||||
@@ -17,7 +20,10 @@ pub trait ToBitsGadget<ConstraintF: Field> {
|
||||
}
|
||||
|
||||
impl<ConstraintF: Field> ToBitsGadget<ConstraintF> for Boolean {
|
||||
fn to_bits<CS: ConstraintSystem<ConstraintF>>(&self, _: CS) -> Result<Vec<Boolean>, SynthesisError> {
|
||||
fn to_bits<CS: ConstraintSystem<ConstraintF>>(
|
||||
&self,
|
||||
_: CS,
|
||||
) -> Result<Vec<Boolean>, SynthesisError> {
|
||||
Ok(vec![self.clone()])
|
||||
}
|
||||
|
||||
@@ -30,7 +36,10 @@ impl<ConstraintF: Field> ToBitsGadget<ConstraintF> for Boolean {
|
||||
}
|
||||
|
||||
impl<ConstraintF: Field> ToBitsGadget<ConstraintF> for [Boolean] {
|
||||
fn to_bits<CS: ConstraintSystem<ConstraintF>>(&self, _cs: CS) -> Result<Vec<Boolean>, SynthesisError> {
|
||||
fn to_bits<CS: ConstraintSystem<ConstraintF>>(
|
||||
&self,
|
||||
_cs: CS,
|
||||
) -> Result<Vec<Boolean>, SynthesisError> {
|
||||
Ok(self.to_vec())
|
||||
}
|
||||
|
||||
@@ -42,7 +51,10 @@ impl<ConstraintF: Field> ToBitsGadget<ConstraintF> for [Boolean] {
|
||||
}
|
||||
}
|
||||
impl<ConstraintF: Field> ToBitsGadget<ConstraintF> for Vec<Boolean> {
|
||||
fn to_bits<CS: ConstraintSystem<ConstraintF>>(&self, _cs: CS) -> Result<Vec<Boolean>, SynthesisError> {
|
||||
fn to_bits<CS: ConstraintSystem<ConstraintF>>(
|
||||
&self,
|
||||
_cs: CS,
|
||||
) -> Result<Vec<Boolean>, SynthesisError> {
|
||||
Ok(self.clone())
|
||||
}
|
||||
|
||||
@@ -55,7 +67,10 @@ impl<ConstraintF: Field> ToBitsGadget<ConstraintF> for Vec<Boolean> {
|
||||
}
|
||||
|
||||
impl<ConstraintF: Field> ToBitsGadget<ConstraintF> for [UInt8] {
|
||||
fn to_bits<CS: ConstraintSystem<ConstraintF>>(&self, _cs: CS) -> Result<Vec<Boolean>, SynthesisError> {
|
||||
fn to_bits<CS: ConstraintSystem<ConstraintF>>(
|
||||
&self,
|
||||
_cs: CS,
|
||||
) -> Result<Vec<Boolean>, SynthesisError> {
|
||||
let mut result = Vec::with_capacity(&self.len() * 8);
|
||||
for byte in self {
|
||||
result.extend_from_slice(&byte.into_bits_le());
|
||||
@@ -72,7 +87,10 @@ impl<ConstraintF: Field> ToBitsGadget<ConstraintF> for [UInt8] {
|
||||
}
|
||||
|
||||
pub trait ToBytesGadget<ConstraintF: Field> {
|
||||
fn to_bytes<CS: ConstraintSystem<ConstraintF>>(&self, cs: CS) -> Result<Vec<UInt8>, SynthesisError>;
|
||||
fn to_bytes<CS: ConstraintSystem<ConstraintF>>(
|
||||
&self,
|
||||
cs: CS,
|
||||
) -> Result<Vec<UInt8>, SynthesisError>;
|
||||
|
||||
/// Additionally checks if the produced list of booleans is 'valid'.
|
||||
fn to_bytes_strict<CS: ConstraintSystem<ConstraintF>>(
|
||||
@@ -82,7 +100,10 @@ pub trait ToBytesGadget<ConstraintF: Field> {
|
||||
}
|
||||
|
||||
impl<ConstraintF: Field> ToBytesGadget<ConstraintF> for [UInt8] {
|
||||
fn to_bytes<CS: ConstraintSystem<ConstraintF>>(&self, _cs: CS) -> Result<Vec<UInt8>, SynthesisError> {
|
||||
fn to_bytes<CS: ConstraintSystem<ConstraintF>>(
|
||||
&self,
|
||||
_cs: CS,
|
||||
) -> Result<Vec<UInt8>, SynthesisError> {
|
||||
Ok(self.to_vec())
|
||||
}
|
||||
|
||||
@@ -94,8 +115,13 @@ impl<ConstraintF: Field> ToBytesGadget<ConstraintF> for [UInt8] {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, ConstraintF: Field, T: 'a + ToBytesGadget<ConstraintF>> ToBytesGadget<ConstraintF> for &'a T {
|
||||
fn to_bytes<CS: ConstraintSystem<ConstraintF>>(&self, cs: CS) -> Result<Vec<UInt8>, SynthesisError> {
|
||||
impl<'a, ConstraintF: Field, T: 'a + ToBytesGadget<ConstraintF>> ToBytesGadget<ConstraintF>
|
||||
for &'a T
|
||||
{
|
||||
fn to_bytes<CS: ConstraintSystem<ConstraintF>>(
|
||||
&self,
|
||||
cs: CS,
|
||||
) -> Result<Vec<UInt8>, SynthesisError> {
|
||||
(*self).to_bytes(cs)
|
||||
}
|
||||
|
||||
@@ -108,7 +134,10 @@ impl<'a, ConstraintF: Field, T: 'a + ToBytesGadget<ConstraintF>> ToBytesGadget<C
|
||||
}
|
||||
|
||||
impl<'a, ConstraintF: Field> ToBytesGadget<ConstraintF> for &'a [UInt8] {
|
||||
fn to_bytes<CS: ConstraintSystem<ConstraintF>>(&self, _cs: CS) -> Result<Vec<UInt8>, SynthesisError> {
|
||||
fn to_bytes<CS: ConstraintSystem<ConstraintF>>(
|
||||
&self,
|
||||
_cs: CS,
|
||||
) -> Result<Vec<UInt8>, SynthesisError> {
|
||||
Ok(self.to_vec())
|
||||
}
|
||||
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
use algebra::{FpParameters, PrimeField, Field};
|
||||
use algebra::{Field, FpParameters, PrimeField};
|
||||
|
||||
use r1cs_core::{ConstraintSystem, LinearCombination, SynthesisError};
|
||||
|
||||
use crate::boolean::{AllocatedBit, Boolean};
|
||||
use crate::Assignment;
|
||||
use crate::prelude::*;
|
||||
use crate::{
|
||||
boolean::{AllocatedBit, Boolean},
|
||||
prelude::*,
|
||||
Assignment,
|
||||
};
|
||||
|
||||
/// Represents an interpretation of 32 `Boolean` objects as an
|
||||
/// unsigned integer.
|
||||
@@ -270,7 +272,10 @@ impl UInt32 {
|
||||
|
||||
impl<ConstraintF: Field> ToBytesGadget<ConstraintF> for UInt32 {
|
||||
#[inline]
|
||||
fn to_bytes<CS: ConstraintSystem<ConstraintF>>(&self, _cs: CS) -> Result<Vec<UInt8>, SynthesisError> {
|
||||
fn to_bytes<CS: ConstraintSystem<ConstraintF>>(
|
||||
&self,
|
||||
_cs: CS,
|
||||
) -> Result<Vec<UInt8>, SynthesisError> {
|
||||
let value_chunks = match self.value.map(|val| {
|
||||
use algebra::bytes::ToBytes;
|
||||
let mut bytes = [0u8; 4];
|
||||
@@ -340,9 +345,9 @@ mod test {
|
||||
use super::UInt32;
|
||||
use crate::{bits::boolean::Boolean, test_constraint_system::TestConstraintSystem};
|
||||
use algebra::fields::{bls12_381::Fr, Field};
|
||||
use r1cs_core::ConstraintSystem;
|
||||
use rand::{Rng, SeedableRng};
|
||||
use rand_xorshift::XorShiftRng;
|
||||
use r1cs_core::ConstraintSystem;
|
||||
|
||||
#[test]
|
||||
fn test_uint32_from_bits() {
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
use algebra::{ToConstraintField, FpParameters, Field, PrimeField};
|
||||
use algebra::{Field, FpParameters, PrimeField, ToConstraintField};
|
||||
|
||||
use r1cs_core::{ConstraintSystem, SynthesisError};
|
||||
|
||||
use crate::boolean::AllocatedBit;
|
||||
use crate::fields::fp::FpGadget;
|
||||
use crate::prelude::*;
|
||||
use crate::Assignment;
|
||||
use crate::{boolean::AllocatedBit, fields::fp::FpGadget, prelude::*, Assignment};
|
||||
use std::borrow::Borrow;
|
||||
|
||||
/// Represents an interpretation of 8 `Boolean` objects as an
|
||||
@@ -53,7 +50,10 @@ impl UInt8 {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn alloc_vec<ConstraintF, CS, T>(mut cs: CS, values: &[T]) -> Result<Vec<Self>, SynthesisError>
|
||||
pub fn alloc_vec<ConstraintF, CS, T>(
|
||||
mut cs: CS,
|
||||
values: &[T],
|
||||
) -> Result<Vec<Self>, SynthesisError>
|
||||
where
|
||||
ConstraintF: Field,
|
||||
CS: ConstraintSystem<ConstraintF>,
|
||||
@@ -69,15 +69,20 @@ impl UInt8 {
|
||||
}
|
||||
|
||||
/// Allocates a vector of `u8`'s by first converting (chunks of) them to
|
||||
/// `ConstraintF` elements, (thus reducing the number of input allocations), and
|
||||
/// then converts this list of `ConstraintF` gadgets back into bytes.
|
||||
pub fn alloc_input_vec<ConstraintF, CS>(mut cs: CS, values: &[u8]) -> Result<Vec<Self>, SynthesisError>
|
||||
/// `ConstraintF` elements, (thus reducing the number of input allocations),
|
||||
/// and then converts this list of `ConstraintF` gadgets back into
|
||||
/// bytes.
|
||||
pub fn alloc_input_vec<ConstraintF, CS>(
|
||||
mut cs: CS,
|
||||
values: &[u8],
|
||||
) -> Result<Vec<Self>, SynthesisError>
|
||||
where
|
||||
ConstraintF: PrimeField,
|
||||
CS: ConstraintSystem<ConstraintF>,
|
||||
{
|
||||
let values_len = values.len();
|
||||
let field_elements: Vec<ConstraintF> = ToConstraintField::<ConstraintF>::to_field_elements(values).unwrap();
|
||||
let field_elements: Vec<ConstraintF> =
|
||||
ToConstraintField::<ConstraintF>::to_field_elements(values).unwrap();
|
||||
|
||||
let max_size = 8 * (ConstraintF::Params::CAPACITY / 8) as usize;
|
||||
let mut allocated_bits = Vec::new();
|
||||
@@ -294,9 +299,9 @@ mod test {
|
||||
use super::UInt8;
|
||||
use crate::{prelude::*, test_constraint_system::TestConstraintSystem};
|
||||
use algebra::fields::bls12_381::Fr;
|
||||
use r1cs_core::ConstraintSystem;
|
||||
use rand::{Rng, SeedableRng};
|
||||
use rand_xorshift::XorShiftRng;
|
||||
use r1cs_core::ConstraintSystem;
|
||||
|
||||
#[test]
|
||||
fn test_uint8_from_bits_to_bits() {
|
||||
|
||||
Reference in New Issue
Block a user