-
Notifications
You must be signed in to change notification settings - Fork 81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: cat breads example #311
Open
SimoneHaddad
wants to merge
2
commits into
main
Choose a base branch
from
feat/cat-breeds-example
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -124,6 +124,8 @@ jobs: | |
run: cargo run --example books | ||
- name: Run brands example | ||
run: cargo run --example brands | ||
- name: Run cat breeds example | ||
run: cargo run --example cat_breeds | ||
- name: Run avocado-prices example | ||
run: cargo run --example avocado-prices | ||
- name: Run plastics example | ||
|
@@ -244,4 +246,4 @@ jobs: | |
- name: Install solhint | ||
run: npm install -g solhint | ||
- name: Run tests | ||
run: solhint -c 'crates/proof-of-sql/.solhint.json' 'crates/proof-of-sql/**/*.sol' -w 0 | ||
run: solhint -c 'crates/proof-of-sql/.solhint.json' 'crates/proof-of-sql/**/*.sol' -w 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NIT: please add a new line here |
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,26 @@ | ||
Name,Origin,LifeSpan,Weight,Temperament,HairLength | ||
Persian,Iran,14.5,4.5,Gentle,Long | ||
Siamese,Thailand,15.0,5.0,Active,Short | ||
Maine Coon,United States,13.0,7.5,Friendly,Long | ||
British Shorthair,United Kingdom,15.0,5.5,Calm,Short | ||
Russian Blue,Russia,15.5,4.0,Gentle,Short | ||
Bengal,United States,14.0,5.5,Energetic,Short | ||
Abyssinian,Ethiopia,13.5,4.0,Playful,Short | ||
Ragdoll,United States,13.5,6.5,Relaxed,Long | ||
Sphynx,Canada,14.0,4.0,Affectionate,Hairless | ||
Norwegian Forest,Norway,14.5,6.0,Independent,Long | ||
Scottish Fold,United Kingdom,13.0,4.0,Sweet,Short | ||
American Shorthair,United States,15.0,5.0,Adaptable,Short | ||
Birman,Myanmar,14.0,4.5,Gentle,Long | ||
Devon Rex,United Kingdom,13.5,3.5,Active,Short | ||
Oriental Shorthair,Thailand,14.0,4.5,Social,Short | ||
Turkish Angora,Turkey,13.5,4.0,Intelligent,Long | ||
Burmese,Myanmar,15.0,4.5,Affectionate,Short | ||
Siberian,Russia,13.5,6.0,Friendly,Long | ||
Egyptian Mau,Egypt,14.0,4.0,Athletic,Short | ||
Japanese Bobtail,Japan,13.0,4.5,Energetic,Medium | ||
Exotic Shorthair,United States,12.5,5.0,Calm,Short | ||
Tonkinese,Canada,14.5,4.0,Social,Short | ||
Cornish Rex,United Kingdom,13.0,3.5,Playful,Short | ||
Chartreux,France,13.5,5.0,Quiet,Short | ||
Himalayan,United States,12.5,5.5,Gentle,Long |
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,137 @@ | ||
//! This is a non-interactive example of using Proof of SQL with a cat breeds dataset. | ||
//! To run this, use `cargo run --release --example cat_breeds`. | ||
//! | ||
//! NOTE: If this doesn't work because you do not have the appropriate GPU drivers installed, | ||
//! you can run `cargo run --release --example cat_breeds --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"4c3a7t5b9r3e2d1s8f6k9m2n5p7q4w9j"; | ||
|
||
/// # 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(), | ||
"cats".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/cat_breeds/cat_breeds.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 cat_breeds_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( | ||
"cats.breeds".parse().unwrap(), | ||
OwnedTable::try_from(cat_breeds_batch).unwrap(), | ||
0, | ||
); | ||
|
||
// Query 1: Calculate average weight by country of origin | ||
prove_and_verify_query( | ||
"SELECT Origin, SUM(Weight)/COUNT(*) AS avg_weight FROM breeds GROUP BY Origin ORDER BY avg_weight DESC", | ||
&accessor, | ||
&prover_setup, | ||
&verifier_setup, | ||
); | ||
|
||
// Query 2: Find top 5 longest living cat breeds | ||
prove_and_verify_query( | ||
"SELECT Name, LifeSpan FROM breeds ORDER BY LifeSpan DESC LIMIT 5", | ||
&accessor, | ||
&prover_setup, | ||
&verifier_setup, | ||
); | ||
|
||
// Query 3: Count breeds by hair length | ||
prove_and_verify_query( | ||
"SELECT HairLength, COUNT(*) as breed_count FROM breeds GROUP BY HairLength ORDER BY breed_count DESC", | ||
&accessor, | ||
&prover_setup, | ||
&verifier_setup, | ||
); | ||
|
||
// Query 4: Find gentle breeds that live longer than 14 years | ||
prove_and_verify_query( | ||
"SELECT Name, Origin, LifeSpan FROM breeds WHERE Temperament = 'Gentle' AND LifeSpan > 14.0 ORDER BY LifeSpan DESC", | ||
&accessor, | ||
&prover_setup, | ||
&verifier_setup, | ||
); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NIT: please add a new line here, this is likely fixed by running
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change is no longer needed and can safely be reverted