-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathstark252.rs
229 lines (208 loc) · 7.16 KB
/
stark252.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
use std::hint::black_box;
use criterion::Criterion;
use lambdaworks_math::{
field::{
element::FieldElement,
fields::{
fft_friendly::stark_252_prime_field::{
MontgomeryConfigStark252PrimeField, Stark252PrimeField,
},
montgomery_backed_prime_fields::IsModulus,
},
},
unsigned_integer::{
element::{UnsignedInteger, U256},
montgomery::MontgomeryAlgorithms,
},
};
use rand::random;
pub type F = FieldElement<Stark252PrimeField>;
#[inline(never)]
#[export_name = "util::rand_field_elements"]
pub fn rand_field_elements(num: usize) -> Vec<(F, F)> {
let mut result = Vec::with_capacity(num);
for _ in 0..result.capacity() {
let rand_a = UnsignedInteger { limbs: random() };
let rand_b = UnsignedInteger { limbs: random() };
result.push((F::new(rand_a), F::new(rand_b)));
}
result
}
pub fn starkfield_ops_benchmarks(c: &mut Criterion) {
let input: Vec<Vec<(F, F)>> = [1, 10, 100, 1000, 10000, 100000, 1000000]
.into_iter()
.map(rand_field_elements)
.collect::<Vec<_>>();
let mut group = c.benchmark_group("Stark FP operations");
for i in input.clone().into_iter() {
group.bench_with_input(format!("add {:?}", &i.len()), &i, |bench, i| {
bench.iter(|| {
for (x, y) in i {
black_box(black_box(x) + black_box(y));
}
});
});
}
for i in input.clone().into_iter() {
group.bench_with_input(format!("mul {:?}", &i.len()), &i, |bench, i| {
bench.iter(|| {
for (x, y) in i {
black_box(black_box(x) * black_box(y));
}
});
});
}
for i in input.clone().into_iter() {
group.bench_with_input(format!("pow by 1 {:?}", &i.len()), &i, |bench, i| {
bench.iter(|| {
for (x, _) in i {
black_box(black_box(x).pow(1_u64));
}
});
});
}
// The non-boxed constants are intentional as they are
// normally computed at compile time.
for i in input.clone().into_iter() {
group.bench_with_input(format!("sos_square {:?}", &i.len()), &i, |bench, i| {
bench.iter(|| {
for (x, _) in i {
MontgomeryAlgorithms::sos_square(
black_box(black_box(x.value())),
&<MontgomeryConfigStark252PrimeField as IsModulus<U256>>::MODULUS,
&Stark252PrimeField::MU,
);
}
});
});
}
for i in input.clone().into_iter() {
group.bench_with_input(format!("square {:?}", &i.len()), &i, |bench, i| {
bench.iter(|| {
for (x, _) in i {
black_box(black_box(x).square());
}
});
});
}
for i in input.clone().into_iter() {
group.bench_with_input(format!("square with pow {:?}", &i.len()), &i, |bench, i| {
bench.iter(|| {
for (x, _) in i {
black_box(black_box(x).pow(2_u64));
}
});
});
}
for i in input.clone().into_iter() {
group.bench_with_input(format!("square with mul {:?}", &i.len()), &i, |bench, i| {
bench.iter(|| {
for (x, _) in i {
black_box(black_box(x) * black_box(x));
}
});
});
}
for i in input.clone().into_iter() {
group.bench_with_input(
format!("pow {:?}", &i.len()),
&(i, 5u64),
|bench, (i, a)| {
bench.iter(|| {
for (x, _) in i {
black_box(black_box(x).pow(*a));
}
});
},
);
}
for i in input.clone().into_iter() {
group.bench_with_input(format!("sub {:?}", &i.len()), &i, |bench, i| {
bench.iter(|| {
for (x, y) in i {
black_box(black_box(x) - black_box(y));
}
});
});
}
for i in input.clone().into_iter() {
group.bench_with_input(format!("inv {:?}", &i.len()), &i, |bench, i| {
bench.iter(|| {
for (x, _) in i {
black_box(black_box(x).inv().unwrap());
}
});
});
}
for i in input.clone().into_iter() {
group.bench_with_input(format!("div {:?}", &i.len()), &i, |bench, i| {
bench.iter(|| {
for (x, y) in i {
black_box(black_box(x) / black_box(y)).unwrap();
}
});
});
}
for i in input.clone().into_iter() {
group.bench_with_input(format!("eq {:?}", &i.len()), &i, |bench, i| {
bench.iter(|| {
for (x, y) in i {
black_box(black_box(x) == black_box(y));
}
});
});
}
for i in input.clone().into_iter() {
group.bench_with_input(format!("sqrt {:?}", &i.len()), &i, |bench, i| {
bench.iter(|| {
for (x, _) in i {
black_box(black_box(x).sqrt());
}
});
});
}
for i in input.clone().into_iter() {
group.bench_with_input(format!("sqrt squared {:?}", &i.len()), &i, |bench, i| {
let i: Vec<F> = i.iter().map(|(x, _)| x * x).collect();
bench.iter(|| {
for x in &i {
black_box(black_box(x).sqrt());
}
});
});
}
for i in input.clone().into_iter() {
group.bench_with_input(format!("bitand {:?}", &i.len()), &i, |bench, i| {
// Note: we should strive to have the number of limbs be generic... ideally this benchmark group itself should have a generic type that we call into from the main runner.
let i: Vec<(UnsignedInteger<4>, UnsignedInteger<4>)> =
i.iter().map(|(x, y)| (*x.value(), *y.value())).collect();
bench.iter(|| {
for (x, y) in &i {
black_box(black_box(*x) & black_box(*y));
}
});
});
}
for i in input.clone().into_iter() {
group.bench_with_input(format!("bitor {:?}", &i.len()), &i, |bench, i| {
let i: Vec<(UnsignedInteger<4>, UnsignedInteger<4>)> =
i.iter().map(|(x, y)| (*x.value(), *y.value())).collect();
bench.iter(|| {
for (x, y) in &i {
black_box(black_box(*x) | black_box(*y));
}
});
});
}
for i in input.clone().into_iter() {
group.bench_with_input(format!("bitxor {:?}", &i.len()), &i, |bench, i| {
let i: Vec<(UnsignedInteger<4>, UnsignedInteger<4>)> =
i.iter().map(|(x, y)| (*x.value(), *y.value())).collect();
bench.iter(|| {
for (x, y) in &i {
black_box(black_box(*x) ^ black_box(*y));
}
});
});
}
}