|
@ -11,16 +11,24 @@ pub mod uint64; |
|
|
pub mod uint8;
|
|
|
pub mod uint8;
|
|
|
|
|
|
|
|
|
pub trait ToBitsGadget<ConstraintF: Field> {
|
|
|
pub trait ToBitsGadget<ConstraintF: Field> {
|
|
|
|
|
|
/// Outputs the canonical bit-wise representation of `self`.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// This is the correct default for 99% of use cases.
|
|
|
fn to_bits<CS: ConstraintSystem<ConstraintF>>(
|
|
|
fn to_bits<CS: ConstraintSystem<ConstraintF>>(
|
|
|
&self,
|
|
|
&self,
|
|
|
cs: CS,
|
|
|
cs: CS,
|
|
|
) -> Result<Vec<Boolean>, SynthesisError>;
|
|
|
) -> Result<Vec<Boolean>, SynthesisError>;
|
|
|
|
|
|
|
|
|
/// Additionally checks if the produced list of booleans is 'valid'.
|
|
|
|
|
|
fn to_bits_strict<CS: ConstraintSystem<ConstraintF>>(
|
|
|
|
|
|
|
|
|
/// Outputs a possibly non-unique bit-wise representation of `self`.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// If you're not absolutely certain that your usecase can get away with a
|
|
|
|
|
|
/// non-canonical representation, please use `self.to_bits(cs)` instead.
|
|
|
|
|
|
fn to_non_unique_bits<CS: ConstraintSystem<ConstraintF>>(
|
|
|
&self,
|
|
|
&self,
|
|
|
cs: CS,
|
|
|
cs: CS,
|
|
|
) -> Result<Vec<Boolean>, SynthesisError>;
|
|
|
|
|
|
|
|
|
) -> Result<Vec<Boolean>, SynthesisError> {
|
|
|
|
|
|
self.to_bits(cs)
|
|
|
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
impl<ConstraintF: Field> ToBitsGadget<ConstraintF> for Boolean {
|
|
|
impl<ConstraintF: Field> ToBitsGadget<ConstraintF> for Boolean {
|
|
@ -30,13 +38,6 @@ impl ToBitsGadget for Boolean { |
|
|
) -> Result<Vec<Boolean>, SynthesisError> {
|
|
|
) -> Result<Vec<Boolean>, SynthesisError> {
|
|
|
Ok(vec![self.clone()])
|
|
|
Ok(vec![self.clone()])
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
fn to_bits_strict<CS: ConstraintSystem<ConstraintF>>(
|
|
|
|
|
|
&self,
|
|
|
|
|
|
_: CS,
|
|
|
|
|
|
) -> Result<Vec<Boolean>, SynthesisError> {
|
|
|
|
|
|
Ok(vec![self.clone()])
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
impl<ConstraintF: Field> ToBitsGadget<ConstraintF> for [Boolean] {
|
|
|
impl<ConstraintF: Field> ToBitsGadget<ConstraintF> for [Boolean] {
|
|
@ -46,14 +47,8 @@ impl ToBitsGadget for [Boolean] { |
|
|
) -> Result<Vec<Boolean>, SynthesisError> {
|
|
|
) -> Result<Vec<Boolean>, SynthesisError> {
|
|
|
Ok(self.to_vec())
|
|
|
Ok(self.to_vec())
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
fn to_bits_strict<CS: ConstraintSystem<ConstraintF>>(
|
|
|
|
|
|
&self,
|
|
|
|
|
|
_cs: CS,
|
|
|
|
|
|
) -> Result<Vec<Boolean>, SynthesisError> {
|
|
|
|
|
|
Ok(self.to_vec())
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
impl<ConstraintF: Field> ToBitsGadget<ConstraintF> for Vec<Boolean> {
|
|
|
impl<ConstraintF: Field> ToBitsGadget<ConstraintF> for Vec<Boolean> {
|
|
|
fn to_bits<CS: ConstraintSystem<ConstraintF>>(
|
|
|
fn to_bits<CS: ConstraintSystem<ConstraintF>>(
|
|
|
&self,
|
|
|
&self,
|
|
@ -61,13 +56,6 @@ impl ToBitsGadget for Vec { |
|
|
) -> Result<Vec<Boolean>, SynthesisError> {
|
|
|
) -> Result<Vec<Boolean>, SynthesisError> {
|
|
|
Ok(self.clone())
|
|
|
Ok(self.clone())
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
fn to_bits_strict<CS: ConstraintSystem<ConstraintF>>(
|
|
|
|
|
|
&self,
|
|
|
|
|
|
_cs: CS,
|
|
|
|
|
|
) -> Result<Vec<Boolean>, SynthesisError> {
|
|
|
|
|
|
Ok(self.clone())
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
impl<ConstraintF: Field> ToBitsGadget<ConstraintF> for [UInt8] {
|
|
|
impl<ConstraintF: Field> ToBitsGadget<ConstraintF> for [UInt8] {
|
|
@ -81,26 +69,27 @@ impl ToBitsGadget for [UInt8] { |
|
|
}
|
|
|
}
|
|
|
Ok(result)
|
|
|
Ok(result)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
fn to_bits_strict<CS: ConstraintSystem<ConstraintF>>(
|
|
|
|
|
|
&self,
|
|
|
|
|
|
cs: CS,
|
|
|
|
|
|
) -> Result<Vec<Boolean>, SynthesisError> {
|
|
|
|
|
|
self.to_bits(cs)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
pub trait ToBytesGadget<ConstraintF: Field> {
|
|
|
pub trait ToBytesGadget<ConstraintF: Field> {
|
|
|
|
|
|
/// Outputs a canonical byte-wise representation of `self`.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// This is the correct default for 99% of use cases.
|
|
|
fn to_bytes<CS: ConstraintSystem<ConstraintF>>(
|
|
|
fn to_bytes<CS: ConstraintSystem<ConstraintF>>(
|
|
|
&self,
|
|
|
&self,
|
|
|
cs: CS,
|
|
|
cs: CS,
|
|
|
) -> Result<Vec<UInt8>, SynthesisError>;
|
|
|
) -> Result<Vec<UInt8>, SynthesisError>;
|
|
|
|
|
|
|
|
|
/// Additionally checks if the produced list of booleans is 'valid'.
|
|
|
|
|
|
fn to_bytes_strict<CS: ConstraintSystem<ConstraintF>>(
|
|
|
|
|
|
|
|
|
/// Outputs a possibly non-unique byte decomposition of `self`.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// If you're not absolutely certain that your usecase can get away with a
|
|
|
|
|
|
/// non-canonical representation, please use `self.to_bytes(cs)` instead.
|
|
|
|
|
|
fn to_non_unique_bytes<CS: ConstraintSystem<ConstraintF>>(
|
|
|
&self,
|
|
|
&self,
|
|
|
cs: CS,
|
|
|
cs: CS,
|
|
|
) -> Result<Vec<UInt8>, SynthesisError>;
|
|
|
|
|
|
|
|
|
) -> Result<Vec<UInt8>, SynthesisError> {
|
|
|
|
|
|
self.to_bytes(cs)
|
|
|
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
impl<ConstraintF: Field> ToBytesGadget<ConstraintF> for [UInt8] {
|
|
|
impl<ConstraintF: Field> ToBytesGadget<ConstraintF> for [UInt8] {
|
|
@ -110,13 +99,6 @@ impl ToBytesGadget for [UInt8] { |
|
|
) -> Result<Vec<UInt8>, SynthesisError> {
|
|
|
) -> Result<Vec<UInt8>, SynthesisError> {
|
|
|
Ok(self.to_vec())
|
|
|
Ok(self.to_vec())
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
fn to_bytes_strict<CS: ConstraintSystem<ConstraintF>>(
|
|
|
|
|
|
&self,
|
|
|
|
|
|
cs: CS,
|
|
|
|
|
|
) -> Result<Vec<UInt8>, SynthesisError> {
|
|
|
|
|
|
self.to_bytes(cs)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
impl<'a, ConstraintF: Field, T: 'a + ToBytesGadget<ConstraintF>> ToBytesGadget<ConstraintF>
|
|
|
impl<'a, ConstraintF: Field, T: 'a + ToBytesGadget<ConstraintF>> ToBytesGadget<ConstraintF>
|
|
@ -128,13 +110,6 @@ impl<'a, ConstraintF: Field, T: 'a + ToBytesGadget> ToBytesGadget |
|
|
) -> Result<Vec<UInt8>, SynthesisError> {
|
|
|
) -> Result<Vec<UInt8>, SynthesisError> {
|
|
|
(*self).to_bytes(cs)
|
|
|
(*self).to_bytes(cs)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
fn to_bytes_strict<CS: ConstraintSystem<ConstraintF>>(
|
|
|
|
|
|
&self,
|
|
|
|
|
|
cs: CS,
|
|
|
|
|
|
) -> Result<Vec<UInt8>, SynthesisError> {
|
|
|
|
|
|
self.to_bytes(cs)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
impl<'a, ConstraintF: Field> ToBytesGadget<ConstraintF> for &'a [UInt8] {
|
|
|
impl<'a, ConstraintF: Field> ToBytesGadget<ConstraintF> for &'a [UInt8] {
|
|
@ -144,11 +119,4 @@ impl<'a, ConstraintF: Field> ToBytesGadget for &'a [UInt8] { |
|
|
) -> Result<Vec<UInt8>, SynthesisError> {
|
|
|
) -> Result<Vec<UInt8>, SynthesisError> {
|
|
|
Ok(self.to_vec())
|
|
|
Ok(self.to_vec())
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
fn to_bytes_strict<CS: ConstraintSystem<ConstraintF>>(
|
|
|
|
|
|
&self,
|
|
|
|
|
|
cs: CS,
|
|
|
|
|
|
) -> Result<Vec<UInt8>, SynthesisError> {
|
|
|
|
|
|
self.to_bytes(cs)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
}
|