Skip to content

Commit 42c0cd5

Browse files
committed
ed448-goldilocks: add basic benchmark suite
1 parent 220ea81 commit 42c0cd5

File tree

3 files changed

+187
-0
lines changed

3 files changed

+187
-0
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ed448-goldilocks/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,14 @@ signing = ["dep:ed448", "dep:signature"]
3737
serde = ["dep:serdect", "ed448?/serde_bytes"]
3838

3939
[dev-dependencies]
40+
criterion = { version = "0.7", default-features = false, features = ["cargo_bench_support"] }
4041
hex-literal = "1"
4142
hex = "0.4"
4243
rand_core = { version = "0.9", features = ["os_rng"] }
4344
rand_chacha = "0.9"
4445
serde_bare = "0.5"
4546
serde_json = "1.0"
47+
48+
[[bench]]
49+
harness = false
50+
name = "bench"

ed448-goldilocks/benches/bench.rs

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
use criterion::{BatchSize, Criterion, criterion_group, criterion_main};
2+
use ed448_goldilocks::{
3+
CompressedDecaf, CompressedEdwardsY, Decaf448, DecafPoint, DecafScalar, EdwardsPoint,
4+
EdwardsScalar, MontgomeryPoint,
5+
};
6+
use elliptic_curve::{Field, Group};
7+
use hash2curve::{ExpandMsgXof, GroupDigest};
8+
use rand_core::{OsRng, TryRngCore};
9+
use sha3::Shake256;
10+
11+
pub fn ed448(c: &mut Criterion) {
12+
let mut group = c.benchmark_group("Ed448");
13+
14+
group.bench_function("scalar multiplication", |b| {
15+
b.iter_batched(
16+
|| {
17+
let point = EdwardsPoint::try_from_rng(&mut OsRng).unwrap();
18+
let scalar = EdwardsScalar::try_from_rng(&mut OsRng).unwrap();
19+
(point, scalar)
20+
},
21+
|(point, scalar)| point * scalar,
22+
BatchSize::SmallInput,
23+
)
24+
});
25+
26+
group.bench_function("point addition", |b| {
27+
b.iter_batched(
28+
|| {
29+
let p1 = EdwardsPoint::try_from_rng(&mut OsRng).unwrap();
30+
let p2 = EdwardsPoint::try_from_rng(&mut OsRng).unwrap();
31+
(p1, p2)
32+
},
33+
|(p1, p2)| p1 + p2,
34+
BatchSize::SmallInput,
35+
)
36+
});
37+
38+
group.bench_function("hash_to_curve", |b| {
39+
b.iter_batched(
40+
|| {
41+
let mut msg = [0; 64];
42+
OsRng.try_fill_bytes(&mut msg).unwrap();
43+
msg
44+
},
45+
|msg| EdwardsPoint::hash_with_defaults(&msg),
46+
BatchSize::SmallInput,
47+
)
48+
});
49+
50+
group.bench_function("encode_to_curve", |b| {
51+
b.iter_batched(
52+
|| {
53+
let mut msg = [0; 64];
54+
OsRng.try_fill_bytes(&mut msg).unwrap();
55+
msg
56+
},
57+
|msg| EdwardsPoint::encode_with_defaults(&msg),
58+
BatchSize::SmallInput,
59+
)
60+
});
61+
62+
group.bench_function("compress", |b| {
63+
b.iter_batched(
64+
|| EdwardsPoint::try_from_rng(&mut OsRng).unwrap(),
65+
|point| point.compress().0,
66+
BatchSize::SmallInput,
67+
)
68+
});
69+
70+
group.bench_function("decompress", |b| {
71+
b.iter_batched(
72+
|| EdwardsPoint::try_from_rng(&mut OsRng).unwrap().compress().0,
73+
|bytes| CompressedEdwardsY(bytes).decompress().unwrap(),
74+
BatchSize::SmallInput,
75+
)
76+
});
77+
78+
group.finish();
79+
}
80+
81+
pub fn decaf448(c: &mut Criterion) {
82+
let mut group = c.benchmark_group("Decaf448");
83+
84+
group.bench_function("scalar multiplication", |b| {
85+
b.iter_batched(
86+
|| {
87+
let point = DecafPoint::try_from_rng(&mut OsRng).unwrap();
88+
let scalar = DecafScalar::try_from_rng(&mut OsRng).unwrap();
89+
(point, scalar)
90+
},
91+
|(point, scalar)| point * scalar,
92+
BatchSize::SmallInput,
93+
)
94+
});
95+
96+
group.bench_function("point addition", |b| {
97+
b.iter_batched(
98+
|| {
99+
let p1 = DecafPoint::try_from_rng(&mut OsRng).unwrap();
100+
let p2 = DecafPoint::try_from_rng(&mut OsRng).unwrap();
101+
(p1, p2)
102+
},
103+
|(p1, p2)| p1 + p2,
104+
BatchSize::SmallInput,
105+
)
106+
});
107+
108+
group.bench_function("hash_to_curve", |b| {
109+
b.iter_batched(
110+
|| {
111+
let mut msg = [0; 64];
112+
OsRng.try_fill_bytes(&mut msg).unwrap();
113+
msg
114+
},
115+
|msg| {
116+
Decaf448::hash_from_bytes::<ExpandMsgXof<Shake256>>(
117+
&[&msg],
118+
&[b"decaf448_XOF:SHAKE256_D448MAP_RO_"],
119+
)
120+
},
121+
BatchSize::SmallInput,
122+
)
123+
});
124+
125+
group.bench_function("encode_to_curve", |b| {
126+
b.iter_batched(
127+
|| {
128+
let mut msg = [0; 64];
129+
OsRng.try_fill_bytes(&mut msg).unwrap();
130+
msg
131+
},
132+
|msg| {
133+
Decaf448::encode_from_bytes::<ExpandMsgXof<Shake256>>(
134+
&[&msg],
135+
&[b"decaf448_XOF:SHAKE256_D448MAP_NU_"],
136+
)
137+
},
138+
BatchSize::SmallInput,
139+
)
140+
});
141+
142+
group.bench_function("compress", |b| {
143+
b.iter_batched(
144+
|| DecafPoint::try_from_rng(&mut OsRng).unwrap(),
145+
|point| point.compress().0,
146+
BatchSize::SmallInput,
147+
)
148+
});
149+
150+
group.bench_function("decompress", |b| {
151+
b.iter_batched(
152+
|| DecafPoint::try_from_rng(&mut OsRng).unwrap().compress().0,
153+
|bytes| CompressedDecaf(bytes).decompress().unwrap(),
154+
BatchSize::SmallInput,
155+
)
156+
});
157+
158+
group.finish();
159+
}
160+
161+
pub fn curve448(c: &mut Criterion) {
162+
let mut group = c.benchmark_group("Curve448");
163+
164+
group.bench_function("scalar multiplication", |b| {
165+
b.iter_batched(
166+
|| {
167+
let mut point = MontgomeryPoint::default();
168+
OsRng.try_fill_bytes(&mut point.0).unwrap();
169+
let scalar = EdwardsScalar::try_from_rng(&mut OsRng).unwrap();
170+
(point, scalar)
171+
},
172+
|(point, scalar)| &point * &scalar,
173+
BatchSize::SmallInput,
174+
)
175+
});
176+
177+
group.finish();
178+
}
179+
180+
criterion_group!(benches, ed448, decaf448, curve448);
181+
criterion_main!(benches);

0 commit comments

Comments
 (0)