Skip to content

Commit 857c2b1

Browse files
authored
Add bpf target to hashers (#396)
* Add target_arch bpf * Tweaks * Fix clippy * More target_arch bpf support * Re-export syscall
1 parent 1e765b6 commit 857c2b1

File tree

9 files changed

+61
-47
lines changed

9 files changed

+61
-47
lines changed

blake3-hasher/Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ blake3 = ["dep:blake3"]
2020
[dependencies]
2121
solana-hash = { workspace = true, features = ["decode"] }
2222

23-
[target.'cfg(not(target_os = "solana"))'.dependencies]
24-
blake3 = { workspace = true, optional = true }
25-
26-
[target.'cfg(target_os = "solana")'.dependencies]
23+
[target.'cfg(any(target_os = "solana", target_arch = "bpf"))'.dependencies]
2724
solana-define-syscall = { workspace = true }
2825

26+
[target.'cfg(not(any(target_os = "solana", target_arch = "bpf")))'.dependencies]
27+
blake3 = { workspace = true, optional = true }
28+
2929
[dev-dependencies]
3030
solana-blake3-hasher = { path = ".", features = ["blake3"] }
3131

blake3-hasher/src/lib.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
//! Hashing with the [blake3] hash function.
22
//!
33
//! [blake3]: https://github.com/BLAKE3-team/BLAKE3
4-
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
54
#![no_std]
5+
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
66

77
pub use solana_hash::{Hash, ParseHashError, HASH_BYTES, MAX_BASE58_LEN};
88

99
#[derive(Clone, Default)]
10-
#[cfg(all(feature = "blake3", not(target_os = "solana")))]
10+
#[cfg(all(
11+
feature = "blake3",
12+
not(any(target_os = "solana", target_arch = "bpf"))
13+
))]
1114
pub struct Hasher {
1215
hasher: blake3::Hasher,
1316
}
1417

