Skip to content

Commit

Permalink
Change handling of security bits
Browse files Browse the repository at this point in the history
  • Loading branch information
fmkra committed Sep 10, 2024
1 parent 23b125e commit 479f3c6
Show file tree
Hide file tree
Showing 8 changed files with 437 additions and 337 deletions.
334 changes: 166 additions & 168 deletions fact_registry/src/fact_registry.cairo
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
use cairo_verifier::{
StarkProofWithSerde, CairoVersion,
fri::fri::{FriLayerWitness, FriVerificationStateConstant, FriVerificationStateVariable},
verifier::InitResult,
};
use starknet::ContractAddress;
use fact_registry::verifier::InitResult;


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

Expand Down Expand Up @@ -55,168 +53,168 @@ trait IFactRegistry<TContractState> {
fn transfer_ownership(ref self: TContractState, new_owner: ContractAddress);
}

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

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

#[event]
#[derive(Drop, starknet::Event)]
enum Event {
FactRegistered: FactRegistered,
OwnershipTransferred: OwnershipTransferred,
VerifierRegistered: VerifierRegistered,
}

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

#[derive(Drop, starknet::Event)]
struct VerifierRegistered {
#[key]
settings: VerifierSettings,
#[key]
address: ContractAddress,
}

#[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_proof_full_and_register_fact(
ref self: ContractState,
stark_proof: StarkProofWithSerde,
cairo_version: CairoVersion,
settings: VerifierSettings,
) {
let verifier_address = self.get_verifier_address(settings);
let fact = ICairoVerifierDispatcher {
contract_address: verifier_address
}.verify_proof_full(stark_proof.into(), cairo_version);

self.emit(Event::FactRegistered(FactRegistered { fact, verifier_address }));
self.facts.write(fact, true);
}

fn verify_proof_initial(
self: @ContractState,
job_id: felt252,
stark_proof_serde: StarkProofWithSerde,
cairo_version: CairoVersion,
settings: VerifierSettings,
) -> InitResult {
ICairoVerifierDispatcher {
contract_address: self.get_verifier_address(settings)
}.verify_proof_initial(job_id, stark_proof_serde, cairo_version)
}

fn verify_proof_step(
self: @ContractState,
job_id: felt252,
state_constant: FriVerificationStateConstant,
state_variable: FriVerificationStateVariable,
witness: FriLayerWitness,
settings: VerifierSettings,
) -> (FriVerificationStateVariable, u32) {
ICairoVerifierDispatcher {
contract_address: self.get_verifier_address(settings)
}.verify_proof_step(job_id, state_constant, state_variable, witness)
}

fn verify_proof_final_and_register_fact(
ref self: ContractState,
job_id: felt252,
state_constant: FriVerificationStateConstant,
state_variable: FriVerificationStateVariable,
last_layer_coefficients: Span<felt252>,
settings: VerifierSettings,
) {
let verifier_address = self.get_verifier_address(settings);
assert(verifier_address.into() != 0, 'VERIFIER_NOT_FOUND');
let fact = ICairoVerifierDispatcher {
contract_address: verifier_address
}.verify_proof_final(job_id, state_constant, state_variable, last_layer_coefficients);

self.emit(Event::FactRegistered(FactRegistered { fact, verifier_address }));
self.facts.write(fact, true);
}

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

