-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
39 changed files
with
1,174 additions
and
688 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
Country,Continent,GDP,GDPP | ||
UnitedStates,NorthAmerica,21137,63543 | ||
China,Asia,14342,10261 | ||
Japan,Asia,5081,40293 | ||
Germany,Europe,3846,46329 | ||
India,Asia,2875,2099 | ||
UnitedKingdom,Europe,2825,42330 | ||
France,Europe,2716,41463 | ||
Italy,Europe,2001,33279 | ||
Brazil,SouthAmerica,1839,8718 | ||
Canada,NorthAmerica,1643,43119 | ||
Russia,EuropeAsia,1637,11229 | ||
SouthKorea,Asia,1622,31489 | ||
Australia,Oceania,1382,53799 | ||
Spain,Europe,1316,28152 | ||
Mexico,NorthAmerica,1265,9958 | ||
Indonesia,Asia,1119,4152 | ||
Netherlands,Europe,902,52477 | ||
SaudiArabia,Asia,793,23206 | ||
Turkey,EuropeAsia,761,9005 | ||
Switzerland,Europe,703,81392 | ||
Argentina,SouthAmerica,449,9921 | ||
Sweden,Europe,528,52073 | ||
Nigeria,Africa,448,2190 | ||
Poland,Europe,594,15673 | ||
Thailand,Asia,509,7306 | ||
SouthAfrica,Africa,350,5883 | ||
Philippines,Asia,402,3685 | ||
Colombia,SouthAmerica,323,6458 | ||
Egypt,Africa,302,3012 | ||
Pakistan,Asia,278,1450 | ||
Bangladesh,Asia,302,1855 | ||
Vietnam,Asia,283,2900 | ||
Chile,SouthAmerica,252,13120 | ||
Finland,Europe,268,48888 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 countries dataset. | ||
//! To run this, use `cargo run --release --example countries`. | ||
//! | ||
//! NOTE: If this doesn't work because you do not have the appropriate GPU drivers installed, | ||
//! you can run `cargo run --release --example countries --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(), | ||
"countries".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/countries/countries_gdp.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 countries_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( | ||
"countries.countries".parse().unwrap(), | ||
OwnedTable::try_from(countries_batch).unwrap(), | ||
0, | ||
); | ||
|
||
prove_and_verify_query( | ||
"SELECT COUNT(*) AS total_countries FROM countries", | ||
&accessor, | ||
&prover_setup, | ||
&verifier_setup, | ||
); | ||
|
||
prove_and_verify_query( | ||
"SELECT country FROM countries WHERE continent = 'Asia'", | ||
&accessor, | ||
&prover_setup, | ||
&verifier_setup, | ||
); | ||
|
||
prove_and_verify_query( | ||
"SELECT country FROM countries WHERE gdp > 500 AND gdp < 1500", | ||
&accessor, | ||
&prover_setup, | ||
&verifier_setup, | ||
); | ||
|
||
prove_and_verify_query( | ||
"SELECT SUM(gdp) AS total_market_cap FROM countries WHERE country = 'China' OR country = 'India'", | ||
&accessor, | ||
&prover_setup, | ||
&verifier_setup, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
//! This is a non-interactive example of using Proof of SQL with an extended books dataset. | ||
//! To run this, use `cargo run --example programming_books`. | ||
//! | ||
//! NOTE: If this doesn't work because you do not have the appropriate GPU drivers installed, | ||
//! you can run `cargo run --example programming_books --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}; | ||
|
||
const DORY_SETUP_MAX_NU: usize = 8; | ||
const DORY_SEED: [u8; 32] = *b"ebab60d58dee4cc69658939b7c2a582d"; | ||
|
||
/// # 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(), | ||
"programming_books".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/programming_books/programming_books.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 books_extra_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( | ||
"programming_books.books".parse().unwrap(), | ||
OwnedTable::try_from(books_extra_batch).unwrap(), | ||
0, | ||
); | ||
|
||
// Query 1: Count the total number of books | ||
prove_and_verify_query( | ||
"SELECT COUNT(*) AS total_books FROM books", | ||
&accessor, | ||
&prover_setup, | ||
&verifier_setup, | ||
); | ||
|
||
// Query 2: Find books with a rating higher than 4.5 | ||
prove_and_verify_query( | ||
"SELECT title, author FROM books WHERE rating > 4.5", | ||
&accessor, | ||
&prover_setup, | ||
&verifier_setup, | ||
); | ||
|
||
// Query 3: List all programming books published after 2000 | ||
prove_and_verify_query( | ||
"SELECT title, publication_year FROM books WHERE genre = 'Programming' AND publication_year > 2000", | ||
&accessor, | ||
&prover_setup, | ||
&verifier_setup, | ||
); | ||
|
||
// Query 4: Find the top 5 authors with the most books | ||
prove_and_verify_query( | ||
"SELECT author, COUNT(*) AS book_count FROM books GROUP BY author ORDER BY book_count DESC LIMIT 5", | ||
&accessor, | ||
&prover_setup, | ||
&verifier_setup, | ||
); | ||
} |
11 changes: 11 additions & 0 deletions
11
crates/proof-of-sql/examples/programming_books/programming_books.csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
title,author,publication_year,genre,rating | ||
The Pragmatic Programmer,Andrew Hunt,1999,Programming,4.5 | ||
Clean Code,Robert C. Martin,2008,Programming,4.7 | ||
The Clean Coder,Robert C. Martin,2011,Programming,4.6 | ||
Design Patterns,Erich Gamma,1994,Software Engineering,4.8 | ||
Refactoring,Martin Fowler,1999,Programming,4.5 | ||
Effective Java,Joshua Bloch,2008,Programming,4.7 | ||
Introduction to Algorithms,Thomas H. Cormen,2009,Computer Science,4.8 | ||
Code Complete,Steve McConnell,2004,Programming,4.6 | ||
The Mythical Man-Month,Fred Brooks,1975,Software Engineering,4.3 | ||
Algorithms,Robert Sedgewick,1983,Computer Science,4.5 |
Oops, something went wrong.