-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathdummy_air.rs
226 lines (187 loc) · 5.63 KB
/
dummy_air.rs
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
use std::marker::PhantomData;
use crate::{
constraints::{
boundary::{BoundaryConstraint, BoundaryConstraints},
transition::TransitionConstraint,
},
context::AirContext,
proof::options::ProofOptions,
trace::TraceTable,
traits::{TransitionEvaluationContext, AIR},
};
use lambdaworks_math::field::{
element::FieldElement, fields::fft_friendly::stark_252_prime_field::Stark252PrimeField,
traits::IsFFTField,
};
type StarkField = Stark252PrimeField;
#[derive(Clone)]
struct FibConstraint<F: IsFFTField> {
phantom: PhantomData<F>,
}
impl<F: IsFFTField> FibConstraint<F> {
pub fn new() -> Self {
Self {
phantom: PhantomData,
}
}
}
impl<F> TransitionConstraint<F, F> for FibConstraint<F>
where
F: IsFFTField + Send + Sync,
{
fn degree(&self) -> usize {
1
}
fn constraint_idx(&self) -> usize {
0
}
fn end_exemptions(&self) -> usize {
2
}
fn evaluate(
&self,
evaluation_context: &TransitionEvaluationContext<F, F>,
transition_evaluations: &mut [FieldElement<F>],
) {
let (frame, _periodic_values, _rap_challenges) = match evaluation_context {
TransitionEvaluationContext::Prover {
frame,
periodic_values,
rap_challenges,
}
| TransitionEvaluationContext::Verifier {
frame,
periodic_values,
rap_challenges,
} => (frame, periodic_values, rap_challenges),
};
let first_step = frame.get_evaluation_step(0);
let second_step = frame.get_evaluation_step(1);
let third_step = frame.get_evaluation_step(2);
let a0 = first_step.get_main_evaluation_element(0, 1);
let a1 = second_step.get_main_evaluation_element(0, 1);
let a2 = third_step.get_main_evaluation_element(0, 1);
let res = a2 - a1 - a0;
transition_evaluations[self.constraint_idx()] = res;
}
}
#[derive(Clone)]
struct BitConstraint<F: IsFFTField> {
phantom: PhantomData<F>,
}
impl<F: IsFFTField> BitConstraint<F> {
pub fn new() -> Self {
Self {
phantom: PhantomData,
}
}
}
impl<F> TransitionConstraint<F, F> for BitConstraint<F>
where
F: IsFFTField + Send + Sync,
{
fn degree(&self) -> usize {
2
}
fn constraint_idx(&self) -> usize {
1
}
fn end_exemptions(&self) -> usize {
0
}
fn evaluate(
&self,
evaluation_context: &TransitionEvaluationContext<F, F>,
transition_evaluations: &mut [FieldElement<F>],
) {
let (frame, _periodic_values, _rap_challenges) = match evaluation_context {
TransitionEvaluationContext::Prover {
frame,
periodic_values,
rap_challenges,
}
| TransitionEvaluationContext::Verifier {
frame,
periodic_values,
rap_challenges,
} => (frame, periodic_values, rap_challenges),
};
let first_step = frame.get_evaluation_step(0);
let bit = first_step.get_main_evaluation_element(0, 0);
let res = bit * (bit - FieldElement::<F>::one());
transition_evaluations[self.constraint_idx()] = res;
}
}
pub struct DummyAIR {
context: AirContext,
trace_length: usize,
transition_constraints: Vec<Box<dyn TransitionConstraint<StarkField, StarkField>>>,
}
impl AIR for DummyAIR {
type Field = StarkField;
type FieldExtension = StarkField;
type PublicInputs = ();
const STEP_SIZE: usize = 1;
fn new(
trace_length: usize,
_pub_inputs: &Self::PublicInputs,
proof_options: &ProofOptions,
) -> Self {
let transition_constraints: Vec<
Box<dyn TransitionConstraint<Self::Field, Self::FieldExtension>>,
> = vec![
Box::new(FibConstraint::new()),
Box::new(BitConstraint::new()),
];
let context = AirContext {
proof_options: proof_options.clone(),
trace_columns: 2,
transition_offsets: vec![0, 1, 2],
num_transition_constraints: 2,
};
Self {
context,
trace_length,
transition_constraints,
}
}
fn boundary_constraints(
&self,
_rap_challenges: &[FieldElement<Self::Field>],
) -> BoundaryConstraints<Self::Field> {
let a0 = BoundaryConstraint::new_main(1, 0, FieldElement::<Self::Field>::one());
let a1 = BoundaryConstraint::new_main(1, 1, FieldElement::<Self::Field>::one());
BoundaryConstraints::from_constraints(vec![a0, a1])
}
fn transition_constraints(
&self,
) -> &Vec<Box<dyn TransitionConstraint<Self::Field, Self::FieldExtension>>> {
&self.transition_constraints
}
fn context(&self) -> &AirContext {
&self.context
}
fn composition_poly_degree_bound(&self) -> usize {
self.trace_length * 2
}
fn trace_layout(&self) -> (usize, usize) {
(2, 0)
}
fn trace_length(&self) -> usize {
self.trace_length
}
fn pub_inputs(&self) -> &Self::PublicInputs {
&()
}
}
pub fn dummy_trace<F: IsFFTField>(trace_length: usize) -> TraceTable<F, F> {
let mut ret: Vec<FieldElement<F>> = vec![];
let a0 = FieldElement::one();
let a1 = FieldElement::one();
ret.push(a0);
ret.push(a1);
for i in 2..(trace_length) {
ret.push(ret[i - 1].clone() + ret[i - 2].clone());
}
TraceTable::from_columns_main(vec![vec![FieldElement::<F>::one(); trace_length], ret], 1)
}