solana_perf/
sigverify.rs

1//! The `sigverify` module provides digital signature verification functions.
2//! By default, signatures are verified in parallel using all available CPU
3//! cores.  When perf-libs are available signature verification is offloaded
4//! to the GPU.
5//!
6use {
7    crate::{
8        cuda_runtime::PinnedVec,
9        packet::{
10            BytesPacketBatch, Packet, PacketBatch, PacketFlags, PacketRef, PacketRefMut,
11            PinnedPacketBatch, PACKET_DATA_SIZE,
12        },
13        perf_libs,
14        recycler::Recycler,
15    },
16    rayon::{prelude::*, ThreadPool},
17    solana_hash::Hash,
18    solana_message::{MESSAGE_HEADER_LENGTH, MESSAGE_VERSION_PREFIX},
19    solana_pubkey::Pubkey,
20    solana_rayon_threadlimit::get_thread_count,
21    solana_short_vec::decode_shortu16_len,
22    solana_signature::Signature,
23    std::{borrow::Cow, convert::TryFrom, mem::size_of},
24};
25
26// Empirically derived to constrain max verify latency to ~8ms at lower packet counts
27pub const VERIFY_PACKET_CHUNK_SIZE: usize = 128;
28
29static PAR_THREAD_POOL: std::sync::LazyLock<ThreadPool> = std::sync::LazyLock::new(|| {
30    rayon::ThreadPoolBuilder::new()
31        .num_threads(get_thread_count())
32        .thread_name(|i| format!("solSigVerify{i:02}"))
33        .build()
34        .unwrap()
35});
36
37pub type TxOffset = PinnedVec<u32>;
38
39type TxOffsets = (TxOffset, TxOffset, TxOffset, TxOffset, Vec<Vec<u32>>);
40
41#[derive(Debug, PartialEq, Eq)]
42struct PacketOffsets {
43    pub sig_len: u32,
44    pub sig_start: u32,
45    pub msg_start: u32,
46    pub pubkey_start: u32,
47    pub pubkey_len: u32,
48    pub instruction_len: u32,
49    pub instruction_start: u32,
50}
51
52impl PacketOffsets {
53    pub fn new(
54        sig_len: u32,
55        sig_start: u32,
56        msg_start: u32,
57        pubkey_start: u32,
58        pubkey_len: u32,
59        instruction_len: u32,
60        instruction_start: u32,
61    ) -> Self {
62        Self {
63            sig_len,
64            sig_start,
65            msg_start,
66            pubkey_start,
67            pubkey_len,
68            instruction_len,
69            instruction_start,
70        }
71    }
72}
73
74#[derive(Debug, PartialEq, Eq)]
75pub enum PacketError {
76    InvalidLen,
77    InvalidPubkeyLen,
78    InvalidShortVec,
79    InvalidSignatureLen,
80    MismatchSignatureLen,
81    PayerNotWritable,
82    InvalidProgramIdIndex,
83    InvalidNumberOfInstructions,
84    UnsupportedVersion,
85}
86
87impl std::convert::From<std::boxed::Box<bincode::ErrorKind>> for PacketError {
88    fn from(_e: std::boxed::Box<bincode::ErrorKind>) -> PacketError {
89        PacketError::InvalidShortVec
90    }
91}
92
93impl std::convert::From<std::num::TryFromIntError> for PacketError {
94    fn from(_e: std::num::TryFromIntError) -> Self {
95        Self::InvalidLen
96    }
97}
98
99pub fn init() {
100    if let Some(api) = perf_libs::api() {
101        unsafe {
102            (api.ed25519_set_verbose)(true);
103            assert!((api.ed25519_init)(), "ed25519_init() failed");
104            (api.ed25519_set_verbose)(false);
105        }
106    }
107}
108
109/// Returns true if the signatrue on the packet verifies.
110/// Caller must do packet.set_discard(true) if this returns false.
111#[must_use]
112fn verify_packet(packet: &mut PacketRefMut, reject_non_vote: bool) -> bool {
113    // If this packet was already marked as discard, drop it
114    if packet.meta().discard() {
115        return false;
116    }
117
118    let packet_offsets = get_packet_offsets(packet, 0, reject_non_vote);
119    let mut sig_start = packet_offsets.sig_start as usize;
120    let mut pubkey_start = packet_offsets.pubkey_start as usize;
121    let msg_start = packet_offsets.msg_start as usize;
122
123    if packet_offsets.sig_len == 0 {
124        return false;
125    }
126
127    if packet.meta().size <= msg_start {
128        return false;
129    }
130
131    for _ in 0..packet_offsets.sig_len {
132        let pubkey_end = pubkey_start.saturating_add(size_of::<Pubkey>());
133        let Some(sig_end) = sig_start.checked_add(size_of::<Signature>()) else {
134            return false;
135        };
136        let Some(Ok(signature)) = packet.data(sig_start..sig_end).map(Signature::try_from) else {
137            return false;
138        };
139        let Some(pubkey) = packet.data(pubkey_start..pubkey_end) else {
140            return false;
141        };
142        let Some(message) = packet.data(msg_start..) else {
143            return false;
144        };
145        if !signature.verify(pubkey, message) {
146            return false;
147        }
148        pubkey_start = pubkey_end;
149        sig_start = sig_end;
150    }
151    true
152}
153
154pub fn count_packets_in_batches(batches: &[PacketBatch]) -> usize {
155    batches.iter().map(|batch| batch.len()).sum()
156}
157
158pub fn count_valid_packets<'a>(batches: impl IntoIterator<Item = &'a PacketBatch>) -> usize {
159    batches
160        .into_iter()
161        .map(|batch| batch.into_iter().filter(|p| !p.meta().discard()).count())
162        .sum()
163}
164
165pub fn count_discarded_packets(batches: &[PacketBatch]) -> usize {
166    batches
167        .iter()
168        .map(|batch| batch.iter().filter(|p| p.meta().discard()).count())
169        .sum()
170}
171
172// internal function to be unit-tested; should be used only by get_packet_offsets
173fn do_get_packet_offsets(
174    packet: PacketRef,
175    current_offset: usize,
176) -> Result<PacketOffsets, PacketError> {
177    // should have at least 1 signature and sig lengths
178    let _ = 1usize
179        .checked_add(size_of::<Signature>())
180        .filter(|v| *v <= packet.meta().size)
181        .ok_or(PacketError::InvalidLen)?;
182
183    // read the length of Transaction.signatures (serialized with short_vec)
184    let (sig_len_untrusted, sig_size) = packet
185        .data(..)
186        .and_then(|bytes| decode_shortu16_len(bytes).ok())
187        .ok_or(PacketError::InvalidShortVec)?;
188    // Using msg_start_offset which is based on sig_len_untrusted introduces uncertainty.
189    // Ultimately, the actual sigverify will determine the uncertainty.
190    let msg_start_offset = sig_len_untrusted
191        .checked_mul(size_of::<Signature>())
192        .and_then(|v| v.checked_add(sig_size))
193        .ok_or(PacketError::InvalidLen)?;
194
195    // Determine the start of the message header by checking the message prefix bit.
196    let msg_header_offset = {
197        // Packet should have data for prefix bit
198        if msg_start_offset >= packet.meta().size {
199            return Err(PacketError::InvalidSignatureLen);
200        }
201
202        // next byte indicates if the transaction is versioned. If the top bit
203        // is set, the remaining bits encode a version number. If the top bit is
204        // not set, this byte is the first byte of the message header.
205        let message_prefix = *packet
206            .data(msg_start_offset)
207            .ok_or(PacketError::InvalidSignatureLen)?;
208        if message_prefix & MESSAGE_VERSION_PREFIX != 0 {
209            let version = message_prefix & !MESSAGE_VERSION_PREFIX;
210            match version {
211                0 => {
212                    // header begins immediately after prefix byte
213                    msg_start_offset
214                        .checked_add(1)
215                        .ok_or(PacketError::InvalidLen)?
216                }
217
218                // currently only v0 is supported
219                _ => return Err(PacketError::UnsupportedVersion),
220            }
221        } else {
222            msg_start_offset
223        }
224    };
225
226    let msg_header_offset_plus_one = msg_header_offset
227        .checked_add(1)
228        .ok_or(PacketError::InvalidLen)?;
229
230    // Packet should have data at least for MessageHeader and 1 byte for Message.account_keys.len
231    let _ = msg_header_offset_plus_one
232        .checked_add(MESSAGE_HEADER_LENGTH)
233        .filter(|v| *v <= packet.meta().size)
234        .ok_or(PacketError::InvalidSignatureLen)?;
235
236    // read MessageHeader.num_required_signatures (serialized with u8)
237    let sig_len_maybe_trusted = *packet
238        .data(msg_header_offset)
239        .ok_or(PacketError::InvalidSignatureLen)?;
240    let message_account_keys_len_offset = msg_header_offset
241        .checked_add(MESSAGE_HEADER_LENGTH)
242        .ok_or(PacketError::InvalidSignatureLen)?;
243
244    // This reads and compares the MessageHeader num_required_signatures and
245    // num_readonly_signed_accounts bytes. If num_required_signatures is not larger than
246    // num_readonly_signed_accounts, the first account is not debitable, and cannot be charged
247    // required transaction fees.
248    let readonly_signer_offset = msg_header_offset_plus_one;
249    if sig_len_maybe_trusted
250        <= *packet
251            .data(readonly_signer_offset)
252            .ok_or(PacketError::InvalidSignatureLen)?
253    {
254        return Err(PacketError::PayerNotWritable);
255    }
256
257    if usize::from(sig_len_maybe_trusted) != sig_len_untrusted {
258        return Err(PacketError::MismatchSignatureLen);
259    }
260
261    // read the length of Message.account_keys (serialized with short_vec)
262    let (pubkey_len, pubkey_len_size) = packet
263        .data(message_account_keys_len_offset..)
264        .and_then(|bytes| decode_shortu16_len(bytes).ok())
265        .ok_or(PacketError::InvalidShortVec)?;
266    let pubkey_start = message_account_keys_len_offset
267        .checked_add(pubkey_len_size)
268        .ok_or(PacketError::InvalidPubkeyLen)?;
269
270    let blockhash_offset = pubkey_len
271        .checked_mul(size_of::<Pubkey>())
272        .and_then(|v| v.checked_add(pubkey_start))
273        .filter(|v| *v <= packet.meta().size)
274        .ok_or(PacketError::InvalidPubkeyLen)?;
275
276    if pubkey_len < sig_len_untrusted {
277        return Err(PacketError::InvalidPubkeyLen);
278    }
279
280    // read instruction length
281    let instructions_len_offset = blockhash_offset
282        .checked_add(size_of::<Hash>())
283        .ok_or(PacketError::InvalidLen)?;
284
285    // Packet should have at least 1 more byte for instructions.len
286    let _ = instructions_len_offset
287        .checked_add(1usize)
288        .filter(|v| *v <= packet.meta().size)
289        .ok_or(PacketError::InvalidLen)?;
290
291    let (instruction_len, instruction_len_size) = packet
292        .data(instructions_len_offset..)
293        .and_then(|bytes| decode_shortu16_len(bytes).ok())
294        .ok_or(PacketError::InvalidLen)?;
295
296    // SIMD-0160: skip if has more than 64 top instructions
297    if instruction_len > solana_transaction_context::MAX_INSTRUCTION_TRACE_LENGTH {
298        return Err(PacketError::InvalidNumberOfInstructions);
299    }
300
301    let instruction_start = instructions_len_offset
302        .checked_add(instruction_len_size)
303        .ok_or(PacketError::InvalidLen)?;
304
305    // Packet should have at least 1 more byte for one instructions_program_id
306    let _ = instruction_start
307        .checked_add(1usize)
308        .filter(|v| *v <= packet.meta().size)
309        .ok_or(PacketError::InvalidLen)?;
310
311    let sig_start = current_offset
312        .checked_add(sig_size)
313        .ok_or(PacketError::InvalidLen)?;
314    let msg_start = current_offset
315        .checked_add(msg_start_offset)
316        .ok_or(PacketError::InvalidLen)?;
317    let pubkey_start = current_offset
318        .checked_add(pubkey_start)
319        .ok_or(PacketError::InvalidLen)?;
320    let instruction_start = current_offset
321        .checked_add(instruction_start)
322        .ok_or(PacketError::InvalidLen)?;
323
324    Ok(PacketOffsets::new(
325        u32::try_from(sig_len_untrusted)?,
326        u32::try_from(sig_start)?,
327        u32::try_from(msg_start)?,
328        u32::try_from(pubkey_start)?,
329        u32::try_from(pubkey_len)?,
330        u32::try_from(instruction_len)?,
331        u32::try_from(instruction_start)?,
332    ))
333}
334
335fn get_packet_offsets(
336    packet: &mut PacketRefMut,
337    current_offset: usize,
338    reject_non_vote: bool,
339) -> PacketOffsets {
340    let unsanitized_packet_offsets = do_get_packet_offsets(packet.as_ref(), current_offset);
341    if let Ok(offsets) = unsanitized_packet_offsets {
342        check_for_simple_vote_transaction(packet, &offsets, current_offset).ok();
343        if !reject_non_vote || packet.meta().is_simple_vote_tx() {
344            return offsets;
345        }
346    }
347    // force sigverify to fail by returning zeros
348    PacketOffsets::new(0, 0, 0, 0, 0, 0, 0)
349}
350
351fn check_for_simple_vote_transaction(
352    packet: &mut PacketRefMut,
353    packet_offsets: &PacketOffsets,
354    current_offset: usize,
355) -> Result<(), PacketError> {
356    // vote could have 1 or 2 sigs; zero sig has already been excluded at
357    // do_get_packet_offsets.
358    if packet_offsets.sig_len > 2 {
359        return Err(PacketError::InvalidSignatureLen);
360    }
361
362    // simple vote should only be legacy message
363    let msg_start = (packet_offsets.msg_start as usize)
364        .checked_sub(current_offset)
365        .ok_or(PacketError::InvalidLen)?;
366    let message_prefix = *packet.data(msg_start).ok_or(PacketError::InvalidLen)?;
367    if message_prefix & MESSAGE_VERSION_PREFIX != 0 {
368        return Ok(());
369    }
370
371    let pubkey_start = (packet_offsets.pubkey_start as usize)
372        .checked_sub(current_offset)
373        .ok_or(PacketError::InvalidLen)?;
374
375    // skip if has more than 1 instruction
376    if packet_offsets.instruction_len != 1 {
377        return Err(PacketError::InvalidNumberOfInstructions);
378    }
379
380    let instruction_start = (packet_offsets.instruction_start as usize)
381        .checked_sub(current_offset)
382        .ok_or(PacketError::InvalidLen)?;
383
384    let instruction_program_id_index: usize = usize::from(
385        *packet
386            .data(instruction_start)
387            .ok_or(PacketError::InvalidLen)?,
388    );
389
390    if instruction_program_id_index >= packet_offsets.pubkey_len as usize {
391        return Err(PacketError::InvalidProgramIdIndex);
392    }
393
394    let instruction_program_id_start = instruction_program_id_index
395        .checked_mul(size_of::<Pubkey>())
396        .and_then(|v| v.checked_add(pubkey_start))
397        .ok_or(PacketError::InvalidLen)?;
398    let instruction_program_id_end = instruction_program_id_start
399        .checked_add(size_of::<Pubkey>())
400        .ok_or(PacketError::InvalidLen)?;
401
402    if packet
403        .data(instruction_program_id_start..instruction_program_id_end)
404        .ok_or(PacketError::InvalidLen)?
405        == solana_sdk_ids::vote::id().as_ref()
406    {
407        packet.meta_mut().flags |= PacketFlags::SIMPLE_VOTE_TX;
408    }
409    Ok(())
410}
411
412pub fn generate_offsets(
413    batches: &mut [PacketBatch],
414    recycler: &Recycler<TxOffset>,
415    reject_non_vote: bool,
416) -> TxOffsets {
417    debug!("allocating..");
418    let mut signature_offsets: PinnedVec<_> = recycler.allocate("sig_offsets");
419    signature_offsets.set_pinnable();
420    let mut pubkey_offsets: PinnedVec<_> = recycler.allocate("pubkey_offsets");
421    pubkey_offsets.set_pinnable();
422    let mut msg_start_offsets: PinnedVec<_> = recycler.allocate("msg_start_offsets");
423    msg_start_offsets.set_pinnable();
424    let mut msg_sizes: PinnedVec<_> = recycler.allocate("msg_size_offsets");
425    msg_sizes.set_pinnable();
426    let mut current_offset: usize = 0;
427    let offsets = batches
428        .iter_mut()
429        .map(|batch| {
430            batch
431                .iter_mut()
432                .map(|mut packet| {
433                    let packet_offsets =
434                        get_packet_offsets(&mut packet, current_offset, reject_non_vote);
435
436                    trace!("pubkey_offset: {}", packet_offsets.pubkey_start);
437
438                    let mut pubkey_offset = packet_offsets.pubkey_start;
439                    let mut sig_offset = packet_offsets.sig_start;
440                    let msg_size = current_offset.saturating_add(packet.meta().size) as u32;
441                    for _ in 0..packet_offsets.sig_len {
442                        signature_offsets.push(sig_offset);
443                        sig_offset = sig_offset.saturating_add(size_of::<Signature>() as u32);
444
445                        pubkey_offsets.push(pubkey_offset);
446                        pubkey_offset = pubkey_offset.saturating_add(size_of::<Pubkey>() as u32);
447
448                        msg_start_offsets.push(packet_offsets.msg_start);
449
450                        let msg_size = msg_size.saturating_sub(packet_offsets.msg_start);
451                        msg_sizes.push(msg_size);
452                    }
453                    current_offset = current_offset.saturating_add(size_of::<Packet>());
454                    packet_offsets.sig_len
455                })
456                .collect()
457        })
458        .collect();
459    (
460        signature_offsets,
461        pubkey_offsets,
462        msg_start_offsets,
463        msg_sizes,
464        offsets,
465    )
466}
467
468fn split_batches(batches: Vec<PacketBatch>) -> (Vec<BytesPacketBatch>, Vec<PinnedPacketBatch>) {
469    let mut bytes_batches = Vec::new();
470    let mut pinned_batches = Vec::new();
471    for batch in batches {
472        match batch {
473            PacketBatch::Bytes(batch) => bytes_batches.push(batch),
474            PacketBatch::Pinned(batch) => pinned_batches.push(batch),
475        }
476    }
477    (bytes_batches, pinned_batches)
478}
479
480macro_rules! shrink_batches_fn {
481    ($fn_name:ident, $batch_ty:ty) => {
482        fn $fn_name(batches: &mut Vec<$batch_ty>) {
483            let mut valid_batch_ix = 0;
484            let mut valid_packet_ix = 0;
485            let mut last_valid_batch = 0;
486            for batch_ix in 0..batches.len() {
487                let cur_batch = batches.get_mut(batch_ix).unwrap();
488                for packet_ix in 0..cur_batch.len() {
489                    if batches[batch_ix][packet_ix].meta().discard() {
490                        continue;
491                    }
492                    last_valid_batch = batch_ix.saturating_add(1);
493                    let mut found_spot = false;
494                    while valid_batch_ix < batch_ix && !found_spot {
495                        while valid_packet_ix < batches[valid_batch_ix].len() {
496                            if batches[valid_batch_ix][valid_packet_ix].meta().discard() {
497                                batches[valid_batch_ix][valid_packet_ix] =
498                                    batches[batch_ix][packet_ix].clone();
499                                batches[batch_ix][packet_ix].meta_mut().set_discard(true);
500                                last_valid_batch = valid_batch_ix.saturating_add(1);
501                                found_spot = true;
502                                break;
503                            }
504                            valid_packet_ix = valid_packet_ix.saturating_add(1);
505                        }
506                        if valid_packet_ix >= batches[valid_batch_ix].len() {
507                            valid_packet_ix = 0;
508                            valid_batch_ix = valid_batch_ix.saturating_add(1);
509                        }
510                    }
511                }
512            }
513            batches.truncate(last_valid_batch);
514        }
515    };
516}
517
518shrink_batches_fn!(shrink_bytes_batches, BytesPacketBatch);
519shrink_batches_fn!(shrink_pinned_batches, PinnedPacketBatch);
520
521pub fn shrink_batches(batches: Vec<PacketBatch>) -> Vec<PacketBatch> {
522    let (mut bytes_batches, mut pinned_batches) = split_batches(batches);
523    shrink_bytes_batches(&mut bytes_batches);
524    shrink_pinned_batches(&mut pinned_batches);
525    bytes_batches
526        .into_iter()
527        .map(PacketBatch::Bytes)
528        .chain(pinned_batches.into_iter().map(PacketBatch::Pinned))
529        .collect()
530}
531
532pub fn ed25519_verify_cpu(batches: &mut [PacketBatch], reject_non_vote: bool, packet_count: usize) {
533    debug!("CPU ECDSA for {packet_count}");
534    PAR_THREAD_POOL.install(|| {
535        batches.par_iter_mut().flatten().for_each(|mut packet| {
536            if !packet.meta().discard() && !verify_packet(&mut packet, reject_non_vote) {
537                packet.meta_mut().set_discard(true);
538            }
539        });
540    });
541}
542
543pub fn ed25519_verify_disabled(batches: &mut [PacketBatch]) {
544    let packet_count = count_packets_in_batches(batches);
545    debug!("disabled ECDSA for {packet_count}");
546    PAR_THREAD_POOL.install(|| {
547        batches.par_iter_mut().flatten().for_each(|mut packet| {
548            packet.meta_mut().set_discard(false);
549        });
550    });
551}
552
553pub fn copy_return_values<I, T>(sig_lens: I, out: &PinnedVec<u8>, rvs: &mut [Vec<u8>])
554where
555    I: IntoIterator<Item = T>,
556    T: IntoIterator<Item = u32>,
557{
558    debug_assert!(rvs.iter().flatten().all(|&rv| rv == 0u8));
559    let mut offset = 0usize;
560    let rvs = rvs.iter_mut().flatten();
561    for (k, rv) in sig_lens.into_iter().flatten().zip(rvs) {
562        let out = out[offset..].iter().take(k as usize).all(|&x| x == 1u8);
563        *rv = u8::from(k != 0u32 && out);
564        offset = offset.saturating_add(k as usize);
565    }
566}
567
568// return true for success, i.e ge unpacks and !ge.is_small_order()
569pub fn check_packed_ge_small_order(ge: &[u8; 32]) -> bool {
570    if let Some(api) = perf_libs::api() {
571        unsafe {
572            // Returns 1 == fail, 0 == success
573            let res = (api.ed25519_check_packed_ge_small_order)(ge.as_ptr());
574
575            return res == 0;
576        }
577    }
578    false
579}
580
581pub fn get_checked_scalar(scalar: &[u8; 32]) -> Result<[u8; 32], PacketError> {
582    let mut out = [0u8; 32];
583    if let Some(api) = perf_libs::api() {
584        unsafe {
585            let res = (api.ed25519_get_checked_scalar)(out.as_mut_ptr(), scalar.as_ptr());
586            if res == 0 {
587                return Ok(out);
588            } else {
589                return Err(PacketError::InvalidLen);
590            }
591        }
592    }
593    Ok(out)
594}
595
596pub fn mark_disabled(batches: &mut [PacketBatch], r: &[Vec<u8>]) {
597    for (batch, v) in batches.iter_mut().zip(r) {
598        for (mut pkt, f) in batch.iter_mut().zip(v) {
599            if !pkt.meta().discard() {
600                pkt.meta_mut().set_discard(*f == 0);
601            }
602        }
603    }
604}
605
606pub fn ed25519_verify(
607    batches: &mut [PacketBatch],
608    recycler: &Recycler<TxOffset>,
609    recycler_out: &Recycler<PinnedVec<u8>>,
610    reject_non_vote: bool,
611    valid_packet_count: usize,
612) {
613    let Some(api) = perf_libs::api() else {
614        return ed25519_verify_cpu(batches, reject_non_vote, valid_packet_count);
615    };
616    let total_packet_count = count_packets_in_batches(batches);
617    // micro-benchmarks show GPU time for smallest batch around 15-20ms
618    // and CPU speed for 64-128 sigverifies around 10-20ms. 64 is a nice
619    // power-of-two number around that accounting for the fact that the CPU
620    // may be busy doing other things while being a real validator
621    // TODO: dynamically adjust this crossover
622    let maybe_valid_percentage = 100usize
623        .wrapping_mul(valid_packet_count)
624        .checked_div(total_packet_count);
625    let Some(valid_percentage) = maybe_valid_percentage else {
626        return;
627    };
628    if valid_percentage < 90 || valid_packet_count < 64 {
629        ed25519_verify_cpu(batches, reject_non_vote, valid_packet_count);
630        return;
631    }
632
633    let (signature_offsets, pubkey_offsets, msg_start_offsets, msg_sizes, sig_lens) =
634        generate_offsets(batches, recycler, reject_non_vote);
635
636    debug!("CUDA ECDSA for {valid_packet_count}");
637    debug!("allocating out..");
638    let mut out = recycler_out.allocate("out_buffer");
639    out.set_pinnable();
640    let mut elems = Vec::new();
641    let mut rvs = Vec::new();
642
643    let mut num_packets: usize = 0;
644    // `BytesPacketBatch` cannot be directly used in CUDA. We have to retrieve
645    // and convert byte batches to pinned batches. We must collect here so that
646    // we keep the batches created by `BytesPacketBatch::to_pinned_packet_batch()`
647    // alive.
648    let pinned_batches = batches
649        .iter_mut()
650        .map(|batch| match batch {
651            PacketBatch::Pinned(batch) => Cow::Borrowed(batch),
652            PacketBatch::Bytes(batch) => Cow::Owned(batch.to_pinned_packet_batch()),
653        })
654        .collect::<Vec<_>>();
655    for batch in pinned_batches.iter() {
656        elems.push(perf_libs::Elems {
657            elems: batch.as_ptr().cast::<u8>(),
658            num: batch.len() as u32,
659        });
660        let v = vec![0u8; batch.len()];
661        rvs.push(v);
662        num_packets = num_packets.saturating_add(batch.len());
663    }
664    out.resize(signature_offsets.len(), 0);
665    trace!("Starting verify num packets: {num_packets}");
666    trace!("elem len: {}", elems.len() as u32);
667    trace!("packet sizeof: {}", size_of::<Packet>() as u32);
668    trace!("len offset: {}", PACKET_DATA_SIZE as u32);
669    const USE_NON_DEFAULT_STREAM: u8 = 1;
670    unsafe {
671        let res = (api.ed25519_verify_many)(
672            elems.as_ptr(),
673            elems.len() as u32,
674            size_of::<Packet>() as u32,
675            num_packets as u32,
676            signature_offsets.len() as u32,
677            msg_sizes.as_ptr(),
678            pubkey_offsets.as_ptr(),
679            signature_offsets.as_ptr(),
680            msg_start_offsets.as_ptr(),
681            out.as_mut_ptr(),
682            USE_NON_DEFAULT_STREAM,
683        );
684        if res != 0 {
685            trace!("RETURN!!!: {res}");
686        }
687    }
688    trace!("done verify");
689    copy_return_values(sig_lens, &out, &mut rvs);
690    mark_disabled(batches, &rvs);
691}
692
693#[cfg(test)]
694#[allow(clippy::arithmetic_side_effects)]
695mod tests {
696    use {
697        super::*,
698        crate::{
699            packet::{
700                to_packet_batches, BytesPacket, BytesPacketBatch, Packet, PinnedPacketBatch,
701                PACKETS_PER_BATCH,
702            },
703            sigverify::{self, PacketOffsets},
704            test_tx::{
705                new_test_tx_with_number_of_ixs, new_test_vote_tx, test_multisig_tx, test_tx,
706            },
707        },
708        bincode::{deserialize, serialize},
709        bytes::{BufMut, Bytes, BytesMut},
710        curve25519_dalek::{edwards::CompressedEdwardsY, scalar::Scalar},
711        rand::{thread_rng, Rng},
712        solana_keypair::Keypair,
713        solana_message::{compiled_instruction::CompiledInstruction, Message, MessageHeader},
714        solana_signature::Signature,
715        solana_signer::Signer,
716        solana_transaction::{versioned::VersionedTransaction, Transaction},
717        std::{
718            iter::repeat_with,
719            sync::atomic::{AtomicU64, Ordering},
720        },
721        test_case::test_case,
722    };
723
724    const SIG_OFFSET: usize = 1;
725
726    pub fn memfind<A: Eq>(a: &[A], b: &[A]) -> Option<usize> {
727        assert!(a.len() >= b.len());
728        let end = a.len() - b.len() + 1;
729        (0..end).find(|&i| a[i..i + b.len()] == b[..])
730    }
731
732    #[test]
733    fn test_copy_return_values() {
734        let mut rng = rand::thread_rng();
735        let sig_lens: Vec<Vec<u32>> = {
736            let size = rng.gen_range(0..64);
737            repeat_with(|| {
738                let size = rng.gen_range(0..16);
739                repeat_with(|| rng.gen_range(0..5)).take(size).collect()
740            })
741            .take(size)
742            .collect()
743        };
744        let out: Vec<Vec<Vec<bool>>> = sig_lens
745            .iter()
746            .map(|sig_lens| {
747                sig_lens
748                    .iter()
749                    .map(|&size| repeat_with(|| rng.gen()).take(size as usize).collect())
750                    .collect()
751            })
752            .collect();
753        let expected: Vec<Vec<u8>> = out
754            .iter()
755            .map(|out| {
756                out.iter()
757                    .map(|out| u8::from(!out.is_empty() && out.iter().all(|&k| k)))
758                    .collect()
759            })
760            .collect();
761        let out =
762            PinnedVec::<u8>::from_vec(out.into_iter().flatten().flatten().map(u8::from).collect());
763        let mut rvs: Vec<Vec<u8>> = sig_lens
764            .iter()
765            .map(|sig_lens| vec![0u8; sig_lens.len()])
766            .collect();
767        copy_return_values(sig_lens, &out, &mut rvs);
768        assert_eq!(rvs, expected);
769    }
770
771    #[test]
772    fn test_mark_disabled() {
773        let batch_size = 1;
774        let mut batch = BytesPacketBatch::with_capacity(batch_size);
775        batch.resize(batch_size, BytesPacket::empty());
776        let mut batches: Vec<PacketBatch> = vec![batch.into()];
777        mark_disabled(&mut batches, &[vec![0]]);
778        assert!(batches[0].get(0).unwrap().meta().discard());
779        batches[0].get_mut(0).unwrap().meta_mut().set_discard(false);
780        mark_disabled(&mut batches, &[vec![1]]);
781        assert!(!batches[0].get(0).unwrap().meta().discard());
782    }
783
784    #[test]
785    fn test_layout() {
786        let tx = test_tx();
787        let tx_bytes = serialize(&tx).unwrap();
788        let packet = serialize(&tx).unwrap();
789        assert_matches!(memfind(&packet, &tx_bytes), Some(0));
790        assert_matches!(memfind(&packet, &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]), None);
791    }
792
793    #[test]
794    fn test_system_transaction_layout() {
795        let tx = test_tx();
796        let tx_bytes = serialize(&tx).unwrap();
797        let message_data = tx.message_data();
798        let mut packet = BytesPacket::from_data(None, tx.clone()).unwrap();
799
800        let packet_offsets = sigverify::get_packet_offsets(&mut packet.as_mut(), 0, false);
801
802        assert_eq!(
803            memfind(&tx_bytes, tx.signatures[0].as_ref()),
804            Some(SIG_OFFSET)
805        );
806        assert_eq!(
807            memfind(&tx_bytes, tx.message().account_keys[0].as_ref()),
808            Some(packet_offsets.pubkey_start as usize)
809        );
810        assert_eq!(
811            memfind(&tx_bytes, &message_data),
812            Some(packet_offsets.msg_start as usize)
813        );
814        assert_eq!(
815            memfind(&tx_bytes, tx.signatures[0].as_ref()),
816            Some(packet_offsets.sig_start as usize)
817        );
818        assert_eq!(packet_offsets.sig_len, 1);
819    }
820
821    fn packet_from_num_sigs(required_num_sigs: u8, actual_num_sigs: usize) -> BytesPacket {
822        let message = Message {
823            header: MessageHeader {
824                num_required_signatures: required_num_sigs,
825                num_readonly_signed_accounts: 12,
826                num_readonly_unsigned_accounts: 11,
827            },
828            account_keys: vec![],
829            recent_blockhash: Hash::default(),
830            instructions: vec![],
831        };
832        let mut tx = Transaction::new_unsigned(message);
833        tx.signatures = vec![Signature::default(); actual_num_sigs];
834        BytesPacket::from_data(None, tx).unwrap()
835    }
836
837    #[test]
838    fn test_untrustworthy_sigs() {
839        let required_num_sigs = 14;
840        let actual_num_sigs = 5;
841
842        let packet = packet_from_num_sigs(required_num_sigs, actual_num_sigs);
843
844        let unsanitized_packet_offsets = sigverify::do_get_packet_offsets(packet.as_ref(), 0);
845
846        assert_eq!(
847            unsanitized_packet_offsets,
848            Err(PacketError::MismatchSignatureLen)
849        );
850    }
851
852    #[test]
853    fn test_small_packet() {
854        let tx = test_tx();
855        let mut data = bincode::serialize(&tx).unwrap();
856
857        data[0] = 0xff;
858        data[1] = 0xff;
859        data.truncate(2);
860
861        let packet = BytesPacket::from_bytes(None, Bytes::from(data));
862        let res = sigverify::do_get_packet_offsets(packet.as_ref(), 0);
863        assert_eq!(res, Err(PacketError::InvalidLen));
864    }
865
866    #[test]
867    fn test_pubkey_too_small() {
868        agave_logger::setup();
869        let mut tx = test_tx();
870        let sig = tx.signatures[0];
871        const NUM_SIG: usize = 18;
872        tx.signatures = vec![sig; NUM_SIG];
873        tx.message.account_keys = vec![];
874        tx.message.header.num_required_signatures = NUM_SIG as u8;
875        let mut packet = BytesPacket::from_data(None, tx).unwrap();
876
877        let res = sigverify::do_get_packet_offsets(packet.as_ref(), 0);
878        assert_eq!(res, Err(PacketError::InvalidPubkeyLen));
879
880        assert!(!verify_packet(&mut packet.as_mut(), false));
881
882        packet.meta_mut().set_discard(false);
883        let mut batches = generate_packet_batches(&packet, 1, 1);
884        ed25519_verify(&mut batches);
885        assert!(batches[0].get(0).unwrap().meta().discard());
886    }
887
888    #[test]
889    fn test_pubkey_len() {
890        // See that the verify cannot walk off the end of the packet
891        // trying to index into the account_keys to access pubkey.
892        agave_logger::setup();
893
894        const NUM_SIG: usize = 17;
895        let keypair1 = Keypair::new();
896        let pubkey1 = keypair1.pubkey();
897        let mut message = Message::new(&[], Some(&pubkey1));
898        message.account_keys.push(pubkey1);
899        message.account_keys.push(pubkey1);
900        message.header.num_required_signatures = NUM_SIG as u8;
901        message.recent_blockhash = Hash::new_from_array(pubkey1.to_bytes());
902        let mut tx = Transaction::new_unsigned(message);
903
904        info!("message: {:?}", tx.message_data());
905        info!("tx: {tx:?}");
906        let sig = keypair1.try_sign_message(&tx.message_data()).unwrap();
907        tx.signatures = vec![sig; NUM_SIG];
908
909        let mut packet = BytesPacket::from_data(None, tx).unwrap();
910
911        let res = sigverify::do_get_packet_offsets(packet.as_ref(), 0);
912        assert_eq!(res, Err(PacketError::InvalidPubkeyLen));
913
914        assert!(!verify_packet(&mut packet.as_mut(), false));
915
916        packet.meta_mut().set_discard(false);
917        let mut batches = generate_packet_batches(&packet, 1, 1);
918        ed25519_verify(&mut batches);
919        assert!(batches[0].get(0).unwrap().meta().discard());
920    }
921
922    #[test]
923    fn test_large_sig_len() {
924        let tx = test_tx();
925        let mut data = bincode::serialize(&tx).unwrap();
926
927        // Make the signatures len huge
928        data[0] = 0x7f;
929
930        let packet = BytesPacket::from_bytes(None, Bytes::from(data));
931        let res = sigverify::do_get_packet_offsets(packet.as_ref(), 0);
932        assert_eq!(res, Err(PacketError::InvalidSignatureLen));
933    }
934
935    #[test]
936    fn test_really_large_sig_len() {
937        let tx = test_tx();
938        let mut data = bincode::serialize(&tx).unwrap();
939
940        // Make the signatures len huge
941        data[0] = 0xff;
942        data[1] = 0xff;
943        data[2] = 0xff;
944        data[3] = 0xff;
945
946        let packet = BytesPacket::from_bytes(None, Bytes::from(data));
947        let res = sigverify::do_get_packet_offsets(packet.as_ref(), 0);
948        assert_eq!(res, Err(PacketError::InvalidShortVec));
949    }
950
951    #[test]
952    fn test_invalid_pubkey_len() {
953        let tx = test_tx();
954        let mut data = bincode::serialize(&tx).unwrap();
955        let packet = BytesPacket::from_bytes(None, Bytes::from(data.clone()));
956
957        let offsets = sigverify::do_get_packet_offsets(packet.as_ref(), 0).unwrap();
958
959        // make pubkey len huge
960        data[offsets.pubkey_start as usize - 1] = 0x7f;
961
962        let packet = BytesPacket::from_bytes(None, Bytes::from(data));
963        let res = sigverify::do_get_packet_offsets(packet.as_ref(), 0);
964        assert_eq!(res, Err(PacketError::InvalidPubkeyLen));
965    }
966
967    #[test]
968    fn test_fee_payer_is_debitable() {
969        let message = Message {
970            header: MessageHeader {
971                num_required_signatures: 1,
972                num_readonly_signed_accounts: 1,
973                num_readonly_unsigned_accounts: 1,
974            },
975            account_keys: vec![],
976            recent_blockhash: Hash::default(),
977            instructions: vec![],
978        };
979        let mut tx = Transaction::new_unsigned(message);
980        tx.signatures = vec![Signature::default()];
981        let packet = BytesPacket::from_data(None, tx).unwrap();
982        let res = sigverify::do_get_packet_offsets(packet.as_ref(), 0);
983
984        assert_eq!(res, Err(PacketError::PayerNotWritable));
985    }
986
987    #[test]
988    fn test_unsupported_version() {
989        let tx = test_tx();
990        let mut data = bincode::serialize(&tx).unwrap();
991        let packet = BytesPacket::from_bytes(None, Bytes::from(data.clone()));
992
993        let res = sigverify::do_get_packet_offsets(packet.as_ref(), 0);
994
995        // set message version to 1
996        data[res.unwrap().msg_start as usize] = MESSAGE_VERSION_PREFIX + 1;
997
998        let packet = BytesPacket::from_bytes(None, Bytes::from(data));
999        let res = sigverify::do_get_packet_offsets(packet.as_ref(), 0);
1000        assert_eq!(res, Err(PacketError::UnsupportedVersion));
1001    }
1002
1003    #[test]
1004    fn test_versioned_message() {
1005        let tx = test_tx();
1006        let packet = BytesPacket::from_data(None, tx).unwrap();
1007
1008        let mut legacy_offsets = sigverify::do_get_packet_offsets(packet.as_ref(), 0).unwrap();
1009
1010        // set message version to 0
1011        let msg_start = legacy_offsets.msg_start as usize;
1012        let msg_bytes = packet.data(msg_start..).unwrap();
1013        let mut buf = BytesMut::with_capacity(packet.meta().size + 1);
1014        buf.put_slice(packet.data(..msg_start).unwrap());
1015        buf.put_u8(MESSAGE_VERSION_PREFIX);
1016        buf.put_slice(msg_bytes);
1017        let packet = BytesPacket::from_bytes(None, buf.freeze());
1018
1019        let offsets = sigverify::do_get_packet_offsets(packet.as_ref(), 0).unwrap();
1020        let expected_offsets = {
1021            legacy_offsets.pubkey_start += 1;
1022            legacy_offsets.instruction_start += 1;
1023            legacy_offsets
1024        };
1025
1026        assert_eq!(expected_offsets, offsets);
1027    }
1028
1029    #[test]
1030    fn test_system_transaction_data_layout() {
1031        let mut tx0 = test_tx();
1032        tx0.message.instructions[0].data = vec![1, 2, 3];
1033        let message0a = tx0.message_data();
1034        let tx_bytes = serialize(&tx0).unwrap();
1035        assert!(tx_bytes.len() <= PACKET_DATA_SIZE);
1036        assert_eq!(
1037            memfind(&tx_bytes, tx0.signatures[0].as_ref()),
1038            Some(SIG_OFFSET)
1039        );
1040        let tx1 = deserialize(&tx_bytes).unwrap();
1041        assert_eq!(tx0, tx1);
1042        assert_eq!(tx1.message().instructions[0].data, vec![1, 2, 3]);
1043
1044        tx0.message.instructions[0].data = vec![1, 2, 4];
1045        let message0b = tx0.message_data();
1046        assert_ne!(message0a, message0b);
1047    }
1048
1049    // Just like get_packet_offsets, but not returning redundant information.
1050    fn get_packet_offsets_from_tx(tx: Transaction, current_offset: u32) -> PacketOffsets {
1051        let mut packet = BytesPacket::from_data(None, tx).unwrap();
1052        let packet_offsets =
1053            sigverify::get_packet_offsets(&mut packet.as_mut(), current_offset as usize, false);
1054        PacketOffsets::new(
1055            packet_offsets.sig_len,
1056            packet_offsets.sig_start - current_offset,
1057            packet_offsets.msg_start - packet_offsets.sig_start,
1058            packet_offsets.pubkey_start - packet_offsets.msg_start,
1059            packet_offsets.pubkey_len,
1060            packet_offsets.instruction_len,
1061            packet_offsets.instruction_start - packet_offsets.pubkey_start,
1062        )
1063    }
1064
1065    #[test]
1066    fn test_get_packet_offsets() {
1067        assert_eq!(
1068            get_packet_offsets_from_tx(test_tx(), 0),
1069            PacketOffsets::new(1, 1, 64, 4, 2, 1, 97)
1070        );
1071        assert_eq!(
1072            get_packet_offsets_from_tx(test_tx(), 100),
1073            PacketOffsets::new(1, 1, 64, 4, 2, 1, 97)
1074        );
1075
1076        // Ensure we're not indexing packet by the `current_offset` parameter.
1077        assert_eq!(
1078            get_packet_offsets_from_tx(test_tx(), 1_000_000),
1079            PacketOffsets::new(1, 1, 64, 4, 2, 1, 97)
1080        );
1081
1082        // Ensure we're returning sig_len, not sig_size.
1083        assert_eq!(
1084            get_packet_offsets_from_tx(test_multisig_tx(), 0),
1085            PacketOffsets::new(2, 1, 128, 4, 4, 1, 161)
1086        );
1087    }
1088
1089    fn generate_data_batches_random_size<T>(
1090        data: &T,
1091        max_packets_per_batch: usize,
1092        num_batches: usize,
1093    ) -> Vec<Vec<Vec<u8>>>
1094    where
1095        T: serde::Serialize,
1096    {
1097        let data = bincode::serialize(data).unwrap();
1098
1099        // generate packet vector
1100        let batches: Vec<_> = (0..num_batches)
1101            .map(|_| {
1102                let num_elems_per_batch = thread_rng().gen_range(1..max_packets_per_batch);
1103                let packet_batch = vec![data.clone(); num_elems_per_batch];
1104                assert_eq!(packet_batch.len(), num_elems_per_batch);
1105                packet_batch
1106            })
1107            .collect();
1108        assert_eq!(batches.len(), num_batches);
1109
1110        batches
1111    }
1112
1113    fn generate_bytes_packet_batches(
1114        packet: &BytesPacket,
1115        num_packets_per_batch: usize,
1116        num_batches: usize,
1117    ) -> Vec<BytesPacketBatch> {
1118        let batches: Vec<BytesPacketBatch> = (0..num_batches)
1119            .map(|_| {
1120                let mut packet_batch = BytesPacketBatch::with_capacity(num_packets_per_batch);
1121                for _ in 0..num_packets_per_batch {
1122                    packet_batch.push(packet.clone());
1123                }
1124                assert_eq!(packet_batch.len(), num_packets_per_batch);
1125                packet_batch
1126            })
1127            .collect();
1128        assert_eq!(batches.len(), num_batches);
1129
1130        batches
1131    }
1132
1133    fn generate_packet_batches(
1134        packet: &BytesPacket,
1135        num_packets_per_batch: usize,
1136        num_batches: usize,
1137    ) -> Vec<PacketBatch> {
1138        // generate packet vector
1139        let batches: Vec<PacketBatch> = (0..num_batches)
1140            .map(|_| {
1141                let mut packet_batch = BytesPacketBatch::with_capacity(num_packets_per_batch);
1142                for _ in 0..num_packets_per_batch {
1143                    packet_batch.push(packet.clone());
1144                }
1145                assert_eq!(packet_batch.len(), num_packets_per_batch);
1146                packet_batch.into()
1147            })
1148            .collect();
1149        assert_eq!(batches.len(), num_batches);
1150
1151        batches
1152    }
1153
1154    fn test_verify_n(n: usize, modify_data: bool) {
1155        let tx = test_tx();
1156        let mut data = bincode::serialize(&tx).unwrap();
1157
1158        // jumble some data to test failure
1159        if modify_data {
1160            data[20] = data[20].wrapping_add(10);
1161        }
1162
1163        let packet = BytesPacket::from_bytes(None, Bytes::from(data));
1164        let mut batches = generate_packet_batches(&packet, n, 2);
1165
1166        // verify packets
1167        ed25519_verify(&mut batches);
1168
1169        // check result
1170        let should_discard = modify_data;
1171        assert!(batches
1172            .iter()
1173            .flat_map(|batch| batch.iter())
1174            .all(|p| p.meta().discard() == should_discard));
1175    }
1176
1177    fn ed25519_verify(batches: &mut [PacketBatch]) {
1178        let recycler = Recycler::default();
1179        let recycler_out = Recycler::default();
1180        let packet_count = sigverify::count_packets_in_batches(batches);
1181        sigverify::ed25519_verify(batches, &recycler, &recycler_out, false, packet_count);
1182    }
1183
1184    #[test]
1185    fn test_verify_tampered_sig_len() {
1186        let mut tx = test_tx();
1187        // pretend malicious leader dropped a signature...
1188        tx.signatures.pop();
1189        let packet = BytesPacket::from_data(None, tx).unwrap();
1190
1191        let mut batches = generate_packet_batches(&packet, 1, 1);
1192
1193        // verify packets
1194        ed25519_verify(&mut batches);
1195        assert!(batches
1196            .iter()
1197            .flat_map(|batch| batch.iter())
1198            .all(|p| p.meta().discard()));
1199    }
1200
1201    #[test]
1202    fn test_verify_zero() {
1203        test_verify_n(0, false);
1204    }
1205
1206    #[test]
1207    fn test_verify_one() {
1208        test_verify_n(1, false);
1209    }
1210
1211    #[test]
1212    fn test_verify_seventy_one() {
1213        test_verify_n(71, false);
1214    }
1215
1216    #[test]
1217    fn test_verify_medium_pass() {
1218        test_verify_n(VERIFY_PACKET_CHUNK_SIZE, false);
1219    }
1220
1221    #[test]
1222    fn test_verify_large_pass() {
1223        test_verify_n(VERIFY_PACKET_CHUNK_SIZE * get_thread_count(), false);
1224    }
1225
1226    #[test]
1227    fn test_verify_medium_fail() {
1228        test_verify_n(VERIFY_PACKET_CHUNK_SIZE, true);
1229    }
1230
1231    #[test]
1232    fn test_verify_large_fail() {
1233        test_verify_n(VERIFY_PACKET_CHUNK_SIZE * get_thread_count(), true);
1234    }
1235
1236    #[test]
1237    fn test_verify_multisig() {
1238        agave_logger::setup();
1239
1240        let tx = test_multisig_tx();
1241        let mut data = bincode::serialize(&tx).unwrap();
1242
1243        let n = 4;
1244        let num_batches = 3;
1245        let packet = BytesPacket::from_bytes(None, Bytes::from(data.clone()));
1246        let mut batches = generate_bytes_packet_batches(&packet, n, num_batches);
1247
1248        data[40] = data[40].wrapping_add(8);
1249        let packet = BytesPacket::from_bytes(None, Bytes::from(data.clone()));
1250
1251        batches[0].push(packet);
1252
1253        // verify packets
1254        let mut batches: Vec<PacketBatch> = batches.into_iter().map(PacketBatch::from).collect();
1255        ed25519_verify(&mut batches);
1256
1257        // check result
1258        let ref_ans = 1u8;
1259        let mut ref_vec = vec![vec![ref_ans; n]; num_batches];
1260        ref_vec[0].push(0u8);
1261        assert!(batches
1262            .iter()
1263            .flat_map(|batch| batch.iter())
1264            .zip(ref_vec.into_iter().flatten())
1265            .all(|(p, discard)| {
1266                if discard == 0 {
1267                    p.meta().discard()
1268                } else {
1269                    !p.meta().discard()
1270                }
1271            }));
1272    }
1273
1274    #[test]
1275    fn test_verify_fuzz() {
1276        agave_logger::setup();
1277
1278        let tx = test_multisig_tx();
1279        let packet = BytesPacket::from_data(None, tx).unwrap();
1280
1281        let recycler = Recycler::default();
1282        let recycler_out = Recycler::default();
1283        for _ in 0..50 {
1284            let num_batches = thread_rng().gen_range(2..30);
1285            let mut batches = generate_data_batches_random_size(&packet, 128, num_batches);
1286
1287            let num_modifications = thread_rng().gen_range(0..5);
1288            for _ in 0..num_modifications {
1289                let batch = thread_rng().gen_range(0..batches.len());
1290                let packet = thread_rng().gen_range(0..batches[batch].len());
1291                let offset = thread_rng().gen_range(0..batches[batch][packet].len());
1292                let add = thread_rng().gen_range(0..255);
1293                batches[batch][packet][offset] = batches[batch][packet][offset].wrapping_add(add);
1294            }
1295
1296            let mut batches: Vec<PacketBatch> = batches
1297                .iter()
1298                .map(|batch| {
1299                    let mut packet_batch = BytesPacketBatch::with_capacity(batch.len());
1300                    for data in batch {
1301                        let packet = BytesPacket::from_bytes(None, Bytes::from(data.clone()));
1302                        packet_batch.push(packet);
1303                    }
1304                    packet_batch.into()
1305                })
1306                .collect();
1307
1308            let batch_to_disable = thread_rng().gen_range(0..batches.len());
1309            for mut p in batches[batch_to_disable].iter_mut() {
1310                p.meta_mut().set_discard(true);
1311            }
1312
1313            // verify from GPU verification pipeline (when GPU verification is enabled) are
1314            // equivalent to the CPU verification pipeline.
1315            let mut batches_cpu = batches.clone();
1316            let packet_count = sigverify::count_packets_in_batches(&batches);
1317            sigverify::ed25519_verify(&mut batches, &recycler, &recycler_out, false, packet_count);
1318            ed25519_verify_cpu(&mut batches_cpu, false, packet_count);
1319
1320            // check result
1321            batches
1322                .iter()
1323                .flat_map(|batch| batch.iter())
1324                .zip(batches_cpu.iter().flat_map(|batch| batch.iter()))
1325                .for_each(|(p1, p2)| assert_eq!(p1, p2));
1326        }
1327    }
1328
1329    #[test]
1330    fn test_verify_fail() {
1331        test_verify_n(5, true);
1332    }
1333
1334    #[test]
1335    fn test_get_checked_scalar() {
1336        agave_logger::setup();
1337        if perf_libs::api().is_none() {
1338            return;
1339        }
1340
1341        let passed_g = AtomicU64::new(0);
1342        let failed_g = AtomicU64::new(0);
1343        (0..4).into_par_iter().for_each(|_| {
1344            let mut input = [0u8; 32];
1345            let mut passed = 0;
1346            let mut failed = 0;
1347            for _ in 0..1_000_000 {
1348                thread_rng().fill(&mut input);
1349                let ans = get_checked_scalar(&input);
1350                let ref_ans = Scalar::from_canonical_bytes(input).into_option();
1351                if let Some(ref_ans) = ref_ans {
1352                    passed += 1;
1353                    assert_eq!(ans.unwrap(), ref_ans.to_bytes());
1354                } else {
1355                    failed += 1;
1356                    assert!(ans.is_err());
1357                }
1358            }
1359            passed_g.fetch_add(passed, Ordering::Relaxed);
1360            failed_g.fetch_add(failed, Ordering::Relaxed);
1361        });
1362        info!(
1363            "passed: {} failed: {}",
1364            passed_g.load(Ordering::Relaxed),
1365            failed_g.load(Ordering::Relaxed)
1366        );
1367    }
1368
1369    #[test]
1370    fn test_ge_small_order() {
1371        agave_logger::setup();
1372        if perf_libs::api().is_none() {
1373            return;
1374        }
1375
1376        let passed_g = AtomicU64::new(0);
1377        let failed_g = AtomicU64::new(0);
1378        (0..4).into_par_iter().for_each(|_| {
1379            let mut input = [0u8; 32];
1380            let mut passed = 0;
1381            let mut failed = 0;
1382            for _ in 0..1_000_000 {
1383                thread_rng().fill(&mut input);
1384                let ans = check_packed_ge_small_order(&input);
1385                let ref_ge = CompressedEdwardsY::from_slice(&input).unwrap();
1386                if let Some(ref_element) = ref_ge.decompress() {
1387                    if ref_element.is_small_order() {
1388                        assert!(!ans);
1389                    } else {
1390                        assert!(ans);
1391                    }
1392                } else {
1393                    assert!(!ans);
1394                }
1395                if ans {
1396                    passed += 1;
1397                } else {
1398                    failed += 1;
1399                }
1400            }
1401            passed_g.fetch_add(passed, Ordering::Relaxed);
1402            failed_g.fetch_add(failed, Ordering::Relaxed);
1403        });
1404        info!(
1405            "passed: {} failed: {}",
1406            passed_g.load(Ordering::Relaxed),
1407            failed_g.load(Ordering::Relaxed)
1408        );
1409    }
1410
1411    #[test]
1412    fn test_is_simple_vote_transaction() {
1413        agave_logger::setup();
1414        let mut rng = rand::thread_rng();
1415
1416        // transfer tx is not
1417        {
1418            let mut tx = test_tx();
1419            tx.message.instructions[0].data = vec![1, 2, 3];
1420            let mut packet = BytesPacket::from_data(None, tx).unwrap();
1421            let packet_offsets = do_get_packet_offsets(packet.as_ref(), 0).unwrap();
1422            check_for_simple_vote_transaction(&mut packet.as_mut(), &packet_offsets, 0).ok();
1423            assert!(!packet.meta().is_simple_vote_tx());
1424        }
1425
1426        // single legacy vote tx is
1427        {
1428            let mut tx = new_test_vote_tx(&mut rng);
1429            tx.message.instructions[0].data = vec![1, 2, 3];
1430            let mut packet = BytesPacket::from_data(None, tx).unwrap();
1431            let packet_offsets = do_get_packet_offsets(packet.as_ref(), 0).unwrap();
1432            check_for_simple_vote_transaction(&mut packet.as_mut(), &packet_offsets, 0).ok();
1433            assert!(packet.meta().is_simple_vote_tx());
1434        }
1435
1436        // single versioned vote tx is not
1437        {
1438            let mut tx = new_test_vote_tx(&mut rng);
1439            tx.message.instructions[0].data = vec![1, 2, 3];
1440            let packet = BytesPacket::from_data(None, tx).unwrap();
1441
1442            // set messager version to v0
1443            let mut packet_offsets = do_get_packet_offsets(packet.as_ref(), 0).unwrap();
1444            let msg_start = packet_offsets.msg_start as usize;
1445            let msg_bytes = packet.data(msg_start..).unwrap();
1446            let mut buf = BytesMut::with_capacity(packet.meta().size + 1);
1447            buf.put_slice(packet.data(..msg_start).unwrap());
1448            buf.put_u8(MESSAGE_VERSION_PREFIX);
1449            buf.put_slice(msg_bytes);
1450            let mut packet = BytesPacket::from_bytes(None, buf.freeze());
1451
1452            packet_offsets = do_get_packet_offsets(packet.as_ref(), 0).unwrap();
1453            check_for_simple_vote_transaction(&mut packet.as_mut(), &packet_offsets, 0).ok();
1454            assert!(!packet.meta().is_simple_vote_tx());
1455        }
1456
1457        // multiple mixed tx is not
1458        {
1459            let key = Keypair::new();
1460            let key1 = Pubkey::new_unique();
1461            let key2 = Pubkey::new_unique();
1462            let tx = Transaction::new_with_compiled_instructions(
1463                &[&key],
1464                &[key1, key2],
1465                Hash::default(),
1466                vec![solana_vote_program::id(), Pubkey::new_unique()],
1467                vec![
1468                    CompiledInstruction::new(3, &(), vec![0, 1]),
1469                    CompiledInstruction::new(4, &(), vec![0, 2]),
1470                ],
1471            );
1472            let mut packet = BytesPacket::from_data(None, tx).unwrap();
1473            let packet_offsets = do_get_packet_offsets(packet.as_ref(), 0).unwrap();
1474            check_for_simple_vote_transaction(&mut packet.as_mut(), &packet_offsets, 0).ok();
1475            assert!(!packet.meta().is_simple_vote_tx());
1476        }
1477
1478        // single legacy vote tx with extra (invalid) signature is not
1479        {
1480            let mut tx = new_test_vote_tx(&mut rng);
1481            tx.signatures.push(Signature::default());
1482            tx.message.header.num_required_signatures = 3;
1483            tx.message.instructions[0].data = vec![1, 2, 3];
1484            let mut packet = BytesPacket::from_data(None, tx).unwrap();
1485            let packet_offsets = do_get_packet_offsets(packet.as_ref(), 0).unwrap();
1486            assert_eq!(
1487                Err(PacketError::InvalidSignatureLen),
1488                check_for_simple_vote_transaction(&mut packet.as_mut(), &packet_offsets, 0)
1489            );
1490            assert!(!packet.meta().is_simple_vote_tx());
1491        }
1492    }
1493
1494    #[test]
1495    fn test_is_simple_vote_transaction_with_offsets() {
1496        agave_logger::setup();
1497        let mut rng = rand::thread_rng();
1498
1499        // batch of legacy messages
1500        {
1501            let mut current_offset = 0usize;
1502            let mut batch = BytesPacketBatch::default();
1503            batch.push(BytesPacket::from_data(None, test_tx()).unwrap());
1504            let tx = new_test_vote_tx(&mut rng);
1505            batch.push(BytesPacket::from_data(None, tx).unwrap());
1506            batch.iter_mut().enumerate().for_each(|(index, packet)| {
1507                let packet_offsets =
1508                    do_get_packet_offsets(packet.as_ref(), current_offset).unwrap();
1509                check_for_simple_vote_transaction(
1510                    &mut packet.as_mut(),
1511                    &packet_offsets,
1512                    current_offset,
1513                )
1514                .ok();
1515                if index == 1 {
1516                    assert!(packet.meta().is_simple_vote_tx());
1517                } else {
1518                    assert!(!packet.meta().is_simple_vote_tx());
1519                }
1520
1521                current_offset = current_offset.saturating_add(size_of::<Packet>());
1522            });
1523        }
1524
1525        // batch of mixed legacy messages and versioned vote tx, which won't be flagged as
1526        // simple_vote_tx
1527        {
1528            let mut current_offset = 0usize;
1529            let mut batch = BytesPacketBatch::default();
1530            batch.push(BytesPacket::from_data(None, test_tx()).unwrap());
1531            // versioned vote tx
1532            let tx = new_test_vote_tx(&mut rng);
1533            let packet = BytesPacket::from_data(None, tx).unwrap();
1534            let packet_offsets = do_get_packet_offsets(packet.as_ref(), 0).unwrap();
1535            let msg_start = packet_offsets.msg_start as usize;
1536            let msg_bytes = packet.data(msg_start..).unwrap();
1537            let mut buf = BytesMut::with_capacity(packet.meta().size + 1);
1538            buf.put_slice(packet.data(..msg_start).unwrap());
1539            buf.put_u8(MESSAGE_VERSION_PREFIX);
1540            buf.put_slice(msg_bytes);
1541            let packet = BytesPacket::from_bytes(None, buf.freeze());
1542            batch.push(packet);
1543
1544            batch.iter_mut().for_each(|packet| {
1545                let packet_offsets =
1546                    do_get_packet_offsets(packet.as_ref(), current_offset).unwrap();
1547                check_for_simple_vote_transaction(
1548                    &mut packet.as_mut(),
1549                    &packet_offsets,
1550                    current_offset,
1551                )
1552                .ok();
1553                assert!(!packet.meta().is_simple_vote_tx());
1554
1555                current_offset = current_offset.saturating_add(size_of::<Packet>());
1556            });
1557        }
1558    }
1559
1560    #[test]
1561    fn test_shrink_fuzz() {
1562        let mut rng = rand::thread_rng();
1563        for _ in 0..5 {
1564            let mut batches: Vec<_> = (0..3)
1565                .map(|_| {
1566                    if rng.gen_bool(0.5) {
1567                        let batch = (0..PACKETS_PER_BATCH)
1568                            .map(|_| {
1569                                BytesPacket::from_data(None, test_tx()).expect("serialize request")
1570                            })
1571                            .collect::<BytesPacketBatch>();
1572                        PacketBatch::Bytes(batch)
1573                    } else {
1574                        let batch = (0..PACKETS_PER_BATCH)
1575                            .map(|_| Packet::from_data(None, test_tx()).expect("serialize request"))
1576                            .collect::<Vec<_>>();
1577                        PacketBatch::Pinned(PinnedPacketBatch::new(batch))
1578                    }
1579                })
1580                .collect();
1581            batches.iter_mut().for_each(|b| {
1582                b.iter_mut()
1583                    .for_each(|mut p| p.meta_mut().set_discard(thread_rng().gen()))
1584            });
1585            //find all the non discarded packets
1586            let mut start = vec![];
1587            batches.iter_mut().for_each(|b| {
1588                b.iter_mut()
1589                    .filter(|p| !p.meta().discard())
1590                    .for_each(|p| start.push(p.data(..).unwrap().to_vec()))
1591            });
1592            start.sort();
1593
1594            let packet_count = count_valid_packets(&batches);
1595            let mut batches = shrink_batches(batches);
1596
1597            //make sure all the non discarded packets are the same
1598            let mut end = vec![];
1599            batches.iter_mut().for_each(|b| {
1600                b.iter_mut()
1601                    .filter(|p| !p.meta().discard())
1602                    .for_each(|p| end.push(p.data(..).unwrap().to_vec()))
1603            });
1604            end.sort();
1605            let packet_count2 = count_valid_packets(&batches);
1606            assert_eq!(packet_count, packet_count2);
1607            assert_eq!(start, end);
1608        }
1609    }
1610
1611    #[test]
1612    fn test_shrink_empty() {
1613        const PACKET_COUNT: usize = 1024;
1614        const BATCH_COUNT: usize = PACKET_COUNT / PACKETS_PER_BATCH;
1615
1616        // No batches
1617        // truncate of 1 on len 0 is a noop
1618        shrink_batches(Vec::new());
1619        // One empty batch
1620        {
1621            let batches = vec![PinnedPacketBatch::with_capacity(0).into()];
1622            let batches = shrink_batches(batches);
1623            assert_eq!(batches.len(), 0);
1624        }
1625        // Many empty batches
1626        {
1627            let batches = (0..BATCH_COUNT)
1628                .map(|_| PinnedPacketBatch::with_capacity(0).into())
1629                .collect::<Vec<_>>();
1630            let batches = shrink_batches(batches);
1631            assert_eq!(batches.len(), 0);
1632        }
1633    }
1634
1635    #[test]
1636    fn test_shrink_vectors() {
1637        const PACKET_COUNT: usize = 1024;
1638        const BATCH_COUNT: usize = PACKET_COUNT / PACKETS_PER_BATCH;
1639
1640        let set_discards = [
1641            // contiguous
1642            // 0
1643            // No discards
1644            |_, _| false,
1645            // All discards
1646            |_, _| true,
1647            // single partitions
1648            // discard last half of packets
1649            |b, p| ((b * PACKETS_PER_BATCH) + p) >= (PACKET_COUNT / 2),
1650            // discard first half of packets
1651            |b, p| ((b * PACKETS_PER_BATCH) + p) < (PACKET_COUNT / 2),
1652            // discard last half of each batch
1653            |_, p| p >= (PACKETS_PER_BATCH / 2),
1654            // 5
1655            // discard first half of each batch
1656            |_, p| p < (PACKETS_PER_BATCH / 2),
1657            // uniform sparse
1658            // discard even packets
1659            |b, p| ((b * PACKETS_PER_BATCH) + p) % 2 == 0,
1660            // discard odd packets
1661            |b, p| ((b * PACKETS_PER_BATCH) + p) % 2 == 1,
1662            // discard even batches
1663            |b, _| b % 2 == 0,
1664            // discard odd batches
1665            |b, _| b % 2 == 1,
1666            // edges
1667            // 10
1668            // discard first batch
1669            |b, _| b == 0,
1670            // discard last batch
1671            |b, _| b == BATCH_COUNT - 1,
1672            // discard first and last batches
1673            |b, _| b == 0 || b == BATCH_COUNT - 1,
1674            // discard all but first and last batches
1675            |b, _| b != 0 && b != BATCH_COUNT - 1,
1676            // discard first packet
1677            |b, p| ((b * PACKETS_PER_BATCH) + p) == 0,
1678            // 15
1679            // discard all but first packet
1680            |b, p| ((b * PACKETS_PER_BATCH) + p) != 0,
1681            // discard last packet
1682            |b, p| ((b * PACKETS_PER_BATCH) + p) == PACKET_COUNT - 1,
1683            // discard all but last packet
1684            |b, p| ((b * PACKETS_PER_BATCH) + p) != PACKET_COUNT - 1,
1685            // discard first packet of each batch
1686            |_, p| p == 0,
1687            // discard all but first packet of each batch
1688            |_, p| p != 0,
1689            // 20
1690            // discard last packet of each batch
1691            |_, p| p == PACKETS_PER_BATCH - 1,
1692            // discard all but last packet of each batch
1693            |_, p| p != PACKETS_PER_BATCH - 1,
1694            // discard first and last packet of each batch
1695            |_, p| p == 0 || p == PACKETS_PER_BATCH - 1,
1696            // discard all but first and last packet of each batch
1697            |_, p| p != 0 && p != PACKETS_PER_BATCH - 1,
1698            // discard all after first packet in second to last batch
1699            |b, p| (b == BATCH_COUNT - 2 && p > 0) || b == BATCH_COUNT - 1,
1700            // 25
1701        ];
1702
1703        let expect_valids = [
1704            // (expected_batches, expected_valid_packets)
1705            //
1706            // contiguous
1707            // 0
1708            (BATCH_COUNT, PACKET_COUNT),
1709            (0, 0),
1710            // single partitions
1711            (BATCH_COUNT / 2, PACKET_COUNT / 2),
1712            (BATCH_COUNT / 2, PACKET_COUNT / 2),
1713            (BATCH_COUNT / 2, PACKET_COUNT / 2),
1714            // 5
1715            (BATCH_COUNT / 2, PACKET_COUNT / 2),
1716            // uniform sparse
1717            (BATCH_COUNT / 2, PACKET_COUNT / 2),
1718            (BATCH_COUNT / 2, PACKET_COUNT / 2),
1719            (BATCH_COUNT / 2, PACKET_COUNT / 2),
1720            (BATCH_COUNT / 2, PACKET_COUNT / 2),
1721            // edges
1722            // 10
1723            (BATCH_COUNT - 1, PACKET_COUNT - PACKETS_PER_BATCH),
1724            (BATCH_COUNT - 1, PACKET_COUNT - PACKETS_PER_BATCH),
1725            (BATCH_COUNT - 2, PACKET_COUNT - 2 * PACKETS_PER_BATCH),
1726            (2, 2 * PACKETS_PER_BATCH),
1727            (BATCH_COUNT, PACKET_COUNT - 1),
1728            // 15
1729            (1, 1),
1730            (BATCH_COUNT, PACKET_COUNT - 1),
1731            (1, 1),
1732            (
1733                (BATCH_COUNT * (PACKETS_PER_BATCH - 1) + PACKETS_PER_BATCH) / PACKETS_PER_BATCH,
1734                (PACKETS_PER_BATCH - 1) * BATCH_COUNT,
1735            ),
1736            (
1737                (BATCH_COUNT + PACKETS_PER_BATCH) / PACKETS_PER_BATCH,
1738                BATCH_COUNT,
1739            ),
1740            // 20
1741            (
1742                (BATCH_COUNT * (PACKETS_PER_BATCH - 1) + PACKETS_PER_BATCH) / PACKETS_PER_BATCH,
1743                (PACKETS_PER_BATCH - 1) * BATCH_COUNT,
1744            ),
1745            (
1746                (BATCH_COUNT + PACKETS_PER_BATCH) / PACKETS_PER_BATCH,
1747                BATCH_COUNT,
1748            ),
1749            (
1750                (BATCH_COUNT * (PACKETS_PER_BATCH - 2) + PACKETS_PER_BATCH) / PACKETS_PER_BATCH,
1751                (PACKETS_PER_BATCH - 2) * BATCH_COUNT,
1752            ),
1753            (
1754                (2 * BATCH_COUNT + PACKETS_PER_BATCH) / PACKETS_PER_BATCH,
1755                PACKET_COUNT - (PACKETS_PER_BATCH - 2) * BATCH_COUNT,
1756            ),
1757            (BATCH_COUNT - 1, PACKET_COUNT - 2 * PACKETS_PER_BATCH + 1),
1758            // 25
1759        ];
1760
1761        let test_cases = set_discards.iter().zip(&expect_valids).enumerate();
1762        for (i, (set_discard, (expect_batch_count, expect_valid_packets))) in test_cases {
1763            debug!("test_shrink case: {i}");
1764            let mut batches = to_packet_batches(
1765                &(0..PACKET_COUNT).map(|_| test_tx()).collect::<Vec<_>>(),
1766                PACKETS_PER_BATCH,
1767            );
1768            assert_eq!(batches.len(), BATCH_COUNT);
1769            assert_eq!(count_valid_packets(&batches), PACKET_COUNT);
1770            batches.iter_mut().enumerate().for_each(|(i, b)| {
1771                b.iter_mut()
1772                    .enumerate()
1773                    .for_each(|(j, mut p)| p.meta_mut().set_discard(set_discard(i, j)))
1774            });
1775            assert_eq!(count_valid_packets(&batches), *expect_valid_packets);
1776            debug!("show valid packets for case {i}");
1777            batches.iter_mut().enumerate().for_each(|(i, b)| {
1778                b.iter_mut().enumerate().for_each(|(j, p)| {
1779                    if !p.meta().discard() {
1780                        trace!("{i} {j}")
1781                    }
1782                })
1783            });
1784            debug!("done show valid packets for case {i}");
1785            let batches = shrink_batches(batches);
1786            let shrunken_batch_count = batches.len();
1787            debug!("shrunk batch test {i} count: {shrunken_batch_count}");
1788            assert_eq!(shrunken_batch_count, *expect_batch_count);
1789            assert_eq!(count_valid_packets(&batches), *expect_valid_packets);
1790        }
1791    }
1792
1793    #[test]
1794    fn test_split_batches() {
1795        let tx = test_tx();
1796
1797        let batches = vec![];
1798        let (bytes_batches, pinned_batches) = split_batches(batches);
1799        assert!(bytes_batches.is_empty());
1800        assert!(pinned_batches.is_empty());
1801
1802        let pinned_packet = Packet::from_data(None, tx.clone()).unwrap();
1803        let bytes_packet = BytesPacket::from_data(None, tx).unwrap();
1804        let batches = vec![
1805            PacketBatch::Pinned(PinnedPacketBatch::new(vec![pinned_packet.clone(); 10])),
1806            PacketBatch::Bytes(BytesPacketBatch::from(vec![bytes_packet.clone(); 10])),
1807            PacketBatch::Pinned(PinnedPacketBatch::new(vec![pinned_packet.clone(); 10])),
1808            PacketBatch::Pinned(PinnedPacketBatch::new(vec![pinned_packet.clone(); 10])),
1809            PacketBatch::Bytes(BytesPacketBatch::from(vec![bytes_packet.clone(); 10])),
1810        ];
1811        let (bytes_batches, pinned_batches) = split_batches(batches);
1812        assert_eq!(
1813            bytes_batches,
1814            vec![
1815                BytesPacketBatch::from(vec![bytes_packet.clone(); 10]),
1816                BytesPacketBatch::from(vec![bytes_packet; 10]),
1817            ]
1818        );
1819        assert_eq!(
1820            pinned_batches,
1821            vec![
1822                PinnedPacketBatch::new(vec![pinned_packet.clone(); 10]),
1823                PinnedPacketBatch::new(vec![pinned_packet.clone(); 10]),
1824                PinnedPacketBatch::new(vec![pinned_packet; 10]),
1825            ]
1826        )
1827    }
1828
1829    #[test_case(false, false; "ok_ixs_legacy")]
1830    #[test_case(true, false; "too_many_ixs_legacy")]
1831    #[test_case(false, true; "ok_ixs_versioned")]
1832    #[test_case(true, true; "too_many_ixs_versioned")]
1833    fn test_number_of_instructions(too_many_ixs: bool, is_versioned_tx: bool) {
1834        let mut number_of_ixs = 64;
1835        if too_many_ixs {
1836            number_of_ixs += 1;
1837        }
1838
1839        let packet = if is_versioned_tx {
1840            let tx: VersionedTransaction = new_test_tx_with_number_of_ixs(number_of_ixs);
1841            BytesPacket::from_data(None, tx.clone()).unwrap()
1842        } else {
1843            let tx: Transaction = new_test_tx_with_number_of_ixs(number_of_ixs);
1844            BytesPacket::from_data(None, tx.clone()).unwrap()
1845        };
1846
1847        if too_many_ixs {
1848            assert_eq!(
1849                do_get_packet_offsets(packet.as_ref(), 0),
1850                Err(PacketError::InvalidNumberOfInstructions),
1851            );
1852        } else {
1853            assert!(do_get_packet_offsets(packet.as_ref(), 0).is_ok());
1854        }
1855    }
1856}