Skip to content

Commit

Permalink
feat: rockets example (#306)
Browse files Browse the repository at this point in the history
# Rationale for this change

Add an additional rockets example to help improve documentation.

# What changes are included in this PR?

Adding an example with a table of launch vehicles.

# Are these changes tested?

Yes.
  • Loading branch information
JayWhite2357 authored Oct 28, 2024
2 parents a0fd293 + f2d6573 commit 3c666a1
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/lint-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ jobs:
run: cargo run --example sushi
- name: Run countries example
run: cargo run --example countries
- name: Run rockets example
run: cargo run --example rockets
- name: Run posql_db example (With Blitzar)
run: bash crates/proof-of-sql/examples/posql_db/run_example.sh
- name: Run posql_db example (Without Blitzar)
Expand Down
4 changes: 4 additions & 0 deletions crates/proof-of-sql/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ required-features = [ "arrow" ]
name = "countries"
required-features = [ "arrow" ]

[[example]]
name = "rockets"
required-features = [ "arrow" ]

[[bench]]
name = "posql_benches"
harness = false
Expand Down
28 changes: 28 additions & 0 deletions crates/proof-of-sql/examples/rockets/launch_vehicles.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name,country,year,mtow
Saturn V,USA,1967,2976000
Falcon Heavy,USA,2018,1420788
Space Shuttle,USA,1981,2041167
Energia,USSR,1987,2400000
Ariane 5,Europe,1996,780000
Delta IV Heavy,USA,2004,733400
Long March 5,China,2016,869000
Proton,USSR/Russia,1965,705000
Atlas V,USA,2002,546700
H-IIA,Japan,2001,445000
Soyuz,USSR/Russia,1966,308000
Falcon 9,USA,2010,549054
Vega,Europe,2012,137000
PSLV,India,1993,320000
GSLV Mk III,India,2017,640000
Titan II,USA,1962,153800
Angara A5,Russia,2014,1335000
Delta II,USA,1989,231870
Electron,New Zealand,2017,12500
Antares,USA,2013,240000
Zenit,USSR/Ukraine,1985,462000
N1,USSR,1969,2735000
New Glenn,USA,2024,1300000
Redstone,USA,1953,29500
Black Arrow,UK,1971,18800
Diamant,France,1965,18000
Pegasus,USA,1990,23300
132 changes: 132 additions & 0 deletions crates/proof-of-sql/examples/rockets/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
//! This is a non-interactive example of using Proof of SQL with a rockets dataset.
//! To run this, use `cargo run --release --example rockets`.
//!
//! NOTE: If this doesn't work because you do not have the appropriate GPU drivers installed,
//! you can run `cargo run --release --example rockets --no-default-features --features="arrow cpu-perf"` instead. It will be slower for proof generation.
use arrow::datatypes::SchemaRef;
use arrow_csv::{infer_schema_from_files, ReaderBuilder};
use proof_of_sql::{
base::database::{
arrow_schema_utility::get_posql_compatible_schema, OwnedTable, OwnedTableTestAccessor,
TestAccessor,
},
proof_primitive::dory::{
DynamicDoryCommitment, DynamicDoryEvaluationProof, ProverSetup, PublicParameters,
VerifierSetup,
},
sql::{parse::QueryExpr, postprocessing::apply_postprocessing_steps, proof::QueryProof},
};
use rand::{rngs::StdRng, SeedableRng};
use std::{fs::File, time::Instant};

// We generate the public parameters and the setups used by the prover and verifier for the Dory PCS.
// The `max_nu` should be set such that the maximum table size is less than `2^(2*max_nu-1)`.
const DORY_SETUP_MAX_NU: usize = 8;
// This should be a "nothing-up-my-sleeve" phrase or number.
const DORY_SEED: [u8; 32] = *b"7a1b3c8d2e4f9g6h5i0j7k2l8m3n9o1p";

/// # Panics
/// Will panic if the query does not parse or the proof fails to verify.
fn prove_and_verify_query(
sql: &str,
accessor: &OwnedTableTestAccessor<DynamicDoryEvaluationProof>,
prover_setup: &ProverSetup,
verifier_setup: &VerifierSetup,
) {
// Parse the query:
println!("Parsing the query: {sql}...");
let now = Instant::now();
let query_plan = QueryExpr::<DynamicDoryCommitment>::try_new(
sql.parse().unwrap(),
"rockets".parse().unwrap(),
accessor,
)
.unwrap();
println!("Done in {} ms.", now.elapsed().as_secs_f64() * 1000.);

// Generate the proof and result:
print!("Generating proof...");
let now = Instant::now();
let (proof, provable_result) = QueryProof::<DynamicDoryEvaluationProof>::new(
query_plan.proof_expr(),
accessor,
&prover_setup,
);
println!("Done in {} ms.", now.elapsed().as_secs_f64() * 1000.);

// Verify the result with the proof:
print!("Verifying proof...");
let now = Instant::now();
let result = proof
.verify(
query_plan.proof_expr(),
accessor,
&provable_result,
&verifier_setup,
)
.unwrap();
let result = apply_postprocessing_steps(result.table, query_plan.postprocessing());
println!("Verified in {} ms.", now.elapsed().as_secs_f64() * 1000.);

// Display the result
println!("Query Result:");
println!("{result:?}");
}

fn main() {
let mut rng = StdRng::from_seed(DORY_SEED);
let public_parameters = PublicParameters::rand(DORY_SETUP_MAX_NU, &mut rng);
let prover_setup = ProverSetup::from(&public_parameters);
let verifier_setup = VerifierSetup::from(&public_parameters);

let filename = "./crates/proof-of-sql/examples/rockets/launch_vehicles.csv";
let inferred_schema =
SchemaRef::new(infer_schema_from_files(&[filename.to_string()], b',', None, true).unwrap());
let posql_compatible_schema = get_posql_compatible_schema(&inferred_schema);

let rockets_batch = ReaderBuilder::new(posql_compatible_schema)
.with_header(true)
.build(File::open(filename).unwrap())
.unwrap()
.next()
.unwrap()
.unwrap();

// Load the table into an "Accessor" so that the prover and verifier can access the data/commitments.
let mut accessor =
OwnedTableTestAccessor::<DynamicDoryEvaluationProof>::new_empty_with_setup(&prover_setup);
accessor.add_table(
"rockets.launch_vehicles".parse().unwrap(),
OwnedTable::try_from(rockets_batch).unwrap(),
0,
);

prove_and_verify_query(
"SELECT COUNT(*) AS total_rockets FROM launch_vehicles",
&accessor,
&prover_setup,
&verifier_setup,
);

prove_and_verify_query(
"SELECT country, MAX(mtow) as max_mtow, COUNT(*) as rocket_count FROM launch_vehicles GROUP BY country ORDER BY max_mtow DESC",
&accessor,
&prover_setup,
&verifier_setup,
);

prove_and_verify_query(
"SELECT name FROM launch_vehicles WHERE country = 'USA'",
&accessor,
&prover_setup,
&verifier_setup,
);

prove_and_verify_query(
"SELECT name FROM launch_vehicles WHERE mtow > 100000 and mtow < 150000",
&accessor,
&prover_setup,
&verifier_setup,
);
}

0 comments on commit 3c666a1

Please sign in to comment.