Struct ark_r1cs_std::bits::uint8::UInt8
source · pub struct UInt8<F: Field> { /* private fields */ }
Expand description
Represents an interpretation of 8 Boolean
objects as an
unsigned integer.
Implementations§
source§impl<F: Field> UInt8<F>
impl<F: Field> UInt8<F>
sourcepub fn constant_vec(values: &[u8]) -> Vec<Self> ⓘ
pub fn constant_vec(values: &[u8]) -> Vec<Self> ⓘ
Construct a constant vector of UInt8
from a vector of u8
This does not create any new variables or constraints.
// We'll use the BLS12-381 scalar field for our constraints.
use ark_test_curves::bls12_381::Fr;
use ark_relations::r1cs::*;
use ark_r1cs_std::prelude::*;
let cs = ConstraintSystem::<Fr>::new_ref();
let var = vec![UInt8::new_witness(cs.clone(), || Ok(2))?];
let constant = UInt8::constant_vec(&[2]);
var.enforce_equal(&constant)?;
assert!(cs.is_satisfied().unwrap());
sourcepub fn constant(value: u8) -> Self
pub fn constant(value: u8) -> Self
Construct a constant UInt8
from a u8
This does not create new variables or constraints.
// We'll use the BLS12-381 scalar field for our constraints.
use ark_test_curves::bls12_381::Fr;
use ark_relations::r1cs::*;
use ark_r1cs_std::prelude::*;
let cs = ConstraintSystem::<Fr>::new_ref();
let var = UInt8::new_witness(cs.clone(), || Ok(2))?;
let constant = UInt8::constant(2);
var.enforce_equal(&constant)?;
assert!(cs.is_satisfied().unwrap());
sourcepub fn new_witness_vec(
cs: impl Into<Namespace<F>>,
values: &[impl Into<Option<u8>> + Copy]
) -> Result<Vec<Self>, SynthesisError>
pub fn new_witness_vec(
cs: impl Into<Namespace<F>>,
values: &[impl Into<Option<u8>> + Copy]
) -> Result<Vec<Self>, SynthesisError>
Allocates a slice of u8
’s as private witnesses.
sourcepub fn new_input_vec(
cs: impl Into<Namespace<F>>,
values: &[u8]
) -> Result<Vec<Self>, SynthesisError>where
F: PrimeField,
pub fn new_input_vec(
cs: impl Into<Namespace<F>>,
values: &[u8]
) -> Result<Vec<Self>, SynthesisError>where
F: PrimeField,
Allocates a slice of u8
’s as public inputs by first packing them into
elements of F
, (thus reducing the number of input allocations),
allocating these elements as public inputs, and then converting
these field variables FpVar<F>
variables back into bytes.
From a user perspective, this trade-off adds constraints, but improves verifier time and verification key size.
// We'll use the BLS12-381 scalar field for our constraints.
use ark_test_curves::bls12_381::Fr;
use ark_relations::r1cs::*;
use ark_r1cs_std::prelude::*;
let cs = ConstraintSystem::<Fr>::new_ref();
let two = UInt8::new_witness(cs.clone(), || Ok(2))?;
let var = vec![two.clone(); 32];
let c = UInt8::new_input_vec(cs.clone(), &[2; 32])?;
var.enforce_equal(&c)?;
assert!(cs.is_satisfied().unwrap());
sourcepub fn from_bits_le(bits: &[Boolean<F>]) -> Self
pub fn from_bits_le(bits: &[Boolean<F>]) -> Self
Converts a little-endian byte order representation of bits into a
UInt8
.
// We'll use the BLS12-381 scalar field for our constraints.
use ark_test_curves::bls12_381::Fr;
use ark_relations::r1cs::*;
use ark_r1cs_std::prelude::*;
let cs = ConstraintSystem::<Fr>::new_ref();
let var = UInt8::new_witness(cs.clone(), || Ok(128))?;
let f = Boolean::FALSE;
let t = Boolean::TRUE;
// Construct [0, 0, 0, 0, 0, 0, 0, 1]
let mut bits = vec![f.clone(); 7];
bits.push(t);
let mut c = UInt8::from_bits_le(&bits);
var.enforce_equal(&c)?;
assert!(cs.is_satisfied().unwrap());
sourcepub fn xor(&self, other: &Self) -> Result<Self, SynthesisError>
pub fn xor(&self, other: &Self) -> Result<Self, SynthesisError>
Outputs self ^ other
.
If at least one of self
and other
are constants, then this method
does not create any constraints or variables.
// We'll use the BLS12-381 scalar field for our constraints.
use ark_test_curves::bls12_381::Fr;
use ark_relations::r1cs::*;
use ark_r1cs_std::prelude::*;
let cs = ConstraintSystem::<Fr>::new_ref();
let a = UInt8::new_witness(cs.clone(), || Ok(16))?;
let b = UInt8::new_witness(cs.clone(), || Ok(17))?;
let c = UInt8::new_witness(cs.clone(), || Ok(1))?;
a.xor(&b)?.enforce_equal(&c)?;
assert!(cs.is_satisfied().unwrap());
Trait Implementations§
source§impl<ConstraintF: Field> AllocVar<u8, ConstraintF> for UInt8<ConstraintF>
impl<ConstraintF: Field> AllocVar<u8, ConstraintF> for UInt8<ConstraintF>
source§fn new_variable<T: Borrow<u8>>(
cs: impl Into<Namespace<ConstraintF>>,
f: impl FnOnce() -> Result<T, SynthesisError>,
mode: AllocationMode
) -> Result<Self, SynthesisError>
fn new_variable<T: Borrow<u8>>(
cs: impl Into<Namespace<ConstraintF>>,
f: impl FnOnce() -> Result<T, SynthesisError>,
mode: AllocationMode
) -> Result<Self, SynthesisError>
Self
in the ConstraintSystem
cs
.
The mode of allocation is decided by mode
.source§fn new_constant(
cs: impl Into<Namespace<F>>,
t: impl Borrow<V>
) -> Result<Self, SynthesisError>
fn new_constant(
cs: impl Into<Namespace<F>>,
t: impl Borrow<V>
) -> Result<Self, SynthesisError>
source§fn new_input<T: Borrow<V>>(
cs: impl Into<Namespace<F>>,
f: impl FnOnce() -> Result<T, SynthesisError>
) -> Result<Self, SynthesisError>
fn new_input<T: Borrow<V>>(
cs: impl Into<Namespace<F>>,
f: impl FnOnce() -> Result<T, SynthesisError>
) -> Result<Self, SynthesisError>
Self
in the ConstraintSystem
cs
.source§fn new_witness<T: Borrow<V>>(
cs: impl Into<Namespace<F>>,
f: impl FnOnce() -> Result<T, SynthesisError>
) -> Result<Self, SynthesisError>
fn new_witness<T: Borrow<V>>(
cs: impl Into<Namespace<F>>,
f: impl FnOnce() -> Result<T, SynthesisError>
) -> Result<Self, SynthesisError>
Self
in the ConstraintSystem
cs
.source§impl<ConstraintF: Field> CondSelectGadget<ConstraintF> for UInt8<ConstraintF>
impl<ConstraintF: Field> CondSelectGadget<ConstraintF> for UInt8<ConstraintF>
source§fn conditionally_select(
cond: &Boolean<ConstraintF>,
true_value: &Self,
false_value: &Self
) -> Result<Self, SynthesisError>
fn conditionally_select(
cond: &Boolean<ConstraintF>,
true_value: &Self,
false_value: &Self
) -> Result<Self, SynthesisError>
source§fn conditionally_select_power_of_two_vector(
position: &[Boolean<ConstraintF>],
values: &[Self]
) -> Result<Self, SynthesisError>
fn conditionally_select_power_of_two_vector(
position: &[Boolean<ConstraintF>],
values: &[Self]
) -> Result<Self, SynthesisError>
values
whose index in represented by position
.
position
is an array of boolean that represents an unsigned integer in
big endian order. Read moresource§impl<ConstraintF: Field> EqGadget<ConstraintF> for UInt8<ConstraintF>
impl<ConstraintF: Field> EqGadget<ConstraintF> for UInt8<ConstraintF>
source§fn is_eq(&self, other: &Self) -> Result<Boolean<ConstraintF>, SynthesisError>
fn is_eq(&self, other: &Self) -> Result<Boolean<ConstraintF>, SynthesisError>
Boolean
value representing whether self.value() == other.value()
.source§fn conditional_enforce_equal(
&self,
other: &Self,
condition: &Boolean<ConstraintF>
) -> Result<(), SynthesisError>
fn conditional_enforce_equal(
&self,
other: &Self,
condition: &Boolean<ConstraintF>
) -> Result<(), SynthesisError>
should_enforce == true
, enforce that self
and other
are equal;
else, enforce a vacuously true statement. Read moresource§fn conditional_enforce_not_equal(
&self,
other: &Self,
condition: &Boolean<ConstraintF>
) -> Result<(), SynthesisError>
fn conditional_enforce_not_equal(
&self,
other: &Self,
condition: &Boolean<ConstraintF>
) -> Result<(), SynthesisError>
should_enforce == true
, enforce that self
and other
are not
equal; else, enforce a vacuously true statement. Read moresource§fn enforce_equal(&self, other: &Self) -> Result<(), SynthesisError>
fn enforce_equal(&self, other: &Self) -> Result<(), SynthesisError>
source§fn enforce_not_equal(&self, other: &Self) -> Result<(), SynthesisError>
fn enforce_not_equal(&self, other: &Self) -> Result<(), SynthesisError>
source§impl<F: Field> R1CSVar<F> for UInt8<F>
impl<F: Field> R1CSVar<F> for UInt8<F>
source§fn cs(&self) -> ConstraintSystemRef<F>
fn cs(&self) -> ConstraintSystemRef<F>
ConstraintSystemRef
. Read moresource§fn value(&self) -> Result<Self::Value, SynthesisError>
fn value(&self) -> Result<Self::Value, SynthesisError>
self
in the underlying
ConstraintSystem
.source§fn is_constant(&self) -> bool
fn is_constant(&self) -> bool
true
if self
is a circuit-generation-time constant.source§impl<F: Field> ToBitsGadget<F> for UInt8<F>
impl<F: Field> ToBitsGadget<F> for UInt8<F>
source§fn to_bits_le(&self) -> Result<Vec<Boolean<F>>, SynthesisError>
fn to_bits_le(&self) -> Result<Vec<Boolean<F>>, SynthesisError>
self
. Read moresource§fn to_non_unique_bits_le(&self) -> Result<Vec<Boolean<F>>, SynthesisError>
fn to_non_unique_bits_le(&self) -> Result<Vec<Boolean<F>>, SynthesisError>
self
. Read moresource§fn to_bits_be(&self) -> Result<Vec<Boolean<F>>, SynthesisError>
fn to_bits_be(&self) -> Result<Vec<Boolean<F>>, SynthesisError>
self
.source§fn to_non_unique_bits_be(&self) -> Result<Vec<Boolean<F>>, SynthesisError>
fn to_non_unique_bits_be(&self) -> Result<Vec<Boolean<F>>, SynthesisError>
self
.