Skip to content

Commit

Permalink
Stark: Prover and Verifier over field extensions v2 (#717)
Browse files Browse the repository at this point in the history
* wip

* cast to fieldextension

* fmt

* clippy

* fmt

* fix parallel

* prover with generic air argument

* verifier with generic argument

* clippy, fmt

* wip, prover refactor

* wip

* fix stone serialization

* add safe scope for unwraps

* minor refactor

* clippy, fmt

* remove commented code. Fix typo in docs

* fix compilation error

* fmt

* fix parallel

* remove unnecessary trait bound

* fix wasm

* fix wasm

* change wasm proof bytes

* fix number of bytes

* fix wasm proof

* fix number of bytes
  • Loading branch information
schouhy authored Dec 20, 2023
1 parent 703db8a commit fb44319
Show file tree
Hide file tree
Showing 18 changed files with 1,874 additions and 1,789 deletions.
9 changes: 7 additions & 2 deletions provers/cairo/benches/criterion_verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@ use stark_platinum_prover::proof::{

pub mod functions;

fn load_proof_and_pub_inputs(input_path: &str) -> (StarkProof<Stark252PrimeField>, PublicInputs) {
fn load_proof_and_pub_inputs(
input_path: &str,
) -> (
StarkProof<Stark252PrimeField, Stark252PrimeField>,
PublicInputs,
) {
let program_content = std::fs::read(input_path).unwrap();
let mut bytes = program_content.as_slice();
let proof_len = usize::from_be_bytes(bytes[0..8].try_into().unwrap());
bytes = &bytes[8..];
let proof: StarkProof<Stark252PrimeField> =
let proof: StarkProof<Stark252PrimeField, Stark252PrimeField> =
serde_cbor::from_slice(&bytes[0..proof_len]).unwrap();
bytes = &bytes[proof_len..];

Expand Down
9 changes: 7 additions & 2 deletions provers/cairo/benches/criterion_verifier_70k.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ use stark_platinum_prover::proof::{

pub mod functions;

fn load_proof_and_pub_inputs(input_path: &str) -> (StarkProof<Stark252PrimeField>, PublicInputs) {
fn load_proof_and_pub_inputs(
input_path: &str,
) -> (
StarkProof<Stark252PrimeField, Stark252PrimeField>,
PublicInputs,
) {
let program_content = std::fs::read(input_path).unwrap();
let mut bytes = program_content.as_slice();
let proof_len = usize::from_be_bytes(bytes[0..8].try_into().unwrap());
bytes = &bytes[8..];
let proof: StarkProof<Stark252PrimeField> =
let proof: StarkProof<Stark252PrimeField, Stark252PrimeField> =
serde_cbor::from_slice(&bytes[0..proof_len]).unwrap();
bytes = &bytes[proof_len..];

Expand Down
11 changes: 6 additions & 5 deletions provers/cairo/src/air.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1275,8 +1275,8 @@ pub fn generate_cairo_proof(
trace: &TraceTable<Stark252PrimeField>,
pub_input: &PublicInputs,
proof_options: &ProofOptions,
) -> Result<StarkProof<Stark252PrimeField>, ProvingError> {
Prover::prove::<CairoAIR>(
) -> Result<StarkProof<Stark252PrimeField, Stark252PrimeField>, ProvingError> {
Prover::<CairoAIR>::prove(
trace,
pub_input,
proof_options,
Expand All @@ -1288,11 +1288,11 @@ pub fn generate_cairo_proof(
/// concrete types.
/// The field is set to Stark252PrimeField and the AIR to CairoAIR.
pub fn verify_cairo_proof(
proof: &StarkProof<Stark252PrimeField>,
proof: &StarkProof<Stark252PrimeField, Stark252PrimeField>,
pub_input: &PublicInputs,
proof_options: &ProofOptions,
) -> bool {
Verifier::verify::<CairoAIR>(
Verifier::<CairoAIR>::verify(
proof,
pub_input,
proof_options,
Expand Down Expand Up @@ -1481,7 +1481,8 @@ mod prop_test {

// At this point, the verifier only knows about the serialized proof, the proof options
// and the public inputs.
let proof: StarkProof<Stark252PrimeField> = serde_cbor::from_slice(&proof_bytes).unwrap();
let proof: StarkProof<Stark252PrimeField, Stark252PrimeField> =
serde_cbor::from_slice(&proof_bytes).unwrap();

// The proof is verified successfully.
assert!(verify_cairo_proof(&proof, &pub_inputs, &proof_options));
Expand Down
14 changes: 10 additions & 4 deletions provers/cairo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ fn try_compile(program_path: &String, out_file_path: &String) -> Result<(), Erro
fn generate_proof(
input_path: &String,
proof_options: &ProofOptions,
) -> Option<(StarkProof<Stark252PrimeField>, PublicInputs)> {
) -> Option<(
StarkProof<Stark252PrimeField, Stark252PrimeField>,
PublicInputs,
)> {
let timer = Instant::now();

let Ok(program_content) = std::fs::read(input_path) else {
Expand Down Expand Up @@ -145,7 +148,10 @@ fn generate_proof_from_trace(
trace_bin_path: &str,
memory_bin_path: &str,
proof_options: &ProofOptions,
) -> Option<(StarkProof<Stark252PrimeField>, PublicInputs)> {
) -> Option<(
StarkProof<Stark252PrimeField, Stark252PrimeField>,
PublicInputs,
)> {
// ## Generating the prover args
let timer = Instant::now();
let Ok((main_trace, pub_inputs)) =
Expand All @@ -172,7 +178,7 @@ fn generate_proof_from_trace(
}

fn verify_proof(
proof: StarkProof<Stark252PrimeField>,
proof: StarkProof<Stark252PrimeField, Stark252PrimeField>,
pub_inputs: PublicInputs,
proof_options: &ProofOptions,
) -> bool {
Expand All @@ -192,7 +198,7 @@ fn verify_proof(
}

fn write_proof(
proof: StarkProof<Stark252PrimeField>,
proof: StarkProof<Stark252PrimeField, Stark252PrimeField>,
pub_inputs: PublicInputs,
proof_path: String,
) {
Expand Down
14 changes: 9 additions & 5 deletions provers/cairo/src/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,19 @@ fn test_verifier_rejects_wrong_authentication_paths() {

// Change order of authentication path hashes
let query = 0;
let merkle_tree = 0;
let mut original_path = proof.deep_poly_openings[query].lde_trace_merkle_proofs[merkle_tree]
let mut original_path = proof.deep_poly_openings[query]
.main_trace_polys
.proof
.merkle_path
.clone();
original_path.swap(0, 1);
// For the test to make sense, we have to make sure
// that the two hashes are different.
assert_ne!(original_path[0], original_path[1]);
proof.deep_poly_openings[query].lde_trace_merkle_proofs[merkle_tree].merkle_path =
original_path;
proof.deep_poly_openings[query]
.main_trace_polys
.proof
.merkle_path = original_path;

// Verifier should reject the proof
assert!(!verify_cairo_proof(&proof, &pub_inputs, &proof_options));
Expand Down Expand Up @@ -182,7 +185,8 @@ fn deserialize_and_verify() {

// At this point, the verifier only knows about the serialized proof, the proof options
// and the public inputs.
let proof: StarkProof<Stark252PrimeField> = serde_cbor::from_slice(&proof_bytes).unwrap();
let proof: StarkProof<Stark252PrimeField, Stark252PrimeField> =
serde_cbor::from_slice(&proof_bytes).unwrap();

// The proof is verified successfully.
assert!(verify_cairo_proof(&proof, &pub_inputs, &proof_options));
Expand Down
4 changes: 2 additions & 2 deletions provers/cairo/src/wasm_wrappers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::collections::HashMap;
use wasm_bindgen::prelude::wasm_bindgen;

#[wasm_bindgen]
pub struct Stark252PrimeFieldProof(StarkProof<Stark252PrimeField>);
pub struct Stark252PrimeFieldProof(StarkProof<Stark252PrimeField, Stark252PrimeField>);

#[wasm_bindgen]
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Eq, PartialEq, Hash)]
Expand Down Expand Up @@ -45,7 +45,7 @@ pub fn verify_cairo_proof_wasm(proof_bytes: &[u8], proof_options: &ProofOptions)
return false;
};

Verifier::verify::<CairoAIR>(
Verifier::<CairoAIR>::verify(
&proof,
&pub_inputs,
proof_options,
Expand Down
Loading

0 comments on commit fb44319

Please sign in to comment.