Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changable hasher #103

Merged
merged 21 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions change_hasher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import argparse
from pathlib import Path
import re
import sys


hashers = ('KECCAK', 'BLAKE')

def process_file(file_path, type):
try:
with open(file_path, 'r', encoding='utf-8') as file:
lines = file.readlines()
except UnicodeDecodeError:
print(f"Skipping file due to encoding issue: {file_path}")
return # Skip this file

in_block = False
current_block_type = None
modified_lines = []

for line in lines:
begin_match = re.match(r'^// === ([A-Z]+) ONLY BEGIN ===', line)
end_match = re.match(r'^// === ([A-Z]+) ONLY END ===', line)

if begin_match:
in_block = True
current_block_type = begin_match.group(1)
modified_lines.append(line)
continue
elif end_match:
in_block = False
modified_lines.append(line)
continue

if in_block:
if current_block_type == type:
# Uncomment lines for blocks of the specified type
if line.startswith('// '):
modified_lines.append(line[3:])
else:
modified_lines.append(line)
else:
# Comment out lines for blocks of other types
if not line.startswith('// '):
modified_lines.append('// ' + line)
else:
modified_lines.append(line)
else:
modified_lines.append(line)

with open(file_path, 'w', encoding='utf-8') as file:
file.writelines(modified_lines)


def main(type=None):
if type is None:
try:
import inquirer
except ImportError:
print("inquirer module is not installed. Please install it using 'pip install inquirer'")
sys.exit(1)
questions = [
inquirer.List('type',
message="Select block type",
choices=hashers,
),
]
answers = inquirer.prompt(questions)
type = answers['type']

if type.upper() not in hashers:
print(f"Invalid block type: {type}")
sys.exit(1)
current_directory = Path('src')
for file_path in current_directory.rglob('*'):
if file_path.is_file():
process_file(file_path, type.upper())


if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Process files based on block type.")
parser.add_argument('-t', '--type', type=str, help=f'Type of block to process {hashers}')
args = parser.parse_args()

