Skip to content

Commit 0e6484d

Browse files
authored
perf: use solana hash precompile (#1400)
1 parent df585e4 commit 0e6484d

File tree

5 files changed

+24
-8
lines changed

5 files changed

+24
-8
lines changed

pythnet/pythnet_sdk/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ name = "pythnet_sdk"
1313

1414
[features]
1515
test-utils = ["dep:wormhole-vaas-serde", "dep:serde_wormhole", "dep:libsecp256k1", "dep:rand"]
16+
solana-program = ["dep:solana-program"]
1617

1718
[dependencies]
1819
bincode = "1.3.1"
@@ -31,6 +32,7 @@ serde_wormhole = {version ="0.1.0", optional = true}
3132
wormhole-vaas-serde = {version = "0.1.0", optional = true}
3233
libsecp256k1 = {version ="0.7.1", optional = true}
3334
rand = {version = "0.8.5", optional = true}
35+
solana-program = {version = ">=1.13.6", optional = true}
3436

3537
[dev-dependencies]
3638
base64 = "0.21.0"

pythnet/pythnet_sdk/src/hashers/keccak256_160.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,31 @@
1+
#[cfg(not(feature = "solana-program"))]
2+
use sha3::{
3+
Digest,
4+
Keccak256,
5+
};
6+
#[cfg(feature = "solana-program")]
7+
use solana_program::keccak::hashv;
18
use {
29
crate::hashers::Hasher,
310
serde::Serialize,
4-
sha3::{
5-
Digest,
6-
Keccak256,
7-
},
811
};
912

13+
1014
#[derive(Clone, Default, Debug, Eq, Hash, PartialEq, Serialize)]
1115
pub struct Keccak160 {}
1216

1317
impl Hasher for Keccak160 {
1418
type Hash = [u8; 20];
1519

20+
#[cfg(feature = "solana-program")]
21+
fn hashv(data: &[impl AsRef<[u8]>]) -> Self::Hash {
22+
let bytes = hashv(&data.iter().map(|x| x.as_ref()).collect::<Vec<&[u8]>>());
23+
let mut hash = [0u8; 20];
24+
hash.copy_from_slice(&bytes.as_ref()[0..20]);
25+
hash
26+
}
27+
28+
#[cfg(not(feature = "solana-program"))]
1629
fn hashv(data: &[impl AsRef<[u8]>]) -> [u8; 20] {
1730
let mut hasher = Keccak256::new();
1831
data.iter().for_each(|d| hasher.update(d));

target_chains/solana/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

target_chains/solana/programs/pyth-solana-receiver/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ test-bpf = []
1717

1818
[dependencies]
1919
anchor-lang = { workspace = true }
20-
pythnet-sdk = { path = "../../../../pythnet/pythnet_sdk" }
20+
pythnet-sdk = { path = "../../../../pythnet/pythnet_sdk", features = ["solana-program"] }
2121
solana-program = { workspace = true }
2222
byteorder = "1.4.3"
2323
wormhole-core-bridge-solana = {workspace = true}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/**
22
* A hard-coded budget for the compute units required for the `verifyEncodedVaa` instruction in the Wormhole program.
33
*/
4-
export const VERIFY_ENCODED_VAA_COMPUTE_BUDGET = 400000;
4+
export const VERIFY_ENCODED_VAA_COMPUTE_BUDGET = 350000;
55
/**
66
* A hard-coded budget for the compute units required for the `postUpdateAtomic` instruction in the Pyth Solana Receiver program.
77
*/
8-
export const POST_UPDATE_ATOMIC_COMPUTE_BUDGET = 400000;
8+
export const POST_UPDATE_ATOMIC_COMPUTE_BUDGET = 170000;
99
/**
1010
* A hard-coded budget for the compute units required for the `postUpdate` instruction in the Pyth Solana Receiver program.
1111
*/
12-
export const POST_UPDATE_COMPUTE_BUDGET = 200000;
12+
export const POST_UPDATE_COMPUTE_BUDGET = 35000;

0 commit comments

Comments
 (0)