15-
#[cfg(all(feature = "blake3", not(target_os = "solana")))]
18+
#[cfg(all(
19+
feature = "blake3",
20+
not(any(target_os = "solana", target_arch = "bpf"))
21+
))]
1622
impl Hasher {
1723
pub fn hash(&mut self, val: &[u8]) {
1824
self.hasher.update(val);
@@ -28,11 +34,11 @@ impl Hasher {
2834
}
2935

3036
/// Return a Blake3 hash for the given data.
31-
#[cfg_attr(target_os = "solana", inline(always))]
37+
#[cfg_attr(any(target_os = "solana", target_arch = "bpf"), inline(always))]
3238
pub fn hashv(vals: &[&[u8]]) -> Hash {
3339
// Perform the calculation inline, calling this from within a program is
3440
// not supported
35-
#[cfg(not(target_os = "solana"))]
41+
#[cfg(not(any(target_os = "solana", target_arch = "bpf")))]
3642
{
3743
#[cfg(feature = "blake3")]
3844
{
@@ -47,7 +53,7 @@ pub fn hashv(vals: &[&[u8]]) -> Hash {
4753
}
4854
}
4955
// Call via a system call to perform the calculation
50-
#[cfg(target_os = "solana")]
56+
#[cfg(any(target_os = "solana", target_arch = "bpf"))]
5157
{
5258
let mut hash_result = core::mem::MaybeUninit::<[u8; solana_hash::HASH_BYTES]>::uninit();
5359
// SAFETY: This is sound as sol_blake3 always fills all 32 bytes of our hash
@@ -63,7 +69,7 @@ pub fn hashv(vals: &[&[u8]]) -> Hash {
6369
}
6470

6571
/// Return a Blake3 hash for the given data.
66-
#[cfg_attr(target_os = "solana", inline(always))]
72+
#[cfg_attr(any(target_os = "solana", target_arch = "bpf"), inline(always))]
6773
pub fn hash(val: &[u8]) -> Hash {
6874
hashv(&[val])
6975
}

keccak-hasher/Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ sha3 = ["dep:sha3"]
2020
[dependencies]
2121
solana-hash = { workspace = true, features = ["decode"] }
2222

23-
[target.'cfg(not(target_os = "solana"))'.dependencies]
24-
sha3 = { workspace = true, optional = true }
25-
26-
[target.'cfg(target_os = "solana")'.dependencies]
23+
[target.'cfg(any(target_os = "solana", target_arch = "bpf"))'.dependencies]
2724
solana-define-syscall = { workspace = true }
2825

26+
[target.'cfg(not(any(target_os = "solana", target_arch = "bpf")))'.dependencies]
27+
sha3 = { workspace = true, optional = true }
28+
2929
[lints]
3030
workspace = true

keccak-hasher/src/lib.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
//! Hashing with the [keccak] (SHA-3) hash function.
22
//!
33
//! [keccak]: https://keccak.team/keccak.html
4-
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
54
#![no_std]
5+
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
66

7-
#[cfg(all(feature = "sha3", not(target_os = "solana")))]
7+
#[cfg(all(feature = "sha3", not(any(target_os = "solana", target_arch = "bpf"))))]
88
use sha3::{Digest, Keccak256};
99
pub use solana_hash::{Hash, ParseHashError, HASH_BYTES, MAX_BASE58_LEN};
1010

1111
#[derive(Clone, Default)]
12-
#[cfg(all(feature = "sha3", not(target_os = "solana")))]
12+
#[cfg(all(feature = "sha3", not(any(target_os = "solana", target_arch = "bpf"))))]
1313
pub struct Hasher {
1414
hasher: Keccak256,
1515
}
1616

17-
#[cfg(all(feature = "sha3", not(target_os = "solana")))]
17+
#[cfg(all(feature = "sha3", not(any(target_os = "solana", target_arch = "bpf"))))]
1818
impl Hasher {
1919
pub fn hash(&mut self, val: &[u8]) {
2020
self.hasher.update(val);
@@ -30,11 +30,11 @@ impl Hasher {
3030
}
3131

3232
/// Return a Keccak256 hash for the given data.
33-
#[cfg_attr(target_os = "solana", inline(always))]
33+
#[cfg_attr(any(target_os = "solana", target_arch = "bpf"), inline(always))]
3434
pub fn hashv(vals: &[&[u8]]) -> Hash {
3535
// Perform the calculation inline, calling this from within a program is
3636
// not supported
37-
#[cfg(not(target_os = "solana"))]
37+
#[cfg(not(any(target_os = "solana", target_arch = "bpf")))]
3838
{
3939
#[cfg(feature = "sha3")]
4040
{
@@ -49,7 +49,7 @@ pub fn hashv(vals: &[&[u8]]) -> Hash {
4949
}
5050
}
5151
// Call via a system call to perform the calculation
52-
#[cfg(target_os = "solana")]
52+
#[cfg(any(target_os = "solana", target_arch = "bpf"))]
5353
{
5454
let mut hash_result = core::mem::MaybeUninit::<[u8; solana_hash::HASH_BYTES]>::uninit();
5555
// SAFETY: This is sound as sol_keccak256 always fills all 32 bytes of our hash
@@ -65,7 +65,7 @@ pub fn hashv(vals: &[&[u8]]) -> Hash {
6565
}
6666

6767
/// Return a Keccak256 hash for the given data.
68-
#[cfg_attr(target_os = "solana", inline(always))]
68+
#[cfg_attr(any(target_os = "solana", target_arch = "bpf"), inline(always))]
6969
pub fn hash(val: &[u8]) -> Hash {
7070
hashv(&[val])
7171
}

scripts/check-no-std.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ cd "${src_root}"
1010
no_std_crates=(
1111
-p solana-address
1212
-p solana-account-view
13+
-p solana-blake3-hasher
1314
-p solana-clock
1415
-p solana-commitment-config
1516
-p solana-define-syscall
1617
-p solana-epoch-info
1718
-p solana-epoch-rewards
1819
-p solana-fee-calculator
1920
-p solana-hash
21+
-p solana-keccak-hasher
2022
-p solana-msg
2123
-p solana-program-error
2224
-p solana-program-log

secp256k1-recover/Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ solana-frozen-abi-macro = { workspace = true, optional = true, features = [
2626
] }
2727
thiserror = { workspace = true }
2828

29-
[target.'cfg(not(target_os = "solana"))'.dependencies]
30-
k256 = { workspace = true }
31-
32-
[target.'cfg(target_os = "solana")'.dependencies]
29+
[target.'cfg(any(target_os = "solana", target_arch = "bpf"))'.dependencies]
3330
solana-define-syscall = { workspace = true }
3431

32+
[target.'cfg(not(any(target_os = "solana", target_arch = "bpf")))'.dependencies]
33+
k256 = { workspace = true }
34+
3535
[dev-dependencies]
3636
anyhow = { workspace = true }
3737
borsh = { workspace = true }

secp256k1-recover/src/lib.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl Secp256k1Pubkey {
8787
}
8888
}
8989

90-
#[cfg(target_os = "solana")]
90+
#[cfg(any(target_os = "solana", target_arch = "bpf"))]
9191
pub use solana_define_syscall::definitions::sol_secp256k1_recover;
9292

9393
/// Recover the public key from a [secp256k1] ECDSA signature and
@@ -402,13 +402,13 @@ pub use solana_define_syscall::definitions::sol_secp256k1_recover;
402402
/// Ok(())
403403
/// }
404404
/// ```
405-
#[cfg_attr(target_os = "solana", inline(always))]
405+
#[cfg_attr(any(target_os = "solana", target_arch = "bpf"), inline(always))]
406406
pub fn secp256k1_recover(
407407
hash: &[u8],
408408
recovery_id: u8,
409409
signature: &[u8],
410410
) -> Result<Secp256k1Pubkey, Secp256k1RecoverError> {
411-
#[cfg(target_os = "solana")]
411+
#[cfg(any(target_os = "solana", target_arch = "bpf"))]
412412
{
413413
let mut pubkey_buffer =
414414
core::mem::MaybeUninit::<[u8; SECP256K1_PUBLIC_KEY_LENGTH]>::uninit();
@@ -421,14 +421,15 @@ pub fn secp256k1_recover(
421421
)
422422
};
423423

424-
// SAFETY: This is sound as in our pass case, all 64 bytes of the pubkey are always initialized by sol_secp256k1_recover
424+
// SAFETY: This is sound as in our pass case, all 64 bytes of the pubkey are
425+
// always initialized by sol_secp256k1_recover
425426
match result {
426427
0 => Ok(Secp256k1Pubkey(unsafe { pubkey_buffer.assume_init() })),
427428
error => Err(Secp256k1RecoverError::from(error)),
428429
}
429430
}
430431

431-
#[cfg(not(target_os = "solana"))]
432+
#[cfg(not(any(target_os = "solana", target_arch = "bpf")))]
432433
{
433434
const HASH_SIZE: usize = 32;
434435
if hash.len() != HASH_SIZE {

sha256-hasher/Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ sha2 = ["dep:sha2"]
1818
[dependencies]
1919
solana-hash = { workspace = true }
2020

21-
[target.'cfg(not(target_os = "solana"))'.dependencies]
22-
sha2 = { workspace = true, optional = true }
23-
24-
[target.'cfg(target_os = "solana")'.dependencies]
21+
[target.'cfg(any(target_os = "solana", target_arch = "bpf"))'.dependencies]
2522
solana-define-syscall = { workspace = true }
2623

24+
[target.'cfg(not(any(target_os = "solana", target_arch = "bpf")))'.dependencies]
25+
sha2 = { workspace = true, optional = true }
26+
2727
[dev-dependencies]
2828
solana-sha256-hasher = { path = ".", features = ["sha2"] }
2929

sha256-hasher/src/lib.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
#![no_std]
22
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
3-
#[cfg(all(feature = "sha2", not(target_os = "solana")))]
4-
use sha2::{Digest, Sha256};
3+
4+
#[cfg(any(target_os = "solana", target_arch = "bpf"))]
5+
pub use solana_define_syscall::definitions::sol_sha256;
56
use solana_hash::Hash;
7+
#[cfg(any(target_os = "solana", target_arch = "bpf"))]
8+
use {core::mem::MaybeUninit, solana_hash::HASH_BYTES};
9+
#[cfg(all(feature = "sha2", not(any(target_os = "solana", target_arch = "bpf"))))]
10+
use {
11+
sha2::{Digest, Sha256},
12+
solana_hash::HASH_BYTES,
13+
};
614

7-
#[cfg(all(feature = "sha2", not(target_os = "solana")))]
15+
#[cfg(all(feature = "sha2", not(any(target_os = "solana", target_arch = "bpf"))))]
816
#[derive(Clone, Default)]
917
pub struct Hasher {
1018
hasher: Sha256,
1119
}
1220

13-
#[cfg(all(feature = "sha2", not(target_os = "solana")))]
21+
#[cfg(all(feature = "sha2", not(any(target_os = "solana", target_arch = "bpf"))))]
1422
impl Hasher {
1523
pub fn hash(&mut self, val: &[u8]) {
1624
self.hasher.update(val);
@@ -21,20 +29,17 @@ impl Hasher {
2129
}
2230
}
2331
pub fn result(self) -> Hash {
24-
let bytes: [u8; solana_hash::HASH_BYTES] = self.hasher.finalize().into();
32+
let bytes: [u8; HASH_BYTES] = self.hasher.finalize().into();
2533
bytes.into()
2634
}
2735
}
2836

29-
#[cfg(target_os = "solana")]
30-
pub use solana_define_syscall::definitions::sol_sha256;
31-
3237
/// Return a Sha256 hash for the given data.
3338
#[cfg_attr(target_os = "solana", inline(always))]
3439
pub fn hashv(vals: &[&[u8]]) -> Hash {
3540
// Perform the calculation inline, calling this from within a program is
3641
// not supported
37-
#[cfg(not(target_os = "solana"))]
42+
#[cfg(not(any(target_os = "solana", target_arch = "bpf")))]
3843
{
3944
#[cfg(feature = "sha2")]
4045
{
@@ -49,9 +54,9 @@ pub fn hashv(vals: &[&[u8]]) -> Hash {
4954
}
5055
}
5156
// Call via a system call to perform the calculation
52-
#[cfg(target_os = "solana")]
57+
#[cfg(any(target_os = "solana", target_arch = "bpf"))]
5358
{
54-
let mut hash_result = core::mem::MaybeUninit::<[u8; solana_hash::HASH_BYTES]>::uninit();
59+
let mut hash_result = MaybeUninit::<[u8; HASH_BYTES]>::uninit();
5560
// SAFETY: This is sound as sol_sha256 always fills all 32 bytes of our hash
5661
unsafe {
5762
sol_sha256(

0 commit comments

Comments
 (0)