if args.type:
main(args.type)
else:
main()
6 changes: 3 additions & 3 deletions src/air/public_input.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use core::{pedersen::PedersenTrait, hash::{HashStateTrait, HashStateExTrait, Hash}};
use cairo_verifier::{
common::{
flip_endianness::FlipEndiannessTrait, array_append::ArrayAppendTrait, blake2s::blake2s,
flip_endianness::FlipEndiannessTrait, array_append::ArrayAppendTrait, hasher::hash,
math::{pow, Felt252PartialOrd, Felt252Div}, asserts::assert_range_u128_le,
array_print::SpanPrintTrait, hash::hash_felts,
},
Expand Down Expand Up @@ -57,7 +57,7 @@ impl PublicInputImpl of PublicInputTrait {
.update_with(AddrValueSize * self.main_page.len());
let main_page_hash = main_page_hash_state.finalize();

let mut hash_data = ArrayTrait::<u32>::new();
let mut hash_data = ArrayTrait::new(); // u32 for blake, u64 for keccak
ArrayAppendTrait::<_, u256>::append_big_endian(ref hash_data, (*self.log_n_steps).into());
ArrayAppendTrait::<_, u256>::append_big_endian(ref hash_data, (*self.rc_min).into());
ArrayAppendTrait::<_, u256>::append_big_endian(ref hash_data, (*self.rc_max).into());
Expand Down Expand Up @@ -121,7 +121,7 @@ impl PublicInputImpl of PublicInputTrait {
i += 1;
};

blake2s(hash_data).flip_endianness()
hash(hash_data).flip_endianness()
}

// Returns the ratio between the product of all public memory cells and z^|public_memory|.
Expand Down
71 changes: 51 additions & 20 deletions src/air/tests/test_public_input.cairo
Original file line number Diff line number Diff line change
@@ -1,17 +1,62 @@
use cairo_verifier::{
tests::stone_proof_fibonacci, air::{public_input::PublicInputTrait}, domains::StarkDomainsTrait
tests::{stone_proof_fibonacci, stone_proof_fibonacci_keccak},
air::{public_input::PublicInputTrait}, domains::StarkDomainsTrait
};

// test data from cairo0-verifier run on stone-prover generated proof
// === BLAKE ONLY BEGIN ===
// #[test]
// #[available_gas(9999999999)]
// fn test_public_input_hash() {
// let public_input = stone_proof_fibonacci::public_input::get();
//
// assert(
// public_input
// .get_public_input_hash() == u256 {
// low: 0xba9d17a3ebd900899148b125421c118f, high: 0x87433b8dd90acbfe5abea8474d795191
// },
// 'Invalid value'
// )
// }
//
// #[test]
// #[available_gas(9999999999)]
// fn test_public_input_validate() {
// let public_input = stone_proof_fibonacci::public_input::get();
//
// let log_trace_domain_size = 0x12;
// let log_n_cosets = 0x4;
// let domain = StarkDomainsTrait::new(log_trace_domain_size, log_n_cosets);
//
// public_input.validate(@domain);
// }
//
// #[test]
// #[available_gas(9999999999)]
// fn test_public_input_verify() {
// let public_input = stone_proof_fibonacci::public_input::get();
// let (program_hash, output_hash) = public_input.verify();
//
// assert(
// program_hash == 0x9f6693f4a5610a46b5d71ef573c43bef5f0d111fc1c5e506d509c458a29bae,
// 'Wrong program hash'
// );
// assert(
// output_hash == 0x3cff7dfd4138a3c9082a6a768b1c094ae290e2f4705482bf0eb2dbb21c46968,
// 'Wrong output hash'
// );
// }
// === BLAKE ONLY END ===

// === KECCAK ONLY BEGIN ===
#[test]
#[available_gas(9999999999)]
fn test_public_input_hash() {
let public_input = stone_proof_fibonacci::public_input::get();
let public_input = stone_proof_fibonacci_keccak::public_input::get();

assert(
public_input
.get_public_input_hash() == u256 {
low: 0xba9d17a3ebd900899148b125421c118f, high: 0x87433b8dd90acbfe5abea8474d795191
low: 0x22b3f4d7841a28271009bef644a84a5e, high: 0x8f17c0c0dcde2144cd36213ab3aaff1b
},
'Invalid value'
)
Expand All @@ -20,28 +65,14 @@ fn test_public_input_hash() {
#[test]
#[available_gas(9999999999)]
fn test_public_input_validate() {
let public_input = stone_proof_fibonacci::public_input::get();
let public_input = stone_proof_fibonacci_keccak::public_input::get();

let log_trace_domain_size = 0x12;
let log_n_cosets = 0x4;
let domain = StarkDomainsTrait::new(log_trace_domain_size, log_n_cosets);

public_input.validate(@domain);
}
// === KECCAK ONLY END ===

#[test]
#[available_gas(9999999999)]
fn test_public_input_verify() {
let public_input = stone_proof_fibonacci::public_input::get();
let (program_hash, output_hash) = public_input.verify();

assert(
program_hash == 0x9f6693f4a5610a46b5d71ef573c43bef5f0d111fc1c5e506d509c458a29bae,
'Wrong program hash'
);
assert(
output_hash == 0x3cff7dfd4138a3c9082a6a768b1c094ae290e2f4705482bf0eb2dbb21c46968,
'Wrong output hash'
);
}

83 changes: 67 additions & 16 deletions src/air/tests/test_traces.cairo
Original file line number Diff line number Diff line change
@@ -1,56 +1,107 @@
use cairo_verifier::{
channel::channel::ChannelImpl,
air::{traces::{traces_commit, traces_decommit}, traces::TracesConfigTrait},
tests::stone_proof_fibonacci,
tests::{stone_proof_fibonacci, stone_proof_fibonacci_keccak},
};

// test data from cairo0-verifier run on stone-prover generated proof
// === BLAKE ONLY BEGIN ===
// #[test]
// #[available_gas(9999999999)]
// fn test_traces_config() {
// let traces_config = stone_proof_fibonacci::traces::config::get();
//
// traces_config.validate(0x16, 0x16);
// }
//
// #[test]
// #[available_gas(9999999999)]
// fn test_traces_commit() {
// let mut channel = ChannelImpl::new_with_counter(
// u256 { low: 0xba9d17a3ebd900899148b125421c118f, high: 0x87433b8dd90acbfe5abea8474d795191 },
// 0x0,
// );
// let public_input = @stone_proof_fibonacci::public_input::get();
// let unsent_commitment = stone_proof_fibonacci::traces::unsent_commitment::get();
// let traces_config = stone_proof_fibonacci::traces::config::get();
//
// assert(
// traces_commit(
// ref channel, public_input, unsent_commitment, traces_config
// ) == stone_proof_fibonacci::traces::commitment::get(),
// 'Invalid value'
// );
//
// assert(
// channel
// .digest == u256 {
// low: 0x8823a41f7994f81c6453f4bc3cad1c10, high: 0x75f85ae3fd3ff6b5f63029a51040037e
// },
// 'Invalid value'
// );
//
// assert(channel.counter == 0x0, 'Invalid value')
// }
//
// #[test]
// #[available_gas(9999999999)]
// fn test_traces_decommit() {
// let queries = stone_proof_fibonacci::queries::get().span();
// let commitment = stone_proof_fibonacci::traces::commitment::get();
// let decommitment = stone_proof_fibonacci::traces::decommitment::get();
// let witness = stone_proof_fibonacci::traces::witness::get();
//
// traces_decommit(queries, commitment, decommitment, witness);
// }
// === BLAKE ONLY END ===

// === KECCAK ONLY BEGIN ===
#[test]
#[available_gas(9999999999)]
fn test_traces_config() {
let traces_config = stone_proof_fibonacci::traces::config::get();
let traces_config = stone_proof_fibonacci_keccak::traces::config::get();

traces_config.validate(0x16, 0x16);
traces_config.validate(0x16, 0x64);
}

// test data from cairo0-verifier run on stone-prover generated proof
#[test]
#[available_gas(9999999999)]
fn test_traces_commit() {
let mut channel = ChannelImpl::new_with_counter(
u256 { low: 0xba9d17a3ebd900899148b125421c118f, high: 0x87433b8dd90acbfe5abea8474d795191 },
u256 { low: 0x22b3f4d7841a28271009bef644a84a5e, high: 0x8f17c0c0dcde2144cd36213ab3aaff1b },
0x0,
);
let public_input = @stone_proof_fibonacci::public_input::get();
let unsent_commitment = stone_proof_fibonacci::traces::unsent_commitment::get();
let traces_config = stone_proof_fibonacci::traces::config::get();
let public_input = @stone_proof_fibonacci_keccak::public_input::get();
let unsent_commitment = stone_proof_fibonacci_keccak::traces::unsent_commitment::get();
let traces_config = stone_proof_fibonacci_keccak::traces::config::get();

assert(
traces_commit(
ref channel, public_input, unsent_commitment, traces_config
) == stone_proof_fibonacci::traces::commitment::get(),
) == stone_proof_fibonacci_keccak::traces::commitment::get(),
'Invalid value'
);

assert(
channel
.digest == u256 {
low: 0x8823a41f7994f81c6453f4bc3cad1c10, high: 0x75f85ae3fd3ff6b5f63029a51040037e
low: 0xa1a3e0273721e6961814f180b2d8caeb, high: 0x6f68726d3fdeb87e6c9b3d3072531b07
},
'Invalid value'
);

assert(channel.counter == 0x0, 'Invalid value')
}

// test data from cairo0-verifier run on stone-prover generated proof
#[test]
#[available_gas(9999999999)]
fn test_traces_decommit() {
let queries = stone_proof_fibonacci::queries::get().span();
let commitment = stone_proof_fibonacci::traces::commitment::get();
let decommitment = stone_proof_fibonacci::traces::decommitment::get();
let witness = stone_proof_fibonacci::traces::witness::get();
let queries = stone_proof_fibonacci_keccak::queries::get().span();
let commitment = stone_proof_fibonacci_keccak::traces::commitment::get();
let decommitment = stone_proof_fibonacci_keccak::traces::decommitment::get();
let witness = stone_proof_fibonacci_keccak::traces::witness::get();

traces_decommit(queries, commitment, decommitment, witness);
}
// === KECCAK ONLY END ===


6 changes: 3 additions & 3 deletions src/benches/fri/commit.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use cairo_verifier::{
channel::channel::ChannelTrait, fri::fri::fri_commit, tests::stone_proof_fibonacci,
channel::channel::ChannelTrait, fri::fri::fri_commit, tests::stone_proof_fibonacci_keccak,
};

fn bench_fri_commit() {
Expand All @@ -8,8 +8,8 @@ fn bench_fri_commit() {
0x0,
);

let fri_config = stone_proof_fibonacci::fri::config::get();
let unsent_commitment = stone_proof_fibonacci::fri::unsent_commitment::get();
let fri_config = stone_proof_fibonacci_keccak::fri::config::get();
let unsent_commitment = stone_proof_fibonacci_keccak::fri::unsent_commitment::get();

fri_commit(ref channel, unsent_commitment, fri_config);
}
10 changes: 5 additions & 5 deletions src/benches/fri/verify.cairo
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use cairo_verifier::{fri::fri::fri_verify, tests::stone_proof_fibonacci,};
use cairo_verifier::{fri::fri::fri_verify, tests::stone_proof_fibonacci_keccak};

fn bench_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();
let queries = stone_proof_fibonacci_keccak::queries::get().span();
let commitment = stone_proof_fibonacci_keccak::fri::commitment::get();
let decommitment = stone_proof_fibonacci_keccak::fri::decommitment::get();
let witness = stone_proof_fibonacci_keccak::fri::witness::get();

fri_verify(queries, commitment, decommitment, witness)
}
Expand Down
11 changes: 6 additions & 5 deletions src/benches/stark/commit.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use cairo_verifier::{
stark::stark_commit::stark_commit, channel::channel::ChannelTrait, tests::stone_proof_fibonacci,
stark::stark_commit::stark_commit, channel::channel::ChannelTrait,
tests::stone_proof_fibonacci_keccak,
};

fn bench_stark_commit() {
Expand All @@ -8,10 +9,10 @@ fn bench_stark_commit() {
0x0
);

let public_input = stone_proof_fibonacci::public_input::get();
let unsent_commitment = stone_proof_fibonacci::stark::unsent_commitment::get();
let config = stone_proof_fibonacci::stark::config::get();
let stark_domains = stone_proof_fibonacci::stark::domains::get();
let public_input = stone_proof_fibonacci_keccak::public_input::get();
let unsent_commitment = stone_proof_fibonacci_keccak::stark::unsent_commitment::get();
let config = stone_proof_fibonacci_keccak::stark::config::get();
let stark_domains = stone_proof_fibonacci_keccak::stark::domains::get();

stark_commit(ref channel, @public_input, @unsent_commitment, @config, @stark_domains);
}
Loading
Loading