use starknet_crypto::{pedersen_hash, FieldElement};
use crate::Felt252;
use crate::stdlib::vec::Vec;
use crate::types::builtin_name::BuiltinName;
use crate::types::relocatable::MaybeRelocatable;
use crate::vm::runners::cairo_pie::StrippedProgram;
type HashFunction = fn(&FieldElement, &FieldElement) -> FieldElement;
#[derive(thiserror_no_std::Error, Debug)]
pub enum HashChainError {
#[error("Data array must contain at least one element.")]
EmptyData,
}
#[derive(thiserror_no_std::Error, Debug)]
pub enum ProgramHashError {
#[error(transparent)]
HashChain(#[from] HashChainError),
#[error(
"Invalid program builtin: builtin name too long to be converted to field element: {0}"
)]
InvalidProgramBuiltin(&'static str),
#[error("Invalid program data: data contains relocatable(s)")]
InvalidProgramData,
#[error("Conversion from Felt252 to FieldElement failed")]
Felt252ToFieldElementConversionFailed,
}
fn compute_hash_chain<'a, I>(
data: I,
hash_func: HashFunction,
) -> Result<FieldElement, HashChainError>
where
I: Iterator<Item = &'a FieldElement> + DoubleEndedIterator,
{
match data.copied().rev().reduce(|x, y| hash_func(&y, &x)) {
Some(result) => Ok(result),
None => Err(HashChainError::EmptyData),
}
}
fn builtin_name_to_field_element(
builtin_name: &BuiltinName,
) -> Result<FieldElement, ProgramHashError> {
FieldElement::from_byte_slice_be(builtin_name.to_str().as_bytes())
.map_err(|_| ProgramHashError::InvalidProgramBuiltin(builtin_name.to_str()))
}
fn felt_to_field_element(felt: &Felt252) -> Result<FieldElement, ProgramHashError> {
let bytes = felt.to_bytes_be();
FieldElement::from_bytes_be(&bytes)
.map_err(|_e| ProgramHashError::Felt252ToFieldElementConversionFailed)
}
fn maybe_relocatable_to_field_element(
maybe_relocatable: &MaybeRelocatable,
) -> Result<FieldElement, ProgramHashError> {
let felt = maybe_relocatable
.get_int_ref()
.ok_or(ProgramHashError::InvalidProgramData)?;
felt_to_field_element(felt)
}
pub fn compute_program_hash_chain(
program: &StrippedProgram,
bootloader_version: usize,
) -> Result<FieldElement, ProgramHashError> {
let program_main = program.main;
let program_main = FieldElement::from(program_main);
let builtin_list: Result<Vec<FieldElement>, _> = program
.builtins
.iter()
.map(builtin_name_to_field_element)
.collect();
let builtin_list = builtin_list?;
let program_header = vec![
FieldElement::from(bootloader_version),
program_main,
FieldElement::from(program.builtins.len()),
];
let program_data: Result<Vec<_>, _> = program
.data
.iter()
.map(maybe_relocatable_to_field_element)
.collect();
let program_data = program_data?;
let data_chain_len = program_header.len() + builtin_list.len() + program_data.len();
let data_chain_len_vec = vec![FieldElement::from(data_chain_len)];
let data_chain = [
&data_chain_len_vec,
&program_header,
&builtin_list,
&program_data,
];
let hash = compute_hash_chain(data_chain.iter().flat_map(|&v| v.iter()), pedersen_hash)?;
Ok(hash)
}
#[cfg(test)]
mod tests {
#[cfg(feature = "std")]
use {crate::types::program::Program, rstest::rstest, std::path::PathBuf};
use starknet_crypto::pedersen_hash;
use super::*;
#[test]
fn test_compute_hash_chain() {
let data: Vec<FieldElement> = vec![
FieldElement::from(1u64),
FieldElement::from(2u64),
FieldElement::from(3u64),
];
let expected_hash = pedersen_hash(
&FieldElement::from(1u64),
&pedersen_hash(&FieldElement::from(2u64), &FieldElement::from(3u64)),
);
let computed_hash = compute_hash_chain(data.iter(), pedersen_hash)
.expect("Hash computation failed unexpectedly");
assert_eq!(computed_hash, expected_hash);
}
#[cfg(feature = "std")]
#[rstest]
#[case::fibonacci(
"../cairo_programs/fibonacci.json",
"0x43b17e9592f33142246af4c06cd2b574b460dd1f718d76b51341175a62b220f"
)]
#[case::field_arithmetic(
"../cairo_programs/field_arithmetic.json",
"0x1031772ca86e618b058101af9c9a3277bac90712b750bcea1cc69d6c7cad8a7"
)]
#[case::keccak_copy_inputs(
"../cairo_programs/keccak_copy_inputs.json",
"0x49484fdc8e7a85061f9f21b7e21fe276d8a88c8e96681101a2518809e686c6c"
)]
fn test_compute_program_hash_chain(
#[case] program_path: PathBuf,
#[case] expected_program_hash: String,
) {
let program =
Program::from_file(program_path.as_path(), Some("main"))
.expect("Could not load program. Did you compile the sample programs? Run `make test` in the root directory.");
let stripped_program = program.get_stripped_program().unwrap();
let bootloader_version = 0;
let program_hash = compute_program_hash_chain(&stripped_program, bootloader_version)
.expect("Failed to compute program hash.");
let program_hash_hex = format!("{:#x}", program_hash);
assert_eq!(program_hash_hex, expected_program_hash);
}
}