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

Extract keccak-hasher crate #3408

Merged
merged 7 commits into from
Nov 20, 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
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ members = [
"sdk/hash",
"sdk/inflation",
"sdk/instruction",
"sdk/keccak-hasher",
"sdk/keypair",
"sdk/logger",
"sdk/macro",
Expand Down Expand Up @@ -463,6 +464,7 @@ solana-hash = { path = "sdk/hash", version = "=2.2.0", default-features = false
solana-inflation = { path = "sdk/inflation", version = "=2.2.0" }
solana-inline-spl = { path = "inline-spl", version = "=2.2.0" }
solana-instruction = { path = "sdk/instruction", version = "=2.2.0", default-features = false }
solana-keccak-hasher = { path = "sdk/keccak-hasher", version = "=2.2.0" }
solana-keypair = { path = "sdk/keypair", version = "=2.2.0" }
solana-last-restart-slot = { path = "sdk/last-restart-slot", version = "=2.2.0" }
solana-lattice-hash = { path = "lattice-hash", version = "=2.2.0" }
Expand Down
11 changes: 11 additions & 0 deletions programs/sbf/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 48 additions & 0 deletions sdk/keccak-hasher/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
[package]
name = "solana-keccak-hasher"
description = "Solana Keccak hashing"
documentation = "https://docs.rs/solana-keccak-hasher"
version = { workspace = true }
authors = { workspace = true }
repository = { workspace = true }
homepage = { workspace = true }
license = { workspace = true }
edition = { workspace = true }

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
all-features = true
rustdoc-args = ["--cfg=docsrs"]

[dependencies]
borsh = { workspace = true, optional = true }
serde = { workspace = true, optional = true }
serde_derive = { workspace = true, optional = true }
solana-frozen-abi = { workspace = true, optional = true, features = [
"frozen-abi",
] }
solana-frozen-abi-macro = { workspace = true, optional = true, features = [
"frozen-abi",
] }
solana-hash = { workspace = true }
solana-sanitize = { workspace = true }

[target.'cfg(not(target_os = "solana"))'.dependencies]
sha3 = { workspace = true }

[target.'cfg(target_os = "solana")'.dependencies]
# sha3 should be removed in the next breaking release,
# as there's no reason to use the crate instead of the syscall
# onchain
sha3 = { workspace = true, optional = true }
solana-define-syscall = { workspace = true }

[features]
borsh = ["dep:borsh", "std"]
frozen-abi = ["dep:solana-frozen-abi", "dep:solana-frozen-abi-macro", "std"]
serde = ["dep:serde", "dep:serde_derive"]
sha3 = ["dep:sha3"]
std = ["solana-hash/std"]

[lints]
workspace = true
90 changes: 50 additions & 40 deletions sdk/program/src/keccak.rs → sdk/keccak-hasher/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,51 @@
//! Hashing with the [keccak] (SHA-3) hash function.
//!
//! [keccak]: https://keccak.team/keccak.html

#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![cfg_attr(feature = "frozen-abi", feature(min_specialization))]
#![no_std]
#[cfg(feature = "std")]
extern crate std;

#[cfg(any(feature = "sha3", not(target_os = "solana")))]
use sha3::{Digest, Keccak256};
pub use solana_hash::{ParseHashError, HASH_BYTES, MAX_BASE58_LEN};
#[cfg(feature = "borsh")]
use borsh::{BorshDeserialize, BorshSchema, BorshSerialize};
use {
sha3::{Digest, Keccak256},
borsh::{BorshDeserialize, BorshSchema, BorshSerialize},
std::string::ToString,
};
use {
core::{fmt, str::FromStr},
solana_sanitize::Sanitize,
std::{convert::TryFrom, fmt, str::FromStr},
thiserror::Error,
};

pub const HASH_BYTES: usize = 32;
/// Maximum string length of a base58 encoded hash
const MAX_BASE58_LEN: usize = 44;
#[cfg_attr(feature = "frozen-abi", derive(AbiExample))]
// TODO: replace this with `solana_hash::Hash` in the
// next breaking change.
// It's a breaking change because the field is public
// here and private in `solana_hash`, and making
// it public in `solana_hash` would break wasm-bindgen
Comment on lines +23 to +27

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Someone tried to combine all the hashes awhile ago, and we couldn't for this exact reason 🙃

#[cfg_attr(feature = "frozen-abi", derive(solana_frozen_abi_macro::AbiExample))]
#[cfg_attr(
feature = "borsh",
derive(BorshSerialize, BorshDeserialize, BorshSchema),
borsh(crate = "borsh")
)]
#[derive(Serialize, Deserialize, Clone, Copy, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[cfg_attr(
feature = "serde",
derive(serde_derive::Deserialize, serde_derive::Serialize)
)]
#[derive(Clone, Copy, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[repr(transparent)]
pub struct Hash(pub [u8; HASH_BYTES]);

#[cfg(any(feature = "sha3", not(target_os = "solana")))]
#[derive(Clone, Default)]
pub struct Hasher {
hasher: Keccak256,
}

