ark-curves-cherry-picked/curve-benches/src/macros/ec.rs
curve-benches/src/macros/ec.rsRaw
1#[macro_export]
2macro_rules! ec_bench {
3 ($projective:ty, $affine:ty) => {
4 fn bench_curve(c: &mut $crate::criterion::Criterion) {
5 let mut group = c.benchmark_group(core::stringify!($projective));
6 group.bench_function("Rand", rand);
7 group.bench_function("MulAssign", mul_assign);
8 group.bench_function("AddAssign", add_assign);
9 group.bench_function("AddAssignMixed", add_assign_mixed);
10 group.bench_function("Serialize w/ compression", ser);
11 group.bench_function("Deserialize w/ compression", deser);
12 group.bench_function("Serialize unchecked", ser_unchecked);
13 group.bench_function("Deserialize unchecked", deser_unchecked);
14 }
15
16
17 fn rand(b: &mut $crate::criterion::Bencher) {
18 let mut rng = XorShiftRng::seed_from_u64(1231275789u64);
19 b.iter(|| <$projective>::rand(&mut rng));
20 }
21
22
23 fn mul_assign(b: &mut $crate::criterion::Bencher) {
24 const SAMPLES: usize = 1000;
25
26 let mut rng = XorShiftRng::seed_from_u64(1231275789u64);
27
28 let v: Vec<($projective, Fr)> = (0..SAMPLES)
29 .map(|_| (<$projective>::rand(&mut rng), Fr::rand(&mut rng)))
30 .collect();
31
32 let mut count = 0;
33 b.iter(|| {
34 let mut tmp = v[count].0;
35 tmp *= v[count].1;
36 count = (count + 1) % SAMPLES;
37 tmp
38 });
39 }
40
41
42 fn add_assign(b: &mut $crate::criterion::Bencher) {
43 const SAMPLES: usize = 1000;
44
45 let mut rng = XorShiftRng::seed_from_u64(1231275789u64);
46
47 let v: Vec<($projective, $projective)> = (0..SAMPLES)
48 .map(|_| (<$projective>::rand(&mut rng), <$projective>::rand(&mut rng)))
49 .collect();
50
51 let mut count = 0;
52 b.iter(|| {
53 let mut tmp = v[count].0;
54 n_fold!(tmp, v, add_assign, count);
55 count = (count + 1) % SAMPLES;
56 tmp
57 });
58 }
59
60
61 fn add_assign_mixed(b: &mut $crate::criterion::Bencher) {
62 const SAMPLES: usize = 1000;
63
64 let mut rng = XorShiftRng::seed_from_u64(1231275789u64);
65
66 let v: Vec<($projective, $affine)> = (0..SAMPLES)
67 .map(|_| {
68 (
69 <$projective>::rand(&mut rng),
70 <$projective>::rand(&mut rng).into(),
71 )
72 })
73 .collect();
74
75 let mut count = 0;
76 b.iter(|| {
77 let mut tmp = v[count].0;
78 n_fold!(tmp, v, add_assign_mixed, count);
79 count = (count + 1) % SAMPLES;
80 tmp
81 });
82 }
83
84
85 fn double(b: &mut $crate::criterion::Bencher) {
86 const SAMPLES: usize = 1000;
87
88 let mut rng = XorShiftRng::seed_from_u64(1231275789u64);
89
90 let v: Vec<($projective, $projective)> = (0..SAMPLES)
91 .map(|_| (<$projective>::rand(&mut rng), <$projective>::rand(&mut rng)))
92 .collect();
93
94 let mut count = 0;
95 b.iter(|| {
96 let mut tmp = v[count].0;
97 n_fold!(tmp, double_in_place);
98 count = (count + 1) % SAMPLES;
99 tmp
100 });
101 }
102
103
104 fn deser(b: &mut $crate::criterion::Bencher) {
105 use ark_ec::ProjectiveCurve;
106 use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
107 const SAMPLES: usize = 1000;
108
109 let mut rng = XorShiftRng::seed_from_u64(1231275789u64);
110
111 let mut num_bytes = 0;
112 let tmp = <$projective>::rand(&mut rng).into_affine();
113 let v: Vec<_> = (0..SAMPLES)
114 .flat_map(|_| {
115 let mut bytes = Vec::with_capacity(1000);
116 tmp.serialize(&mut bytes).unwrap();
117 num_bytes = bytes.len();
118 bytes
119 })
120 .collect();
121
122 let mut count = 0;
123 b.iter(|| {
124 count = (count + 1) % SAMPLES;
125 let index = count * num_bytes;
126 <$affine>::deserialize(&v[index..(index + num_bytes)]).unwrap()
127 });
128 }
129
130
131 fn ser(b: &mut $crate::criterion::Bencher) {
132 use ark_ec::ProjectiveCurve;
133 use ark_serialize::CanonicalSerialize;
134 const SAMPLES: usize = 1000;
135
136 let mut rng = XorShiftRng::seed_from_u64(1231275789u64);
137
138 let mut v: Vec<_> = (0..SAMPLES)
139 .map(|_| <$projective>::rand(&mut rng))
140 .collect();
141 let v = <$projective>::batch_normalization_into_affine(v.as_mut_slice());
142 let mut bytes = Vec::with_capacity(1000);
143
144 let mut count = 0;
145 b.iter(|| {
146 let tmp = v[count];
147 count = (count + 1) % SAMPLES;
148 bytes.clear();
149 tmp.serialize(&mut bytes)
150 });
151 }
152
153
154 fn deser_unchecked(b: &mut $crate::criterion::Bencher) {
155 use ark_ec::ProjectiveCurve;
156 use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
157 const SAMPLES: usize = 1000;
158
159 let mut rng = XorShiftRng::seed_from_u64(1231275789u64);
160
161 let mut num_bytes = 0;
162 let tmp = <$projective>::rand(&mut rng).into_affine();
163 let v: Vec<_> = (0..SAMPLES)
164 .flat_map(|_| {
165 let mut bytes = Vec::with_capacity(1000);
166 tmp.serialize_unchecked(&mut bytes).unwrap();
167 num_bytes = bytes.len();
168 bytes
169 })
170 .collect();
171
172 let mut count = 0;
173 b.iter(|| {
174 count = (count + 1) % SAMPLES;
175 let index = count * num_bytes;
176 <$affine>::deserialize_unchecked(&v[index..(index + num_bytes)]).unwrap()
177 });
178 }
179
180
181 fn ser_unchecked(b: &mut $crate::criterion::Bencher) {
182 use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
183 const SAMPLES: usize = 1000;
184
185 let mut rng = XorShiftRng::seed_from_u64(1231275789u64);
186
187 let mut v: Vec<_> = (0..SAMPLES)
188 .map(|_| <$projective>::rand(&mut rng))
189 .collect();
190 let v = <$projective>::batch_normalization_into_affine(v.as_mut_slice());
191 let mut bytes = Vec::with_capacity(1000);
192
193 let mut count = 0;
194 b.iter(|| {
195 let tmp = v[count];
196 count = (count + 1) % SAMPLES;
197 bytes.clear();
198 tmp.serialize_unchecked(&mut bytes)
199 });
200 }
201
202 $crate::criterion::criterion_group!(group_ops, bench_curve);
203 };
204}