1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
pub use crate::{
gates::{
flex_gate::GateInstructions,
range::{RangeChip, RangeInstructions},
},
utils::ScalarField,
AssignedValue, Context,
QuantumCell::{self, Constant, Existing, Witness},
};
use std::cmp::{max, min};
#[cfg(test)]
pub mod tests;
type RawAssignedValues<F> = Vec<AssignedValue<F>>;
const BITS_PER_BYTE: usize = 8;
#[derive(Clone, Debug)]
pub struct SafeType<F: ScalarField, const BYTES_PER_ELE: usize, const TOTAL_BITS: usize> {
value: RawAssignedValues<F>,
}
impl<F: ScalarField, const BYTES_PER_ELE: usize, const TOTAL_BITS: usize>
SafeType<F, BYTES_PER_ELE, TOTAL_BITS>
{
pub const BYTES_PER_ELE: usize = BYTES_PER_ELE;
pub const TOTAL_BITS: usize = TOTAL_BITS;
pub const BITS_PER_ELE: usize = min(TOTAL_BITS, BYTES_PER_ELE * BITS_PER_BYTE);
pub const VALUE_LENGTH: usize =
(TOTAL_BITS + BYTES_PER_ELE * BITS_PER_BYTE - 1) / (BYTES_PER_ELE * BITS_PER_BYTE);
fn new(raw_values: RawAssignedValues<F>) -> Self {
assert!(raw_values.len() == Self::VALUE_LENGTH, "Invalid raw values length");
Self { value: raw_values }
}
pub fn value(&self) -> &RawAssignedValues<F> {
&self.value
}
}
#[allow(type_alias_bounds)]
type CompactSafeType<F: ScalarField, const TOTAL_BITS: usize> =
SafeType<F, { ((F::NUM_BITS - 1) / 8) as usize }, TOTAL_BITS>;
pub type SafeBool<F> = CompactSafeType<F, 1>;
pub type SafeUint8<F> = CompactSafeType<F, 8>;
pub type SafeUint16<F> = CompactSafeType<F, 16>;
pub type SafeUint32<F> = CompactSafeType<F, 32>;
pub type SafeUint64<F> = CompactSafeType<F, 64>;
pub type SafeUint128<F> = CompactSafeType<F, 128>;
pub type SafeUint256<F> = CompactSafeType<F, 256>;
pub type SafeBytes32<F> = SafeType<F, 1, 256>;
pub struct SafeTypeChip<'a, F: ScalarField> {
range_chip: &'a RangeChip<F>,
}
impl<'a, F: ScalarField> SafeTypeChip<'a, F> {
pub fn new(range_chip: &'a RangeChip<F>) -> Self {
Self { range_chip }
}
pub fn raw_bytes_to<const BYTES_PER_ELE: usize, const TOTAL_BITS: usize>(
&self,
ctx: &mut Context<F>,
inputs: RawAssignedValues<F>,
) -> SafeType<F, BYTES_PER_ELE, TOTAL_BITS> {
let element_bits = SafeType::<F, BYTES_PER_ELE, TOTAL_BITS>::BITS_PER_ELE;
let bits = TOTAL_BITS;
assert!(
inputs.len() * BITS_PER_BYTE == max(bits, BITS_PER_BYTE),
"number of bits doesn't match"
);
self.add_bytes_constraints(ctx, &inputs, bits);
if bits == 1 || element_bits == BITS_PER_BYTE {
return SafeType::<F, BYTES_PER_ELE, TOTAL_BITS>::new(inputs);
};
let byte_base = (0..BYTES_PER_ELE)
.map(|i| Witness(self.range_chip.gate.pow_of_two[i * BITS_PER_BYTE]))
.collect::<Vec<_>>();
let value = inputs
.chunks(BYTES_PER_ELE)
.map(|chunk| {
self.range_chip.gate.inner_product(
ctx,
chunk.to_vec(),
byte_base[..chunk.len()].to_vec(),
)
})
.collect::<Vec<_>>();
SafeType::<F, BYTES_PER_ELE, TOTAL_BITS>::new(value)
}
fn add_bytes_constraints(
&self,
ctx: &mut Context<F>,
inputs: &RawAssignedValues<F>,
bits: usize,
) {
let mut bits_left = bits;
for input in inputs {
let num_bit = min(bits_left, BITS_PER_BYTE);
self.range_chip.range_check(ctx, *input, num_bit);
bits_left -= num_bit;
}
}
}