Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/proxy_contract' into autogenerat…
Browse files Browse the repository at this point in the history
…ed_split
  • Loading branch information
fmkra committed Sep 9, 2024
2 parents 26f49e1 + 9a8134e commit e0293ee
Show file tree
Hide file tree
Showing 27 changed files with 609 additions and 523 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
25 changes: 20 additions & 5 deletions Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,27 @@
name = "cairo_verifier"
version = "0.1.0"

[lib]
sierra = true
[dependencies]
snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry", tag = "v0.24.0" }

[[target.starknet-contract]]
sierra = true
casm = true
# sierra = true

[dependencies]
snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry", tag = "v0.24.0" }
[lib]
sierra = true
casm = false

[features]

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

keccak = []
blake2s = []

default = ["recursive", "keccak"]
131 changes: 129 additions & 2 deletions fact_registry/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,2 +1,129 @@
mod verifier;
mod fact_registry;
use cairo_verifier::{StarkProofWithSerde, CairoVersion};
use starknet::ContractAddress;


#[derive(Drop, Copy, Serde)]
struct VerifierSettings {
layout: felt252,
hasher: felt252,
security_bits: felt252,
version: felt252,
}

#[starknet::interface]
trait IFactRegistry<TContractState> {
fn verify_and_register_fact(
ref self: TContractState,
stark_proof: StarkProofWithSerde,
cairo_version: CairoVersion,
settings: VerifierSettings,
);
fn is_valid(self: @TContractState, fact: felt252) -> bool;
fn register_verifier(ref self: TContractState, settings: VerifierSettings, address: ContractAddress);
fn transfer_ownership(ref self: TContractState, new_owner: ContractAddress);
}

#[starknet::contract]
mod FactRegistry {
use cairo_verifier::{StarkProofWithSerde, CairoVersion};
use starknet::{ContractAddress, get_caller_address};
use core::{
poseidon::{Poseidon, PoseidonImpl, HashStateImpl}, keccak::keccak_u256s_be_inputs,
starknet::event::EventEmitter
};
use cairo_verifier::verifier::{ICairoVerifierDispatcher, ICairoVerifierDispatcherTrait, StarkProof};
use super::{VerifierSettings, IFactRegistry};

#[storage]
struct Storage {
owner: ContractAddress,
verifiers: LegacyMap<felt252, ContractAddress>,
facts: LegacyMap<felt252, bool>,
}

#[event]
#[derive(Drop, starknet::Event)]
enum Event {
// #[flat]
// CairoVerifierEvent: CairoVerifier::Event,
FactRegistered: FactRegistered,
OwnershipTransferred: OwnershipTransferred,
}

#[derive(Drop, starknet::Event)]
struct FactRegistered {
#[key]
fact: felt252,
}

#[derive(Drop, starknet::Event)]
struct OwnershipTransferred {
previous_owner: ContractAddress,
new_owner: ContractAddress
}

#[constructor]
fn constructor(ref self: ContractState, owner: ContractAddress) {
self.owner.write(owner);
}

#[abi(embed_v0)]
impl FactRegistryImpl of IFactRegistry<ContractState> {
fn verify_and_register_fact(
ref self: ContractState,
stark_proof: StarkProofWithSerde,
cairo_version: CairoVersion,
settings: VerifierSettings,
) {
let verifier_address = self.verifiers.read(self._hash_settings(settings));
assert(verifier_address.into() != 0, 'VERIFIER_NOT_FOUND');
let (program_hash, output_hash) = ICairoVerifierDispatcher {
contract_address: verifier_address
}.verify_proof(stark_proof.into(), cairo_version);
self._register_fact(program_hash, output_hash);
}

fn is_valid(self: @ContractState, fact: felt252) -> bool {
self.facts.read(fact)
}

fn register_verifier(ref self: ContractState, settings: VerifierSettings, address: ContractAddress) {
assert(self.owner.read() == get_caller_address(), 'ONLY_OWNER');
assert(address.into() != 0, 'INVALID_VERIFIER_ADDRESS');
let settings_hash = self._hash_settings(settings);
assert(self.verifiers.read(settings_hash).into() == 0, 'VERIFIER_ALREADY_EXISTS');
self.verifiers.write(settings_hash, address);
}

fn transfer_ownership(ref self: ContractState, new_owner: ContractAddress) {
let caller = get_caller_address();
assert(self.owner.read() == caller, 'ONLY_OWNER');
self.owner.write(new_owner);

self
.emit(
Event::OwnershipTransferred(
OwnershipTransferred { previous_owner: caller, new_owner }
)
);
}
}

#[generate_trait]
impl InternalFactRegistry of InternalFactRegistryTrait {
fn _register_fact(ref self: ContractState, program_hash: felt252, output_hash: felt252,) {
let fact = PoseidonImpl::new().update(program_hash).update(output_hash).finalize();
self.emit(Event::FactRegistered(FactRegistered { fact }));
self.facts.write(fact, true);
}

fn _hash_settings(self: @ContractState, settings: VerifierSettings) -> felt252 {
PoseidonImpl::new()
.update(settings.layout)
.update(settings.hasher)
.update(settings.security_bits)
.update(settings.version)
.finalize()
}
}
}
6 changes: 6 additions & 0 deletions fact_registry/starknet/deploy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[[call]]
call_type = "deploy"
class_hash = "0x79b972400cbbbc9afe4056a377366773bc069e292e1b9657bec2e48b4353ad3"
inputs = ["REPLACE WITH YOUR ACCOUNT ADDRESS"]
unique = false
id = "fact_registry"
18 changes: 18 additions & 0 deletions fact_registry/starknet/register_verifier.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[[call]]
call_type = "deploy"
class_hash = "VERIFIER CLASS HASH"
inputs = []
unique = false
id = "verifier"

[[call]]
call_type = "invoke"
contract_address = "REPLACE WITH FACT REGISTRY ADDRESS"
function = "register_verifier"
inputs = [
"SETTINGS.LAYOUT",
"SETTINGS.HASHER",
"SETTINGS.SECURITY_BITS",
"SETTINGS.VERSION",
"verifier"
]
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;
35 changes: 15 additions & 20 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,7 +178,8 @@ fn verify_cairo1_public_input(public_input: @PublicInput) -> (felt252, felt252)
(program_hash, output_hash)
}

// === RECURSIVE BEGIN ===
#[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
Loading

0 comments on commit e0293ee

Please sign in to comment.