arnaucube
arnaucube/ark-r1cs-stdPublic Self-hosted repository
ark-r1cs-std/r1cs-std/src/bits/mod.rs
r1cs-std/src/bits/mod.rsRaw
  1use crate::{
  2    bits::{boolean::Boolean, uint8::UInt8},
  3    Vec,
  4};
  5use algebra::Field;
  6use r1cs_core::{ConstraintSystem, SynthesisError};
  7
  8pub mod boolean;
  9pub mod uint32;
 10pub mod uint8;
 11
 12pub trait ToBitsGadget<ConstraintF: Field> {
 13    fn to_bits<CS: ConstraintSystem<ConstraintF>>(
 14        &self,
 15        cs: CS,
 16    ) -> Result<Vec<Boolean>, SynthesisError>;
 17
 18    /// Additionally checks if the produced list of booleans is 'valid'.
 19    fn to_bits_strict<CS: ConstraintSystem<ConstraintF>>(
 20        &self,
 21        cs: CS,
 22    ) -> Result<Vec<Boolean>, SynthesisError>;
 23}
 24
 25impl<ConstraintF: Field> ToBitsGadget<ConstraintF> for Boolean {
 26    fn to_bits<CS: ConstraintSystem<ConstraintF>>(
 27        &self,
 28        _: CS,
 29    ) -> Result<Vec<Boolean>, SynthesisError> {
 30        Ok(vec![self.clone()])
 31    }
 32
 33    fn to_bits_strict<CS: ConstraintSystem<ConstraintF>>(
 34        &self,
 35        _: CS,
 36    ) -> Result<Vec<Boolean>, SynthesisError> {
 37        Ok(vec![self.clone()])
 38    }
 39}
 40
 41impl<ConstraintF: Field> ToBitsGadget<ConstraintF> for [Boolean] {
 42    fn to_bits<CS: ConstraintSystem<ConstraintF>>(
 43        &self,
 44        _cs: CS,
 45    ) -> Result<Vec<Boolean>, SynthesisError> {
 46        Ok(self.to_vec())
 47    }
 48
 49    fn to_bits_strict<CS: ConstraintSystem<ConstraintF>>(
 50        &self,
 51        _cs: CS,
 52    ) -> Result<Vec<Boolean>, SynthesisError> {
 53        Ok(self.to_vec())
 54    }
 55}
 56impl<ConstraintF: Field> ToBitsGadget<ConstraintF> for Vec<Boolean> {
 57    fn to_bits<CS: ConstraintSystem<ConstraintF>>(
 58        &self,
 59        _cs: CS,
 60    ) -> Result<Vec<Boolean>, SynthesisError> {
 61        Ok(self.clone())
 62    }
 63
 64    fn to_bits_strict<CS: ConstraintSystem<ConstraintF>>(
 65        &self,
 66        _cs: CS,
 67    ) -> Result<Vec<Boolean>, SynthesisError> {
 68        Ok(self.clone())
 69    }
 70}
 71
 72impl<ConstraintF: Field> ToBitsGadget<ConstraintF> for [UInt8] {
 73    fn to_bits<CS: ConstraintSystem<ConstraintF>>(
 74        &self,
 75        _cs: CS,
 76    ) -> Result<Vec<Boolean>, SynthesisError> {
 77        let mut result = Vec::with_capacity(&self.len() * 8);
 78        for byte in self {
 79            result.extend_from_slice(&byte.into_bits_le());
 80        }
 81        Ok(result)
 82    }
 83
 84    fn to_bits_strict<CS: ConstraintSystem<ConstraintF>>(
 85        &self,
 86        cs: CS,
 87    ) -> Result<Vec<Boolean>, SynthesisError> {
 88        self.to_bits(cs)
 89    }
 90}
 91
 92pub trait ToBytesGadget<ConstraintF: Field> {
 93    fn to_bytes<CS: ConstraintSystem<ConstraintF>>(
 94        &self,
 95        cs: CS,
 96    ) -> Result<Vec<UInt8>, SynthesisError>;
 97
 98    /// Additionally checks if the produced list of booleans is 'valid'.
 99    fn to_bytes_strict<CS: ConstraintSystem<ConstraintF>>(
100        &self,
101        cs: CS,
102    ) -> Result<Vec<UInt8>, SynthesisError>;
103}
104
105impl<ConstraintF: Field> ToBytesGadget<ConstraintF> for [UInt8] {
106    fn to_bytes<CS: ConstraintSystem<ConstraintF>>(
107        &self,
108        _cs: CS,
109    ) -> Result<Vec<UInt8>, SynthesisError> {
110        Ok(self.to_vec())
111    }
112
113    fn to_bytes_strict<CS: ConstraintSystem<ConstraintF>>(
114        &self,
115        cs: CS,
116    ) -> Result<Vec<UInt8>, SynthesisError> {
117        self.to_bytes(cs)
118    }
119}
120
121impl<'a, ConstraintF: Field, T: 'a + ToBytesGadget<ConstraintF>> ToBytesGadget<ConstraintF>
122    for &'a T
123{
124    fn to_bytes<CS: ConstraintSystem<ConstraintF>>(
125        &self,
126        cs: CS,
127    ) -> Result<Vec<UInt8>, SynthesisError> {
128        (*self).to_bytes(cs)
129    }
130
131    fn to_bytes_strict<CS: ConstraintSystem<ConstraintF>>(
132        &self,
133        cs: CS,
134    ) -> Result<Vec<UInt8>, SynthesisError> {
135        self.to_bytes(cs)
136    }
137}
138
139impl<'a, ConstraintF: Field> ToBytesGadget<ConstraintF> for &'a [UInt8] {
140    fn to_bytes<CS: ConstraintSystem<ConstraintF>>(
141        &self,
142        _cs: CS,
143    ) -> Result<Vec<UInt8>, SynthesisError> {
144        Ok(self.to_vec())
145    }
146
147    fn to_bytes_strict<CS: ConstraintSystem<ConstraintF>>(
148        &self,
149        cs: CS,
150    ) -> Result<Vec<UInt8>, SynthesisError> {
151        self.to_bytes(cs)
152    }
153}