Skip to content

Commit

Permalink
Features support
Browse files Browse the repository at this point in the history
  • Loading branch information
fmkra committed Jul 31, 2024
1 parent 251c4ca commit 9a8134e
Show file tree
Hide file tree
Showing 23 changed files with 448 additions and 514 deletions.
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
scarb 2.6.3
scarb nightly-2024-04-20
starknet-foundry 0.24.0
16 changes: 15 additions & 1 deletion Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,18 @@ casm = true

[lib]
sierra = true
casm = false
casm = false

[features]

recursive = []
recursive_with_poseidon = []
dex = []
small = []
starknet = []
starknet_with_keccak = []

keccak = []
blake2s = []

default = ["recursive", "keccak"]
30 changes: 11 additions & 19 deletions src/air/layouts.cairo
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
// === DEX BEGIN ===
// mod dex;
// === DEX END ===
// === RECURSIVE BEGIN ===
#[cfg(feature: 'dex')]
mod dex;
#[cfg(feature: 'recursive')]
mod recursive;
// === RECURSIVE END ===
// === RECURSIVE_WITH_POSEIDON BEGIN ===
// mod recursive_with_poseidon;
// === RECURSIVE_WITH_POSEIDON END ===
// === SMALL BEGIN ===
// mod small;
// === SMALL END ===
// === STARKNET BEGIN ===
// mod starknet;
// === STARKNET END ===
// === STARKNET_WITH_KECCAK BEGIN ===
// mod starknet_with_keccak;
// === STARKNET_WITH_KECCAK END ===


#[cfg(feature: 'recursive_with_poseidon')]
mod recursive_with_poseidon;
#[cfg(feature: 'small')]
mod small;
#[cfg(feature: 'starknet')]
mod starknet;
#[cfg(feature: 'starknet_with_keccak')]
mod starknet_with_keccak;
34 changes: 15 additions & 19 deletions src/air/public_input.cairo
Original file line number Diff line number Diff line change
@@ -1,23 +1,5 @@
use cairo_verifier::{
domains::StarkDomains, air::constants::{MAX_ADDRESS, INITIAL_PC},
// === DEX BEGIN ===
// air::layouts::dex::constants::segments,
// === DEX END ===
// === RECURSIVE BEGIN ===
air::layouts::recursive::constants::segments,
// === RECURSIVE END ===
// === RECURSIVE_WITH_POSEIDON BEGIN ===
// air::layouts::recursive_with_poseidon::constants::segments,
// === RECURSIVE_WITH_POSEIDON END ===
// === SMALL BEGIN ===
// air::layouts::small::constants::segments,
// === SMALL END ===
// === STARKNET BEGIN ===
// air::layouts::starknet::constants::segments,
// === STARKNET END ===
// === STARKNET_WITH_KECCAK BEGIN ===
// air::layouts::starknet_with_keccak::constants::segments,
// === STARKNET_WITH_KECCAK END ===
air::public_memory::{
Page, PageTrait, ContinuousPageHeader, get_continuous_pages_product, AddrValueSize
},
Expand All @@ -26,9 +8,21 @@ use cairo_verifier::{
math::{pow, Felt252PartialOrd, Felt252Div},
},
};

use core::{pedersen::PedersenTrait, hash::{HashStateTrait, HashStateExTrait, Hash}};
use poseidon::poseidon_hash_span;
#[cfg(feature: 'dex')]
use cairo_verifier::air::layouts::dex::constants::segments;
#[cfg(feature: 'recursive')]
use cairo_verifier::air::layouts::recursive::constants::segments;
#[cfg(feature: 'recursive_with_poseidon')]
use cairo_verifier::air::layouts::recursive_with_poseidon::constants::segments;
#[cfg(feature: 'small')]
use cairo_verifier::air::layouts::small::constants::segments;
#[cfg(feature: 'starknet')]
use cairo_verifier::air::layouts::starknet::constants::segments;
#[cfg(feature: 'starknet_with_keccak')]
use cairo_verifier::air::layouts::starknet_with_keccak::constants::segments;


#[derive(Drop, Copy, PartialEq, Serde)]
struct SegmentInfo {
Expand Down Expand Up @@ -184,6 +178,8 @@ fn verify_cairo1_public_input(public_input: @PublicInput) -> (felt252, felt252)
(program_hash, output_hash)
}

