Skip to content

Commit

Permalink
[runtime-transaction] add secp256r1 to precompile signature details (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
samkim-crypto authored Dec 4, 2024
1 parent cfc53c7 commit df080d3
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion cost-model/src/transaction_cost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ impl solana_runtime_transaction::transaction_meta::StaticMeta for WritableKeysTr

fn signature_details(&self) -> &solana_sdk::message::TransactionSignatureDetails {
const DUMMY: solana_sdk::message::TransactionSignatureDetails =
solana_sdk::message::TransactionSignatureDetails::new(0, 0, 0);
solana_sdk::message::TransactionSignatureDetails::new(0, 0, 0, 0);
&DUMMY
}

Expand Down
2 changes: 2 additions & 0 deletions programs/sbf/Cargo.lock

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

1 change: 1 addition & 0 deletions runtime-transaction/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ solana-builtins-default-costs = { workspace = true }
solana-compute-budget = { workspace = true }
solana-pubkey = { workspace = true }
solana-sdk = { workspace = true }
solana-sdk-ids = { workspace = true }
solana-svm-transaction = { workspace = true }
thiserror = { workspace = true }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ impl RuntimeTransaction<SanitizedVersionedTransaction> {
),
precompile_signature_details.num_secp256k1_instruction_signatures,
precompile_signature_details.num_ed25519_instruction_signatures,
precompile_signature_details.num_secp256r1_instruction_signatures,
);
let compute_budget_instruction_details = ComputeBudgetInstructionDetails::try_from(
sanitized_versioned_tx
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ impl<D: TransactionData> RuntimeTransaction<SanitizedTransactionView<D>> {
u64::from(transaction.num_required_signatures()),
precompile_signature_details.num_secp256k1_instruction_signatures,
precompile_signature_details.num_ed25519_instruction_signatures,
precompile_signature_details.num_secp256r1_instruction_signatures,
);
let compute_budget_instruction_details =
ComputeBudgetInstructionDetails::try_from(transaction.program_instructions_iter())?;
Expand Down
16 changes: 16 additions & 0 deletions runtime-transaction/src/signature_details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use {
pub struct PrecompileSignatureDetails {
pub num_secp256k1_instruction_signatures: u64,
pub num_ed25519_instruction_signatures: u64,
pub num_secp256r1_instruction_signatures: u64,
}

/// Get transaction signature details.
Expand All @@ -20,6 +21,7 @@ pub fn get_precompile_signature_details<'a>(
// is low enough that the sum of all signatures will not overflow a u64.
let mut num_secp256k1_instruction_signatures: u64 = 0;
let mut num_ed25519_instruction_signatures: u64 = 0;
let mut num_secp256r1_instruction_signatures: u64 = 0;
for (program_id, instruction) in instructions {
let program_id_index = instruction.program_id_index;
match filter.is_signature(program_id_index, program_id) {
Expand All @@ -32,12 +34,17 @@ pub fn get_precompile_signature_details<'a>(
num_ed25519_instruction_signatures = num_ed25519_instruction_signatures
.wrapping_add(get_num_signatures_in_instruction(&instruction));
}
ProgramIdStatus::Secp256r1 => {
num_secp256r1_instruction_signatures = num_secp256r1_instruction_signatures
.wrapping_add(get_num_signatures_in_instruction(&instruction));
}
}
}

PrecompileSignatureDetails {
num_secp256k1_instruction_signatures,
num_ed25519_instruction_signatures,
num_secp256r1_instruction_signatures,
}
}

Expand All @@ -51,6 +58,7 @@ enum ProgramIdStatus {
NotSignature,
Secp256k1,
Ed25519,
Secp256r1,
}

struct SignatureDetailsFilter {
Expand Down Expand Up @@ -86,6 +94,8 @@ impl SignatureDetailsFilter {
ProgramIdStatus::Secp256k1
} else if program_id == &solana_sdk::ed25519_program::ID {
ProgramIdStatus::Ed25519
} else if program_id == &solana_sdk_ids::secp256r1_program::ID {
ProgramIdStatus::Secp256r1
} else {
ProgramIdStatus::NotSignature
}
Expand Down Expand Up @@ -140,26 +150,31 @@ mod tests {
Pubkey::new_unique(),
solana_sdk::secp256k1_program::ID,
solana_sdk::ed25519_program::ID,
solana_sdk_ids::secp256r1_program::ID,
];
let instructions = [
make_instruction(&program_ids, 1, &[5]),
make_instruction(&program_ids, 2, &[3]),
make_instruction(&program_ids, 3, &[4]),
make_instruction(&program_ids, 0, &[]),
make_instruction(&program_ids, 2, &[2]),
make_instruction(&program_ids, 1, &[1]),
make_instruction(&program_ids, 0, &[]),
make_instruction(&program_ids, 3, &[3]),
];

let signature_details = get_precompile_signature_details(instructions.into_iter());
assert_eq!(signature_details.num_secp256k1_instruction_signatures, 6);
assert_eq!(signature_details.num_ed25519_instruction_signatures, 5);
assert_eq!(signature_details.num_secp256r1_instruction_signatures, 7);
}

