Skip to content

Commit

Permalink
Change program hash for cairo1 memory verification
Browse files Browse the repository at this point in the history
  • Loading branch information
fmkra committed Nov 7, 2024
1 parent f065075 commit 567dfc3
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions src/air/public_input.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -159,28 +159,38 @@ fn get_public_memory_product(
fn verify_cairo1_public_input(public_input: @PublicInput) -> (felt252, felt252) {
let public_segments = public_input.segments;

let initial_pc = *public_segments.at(segments::PROGRAM).begin_addr;
let initial_ap = *public_segments.at(segments::EXECUTION).begin_addr;
let final_ap = *public_segments.at(segments::EXECUTION).stop_ptr;
let output_start = *public_segments.at(segments::OUTPUT).begin_addr;
let output_stop = *public_segments.at(segments::OUTPUT).stop_ptr;
let output_len: u32 = (output_stop - output_start).try_into().unwrap();
let initial_pc: u32 = (*public_segments.at(segments::PROGRAM).begin_addr).try_into().unwrap();
let initial_ap: u32 = (*public_segments.at(segments::EXECUTION).begin_addr).try_into().unwrap();
let final_ap: u32 = (*public_segments.at(segments::EXECUTION).stop_ptr).try_into().unwrap();
let initial_fp: u32 = initial_ap;
let output_start: u32 = (*public_segments.at(segments::OUTPUT).begin_addr).try_into().unwrap();
let output_stop: u32 = (*public_segments.at(segments::OUTPUT).stop_ptr).try_into().unwrap();
let output_len: u32 = output_stop - output_start;

assert(initial_ap < MAX_ADDRESS, 'Invalid initial_ap');
assert(final_ap < MAX_ADDRESS, 'Invalid final_ap');
assert(initial_ap.into() < MAX_ADDRESS, 'Invalid initial_ap');
assert(final_ap.into() < MAX_ADDRESS, 'Invalid final_ap');

// TODO support continuous memory pages
assert(public_input.continuous_page_headers.len() == 0, 'Invalid continuous_page_headers');

let memory = public_input.main_page;

let mut memory_index: u32 = 0;

// 1. Program segment
assert(initial_pc == INITIAL_PC, 'Invalid initial_pc');
let program = memory.extract_range_unchecked(0, memory.len() - output_len);
let program_end_pc: u32 = (initial_fp - 2);
let program_len = program_end_pc - initial_pc;

let program = memory.extract_range(initial_pc, program_len, ref memory_index);
let program_hash = poseidon_hash_span(program);

// TODO: remove this hacky way with proper asserts
while (*memory[memory_index]).address < output_start.into() {
memory_index += 1;
};

// 2. Output segment
let output = memory.extract_range_unchecked(memory.len() - output_len, output_len);
let output = memory.extract_range(output_start, output_len, ref memory_index);
let output_hash = poseidon_hash_span(output);
(program_hash, output_hash)
}
Expand Down

0 comments on commit 567dfc3

Please sign in to comment.