#[cfg(any(feature = "sha3", not(target_os = "solana")))]
impl Hasher {
pub fn hash(&mut self, val: &[u8]) {
self.hasher.update(val);
Expand All @@ -43,6 +60,18 @@ impl Hasher {
}
}

impl From<solana_hash::Hash> for Hash {
fn from(val: solana_hash::Hash) -> Self {
Self(val.to_bytes())
}
}

impl From<Hash> for solana_hash::Hash {
fn from(val: Hash) -> Self {
Self::new_from_array(val.0)
}
}

impl Sanitize for Hash {}

impl AsRef<[u8]> for Hash {
Expand All @@ -53,46 +82,32 @@ impl AsRef<[u8]> for Hash {

impl fmt::Debug for Hash {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", bs58::encode(self.0).into_string())
let converted: solana_hash::Hash = (*self).into();
fmt::Debug::fmt(&converted, f)
}
}

impl fmt::Display for Hash {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", bs58::encode(self.0).into_string())
let converted: solana_hash::Hash = (*self).into();
fmt::Display::fmt(&converted, f)
}
}

#[derive(Debug, Clone, PartialEq, Eq, Error)]
pub enum ParseHashError {
#[error("string decoded to wrong size for hash")]
WrongSize,
#[error("failed to decoded string to hash")]
Invalid,
}

impl FromStr for Hash {
type Err = ParseHashError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.len() > MAX_BASE58_LEN {
return Err(ParseHashError::WrongSize);
}
bs58::decode(s)
.into_vec()
.map_err(|_| ParseHashError::Invalid)
.and_then(|bytes| {
<[u8; HASH_BYTES]>::try_from(bytes)
.map(Hash::new_from_array)
.map_err(|_| ParseHashError::WrongSize)
})
let unconverted = solana_hash::Hash::from_str(s)?;
Ok(unconverted.into())
}
}

impl Hash {
#[deprecated(since = "2.2.0", note = "Use 'Hash::new_from_array' instead")]
pub fn new(hash_slice: &[u8]) -> Self {
Hash(<[u8; HASH_BYTES]>::try_from(hash_slice).unwrap())
#[allow(deprecated)]
Self::from(solana_hash::Hash::new(hash_slice))
}

pub const fn new_from_array(hash_array: [u8; HASH_BYTES]) -> Self {
Expand All @@ -101,13 +116,7 @@ impl Hash {

/// unique Hash for tests and benchmarks.
pub fn new_unique() -> Self {
use solana_atomic_u64::AtomicU64;
static I: AtomicU64 = AtomicU64::new(1);

let mut b = [0u8; HASH_BYTES];
let i = I.fetch_add(1);
b[0..8].copy_from_slice(&i.to_le_bytes());
Self::new_from_array(b)
Self::from(solana_hash::Hash::new_unique())
}

pub fn to_bytes(self) -> [u8; HASH_BYTES] {
Expand All @@ -130,7 +139,7 @@ pub fn hashv(vals: &[&[u8]]) -> Hash {
{
let mut hash_result = [0; HASH_BYTES];
unsafe {
crate::syscalls::sol_keccak256(
solana_define_syscall::definitions::sol_keccak256(
vals as *const _ as *const u8,
vals.len() as u64,
&mut hash_result as *mut _ as *mut u8,
Expand All @@ -145,6 +154,7 @@ pub fn hash(val: &[u8]) -> Hash {
hashv(&[val])
}

#[cfg(feature = "std")]
/// Return the hash of the given hash extended with the given value.
pub fn extend_and_hash(id: &Hash, val: &[u8]) -> Hash {
let mut hash_data = id.as_ref().to_vec();
Expand Down
1 change: 1 addition & 0 deletions sdk/program/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ solana-instruction = { workspace = true, default-features = false, features = [
"serde",
"std",
] }
solana-keccak-hasher = { workspace = true, features = ["sha3"] }
solana-last-restart-slot = { workspace = true, features = ["serde", "sysvar"] }
solana-msg = { workspace = true }
solana-native-token = { workspace = true }
Expand Down
3 changes: 2 additions & 1 deletion sdk/program/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,6 @@ pub mod feature;
pub mod hash;
pub mod incinerator;
pub mod instruction;
pub mod keccak;
pub mod lamports;
pub mod loader_instruction;
pub mod loader_upgradeable_instruction;
Expand Down Expand Up @@ -522,6 +521,8 @@ pub use solana_borsh::v1 as borsh1;
pub use solana_epoch_rewards as epoch_rewards;
#[deprecated(since = "2.1.0", note = "Use `solana-fee-calculator` crate instead")]
pub use solana_fee_calculator as fee_calculator;
#[deprecated(since = "2.2.0", note = "Use `solana-keccak-hasher` crate instead")]
pub use solana_keccak_hasher as keccak;
#[deprecated(since = "2.1.0", note = "Use `solana-last-restart-slot` crate instead")]
pub use solana_last_restart_slot as last_restart_slot;
#[deprecated(since = "2.1.0", note = "Use `solana-program-memory` crate instead")]
Expand Down
11 changes: 11 additions & 0 deletions svm/examples/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading