solana_runtime/
verify_precompiles.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
use {
    solana_feature_set::FeatureSet,
    solana_sdk::{
        instruction::InstructionError,
        precompiles::get_precompiles,
        transaction::{Result, TransactionError},
    },
    solana_svm_transaction::svm_message::SVMMessage,
};

pub fn verify_precompiles(message: &impl SVMMessage, feature_set: &FeatureSet) -> Result<()> {
    let mut all_instruction_data = None; // lazily collect this on first pre-compile

    let precompiles = get_precompiles();
    for (index, (program_id, instruction)) in message.program_instructions_iter().enumerate() {
        for precompile in precompiles {
            if precompile.check_id(program_id, |id| feature_set.is_active(id)) {
                let all_instruction_data: &Vec<&[u8]> = all_instruction_data
                    .get_or_insert_with(|| message.instructions_iter().map(|ix| ix.data).collect());
                precompile
                    .verify(instruction.data, all_instruction_data, feature_set)
                    .map_err(|err| {
                        TransactionError::InstructionError(
                            index as u8,
                            InstructionError::Custom(err as u32),
                        )
                    })?;
                break;
            }
        }
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use {
        super::*,
        rand0_7::{thread_rng, Rng},
        solana_sdk::{
            ed25519_instruction::new_ed25519_instruction,
            hash::Hash,
            pubkey::Pubkey,
            secp256k1_instruction::new_secp256k1_instruction,
            signature::Keypair,
            signer::Signer,
            system_instruction, system_transaction,
            transaction::{SanitizedTransaction, Transaction},
        },
    };

    #[test]
    fn test_verify_precompiles_simple_transaction() {
        let tx = SanitizedTransaction::from_transaction_for_tests(system_transaction::transfer(
            &Keypair::new(),
            &Pubkey::new_unique(),
            1,
            Hash::default(),
        ));
        assert!(verify_precompiles(&tx, &FeatureSet::all_enabled()).is_ok());
    }

    #[test]
    fn test_verify_precompiles_secp256k1() {
        let secp_privkey = libsecp256k1::SecretKey::random(&mut thread_rng());
        let message_arr = b"hello";
        let mut secp_instruction = new_secp256k1_instruction(&secp_privkey, message_arr);
        let mint_keypair = Keypair::new();
        let feature_set = FeatureSet::all_enabled();

        let tx =
            SanitizedTransaction::from_transaction_for_tests(Transaction::new_signed_with_payer(
                &[secp_instruction.clone()],
                Some(&mint_keypair.pubkey()),
                &[&mint_keypair],
                Hash::default(),
            ));

        assert!(verify_precompiles(&tx, &feature_set).is_ok());

        let index = thread_rng().gen_range(0, secp_instruction.data.len());
        secp_instruction.data[index] = secp_instruction.data[index].wrapping_add(12);
        let tx =
            SanitizedTransaction::from_transaction_for_tests(Transaction::new_signed_with_payer(
                &[secp_instruction],
                Some(&mint_keypair.pubkey()),
                &[&mint_keypair],
                Hash::default(),
            ));

        assert!(verify_precompiles(&tx, &feature_set).is_err());
    }

    #[test]
    fn test_verify_precompiles_ed25519() {
        let privkey = ed25519_dalek::Keypair::generate(&mut thread_rng());
        let message_arr = b"hello";
        let mut instruction = new_ed25519_instruction(&privkey, message_arr);
        let mint_keypair = Keypair::new();
        let feature_set = FeatureSet::all_enabled();

        let tx =
            SanitizedTransaction::from_transaction_for_tests(Transaction::new_signed_with_payer(
                &[instruction.clone()],
                Some(&mint_keypair.pubkey()),
                &[&mint_keypair],
                Hash::default(),
            ));

        assert!(verify_precompiles(&tx, &feature_set).is_ok());

        let index = loop {
            let index = thread_rng().gen_range(0, instruction.data.len());
            // byte 1 is not used, so this would not cause the verify to fail
            if index != 1 {
                break index;
            }
        };

        instruction.data[index] = instruction.data[index].wrapping_add(12);
        let tx =
            SanitizedTransaction::from_transaction_for_tests(Transaction::new_signed_with_payer(
                &[instruction],
                Some(&mint_keypair.pubkey()),
                &[&mint_keypair],
                Hash::default(),
            ));
        assert!(verify_precompiles(&tx, &feature_set).is_err());
    }

    #[test]
    fn test_verify_precompiles_mixed() {
        let message_arr = b"hello";
        let secp_privkey = libsecp256k1::SecretKey::random(&mut thread_rng());
        let mut secp_instruction = new_secp256k1_instruction(&secp_privkey, message_arr);
        let ed25519_privkey = ed25519_dalek::Keypair::generate(&mut thread_rng());
        let ed25519_instruction = new_ed25519_instruction(&ed25519_privkey, message_arr);

        let mint_keypair = Keypair::new();
        let feature_set = FeatureSet::all_enabled();

        let tx =
            SanitizedTransaction::from_transaction_for_tests(Transaction::new_signed_with_payer(
                &[
                    secp_instruction.clone(),
                    ed25519_instruction.clone(),
                    system_instruction::transfer(&mint_keypair.pubkey(), &Pubkey::new_unique(), 1),
                ],
                Some(&mint_keypair.pubkey()),
                &[&mint_keypair],
                Hash::default(),
            ));
        assert!(verify_precompiles(&tx, &feature_set).is_ok());

        let index = thread_rng().gen_range(0, secp_instruction.data.len());
        secp_instruction.data[index] = secp_instruction.data[index].wrapping_add(12);
        let tx =
            SanitizedTransaction::from_transaction_for_tests(Transaction::new_signed_with_payer(
                &[
                    secp_instruction,
                    ed25519_instruction,
                    system_instruction::transfer(&mint_keypair.pubkey(), &Pubkey::new_unique(), 1),
                ],
                Some(&mint_keypair.pubkey()),
                &[&mint_keypair],
                Hash::default(),
            ));
        assert!(verify_precompiles(&tx, &feature_set).is_err());
    }
}