-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathfibonacci_2_columns.rs
237 lines (198 loc) · 6.11 KB
/
fibonacci_2_columns.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
227
228
229
230
231
232
233
234
235
236
237
use std::marker::PhantomData;
use super::simple_fibonacci::FibonacciPublicInputs;
use crate::{
constraints::{
boundary::{BoundaryConstraint, BoundaryConstraints},
transition::TransitionConstraint,
},
context::AirContext,
proof::options::ProofOptions,
trace::TraceTable,
traits::{TransitionEvaluationContext, AIR},
};
use lambdaworks_math::field::{element::FieldElement, traits::IsFFTField};
#[derive(Clone)]
struct FibTransition1<F: IsFFTField> {
phantom: PhantomData<F>,
}
impl<F: IsFFTField> FibTransition1<F> {
pub fn new() -> Self {
Self {
phantom: PhantomData,
}
}
}
impl<F> TransitionConstraint<F, F> for FibTransition1<F>
where
F: IsFFTField + Send + Sync,
{
fn degree(&self) -> usize {
1
}
fn constraint_idx(&self) -> usize {
0
}
fn end_exemptions(&self) -> usize {
1
}
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);
// s_{0, i+1} = s_{0, i} + s_{1, i}
let s0_0 = first_step.get_main_evaluation_element(0, 0);
let s0_1 = first_step.get_main_evaluation_element(0, 1);
let s1_0 = second_step.get_main_evaluation_element(0, 0);
let res = s1_0 - s0_0 - s0_1;
transition_evaluations[self.constraint_idx()] = res;
}
}
#[derive(Clone)]
struct FibTransition2<F: IsFFTField> {
phantom: PhantomData<F>,
}
impl<F: IsFFTField> FibTransition2<F> {
pub fn new() -> Self {
Self {
phantom: PhantomData,
}
}
}
impl<F> TransitionConstraint<F, F> for FibTransition2<F>
where
F: IsFFTField + Send + Sync,
{
fn degree(&self) -> usize {
1
}
fn constraint_idx(&self) -> usize {
1
}
fn end_exemptions(&self) -> usize {
1
}
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);
// s_{1, i+1} = s_{1, i} + s_{0, i+1}
let s0_1 = first_step.get_main_evaluation_element(0, 1);
let s1_0 = second_step.get_main_evaluation_element(0, 0);
let s1_1 = second_step.get_main_evaluation_element(0, 1);
let res = s1_1 - s0_1 - s1_0;
transition_evaluations[self.constraint_idx()] = res;
}
}
pub struct Fibonacci2ColsAIR<F>
where
F: IsFFTField,
{
context: AirContext,
trace_length: usize,
pub_inputs: FibonacciPublicInputs<F>,
constraints: Vec<Box<dyn TransitionConstraint<F, F>>>,
}
/// The AIR for to a 2 column trace, where the columns form a Fibonacci sequence when
/// stacked in row-major order.
impl<F> AIR for Fibonacci2ColsAIR<F>
where
F: IsFFTField + Send + Sync + 'static,
{
type Field = F;
type FieldExtension = F;
type PublicInputs = FibonacciPublicInputs<Self::Field>;
const STEP_SIZE: usize = 1;
fn new(
trace_length: usize,
pub_inputs: &Self::PublicInputs,
proof_options: &ProofOptions,
) -> Self {
let constraints: Vec<Box<dyn TransitionConstraint<Self::Field, Self::FieldExtension>>> = vec![
Box::new(FibTransition1::new()),
Box::new(FibTransition2::new()),
];
let context = AirContext {
proof_options: proof_options.clone(),
transition_offsets: vec![0, 1],
num_transition_constraints: constraints.len(),
trace_columns: 2,
};
Self {
trace_length,
context,
constraints,
pub_inputs: pub_inputs.clone(),
}
}
fn boundary_constraints(
&self,
_rap_challenges: &[FieldElement<Self::Field>],
) -> BoundaryConstraints<Self::Field> {
let a0 = BoundaryConstraint::new_main(0, 0, self.pub_inputs.a0.clone());
let a1 = BoundaryConstraint::new_main(1, 0, self.pub_inputs.a1.clone());
BoundaryConstraints::from_constraints(vec![a0, a1])
}
fn transition_constraints(&self) -> &Vec<Box<dyn TransitionConstraint<F, F>>> {
&self.constraints
}
fn context(&self) -> &AirContext {
&self.context
}
fn composition_poly_degree_bound(&self) -> usize {
self.trace_length()
}
fn trace_length(&self) -> usize {
self.trace_length
}
fn trace_layout(&self) -> (usize, usize) {
(2, 0)
}
fn pub_inputs(&self) -> &Self::PublicInputs {
&self.pub_inputs
}
}
pub fn compute_trace<F: IsFFTField>(
initial_values: [FieldElement<F>; 2],
trace_length: usize,
) -> TraceTable<F, F> {
let mut ret1: Vec<FieldElement<F>> = vec![];
let mut ret2: Vec<FieldElement<F>> = vec![];
ret1.push(initial_values[0].clone());
ret2.push(initial_values[1].clone());
for i in 1..(trace_length) {
let new_val = ret1[i - 1].clone() + ret2[i - 1].clone();
ret1.push(new_val.clone());
ret2.push(new_val + ret2[i - 1].clone());
}
TraceTable::from_columns_main(vec![ret1, ret2], 1)
}