From 96db6ae7df8bebe6ca564eb71dd4ba8c9190fee4 Mon Sep 17 00:00:00 2001
From: Jonathan Wang <31040440+jonathanpwang@users.noreply.github.com>
Date: Thu, 8 Aug 2024 20:25:13 -0400
Subject: [PATCH 1/4] feat: add openssl-sha256 to benchmark
---
Cargo.lock | 76 +++++++++++++++++++
...abyBearKeccak.toml => BabyBearSha256.toml} | 2 +-
benchmark/src/commands/predicate/mod.rs | 7 ++
benchmark/src/commands/rw/mod.rs | 7 ++
bin/afs/Cargo.toml | 4 +-
bin/afs/src/cli/mod.rs | 17 ++++-
bin/olap/src/cli/mod.rs | 17 ++++-
test-utils/Cargo.toml | 2 +
test-utils/src/config/baby_bear_sha256.rs | 53 +++++++++++++
test-utils/src/config/mod.rs | 3 +
test-utils/src/lib.rs | 2 +
test-utils/src/sha256/mod.rs | 27 +++++++
12 files changed, 206 insertions(+), 11 deletions(-)
rename benchmark/config/single_filter/{BabyBearKeccak.toml => BabyBearSha256.toml} (89%)
create mode 100644 test-utils/src/config/baby_bear_sha256.rs
create mode 100644 test-utils/src/sha256/mod.rs
diff --git a/Cargo.lock b/Cargo.lock
index 2ce6c11690..7e79281a10 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -231,6 +231,7 @@ dependencies = [
"afs-stark-backend",
"derive_more",
"itertools 0.13.0",
+ "openssl",
"p3-air",
"p3-baby-bear",
"p3-blake3",
@@ -1336,6 +1337,21 @@ version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
+[[package]]
+name = "foreign-types"
+version = "0.3.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1"
+dependencies = [
+ "foreign-types-shared",
+]
+
+[[package]]
+name = "foreign-types-shared"
+version = "0.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b"
+
[[package]]
name = "funty"
version = "2.0.0"
@@ -1883,6 +1899,54 @@ version = "11.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b410bbe7e14ab526a0e86877eb47c6996a2bd7746f027ba551028c925390e4e9"
+[[package]]
+name = "openssl"
+version = "0.10.66"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9529f4786b70a3e8c61e11179af17ab6188ad8d0ded78c5529441ed39d4bd9c1"
+dependencies = [
+ "bitflags 2.5.0",
+ "cfg-if",
+ "foreign-types",
+ "libc",
+ "once_cell",
+ "openssl-macros",
+ "openssl-sys",
+]
+
+[[package]]
+name = "openssl-macros"
+version = "0.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn 2.0.66",
+]
+
+[[package]]
+name = "openssl-src"
+version = "300.3.1+3.3.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7259953d42a81bf137fbbd73bd30a8e1914d6dce43c2b90ed575783a22608b91"
+dependencies = [
+ "cc",
+]
+
+[[package]]
+name = "openssl-sys"
+version = "0.9.103"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7f9e8deee91df40a943c71b917e5874b951d32a802526c85721ce3b776c929d6"
+dependencies = [
+ "cc",
+ "libc",
+ "openssl-src",
+ "pkg-config",
+ "vcpkg",
+]
+
[[package]]
name = "overload"
version = "0.1.1"
@@ -2275,6 +2339,12 @@ dependencies = [
"spki",
]
+[[package]]
+name = "pkg-config"
+version = "0.3.30"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec"
+
[[package]]
name = "plotters"
version = "0.3.6"
@@ -3336,6 +3406,12 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d"
+[[package]]
+name = "vcpkg"
+version = "0.2.15"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
+
[[package]]
name = "version_check"
version = "0.9.4"
diff --git a/benchmark/config/single_filter/BabyBearKeccak.toml b/benchmark/config/single_filter/BabyBearSha256.toml
similarity index 89%
rename from benchmark/config/single_filter/BabyBearKeccak.toml
rename to benchmark/config/single_filter/BabyBearSha256.toml
index ae0b5923b7..157d748301 100644
--- a/benchmark/config/single_filter/BabyBearKeccak.toml
+++ b/benchmark/config/single_filter/BabyBearSha256.toml
@@ -12,4 +12,4 @@ num_queries = 290
proof_of_work_bits = 0
[stark_engine]
-engine = "BabyBearKeccak"
+engine = "BabyBearSha256"
diff --git a/benchmark/src/commands/predicate/mod.rs b/benchmark/src/commands/predicate/mod.rs
index aa3949c64c..c67f1f7e01 100644
--- a/benchmark/src/commands/predicate/mod.rs
+++ b/benchmark/src/commands/predicate/mod.rs
@@ -8,11 +8,13 @@ use afs_test_utils::{
baby_bear_bytehash::engine_from_byte_hash,
baby_bear_keccak::BabyBearKeccakEngine,
baby_bear_poseidon2::{self, BabyBearPoseidon2Engine},
+ baby_bear_sha256::BabyBearSha256Engine,
goldilocks_poseidon::{self, GoldilocksPoseidonEngine},
EngineType,
},
engine::StarkEngine,
page_config::PageConfig,
+ sha256::Sha256,
};
use clap::Parser;
use color_eyre::eyre::Result;
@@ -135,6 +137,11 @@ pub fn run_bench_predicate(config: &PageConfig, extra_data: String) -> Result
{
+ let engine: BabyBearSha256Engine =
+ engine_from_byte_hash(Sha256, pcs_log_degree, fri_params);
+ PredicateCommand::bench_all(config, &engine, extra_data)
+ }
EngineType::GoldilocksPoseidon => {
let perm = goldilocks_poseidon::random_perm();
let engine: GoldilocksPoseidonEngine =
diff --git a/benchmark/src/commands/rw/mod.rs b/benchmark/src/commands/rw/mod.rs
index 3576bfc51b..7f220e0a53 100644
--- a/benchmark/src/commands/rw/mod.rs
+++ b/benchmark/src/commands/rw/mod.rs
@@ -11,11 +11,13 @@ use afs_test_utils::{
baby_bear_bytehash::engine_from_byte_hash,
baby_bear_keccak::BabyBearKeccakEngine,
baby_bear_poseidon2::{self, BabyBearPoseidon2Engine},
+ baby_bear_sha256::BabyBearSha256Engine,
goldilocks_poseidon::{self, GoldilocksPoseidonEngine},
EngineType,
},
engine::StarkEngine,
page_config::PageConfig,
+ sha256::Sha256,
};
use clap::Parser;
use color_eyre::eyre::Result;
@@ -141,6 +143,11 @@ pub fn run_bench_rw(config: &PageConfig, extra_data: String) -> Result {
+ let engine: BabyBearSha256Engine =
+ engine_from_byte_hash(Sha256, pcs_log_degree, fri_params);
+ RwCommand::bench_all(config, &engine, extra_data)
+ }
EngineType::GoldilocksPoseidon => {
let perm = goldilocks_poseidon::random_perm();
let engine: GoldilocksPoseidonEngine =
diff --git a/bin/afs/Cargo.toml b/bin/afs/Cargo.toml
index 3c18a4778c..09bbf8e8b0 100644
--- a/bin/afs/Cargo.toml
+++ b/bin/afs/Cargo.toml
@@ -16,14 +16,14 @@ p3-util = { workspace = true }
afs-stark-backend = { path = "../../stark-backend", default-features = false }
afs-derive = { path = "../../derive" }
afs-primitives = { path = "../../primitives", default-features = false }
-afs-test-utils = { path = "../../test-utils" }
+afs-test-utils = { path = "../../test-utils", features = ["sha256"] }
afs-page = { path = "../../db/page", default-features = false }
bin-common = { path = "../common" }
p3-uni-stark = { workspace = true }
p3-field = { workspace = true }
p3-blake3 = { workspace = true }
p3-keccak = { workspace = true }
-p3-maybe-rayon = { workspace = true } # the "parallel" feature is NOT on by default to allow single-threaded benchmarking
+p3-maybe-rayon = { workspace = true } # the "parallel" feature is NOT on by default to allow single-threaded benchmarking
tracing = "0.1.37"
diff --git a/bin/afs/src/cli/mod.rs b/bin/afs/src/cli/mod.rs
index 07ee249525..0d59565639 100644
--- a/bin/afs/src/cli/mod.rs
+++ b/bin/afs/src/cli/mod.rs
@@ -1,13 +1,17 @@
use afs_stark_backend::config::{Com, PcsProof, PcsProverData};
use afs_test_utils::{
config::{
- baby_bear_blake3::BabyBearBlake3Engine, baby_bear_bytehash::engine_from_byte_hash,
- baby_bear_keccak::BabyBearKeccakEngine, baby_bear_poseidon2,
- baby_bear_poseidon2::BabyBearPoseidon2Engine, goldilocks_poseidon,
- goldilocks_poseidon::GoldilocksPoseidonEngine, EngineType,
+ baby_bear_blake3::BabyBearBlake3Engine,
+ baby_bear_bytehash::engine_from_byte_hash,
+ baby_bear_keccak::BabyBearKeccakEngine,
+ baby_bear_poseidon2::{self, BabyBearPoseidon2Engine},
+ baby_bear_sha256::BabyBearSha256Engine,
+ goldilocks_poseidon::{self, GoldilocksPoseidonEngine},
+ EngineType,
},
engine::StarkEngine,
page_config::PageConfig,
+ sha256::Sha256,
};
use clap::{Parser, Subcommand};
use p3_blake3::Blake3;
@@ -142,6 +146,11 @@ pub fn run(config: &PageConfig) {
baby_bear_poseidon2::engine_from_perm(perm, pcs_log_degree, fri_params);
Cli::run_with_engine(config, &engine)
}
+ EngineType::BabyBearSha256 => {
+ let engine: BabyBearSha256Engine =
+ engine_from_byte_hash(Sha256, pcs_log_degree, fri_params);
+ Cli::run_with_engine(config, &engine)
+ }
EngineType::GoldilocksPoseidon => {
let perm = goldilocks_poseidon::random_perm();
let engine: GoldilocksPoseidonEngine =
diff --git a/bin/olap/src/cli/mod.rs b/bin/olap/src/cli/mod.rs
index 406dbf4e45..20798b2ef0 100644
--- a/bin/olap/src/cli/mod.rs
+++ b/bin/olap/src/cli/mod.rs
@@ -1,13 +1,17 @@
use afs_stark_backend::config::{Com, PcsProof, PcsProverData};
use afs_test_utils::{
config::{
- baby_bear_blake3::BabyBearBlake3Engine, baby_bear_bytehash::engine_from_byte_hash,
- baby_bear_keccak::BabyBearKeccakEngine, baby_bear_poseidon2,
- baby_bear_poseidon2::BabyBearPoseidon2Engine, goldilocks_poseidon,
- goldilocks_poseidon::GoldilocksPoseidonEngine, EngineType,
+ baby_bear_blake3::BabyBearBlake3Engine,
+ baby_bear_bytehash::engine_from_byte_hash,
+ baby_bear_keccak::BabyBearKeccakEngine,
+ baby_bear_poseidon2::{self, BabyBearPoseidon2Engine},
+ baby_bear_sha256::BabyBearSha256Engine,
+ goldilocks_poseidon::{self, GoldilocksPoseidonEngine},
+ EngineType,
},
engine::StarkEngine,
page_config::PageConfig,
+ sha256::Sha256,
};
use clap::{Parser, Subcommand};
use p3_blake3::Blake3;
@@ -121,6 +125,11 @@ pub fn run(config: &PageConfig) {
baby_bear_poseidon2::engine_from_perm(perm, pcs_log_degree, fri_params);
Cli::run_with_engine(config, &engine)
}
+ EngineType::BabyBearSha256 => {
+ let engine: BabyBearSha256Engine =
+ engine_from_byte_hash(Sha256, pcs_log_degree, fri_params);
+ Cli::run_with_engine(config, &engine)
+ }
EngineType::GoldilocksPoseidon => {
let perm = goldilocks_poseidon::random_perm();
let engine: GoldilocksPoseidonEngine =
diff --git a/test-utils/Cargo.toml b/test-utils/Cargo.toml
index bb9ed46aaf..02f992e222 100644
--- a/test-utils/Cargo.toml
+++ b/test-utils/Cargo.toml
@@ -26,6 +26,7 @@ zkhash = { workspace = true }
itertools.workspace = true
tracing.workspace = true
+openssl = { version = "0.10", optional = true } # for sha256
serde = { version = "1.0", default-features = false, features = [
"derive",
"alloc",
@@ -41,3 +42,4 @@ afs-stark-backend = { path = "../stark-backend", default-features = false }
[features]
default = []
parallel = ["afs-stark-backend/parallel"]
+sha256 = ["openssl/vendored"]
diff --git a/test-utils/src/config/baby_bear_sha256.rs b/test-utils/src/config/baby_bear_sha256.rs
new file mode 100644
index 0000000000..449cecdcf1
--- /dev/null
+++ b/test-utils/src/config/baby_bear_sha256.rs
@@ -0,0 +1,53 @@
+use afs_stark_backend::{rap::AnyRap, verifier::VerificationError};
+use p3_baby_bear::BabyBear;
+use p3_matrix::{dense::DenseMatrix, Matrix};
+use p3_util::log2_strict_usize;
+
+use super::{
+ baby_bear_bytehash::{
+ self, config_from_byte_hash, BabyBearByteHashConfig, BabyBearByteHashEngine,
+ },
+ fri_params::default_fri_params,
+};
+use crate::{engine::StarkEngine, sha256::Sha256};
+
+pub type BabyBearSha256Config = BabyBearByteHashConfig;
+pub type BabyBearSha256Engine = BabyBearByteHashEngine;
+
+/// `pcs_log_degree` is the upper bound on the log_2(PCS polynomial degree).
+pub fn default_engine(pcs_log_degree: usize) -> BabyBearSha256Engine {
+ baby_bear_bytehash::default_engine(pcs_log_degree, Sha256)
+}
+
+/// `pcs_log_degree` is the upper bound on the log_2(PCS polynomial degree).
+pub fn default_config(pcs_log_degree: usize) -> BabyBearSha256Config {
+ let fri_params = default_fri_params();
+ config_from_byte_hash(Sha256, pcs_log_degree, fri_params)
+}
+
+/// Runs a single end-to-end test for a given set of chips and traces.
+/// This includes proving/verifying key generation, creating a proof, and verifying the proof.
+/// This function should only be used on chips where the main trace is **not** partitioned.
+///
+/// Do not use this if you want to generate proofs for different traces with the same proving key.
+///
+/// - `chips`, `traces`, `public_values` should be zipped.
+pub fn run_simple_test(
+ chips: Vec<&dyn AnyRap>,
+ traces: Vec>,
+ public_values: Vec>,
+) -> Result<(), VerificationError> {
+ let max_trace_height = traces.iter().map(|trace| trace.height()).max().unwrap();
+ let max_log_degree = log2_strict_usize(max_trace_height);
+ let engine = default_engine(max_log_degree);
+ engine.run_simple_test(chips, traces, public_values)
+}
+
+/// [run_simple_test] without public values
+pub fn run_simple_test_no_pis(
+ chips: Vec<&dyn AnyRap>,
+ traces: Vec>,
+) -> Result<(), VerificationError> {
+ let num_chips = chips.len();
+ run_simple_test(chips, traces, vec![vec![]; num_chips])
+}
diff --git a/test-utils/src/config/mod.rs b/test-utils/src/config/mod.rs
index 3c618ba10a..c7121ae58d 100644
--- a/test-utils/src/config/mod.rs
+++ b/test-utils/src/config/mod.rs
@@ -7,6 +7,8 @@ pub mod baby_bear_blake3;
pub mod baby_bear_bytehash;
pub mod baby_bear_keccak;
pub mod baby_bear_poseidon2;
+#[cfg(feature = "sha256")]
+pub mod baby_bear_sha256;
pub mod fri_params;
pub mod goldilocks_poseidon;
pub mod instrument;
@@ -34,6 +36,7 @@ pub struct FriParameters {
pub enum EngineType {
#[default]
BabyBearPoseidon2,
+ BabyBearSha256,
BabyBearBlake3,
BabyBearKeccak,
GoldilocksPoseidon,
diff --git a/test-utils/src/lib.rs b/test-utils/src/lib.rs
index fe98184d26..5e2d242591 100644
--- a/test-utils/src/lib.rs
+++ b/test-utils/src/lib.rs
@@ -2,4 +2,6 @@ pub mod config;
pub mod engine;
pub mod interaction;
pub mod page_config;
+#[cfg(feature = "sha256")]
+pub mod sha256;
pub mod utils;
diff --git a/test-utils/src/sha256/mod.rs b/test-utils/src/sha256/mod.rs
new file mode 100644
index 0000000000..f52c113f33
--- /dev/null
+++ b/test-utils/src/sha256/mod.rs
@@ -0,0 +1,27 @@
+use openssl::sha;
+use p3_symmetric::CryptographicHasher;
+
+/// The SHA2-256 hash function.
+#[derive(Copy, Clone, Debug)]
+pub struct Sha256;
+
+impl CryptographicHasher for Sha256 {
+ fn hash_iter(&self, input: I) -> [u8; 32]
+ where
+ I: IntoIterator- ,
+ {
+ let input = input.into_iter().collect::>();
+ self.hash_iter_slices([input.as_slice()])
+ }
+
+ fn hash_iter_slices<'a, I>(&self, input: I) -> [u8; 32]
+ where
+ I: IntoIterator
- ,
+ {
+ let mut hasher = sha::Sha256::new();
+ for chunk in input.into_iter() {
+ hasher.update(chunk);
+ }
+ hasher.finish()
+ }
+}
From 171c442588ce63745ff9a413cd7f92ee5128799a Mon Sep 17 00:00:00 2001
From: Jonathan Wang <31040440+jonathanpwang@users.noreply.github.com>
Date: Thu, 8 Aug 2024 20:32:19 -0400
Subject: [PATCH 2/4] chore: trigger bench workflow
---
.github/workflows/db.yml | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/.github/workflows/db.yml b/.github/workflows/db.yml
index 16dff07b36..9ec052d701 100644
--- a/.github/workflows/db.yml
+++ b/.github/workflows/db.yml
@@ -5,10 +5,10 @@ on:
branches: ["main"]
pull_request:
branches: ["main"]
- paths:
- - "stark-backend/**"
- - "primitives/**"
- - "db/**"
+ # paths:
+ # - "stark-backend/**"
+ # - "primitives/**"
+ # - "db/**"
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }}
From 4f634be213123251325aba576630058ece525f9b Mon Sep 17 00:00:00 2001
From: Jonathan Wang <31040440+jonathanpwang@users.noreply.github.com>
Date: Thu, 8 Aug 2024 20:32:26 -0400
Subject: [PATCH 3/4] Revert "chore: trigger bench workflow"
This reverts commit 171c442588ce63745ff9a413cd7f92ee5128799a.
---
.github/workflows/db.yml | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/.github/workflows/db.yml b/.github/workflows/db.yml
index 9ec052d701..16dff07b36 100644
--- a/.github/workflows/db.yml
+++ b/.github/workflows/db.yml
@@ -5,10 +5,10 @@ on:
branches: ["main"]
pull_request:
branches: ["main"]
- # paths:
- # - "stark-backend/**"
- # - "primitives/**"
- # - "db/**"
+ paths:
+ - "stark-backend/**"
+ - "primitives/**"
+ - "db/**"
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }}
From aeb4b2a14f4793ec83a65b823857be604bb0356c Mon Sep 17 00:00:00 2001
From: Jonathan Wang <31040440+jonathanpwang@users.noreply.github.com>
Date: Thu, 8 Aug 2024 20:42:39 -0400
Subject: [PATCH 4/4] chore: change bench configs
---
benchmark/src/config/config_gen.rs | 11 ++++++-----
1 file changed, 6 insertions(+), 5 deletions(-)
diff --git a/benchmark/src/config/config_gen.rs b/benchmark/src/config/config_gen.rs
index a4e3b01266..fdea6aae26 100644
--- a/benchmark/src/config/config_gen.rs
+++ b/benchmark/src/config/config_gen.rs
@@ -37,22 +37,23 @@ pub fn generate_configs() -> Vec {
let fri_params_vec = vec![
// fri_params_with_80_bits_of_security()[0],
// fri_params_with_80_bits_of_security()[1],
- fri_params_with_80_bits_of_security()[2],
+ fri_params_with_80_bits_of_security()[3],
// fri_params_with_100_bits_of_security()[0],
// fri_params_with_100_bits_of_security()[1],
];
let idx_bytes_vec = vec![32];
- let data_bytes_vec = vec![32, 256, 1024];
+ let data_bytes_vec = vec![32, 160, 1024];
let height_vec = vec![65536, 262_144, 1_048_576];
// let height_vec = vec![256, 1024]; // Run a mini-benchmark for testing
// max_rw_ops as the number of log_2 of height
- let max_rw_ops_shift_vec = vec![0, 1, 2, 3, 4];
+ let max_rw_ops_shift_vec = vec![0]; // vec![0, 1, 2, 3, 4];
let engine_vec = vec![
- EngineType::BabyBearPoseidon2,
- // EngineType::BabyBearBlake3,
+ // EngineType::BabyBearPoseidon2,
+ EngineType::BabyBearBlake3,
+ EngineType::BabyBearSha256,
// EngineType::BabyBearKeccak,
];