fn get_verifier_address(self: @ContractState, settings: VerifierSettings) -> ContractAddress {
let verifier_address = self.verifiers.read(self._hash_settings(settings));
assert(verifier_address.into() != 0, 'VERIFIER_NOT_FOUND');
verifier_address
}

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);
self.emit(Event::VerifierRegistered(VerifierRegistered {
settings, 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 _hash_settings(self: @ContractState, settings: VerifierSettings) -> felt252 {
PoseidonImpl::new()
.update(settings.layout)
.update(settings.hasher)
.update(settings.security_bits)
.update(settings.version)
.finalize()
}
}
}
// #[starknet::contract]
// mod FactRegistry {
// use cairo_verifier::{
// StarkProofWithSerde, StarkProof, CairoVersion,
// fri::fri::{FriLayerWitness, FriVerificationStateConstant, FriVerificationStateVariable},
// };
// use starknet::{ContractAddress, get_caller_address};
// use core::{
// poseidon::{Poseidon, PoseidonImpl, HashStateImpl}, keccak::keccak_u256s_be_inputs,
// starknet::event::EventEmitter
// };
// use fact_registry::verifier::{
// InitResult, ICairoVerifierDispatcher, ICairoVerifierDispatcherTrait
// };
// use super::{VerifierSettings, IFactRegistry};

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

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

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

// #[derive(Drop, starknet::Event)]
// struct VerifierRegistered {
// #[key]
// settings: VerifierSettings,
// #[key]
// address: ContractAddress,
// }

// #[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_proof_full_and_register_fact(
// ref self: ContractState,
// stark_proof: StarkProofWithSerde,
// cairo_version: CairoVersion,
// settings: VerifierSettings,
// ) {
// let verifier_address = self.get_verifier_address(settings);
// let (fact, security_bits) = ICairoVerifierDispatcher {
// contract_address: verifier_address
// }.verify_proof_full(stark_proof.into(), cairo_version);

// self.emit(Event::FactRegistered(FactRegistered { fact, verifier_address, security_bits }));
// self.facts.write(fact, true);
// }

// fn verify_proof_initial(
// self: @ContractState,
// job_id: felt252,
// stark_proof_serde: StarkProofWithSerde,
// cairo_version: CairoVersion,
// settings: VerifierSettings,
// ) -> InitResult {
// ICairoVerifierDispatcher {
// contract_address: self.get_verifier_address(settings)
// }.verify_proof_initial(job_id, stark_proof_serde, cairo_version)
// }

// fn verify_proof_step(
// self: @ContractState,
// job_id: felt252,
// state_constant: FriVerificationStateConstant,
// state_variable: FriVerificationStateVariable,
// witness: FriLayerWitness,
// settings: VerifierSettings,
// ) -> (FriVerificationStateVariable, u32) {
// ICairoVerifierDispatcher {
// contract_address: self.get_verifier_address(settings)
// }.verify_proof_step(job_id, state_constant, state_variable, witness)
// }

// fn verify_proof_final_and_register_fact(
// ref self: ContractState,
// job_id: felt252,
// state_constant: FriVerificationStateConstant,
// state_variable: FriVerificationStateVariable,
// last_layer_coefficients: Span<felt252>,
// settings: VerifierSettings,
// ) {
// let verifier_address = self.get_verifier_address(settings);
// assert(verifier_address.into() != 0, 'VERIFIER_NOT_FOUND');
// let (fact, security_bits) = ICairoVerifierDispatcher {
// contract_address: verifier_address
// }.verify_proof_final(job_id, state_constant, state_variable, last_layer_coefficients);

// self.emit(Event::FactRegistered(FactRegistered { fact, verifier_address, security_bits }));
// self.facts.write(fact, true);
// }

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

// fn get_verifier_address(self: @ContractState, settings: VerifierSettings) -> ContractAddress {
// let verifier_address = self.verifiers.read(self._hash_settings(settings));
// assert(verifier_address.into() != 0, 'VERIFIER_NOT_FOUND');
// verifier_address
// }

// 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);
// self.emit(Event::VerifierRegistered(VerifierRegistered {
// settings, 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 _hash_settings(self: @ContractState, settings: VerifierSettings) -> felt252 {
// PoseidonImpl::new()
// .update(settings.layout)
// .update(settings.hasher)
// .update(settings.security_bits)
// .update(settings.version)
// .finalize()
// }
// }
// }
Loading

0 comments on commit 479f3c6

Please sign in to comment.