1use std::ops::Deref;
23use crate::QuantumCell;
45use super::*;
6/// SafeType for bool (1 bit).
7///
8/// This is a separate struct from `CompactSafeType` with the same behavior. Because
9/// we know only one [`AssignedValue`] is needed to hold the boolean value, we avoid
10/// using `CompactSafeType` to avoid the additional heap allocation from a length 1 vector.
11#[derive(Clone, Copy, Debug)]
12pub struct SafeBool<F: ScalarField>(pub(super) AssignedValue<F>);
1314/// SafeType for byte (8 bits).
15///
16/// This is a separate struct from `CompactSafeType` with the same behavior. Because
17/// we know only one [`AssignedValue`] is needed to hold the boolean value, we avoid
18/// using `CompactSafeType` to avoid the additional heap allocation from a length 1 vector.
19#[derive(Clone, Copy, Debug)]
20pub struct SafeByte<F: ScalarField>(pub(super) AssignedValue<F>);
2122macro_rules! safe_primitive_impls {
23 ($SafePrimitive:ty) => {
24impl<F: ScalarField> AsRef<AssignedValue<F>> for $SafePrimitive {
25fn as_ref(&self) -> &AssignedValue<F> {
26&self.0
27}
28 }
2930impl<F: ScalarField> Borrow<AssignedValue<F>> for $SafePrimitive {
31fn borrow(&self) -> &AssignedValue<F> {
32&self.0
33}
34 }
3536impl<F: ScalarField> Deref for $SafePrimitive {
37type Target = AssignedValue<F>;
3839fn deref(&self) -> &Self::Target {
40&self.0
41}
42 }
4344impl<F: ScalarField> From<$SafePrimitive> for AssignedValue<F> {
45fn from(safe_primitive: $SafePrimitive) -> Self {
46 safe_primitive.0
47}
48 }
4950impl<F: ScalarField> From<$SafePrimitive> for QuantumCell<F> {
51fn from(safe_primitive: $SafePrimitive) -> Self {
52 QuantumCell::Existing(safe_primitive.0)
53 }
54 }
55 };
56}
5758safe_primitive_impls!(SafeBool<F>);
59safe_primitive_impls!(SafeByte<F>);