-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscript_builder.rs
196 lines (174 loc) · 5.82 KB
/
script_builder.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
use alloc::collections::BTreeMap;
use alloc::format;
use alloc::vec::Vec;
use basic::field::BfField;
use bitcoin_script_stack::stack::{StackTracker, StackVariable};
use p3_air::{AirBuilder, AirBuilderWithPublicValues};
use p3_matrix::dense::RowMajorMatrix;
use scripts::treepp::*;
use super::{Dsl, ValueVariable, Variable};
use crate::ValueCounter;
pub struct ScriptConstraintBuilder<F: BfField> {
pub main: RowMajorMatrix<ValueVariable<F>>,
pub public_values: Vec<Variable>,
pub is_first_row: Dsl<F>,
pub is_last_row: Dsl<F>,
pub is_transition: Dsl<F>,
pub constraints: Vec<Dsl<F>>,
pub alpha: Dsl<F>,
}
impl<F: BfField> ScriptConstraintBuilder<F> {
pub fn new(
local: Vec<F>,
next: Vec<F>,
num_public_values: usize,
is_first_row: F,
is_last_row: F,
is_transition: F,
alpha: F,
) -> Self {
Self::new_with_expr(
local,
next,
num_public_values,
Dsl::constant_f(is_first_row),
Dsl::constant_f(is_last_row),
Dsl::constant_f(is_transition),
Dsl::constant_f(alpha),
)
}
pub fn new_with_expr(
local: Vec<F>,
next: Vec<F>,
num_public_values: usize,
is_first_row: Dsl<F>,
is_last_row: Dsl<F>,
is_transition: Dsl<F>,
alpha: Dsl<F>,
) -> Self {
let width = local.len();
let main_variables: Vec<ValueVariable<F>> = [local, next]
.into_iter()
.enumerate()
.flat_map(|(row_index, row_values)| {
(0..width).map(move |column_index| {
ValueVariable::new(
Variable::new_with_size(row_index, column_index, F::U32_SIZE as u32),
row_values[column_index],
)
})
})
.collect();
let public_values = (0..num_public_values)
// set the row_index of Variable as u32::MAX to mark public_values
.map(move |index| Variable::new(u32::MAX as usize, index))
.collect();
Self {
main: RowMajorMatrix::new(main_variables, width),
public_values: public_values,
is_first_row: is_first_row,
is_last_row: is_last_row,
is_transition: is_transition,
constraints: Vec::new(),
alpha: alpha,
}
}
pub fn get_accmulator_expr(&self) -> Dsl<F> {
let mut acc = self.constraints[0].clone();
for i in 1..self.constraints.len() {
acc = acc * self.alpha.clone() + self.constraints[i].clone();
}
acc
}
pub fn set_variable_values<Base: BfField>(
&self,
public_values: &Vec<Base>,
bmap: &mut BTreeMap<Variable, StackVariable>,
stack: &mut StackTracker,
) {
assert_eq!(self.public_values().len(), public_values.len());
for i in 0..self.public_values().len() {
let u32_vec = F::from_canonical_u32(public_values[i].as_u32_vec()[0]).as_u32_vec();
assert_eq!(u32_vec.len(), 4);
bmap.insert(
self.public_values()[i],
stack.var(
F::U32_SIZE as u32,
script! { {u32_vec[3]} {u32_vec[2]} {u32_vec[1]} {u32_vec[0]} },
&format!("public_var index={}", i),
),
);
}
for i in 0..self.main().values.len() {
let u32_vec = self.main().values[i].get_value().unwrap().as_u32_vec();
bmap.insert(
self.main().values[i].get_var().clone(),
stack.var(
F::U32_SIZE as u32,
script! { {u32_vec[3]} {u32_vec[2]} {u32_vec[1]} {u32_vec[0]} },
&format!(
"main_trace row_index={} column_index={}",
i / self.main().width,
i % self.main().width
),
),
);
}
}
pub fn set_value_count(&self, vc: &mut ValueCounter) {
// for i in 0..self.public_values().len() {
// let value = self.var
// vc.get_or_set(self.public_values()[i].as_u32());
// }
for i in 0..self.main().values.len() {
let fvalue = self.main.values[i].get_value().unwrap().as_u32_vec();
for j in 0..fvalue.len() {
vc.get_or_set(fvalue[j]);
}
}
}
pub fn drop_variable_values(
&self,
bmap: &mut BTreeMap<Variable, StackVariable>,
stack: &mut StackTracker,
) {
for i in (0..self.main().values.len()).rev() {
stack.drop(*bmap.get(self.main().values[i].get_var()).unwrap());
}
for i in (0..self.public_values().len()).rev() {
stack.drop(*bmap.get(&self.public_values()[i]).unwrap());
}
}
}
impl<F: BfField> AirBuilder for ScriptConstraintBuilder<F> {
type F = F;
type Expr = Dsl<F>;
type Var = ValueVariable<F>;
type M = RowMajorMatrix<Self::Var>;
fn main(&self) -> Self::M {
self.main.clone()
}
fn is_first_row(&self) -> Self::Expr {
self.is_first_row.clone()
}
fn is_last_row(&self) -> Self::Expr {
self.is_last_row.clone()
}
fn is_transition_window(&self, size: usize) -> Self::Expr {
if size == 2 {
self.is_transition.clone()
} else {
panic!("uni-stark only supports a window size of 2")
}
}
fn assert_zero<I: Into<Self::Expr>>(&mut self, x: I) {
let x = x.into();
self.constraints.push(x);
}
}
impl<F: BfField> AirBuilderWithPublicValues for ScriptConstraintBuilder<F> {
type PublicVar = Variable;
fn public_values(&self) -> &[Self::PublicVar] {
&self.public_values
}
}