#[cfg(feature: 'recursive')]
#[cfg(feature: 'keccak')]
#[cfg(test)]
mod tests {
use super::get_public_input_hash;
Expand Down
40 changes: 22 additions & 18 deletions src/common/hasher.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,42 @@ use cairo_verifier::common::{
flip_endianness::FlipEndiannessTrait
};

// === BLAKE2S BEGIN ===
// fn hash_n_bytes(mut data: Array<u8>, n: u8, hash_len: bool) -> u256 {
// if hash_len {
// data.append(n);
// }
// blake2s_u8(data)
// }
// fn hash_truncated(data: Array<u32>) -> felt252 {
// truncated_blake2s(data)
// }
// fn hash(data: Array<u32>) -> u256 {
// blake2s(data)
// }
// === BLAKE2S END ===
#[cfg(feature: 'blake2s')]
fn hash_n_bytes(mut data: Array<u8>, n: u8, hash_len: bool) -> u256 {
if hash_len {
data.append(n);
}
blake2s_u8(data)
}

// === KECCAK BEGIN ===
#[cfg(feature: 'blake2s')]
fn hash_truncated(data: Array<u32>) -> felt252 {
truncated_blake2s(data)
}

#[cfg(feature: 'blake2s')]
fn hash(data: Array<u32>) -> u256 {
blake2s(data)
}

#[cfg(feature: 'keccak')]
fn hash_n_bytes(mut data: Array<u64>, n: u8, hash_len: bool) -> u256 {
if hash_len {
keccak::cairo_keccak(ref data, n.into(), 1)
} else {
keccak::cairo_keccak(ref data, 0, 0)
}
}

#[cfg(feature: 'keccak')]
fn hash_truncated(mut data: Array<u64>) -> felt252 {
(keccak::cairo_keccak(ref data, 0, 0)
.flip_endianness() % 0x10000000000000000000000000000000000000000)
.try_into()
.unwrap()
}

#[cfg(feature: 'keccak')]
fn hash(mut data: Array<u64>) -> u256 {
keccak::cairo_keccak(ref data, 0, 0)
}
// === KECCAK END ===


30 changes: 12 additions & 18 deletions src/deserialization/stark.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,6 @@ use cairo_verifier::{
public_input::{ContinuousPageHeader, PublicInput, SegmentInfo},
public_memory::{AddrValue, Page},
},
// === DEX BEGIN ===
// air::layouts::dex::traces::TracesConfig,
// === DEX END ===
// === RECURSIVE BEGIN ===
air::layouts::recursive::traces::TracesConfig,
// === RECURSIVE END ===
// === RECURSIVE_WITH_POSEIDON BEGIN ===
// air::layouts::recursive_with_poseidon::traces::TracesConfig,
// === RECURSIVE_WITH_POSEIDON END ===
// === SMALL BEGIN ===
// air::layouts::small::traces::TracesConfig,
// === SMALL END ===
// === STARKNET BEGIN ===
// air::layouts::starknet::traces::TracesConfig,
// === STARKNET END ===
// === STARKNET_WITH_KECCAK BEGIN ===
// air::layouts::starknet_with_keccak::traces::TracesConfig,
// === STARKNET_WITH_KECCAK END ===
deserialization::{
traces::{
TracesConfigWithSerde, TracesDecommitmentWithSerde, TracesWitnessWithSerde,
Expand All @@ -32,6 +14,18 @@ use cairo_verifier::{
},
stark::{StarkProof, StarkConfig, StarkUnsentCommitment, StarkWitness},
};
#[cfg(feature: 'dex')]
use cairo_verifier::air::layouts::dex::traces::TracesConfig;
#[cfg(feature: 'recursive')]
use cairo_verifier::air::layouts::recursive::traces::TracesConfig;
#[cfg(feature: 'recursive_with_poseidon')]
use cairo_verifier::air::layouts::recursive_with_poseidon::traces::TracesConfig;
#[cfg(feature: 'small')]
use cairo_verifier::air::layouts::small::traces::TracesConfig;
#[cfg(feature: 'starknet')]
use cairo_verifier::air::layouts::starknet::traces::TracesConfig;
#[cfg(feature: 'starknet_with_keccak')]
use cairo_verifier::air::layouts::starknet_with_keccak::traces::TracesConfig;

#[derive(Drop, Serde)]
struct StarkProofWithSerde {
Expand Down
54 changes: 24 additions & 30 deletions src/deserialization/traces.cairo
Original file line number Diff line number Diff line change
@@ -1,34 +1,4 @@
use cairo_verifier::{
// === DEX BEGIN ===
// air::layouts::dex::{
// traces::TracesConfig, traces::{TracesUnsentCommitment, TracesDecommitment, TracesWitness}
// },
// === DEX END ===
// === RECURSIVE BEGIN ===
air::layouts::recursive::{
traces::TracesConfig, traces::{TracesUnsentCommitment, TracesDecommitment, TracesWitness}
},
// === RECURSIVE END ===
// === RECURSIVE_WITH_POSEIDON BEGIN ===
// air::layouts::recursive_with_poseidon::{
// traces::TracesConfig, traces::{TracesUnsentCommitment, TracesDecommitment, TracesWitness}
// },
// === RECURSIVE_WITH_POSEIDON END ===
// === SMALL BEGIN ===
// air::layouts::small::{
// traces::TracesConfig, traces::{TracesUnsentCommitment, TracesDecommitment, TracesWitness}
// },
// === SMALL END ===
// === STARKNET BEGIN ===
// air::layouts::starknet::{
// traces::TracesConfig, traces::{TracesUnsentCommitment, TracesDecommitment, TracesWitness}
// },
// === STARKNET END ===
// === STARKNET_WITH_KECCAK BEGIN ===
// air::layouts::starknet_with_keccak::{
// traces::TracesConfig, traces::{TracesUnsentCommitment, TracesDecommitment, TracesWitness}
// },
// === STARKNET_WITH_KECCAK END ===
deserialization::{
vector::{
VectorCommitmentConfig, VectorCommitmentWitness, VectorCommitmentConfigWithSerde,
Expand All @@ -43,6 +13,30 @@ use cairo_verifier::{
TableCommitmentConfig, TableCommitmentWitness, TableDecommitment
},
};
#[cfg(feature: 'dex')]
use cairo_verifier::air::layouts::dex::{
traces::TracesConfig, traces::{TracesUnsentCommitment, TracesDecommitment, TracesWitness}
};
#[cfg(feature: 'recursive')]
use cairo_verifier::air::layouts::recursive::{
traces::TracesConfig, traces::{TracesUnsentCommitment, TracesDecommitment, TracesWitness}
};
#[cfg(feature: 'recursive_with_poseidon')]
use cairo_verifier::air::layouts::recursive_with_poseidon::{
traces::TracesConfig, traces::{TracesUnsentCommitment, TracesDecommitment, TracesWitness}
};
#[cfg(feature: 'small')]
use cairo_verifier::air::layouts::small::{
traces::TracesConfig, traces::{TracesUnsentCommitment, TracesDecommitment, TracesWitness}
};
#[cfg(feature: 'starknet')]
use cairo_verifier::air::layouts::starknet::{
traces::TracesConfig, traces::{TracesUnsentCommitment, TracesDecommitment, TracesWitness}
};
#[cfg(feature: 'starknet_with_keccak')]
use cairo_verifier::air::layouts::starknet_with_keccak::{
traces::TracesConfig, traces::{TracesUnsentCommitment, TracesDecommitment, TracesWitness}
};

#[derive(Drop, Serde)]
struct TracesConfigWithSerde {
Expand Down
6 changes: 4 additions & 2 deletions src/fri/tests.cairo
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// === RECURSIVE BEGIN ===
#[cfg(recursive: 'recursive')]
mod test_fri_commit;
#[cfg(recursive: 'recursive')]
mod test_fri_config;
#[cfg(recursive: 'recursive')]
mod test_fri_verify;
// === RECURSIVE END ===

mod test_fri_formula;
mod test_last_layer;
mod test_next_layer;
26 changes: 11 additions & 15 deletions src/fri/tests/test_fri_verify.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@ use cairo_verifier::{
fri::fri::fri_verify, tests::{stone_proof_fibonacci, stone_proof_fibonacci_keccak}
};

// === BLAKE2S BEGIN ===
// #[test]
// #[available_gas(9999999999)]
// fn test_fri_verify() {
// let queries = stone_proof_fibonacci::queries::get().span();
// let commitment = stone_proof_fibonacci::fri::commitment::get();
// let decommitment = stone_proof_fibonacci::fri::decommitment::get();
// let witness = stone_proof_fibonacci::fri::witness::get();
#[cfg(feature: 'blake2s')]
#[test]
#[available_gas(9999999999)]
fn test_fri_verify() {
let queries = stone_proof_fibonacci::queries::get().span();
let commitment = stone_proof_fibonacci::fri::commitment::get();
let decommitment = stone_proof_fibonacci::fri::decommitment::get();
let witness = stone_proof_fibonacci::fri::witness::get();

// fri_verify(queries, commitment, decommitment, witness)
// }
// === BLAKE2S END ===
fri_verify(queries, commitment, decommitment, witness)
}

// === KECCAK BEGIN ===
#[cfg(feature: 'keccak')]
#[test]
#[available_gas(9999999999)]
fn test_fri_verify() {
Expand All @@ -26,6 +25,3 @@ fn test_fri_verify() {

fri_verify(queries, commitment, decommitment, witness)
}
// === KECCAK END ===


40 changes: 17 additions & 23 deletions src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
mod air;
// === RECURSIVE BEGIN ===
mod benches;
// === RECURSIVE END ===
mod channel;
mod common;
mod deserialization;
Expand All @@ -15,33 +12,30 @@ mod table_commitment;
mod vector_commitment;
mod verifier;

// === RECURSIVE BEGIN ===
#[cfg(feature: 'recursive')]
mod benches;
#[cfg(feature: 'recursive')]
mod tests;
// === RECURSIVE END ===

use cairo_verifier::{
air::public_input::CairoVersion, deserialization::stark::StarkProofWithSerde,
stark::{StarkProof, StarkProofImpl},
// === DEX BEGIN ===
// air::layouts::dex::public_input::DexPublicInputImpl as PublicInputImpl,
// === DEX END ===
// === RECURSIVE BEGIN ===
air::layouts::recursive::public_input::RecursivePublicInputImpl as PublicInputImpl,
// === RECURSIVE END ===
// === RECURSIVE_WITH_POSEIDON BEGIN ===
// air::layouts::recursive_with_poseidon::public_input::RecursiveWithPoseidonPublicInputImpl as PublicInputImpl,
// === RECURSIVE_WITH_POSEIDON END ===
// === SMALL BEGIN ===
// air::layouts::small::public_input::SmallPublicInputImpl as PublicInputImpl,
// === SMALL END ===
// === STARKNET BEGIN ===
// air::layouts::starknet::public_input::StarknetPublicInputImpl as PublicInputImpl,
// === STARKNET END ===
// === STARKNET_WITH_KECCAK BEGIN ===
// air::layouts::starknet_with_keccak::public_input::StarknetWithKeccakPublicInputImpl as PublicInputImpl,
// === STARKNET_WITH_KECCAK END ===
};

#[cfg(feature: 'dex')]
use cairo_verifier::air::layouts::dex::public_input::DexPublicInputImpl as PublicInputImpl;
#[cfg(feature: 'recursive')]
use cairo_verifier::air::layouts::recursive::public_input::RecursivePublicInputImpl as PublicInputImpl;
#[cfg(feature: 'recursive_with_poseidon')]
use cairo_verifier::air::layouts::recursive_with_poseidon::public_input::RecursiveWithPoseidonPublicInputImpl as PublicInputImpl;
#[cfg(feature: 'small')]
use cairo_verifier::air::layouts::small::public_input::SmallPublicInputImpl as PublicInputImpl;
#[cfg(feature: 'starknet')]
use cairo_verifier::air::layouts::starknet::public_input::StarknetPublicInputImpl as PublicInputImpl;
#[cfg(feature: 'starknet_with_keccak')]
use cairo_verifier::air::layouts::starknet_with_keccak::public_input::StarknetWithKeccakPublicInputImpl as PublicInputImpl;


const SECURITY_BITS: felt252 = 50;

fn main(mut serialized: Span<felt252>, cairo_version: CairoVersion) -> (felt252, felt252) {
Expand Down
Loading

0 comments on commit 9a8134e

Please sign in to comment.