-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ed25519/secp256k1 precompiles (#102)
* feat: add ed25519/secp256k1 precompiles * implement pr feedback * replicate banking stage approach to precompiles * don't do what banking does so we actually load the precompiles * update change log * follow pre-existing changelog style * remove unused import
- Loading branch information
1 parent
0c0b46c
commit 609734c
Showing
8 changed files
with
151 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,10 @@ | |
|
||
## [Unreleased] | ||
|
||
### Added | ||
|
||
- Add `LiteSVM::with_precompiles`. | ||
|
||
## [0.3.0] - 2024-10-12 | ||
|
||
### Added | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use solana_sdk::{ | ||
account::{AccountSharedData, WritableAccount}, | ||
feature_set::FeatureSet, | ||
native_loader, | ||
precompiles::get_precompiles, | ||
}; | ||
|
||
use crate::LiteSVM; | ||
|
||
pub(crate) fn load_precompiles(svm: &mut LiteSVM, feature_set: FeatureSet) { | ||
let mut account = AccountSharedData::default(); | ||
account.set_owner(native_loader::id()); | ||
account.set_lamports(1); | ||
account.set_executable(true); | ||
|
||
for precompile in get_precompiles() { | ||
if precompile | ||
.feature | ||
.map_or(true, |feature_id| feature_set.is_active(&feature_id)) | ||
{ | ||
svm.set_account(precompile.program_id, account.clone().into()) | ||
.unwrap(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
use litesvm::LiteSVM; | ||
use solana_sdk::{ | ||
ed25519_instruction::{self, new_ed25519_instruction}, | ||
message::Message, | ||
secp256k1_instruction::{self, new_secp256k1_instruction}, | ||
signature::Keypair, | ||
signer::Signer, | ||
transaction::{Transaction, TransactionError}, | ||
}; | ||
|
||
#[test_log::test] | ||
fn ed25519_precompile_ok() { | ||
let kp = Keypair::new(); | ||
let kp_dalek = ed25519_dalek::Keypair::from_bytes(&kp.to_bytes()).unwrap(); | ||
|
||
let mut svm = LiteSVM::new(); | ||
svm.airdrop(&kp.pubkey(), 10u64.pow(9)).unwrap(); | ||
|
||
// Act - Produce a valid ed25519 instruction. | ||
let ix = new_ed25519_instruction(&kp_dalek, b"hello world"); | ||
let tx = Transaction::new( | ||
&[&kp], | ||
Message::new(&[ix], Some(&kp.pubkey())), | ||
svm.latest_blockhash(), | ||
); | ||
let res = svm.send_transaction(tx); | ||
|
||
// Assert - Transaction passes. | ||
assert!(res.is_ok()); | ||
} | ||
|
||
#[test_log::test] | ||
fn ed25519_precompile_err() { | ||
let kp = Keypair::new(); | ||
let kp_dalek = ed25519_dalek::Keypair::from_bytes(&kp.to_bytes()).unwrap(); | ||
|
||
let mut svm = LiteSVM::new(); | ||
svm.airdrop(&kp.pubkey(), 10u64.pow(9)).unwrap(); | ||
|
||
// Act - Produce an invalid ed25519 instruction. | ||
let mut ix = new_ed25519_instruction(&kp_dalek, b"hello world"); | ||
ix.data[ed25519_instruction::DATA_START + 32] += 1; | ||
let tx = Transaction::new( | ||
&[&kp], | ||
Message::new(&[ix], Some(&kp.pubkey())), | ||
svm.latest_blockhash(), | ||
); | ||
let res = svm.send_transaction(tx); | ||
|
||
// Assert - Transaction fails. | ||
assert_eq!( | ||
res.err().map(|fail| fail.err), | ||
Some(TransactionError::InvalidAccountIndex) | ||
); | ||
} | ||
|
||
#[test_log::test] | ||
fn secp256k1_precompile_ok() { | ||
let kp = Keypair::new(); | ||
let kp_secp256k1 = libsecp256k1::SecretKey::parse_slice(&[1; 32]).unwrap(); | ||
|
||
let mut svm = LiteSVM::new(); | ||
svm.airdrop(&kp.pubkey(), 10u64.pow(9)).unwrap(); | ||
|
||
// Act - Produce a valid secp256k1 instruction. | ||
let ix = new_secp256k1_instruction(&kp_secp256k1, b"hello world"); | ||
let tx = Transaction::new( | ||
&[&kp], | ||
Message::new(&[ix], Some(&kp.pubkey())), | ||
svm.latest_blockhash(), | ||
); | ||
let res = svm.send_transaction(tx); | ||
|
||
// Assert - Transaction passes. | ||
assert!(res.is_ok()); | ||
} | ||
|
||
#[test_log::test] | ||
fn secp256k1_precompile_err() { | ||
let kp = Keypair::new(); | ||
let kp_secp256k1 = libsecp256k1::SecretKey::parse_slice(&[1; 32]).unwrap(); | ||
|
||
let mut svm = LiteSVM::new(); | ||
svm.airdrop(&kp.pubkey(), 10u64.pow(9)).unwrap(); | ||
|
||
// Act - Produce an invalid secp256k1 instruction. | ||
let mut ix = new_secp256k1_instruction(&kp_secp256k1, b"hello world"); | ||
ix.data[secp256k1_instruction::DATA_START + 32] += 1; | ||
let tx = Transaction::new( | ||
&[&kp], | ||
Message::new(&[ix], Some(&kp.pubkey())), | ||
svm.latest_blockhash(), | ||
); | ||
let res = svm.send_transaction(tx); | ||
|
||
// Assert - Transaction fails. | ||
assert_eq!( | ||
res.err().map(|fail| fail.err), | ||
Some(TransactionError::InvalidAccountIndex) | ||
); | ||
} |