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
147
use itertools::izip;
use num::BigUint;
use p3_air::AirBuilder;
use p3_field::AbstractField;
use p3_field::PrimeField32;
use sp1_derive::AlignedBorrow;
use crate::{
air::Polynomial,
bytes::{event::ByteRecord, ByteLookupEvent, ByteOpcode},
stark::SP1AirBuilder,
};
use super::params::FieldParameters;
use super::params::Limbs;
/// Operation columns for verifying that an element is within the range `[0, modulus)`.
#[derive(Debug, Clone, AlignedBorrow)]
#[repr(C)]
pub struct FieldRangeCols<T, P: FieldParameters> {
/// Boolean flags to indicate the first byte in which the element is smaller than the modulus.
pub(crate) byte_flags: Limbs<T, P::Limbs>,
pub(crate) comparison_byte: T,
}
impl<F: PrimeField32, P: FieldParameters> FieldRangeCols<F, P> {
pub fn populate(
&mut self,
record: &mut impl ByteRecord,
shard: u32,
channel: u32,
value: &BigUint,
) {
let value_limbs = P::to_limbs(value);
let modulus_limbs = P::to_limbs(&P::modulus());
let mut byte_flags = vec![0u8; P::NB_LIMBS];
for (byte, modulus_byte, flag) in izip!(
value_limbs.iter().rev(),
modulus_limbs.iter().rev(),
byte_flags.iter_mut().rev()
) {
assert!(byte <= modulus_byte);
if byte < modulus_byte {
*flag = 1;
self.comparison_byte = F::from_canonical_u8(*byte);
record.add_byte_lookup_event(ByteLookupEvent {
opcode: ByteOpcode::LTU,
shard,
channel,
a1: 1,
a2: 0,
b: *byte as u32,
c: *modulus_byte as u32,
});
break;
}
}
for (byte, flag) in izip!(byte_flags.iter(), self.byte_flags.0.iter_mut()) {
*flag = F::from_canonical_u8(*byte);
}
}
}
impl<V: Copy, P: FieldParameters> FieldRangeCols<V, P> {
pub fn eval<AB: SP1AirBuilder<Var = V>, E: Into<Polynomial<AB::Expr>> + Clone>(
&self,
builder: &mut AB,
element: &E,
shard: impl Into<AB::Expr> + Clone,
channel: impl Into<AB::Expr> + Clone,
is_real: impl Into<AB::Expr> + Clone,
) where
V: Into<AB::Expr>,
Limbs<V, P::Limbs>: Copy,
{
// The byte flags give a specification of which byte is `first_eq`, i,e, the first most
// significant byte for which the element is smaller than the modulus. To verify the
// less-than claim we need to check that:
// * For all bytes until `first_eq` the element byte is equal to the modulus byte.
// * For the `first_eq` byte the element byte is smaller than the modulus byte.
// * all byte flags are boolean.
// * only one byte flag is set to one, and the rest are set to zero.
// Check the flags are of valid form.
// Verrify that only one flag is set to one.
let mut sum_flags: AB::Expr = AB::Expr::zero();
for &flag in self.byte_flags.0.iter() {
// Assert that the flag is boolean.
builder.assert_bool(flag);
// Add the flag to the sum.
sum_flags += flag.into();
}
// Assert that the sum is equal to one.
builder.assert_one(sum_flags);
// Check the less-than condition.
// A flag to indicate whether an equality check is necessary (this is for all bytes from
// most significant until the first inequality.
let mut is_inequality_visited = AB::Expr::zero();
// The bytes of the modulus.
let modulus_bytes = P::MODULUS.to_vec();
let element: Polynomial<_> = element.clone().into();
let mut first_lt_byte = AB::Expr::zero();
let mut modulus_comparison_byte = AB::Expr::zero();
for (byte, modulus_byte, &flag) in izip!(
element.coefficients().iter().rev(),
modulus_bytes.into_iter().rev(),
self.byte_flags.0.iter().rev()
) {
// Once the byte flag was set to one, we turn off the quality check flag.
// We can do this by calculating the sum of the flags since only `1` is set to `1`.
is_inequality_visited += flag.into();
first_lt_byte += byte.clone() * flag;
modulus_comparison_byte += flag.into() * AB::F::from_canonical_u8(modulus_byte);
builder
.when_not(is_inequality_visited.clone())
.assert_eq(byte.clone(), AB::F::from_canonical_u8(modulus_byte));
}
builder.assert_eq(self.comparison_byte, first_lt_byte);
// Send the comparison interaction.
builder.send_byte(
ByteOpcode::LTU.as_field::<AB::F>(),
AB::F::one(),
self.comparison_byte,
modulus_comparison_byte,
shard,
channel,
is_real,
)
}
}