#[test]
fn test_get_signature_details_missing_num_signatures() {
let program_ids = [
solana_sdk::secp256k1_program::ID,
solana_sdk::ed25519_program::ID,
solana_sdk_ids::secp256r1_program::ID,
];
let instructions = [
make_instruction(&program_ids, 0, &[]),
Expand All @@ -169,5 +184,6 @@ mod tests {
let signature_details = get_precompile_signature_details(instructions.into_iter());
assert_eq!(signature_details.num_secp256k1_instruction_signatures, 0);
assert_eq!(signature_details.num_ed25519_instruction_signatures, 0);
assert_eq!(signature_details.num_secp256r1_instruction_signatures, 0);
}
}
18 changes: 17 additions & 1 deletion sdk/message/src/sanitized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use {
solana_instruction::{BorrowedAccountMeta, BorrowedInstruction},
solana_pubkey::Pubkey,
solana_sanitize::Sanitize,
solana_sdk_ids::{ed25519_program, secp256k1_program},
solana_sdk_ids::{ed25519_program, secp256k1_program, secp256r1_program},
std::{borrow::Cow, collections::HashSet, convert::TryFrom},
};

Expand Down Expand Up @@ -411,6 +411,13 @@ impl SanitizedMessage {
.num_ed25519_instruction_signatures
.saturating_add(u64::from(*num_verifies));
}
} else if secp256r1_program::check_id(program_id) {
if let Some(num_verifies) = instruction.data.first() {
transaction_signature_details.num_secp256r1_instruction_signatures =
transaction_signature_details
.num_secp256r1_instruction_signatures
.saturating_add(u64::from(*num_verifies));
}
}
}

Expand All @@ -425,18 +432,21 @@ pub struct TransactionSignatureDetails {
num_transaction_signatures: u64,
num_secp256k1_instruction_signatures: u64,
num_ed25519_instruction_signatures: u64,
num_secp256r1_instruction_signatures: u64,
}

impl TransactionSignatureDetails {
pub const fn new(
num_transaction_signatures: u64,
num_secp256k1_instruction_signatures: u64,
num_ed25519_instruction_signatures: u64,
num_secp256r1_instruction_signatures: u64,
) -> Self {
Self {
num_transaction_signatures,
num_secp256k1_instruction_signatures,
num_ed25519_instruction_signatures,
num_secp256r1_instruction_signatures,
}
}

Expand All @@ -445,6 +455,7 @@ impl TransactionSignatureDetails {
self.num_transaction_signatures
.saturating_add(self.num_secp256k1_instruction_signatures)
.saturating_add(self.num_ed25519_instruction_signatures)
.saturating_add(self.num_secp256r1_instruction_signatures)
}

/// return the number of transaction signatures
Expand All @@ -461,6 +472,11 @@ impl TransactionSignatureDetails {
pub fn num_ed25519_instruction_signatures(&self) -> u64 {
self.num_ed25519_instruction_signatures
}

/// return the number of secp256k1 instruction signatures
pub fn num_secp256r1_instruction_signatures(&self) -> u64 {
self.num_secp256r1_instruction_signatures
}
}

#[cfg(test)]
Expand Down
2 changes: 2 additions & 0 deletions svm/examples/Cargo.lock

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

1 change: 1 addition & 0 deletions transaction-view/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ edition = { workspace = true }

[dependencies]
solana-sdk = { workspace = true }
solana-sdk-ids = { workspace = true }
solana-svm-transaction = { workspace = true }

[dev-dependencies]
Expand Down
8 changes: 8 additions & 0 deletions transaction-view/src/resolved_transaction_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use {
secp256k1_program,
signature::Signature,
},
solana_sdk_ids::secp256r1_program,
solana_svm_transaction::{
instruction::SVMInstruction, message_address_table_lookup::SVMMessageAddressTableLookup,
svm_message::SVMMessage, svm_transaction::SVMTransaction,
Expand Down Expand Up @@ -165,6 +166,7 @@ impl<D: TransactionData> ResolvedTransactionView<D> {
// counting the number of pre-processor operations separately
let mut num_secp256k1_instruction_signatures: u64 = 0;
let mut num_ed25519_instruction_signatures: u64 = 0;
let mut num_secp256r1_instruction_signatures: u64 = 0;
for (program_id, instruction) in self.program_instructions_iter() {
if secp256k1_program::check_id(program_id) {
if let Some(num_verifies) = instruction.data.first() {
Expand All @@ -176,13 +178,19 @@ impl<D: TransactionData> ResolvedTransactionView<D> {
num_ed25519_instruction_signatures =
num_ed25519_instruction_signatures.wrapping_add(u64::from(*num_verifies));
}
} else if secp256r1_program::check_id(program_id) {
if let Some(num_verifies) = instruction.data.first() {
num_secp256r1_instruction_signatures =
num_secp256r1_instruction_signatures.wrapping_add(u64::from(*num_verifies));
}
}
}

TransactionSignatureDetails::new(
u64::from(self.view.num_required_signatures()),
num_secp256k1_instruction_signatures,
num_ed25519_instruction_signatures,
num_secp256r1_instruction_signatures,
)
}
}
Expand Down

0 comments on commit df080d3

Please sign in to comment.