Skip to content

Commit

Permalink
test(benchmark): benchmark only the permutation part for consitency w…
Browse files Browse the repository at this point in the history
…ith Poseidon2

update the Poseidon benchmark to measure only the permutation part,
aligning with the benchmarking approach used in Horizen and Plonky3
for consistency. This change ensures that benchmarking
across different implementations is standardized and comparable.
  • Loading branch information
chokobole committed May 14, 2024
1 parent 4c207f2 commit 970bd23
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 72 deletions.
6 changes: 3 additions & 3 deletions Cargo.Bazel.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"checksum": "aff93f09e6d25ede7e662cd0296a5145b6365cc76ad58cea5a2f560b10b75bf7",
"checksum": "f90b6ff89b5b13e14ad4372e91a7511217c49f5680c90b63c0d9a2c8b9e4d62e",
"crates": {
"ahash 0.8.3": {
"name": "ahash",
Expand Down Expand Up @@ -170,9 +170,9 @@
"version": "0.4.0",
"repository": {
"Git": {
"remote": "https://github.com/arkworks-rs/crypto-primitives.git",
"remote": "https://github.com/kroma-network/crypto-primitives.git",
"commitish": {
"Tag": "v0.4.0"
"Rev": "99f5aff"
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified benchmark/poseidon/Poseidon Benchmark.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 14 additions & 14 deletions benchmark/poseidon/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ CPU Caches:
```

```shell
bazel run -c opt --//:has_openmp --//:has_rtti --//:has_matplotlib //benchmark/poseidon:poseidon_benchmark -- -s 10000 -a 10000
bazel run -c opt --//:has_openmp --//:has_rtti --//:has_matplotlib //benchmark/poseidon:poseidon_benchmark
```

| Repetition | Tachyon | Arkworks |
| :--------: | -------- | ------------ |
| 0 | 0.365703 | **0.26695** |
| 1 | 0.383136 | **0.267033** |
| 2 | 0.383506 | **0.272285** |
| 3 | 0.374358 | **0.257264** |
| 4 | 0.370592 | **0.25442** |
| 5 | 0.371024 | **0.2545** |
| 6 | 0.373796 | **0.263127** |
| 7 | 0.375338 | **0.27176** |
| 8 | 0.374944 | **0.267881** |
| 9 | 0.374335 | **0.262994** |
| avg | 0.374673 | **0.263821** |
| Repetition | Tachyon | Arkworks |
| :--------: | -------- | ------------- |
| 0 | 0.000124 | **0.000114** |
| 1 | 0.000125 | **0.00011** |
| 2 | 0.000124 | **0.00011** |
| 3 | 0.000124 | **0.000106** |
| 4 | 0.000124 | **0.00011** |
| 5 | 0.000124 | **0.000109** |
| 6 | 0.000125 | **0.000108** |
| 7 | 0.000123 | **0.00011** |
| 8 | 0.000123 | **0.000109** |
| 9 | 0.000124 | **0.000106** |
| avg | 0.000124 | **0.0001092** |

![image](/benchmark/poseidon/Poseidon%20Benchmark.png)
4 changes: 2 additions & 2 deletions benchmark/poseidon/arkworks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ publish = false
[dependencies]
ark-ff = { version = "^0.4.0", default-features = false }
ark-bn254 = "0.4.0"
ark-crypto-primitives = { git = "https://github.com/arkworks-rs/crypto-primitives.git", features = [
ark-crypto-primitives = { git = "https://github.com/kroma-network/crypto-primitives.git", features = [
"sponge",
], tag = "v0.4.0" }
], rev = "99f5aff" }
tachyon_rs = { path = "../../../tachyon/rs" }
24 changes: 5 additions & 19 deletions benchmark/poseidon/arkworks/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
use ark_bn254::Fr;
use ark_crypto_primitives::sponge::poseidon::traits::find_poseidon_ark_and_mds;
use ark_crypto_primitives::sponge::poseidon::{PoseidonConfig, PoseidonSponge};
use ark_crypto_primitives::sponge::{CryptographicSponge, FieldBasedCryptographicSponge};
use std::{mem, slice, time::Instant};
use ark_crypto_primitives::sponge::CryptographicSponge;
use std::time::Instant;
use tachyon_rs::math::elliptic_curves::bn::bn254::Fr as CppFr;

#[no_mangle]
pub extern "C" fn run_poseidon_arkworks(
pre_images: *const CppFr,
absorbing_num: usize,
squeezing_num: usize,
duration: *mut u64,
) -> *mut CppFr {
pub extern "C" fn run_poseidon_arkworks(duration: *mut u64) -> *mut CppFr {
let (ark, mds) = find_poseidon_ark_and_mds::<Fr>(254, 8, 8, 63, 0);
let poseidon_config = PoseidonConfig {
full_rounds: 8,
Expand All @@ -23,22 +18,13 @@ pub extern "C" fn run_poseidon_arkworks(
capacity: 1,
};

let pre_images = unsafe {
let pre_images: &[CppFr] = slice::from_raw_parts(pre_images, absorbing_num);
let pre_images: &[Fr] = mem::transmute(pre_images);

pre_images
};
let mut sponge = PoseidonSponge::new(&poseidon_config);

let start = Instant::now();
for pre_image in pre_images {
sponge.absorb(pre_image);
}
let squeezed_elements = sponge.squeeze_native_field_elements(squeezing_num);
sponge.permute();
unsafe {
duration.write(start.elapsed().as_micros() as u64);
}

Box::into_raw(Box::new(squeezed_elements[0])) as *mut CppFr
Box::into_raw(Box::new(sponge.state[1])) as *mut CppFr
}
4 changes: 1 addition & 3 deletions benchmark/poseidon/poseidon_benchmark.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ using namespace crypto;

using Field = math::bn254::Fr;

extern "C" tachyon_bn254_fr* run_poseidon_arkworks(
const tachyon_bn254_fr* pre_images, size_t aborbing_num,
size_t squeezing_num, uint64_t* duration);
extern "C" tachyon_bn254_fr* run_poseidon_arkworks(uint64_t* duration);

int RealMain(int argc, char** argv) {
tachyon::PoseidonConfig config;
Expand Down
31 changes: 11 additions & 20 deletions benchmark/poseidon/poseidon_benchmark_runner.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,52 +26,43 @@ class PoseidonBenchmarkRunner {
public:
using CPrimeField = typename c::base::TypeTraits<Field>::CType;

typedef CPrimeField* (*PoseidonExternalFn)(const CPrimeField* pre_images,
size_t absorbing_num,
size_t squeezing_num,
uint64_t* duration);
typedef CPrimeField* (*PoseidonExternalFn)(uint64_t* duration);

PoseidonBenchmarkRunner(SimplePoseidonBenchmarkReporter* reporter,
PoseidonConfig* config)
: reporter_(reporter), config_(config) {
pre_images_ = base::CreateVector(config->absorbing_num(),
[]() { return Field::Random(); });
}
: reporter_(reporter), config_(config) {}

Field Run() {
std::vector<Field> squeezed_elements;
Field ret;
for (size_t i = 0; i < config_->repeating_num(); ++i) {
crypto::PoseidonConfig<Field> config =
crypto::PoseidonConfig<Field>::CreateCustom(8, 5, 8, 63, 0);
crypto::PoseidonSponge<Field> sponge(config);
base::TimeTicks start = base::TimeTicks::Now();
for (size_t j = 0; j < config_->absorbing_num(); ++j) {
sponge.Absorb(pre_images_[j]);
}
squeezed_elements = sponge.SqueezeFieldElements(config_->squeezing_num());
sponge.Permute();
reporter_->AddTime(i, (base::TimeTicks::Now() - start).InSecondsF());
if (i == 0) {
ret = sponge.state.elements[1];
}
}
return squeezed_elements[0];
return ret;
}

Field RunExternal(PoseidonExternalFn fn) {
std::unique_ptr<CPrimeField> last_squeezed_element;
std::unique_ptr<CPrimeField> ret;
for (size_t i = 0; i < config_->repeating_num(); ++i) {
uint64_t duration_in_us;
last_squeezed_element.reset(fn(
reinterpret_cast<const CPrimeField*>(pre_images_.data()),
config_->absorbing_num(), config_->squeezing_num(), &duration_in_us));
ret.reset(fn(&duration_in_us));
reporter_->AddTime(i, base::Microseconds(duration_in_us).InSecondsF());
}
return *reinterpret_cast<Field*>(last_squeezed_element.get());
return *reinterpret_cast<Field*>(ret.get());
}

private:
// not owned
SimplePoseidonBenchmarkReporter* const reporter_;
// not owned
PoseidonConfig* const config_;
std::vector<Field> pre_images_;
};

} // namespace tachyon
Expand Down
6 changes: 0 additions & 6 deletions benchmark/poseidon/poseidon_config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ bool PoseidonConfig::Parse(int argc, char** argv) {
parser.AddFlag<base::Flag<size_t>>(&repeating_num_)
.set_short_name("-n")
.set_help("Specify the number of repetition 'n'. By default, 10.");
parser.AddFlag<base::Flag<size_t>>(&absorbing_num_)
.set_short_name("-a")
.set_help("Specify the number of absorptions 'a'. By default, 100.");
parser.AddFlag<base::Flag<size_t>>(&squeezing_num_)
.set_short_name("-s")
.set_help("Specify the number of squeeze 's'. By default, 100.");
parser.AddFlag<base::Flag<bool>>(&check_results_)
.set_long_name("--check_results")
.set_help("Whether checks results generated by each poseidon runner.");
Expand Down
4 changes: 0 additions & 4 deletions benchmark/poseidon/poseidon_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,12 @@ class PoseidonConfig {

bool check_results() const { return check_results_; }
size_t repeating_num() const { return repeating_num_; }
size_t absorbing_num() const { return absorbing_num_; }
size_t squeezing_num() const { return squeezing_num_; }

bool Parse(int argc, char** argv);

private:
bool check_results_ = false;
size_t repeating_num_ = 10;
size_t absorbing_num_ = 100;
size_t squeezing_num_ = 100;
};

} // namespace tachyon
Expand Down

0 comments on commit 970bd23

Please sign in to comment.