Skip to content

Commit

Permalink
feat: optimize storage for Digest
Browse files Browse the repository at this point in the history
  • Loading branch information
Th0rgal committed Nov 4, 2024
1 parent cce4fbd commit 9c961ea
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions src/utils/digest.cairo
Original file line number Diff line number Diff line change
@@ -1,37 +1,51 @@
use starknet::storage_access::{Store, StorageBaseAddress};
use utils::hash::Digest;
use utils::hash::{Digest, U256IntoDigest, DigestIntoU256};

// todo: pack the felts to save on storage
pub impl DigestStore of starknet::Store<Digest> {
fn read(address_domain: u32, base: StorageBaseAddress) -> starknet::SyscallResult<Digest> {
match Store::<[u32; 8]>::read(address_domain, base) {
Result::Ok(value) => Result::Ok(Digest { value }),
match Store::<felt252>::read(address_domain, base) {
Result::Ok(value) => {
let value_as_u256: u256 = value.into();
Result::Ok(value_as_u256.into())
},
Result::Err(err) => Result::Err(err),
}
}

fn write(
address_domain: u32, base: StorageBaseAddress, value: Digest
) -> starknet::SyscallResult<()> {
Store::<[u32; 8]>::write(address_domain, base, value.value)
let value_as_u256: u256 = value.into();
Store::<felt252>::write(address_domain, base, value_as_u256.try_into().unwrap())
}

fn read_at_offset(
address_domain: u32, base: StorageBaseAddress, offset: u8
) -> starknet::SyscallResult<Digest> {
match Store::<[u32; 8]>::read_at_offset(address_domain, base, offset) {
Result::Ok(value) => Result::Ok(Digest { value }),
match Store::<felt252>::read_at_offset(address_domain, base, offset) {
Result::Ok(value) => {
let value_as_u256: u256 = value.into();
Result::Ok(value_as_u256.into())
},
Result::Err(err) => Result::Err(err),
}
}

fn write_at_offset(
address_domain: u32, base: StorageBaseAddress, offset: u8, value: Digest
) -> starknet::SyscallResult<()> {
Store::<[u32; 8]>::write_at_offset(address_domain, base, offset, value.value)
let value_as_u256: u256 = value.into();
Store::<
felt252
>::write_at_offset(address_domain, base, offset, value_as_u256.try_into().unwrap())
}

// Returns 1 since we store the Bitcoin hash (256 bits) as a single felt (251 bits).
// This is possible because in Bitcoin mainnet, due to minimum difficulty requirements,
// the first 32 bits of the hash are always zeros.
fn size() -> u8 {
Store::<[u32; 8]>::size()
// Store::<[u32; 8]>::size()
1
}
}

0 comments on commit 9c961ea

Please sign in to comment.