Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No std best practice #21

Merged
merged 3 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions protocol/src/chacha20poly1305.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ impl ChaCha20Poly1305 {
#[cfg(test)]
mod tests {
use super::*;
use alloc::vec::Vec;
use hex::prelude::*;

#[test]
Expand Down
4 changes: 4 additions & 0 deletions protocol/src/chacha20poly1305/chacha20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,9 @@ fn keystream_at_slice(key: [u8; 32], nonce: [u8; 12], count: u32, seek: usize) -
#[cfg(test)]
mod tests {
use super::*;
use alloc::vec::Vec;
use hex::prelude::*;
#[cfg(feature = "std")]
use rand::Rng;

#[test]
Expand Down Expand Up @@ -424,13 +426,15 @@ mod tests {
assert_eq!(binding, to);
}

#[cfg(feature = "std")]
fn gen_garbage(garbage_len: u32) -> Vec<u8> {
let mut rng = rand::thread_rng();
let buffer: Vec<u8> = (0..garbage_len).map(|_| rng.gen()).collect();
buffer
}

#[test]
#[cfg(feature = "std")]
fn test_fuzz_other() {
for _ in 0..100 {
let garbage_key = gen_garbage(32);
Expand Down
1 change: 1 addition & 0 deletions protocol/src/chacha20poly1305/poly1305.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ fn _print_acc(num: &[u32; 5]) {
#[cfg(test)]
mod tests {
use super::*;
use alloc::vec::Vec;
use hex::prelude::*;

#[test]
Expand Down
1 change: 1 addition & 0 deletions protocol/src/hkdf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ impl<T: Hash> Hkdf<T> {
#[cfg(test)]
mod tests {
use super::*;
use alloc::vec::Vec;
use bitcoin_hashes::sha256;
use hex::prelude::*;

Expand Down
30 changes: 25 additions & 5 deletions protocol/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
//! BIP 324 encrypted transport for exchanging Bitcoin P2P messages. Read more about the [specification](https://github.com/bitcoin/bips/blob/master/bip-0324.mediawiki).

#![cfg_attr(all(not(feature = "std"), not(test)), no_std)]
#![no_std]

extern crate alloc;
#[cfg(feature = "std")]
extern crate std;

mod chacha20poly1305;
mod error;
Expand Down Expand Up @@ -747,24 +748,30 @@ fn split_garbage_and_version<'a>(

#[cfg(test)]
mod tests {
//! Any tests requiring a random number generator are currently
//! gated with the std feature flag.

use super::*;
use core::str::FromStr;
use hex::prelude::*;

#[cfg(feature = "std")]
fn gen_garbage(garbage_len: u32, rng: &mut impl Rng) -> Vec<u8> {
let buffer: Vec<u8> = (0..garbage_len).map(|_| rng.gen()).collect();
buffer
}

#[test]
#[cfg(feature = "std")]
fn test_sec_keygen() {
let mut rng = rand::thread_rng();
gen_key(&mut rng).unwrap();
}

#[test]
#[cfg(feature = "std")]
fn test_initial_message() {
let mut message = vec![0u8; 64];
let mut message = [0u8; 64];
let handshake =
Handshake::new(Network::Mainnet, Role::Initiator, None, &mut message).unwrap();
let message = message.to_lower_hex_string();
Expand All @@ -773,11 +780,12 @@ mod tests {
}

#[test]
#[cfg(feature = "std")]
fn test_message_response() {
let mut message = vec![0u8; 64];
let mut message = [0u8; 64];
Handshake::new(Network::Mainnet, Role::Initiator, None, &mut message).unwrap();

let mut response_message = vec![0u8; 100];
let mut response_message = [0u8; 100];
let mut response = Handshake::new(
Network::Mainnet,
Role::Responder,
Expand All @@ -792,6 +800,7 @@ mod tests {
}

#[test]
#[cfg(feature = "std")]
fn test_expand_extract() {
let ikm = Vec::from_hex("c6992a117f5edbea70c3f511d32d26b9798be4b81a62eaee1a5acaa8459a3592")
.unwrap();
Expand Down Expand Up @@ -883,6 +892,7 @@ mod tests {
}

#[test]
#[cfg(feature = "std")]
fn test_fuzz_packets() {
let mut rng = rand::thread_rng();
let alice =
Expand Down Expand Up @@ -922,6 +932,7 @@ mod tests {
}

#[test]
#[cfg(feature = "std")]
fn test_authenticated_garbage() {
let mut rng = rand::thread_rng();
let alice =
Expand All @@ -948,6 +959,7 @@ mod tests {
}

#[test]
#[cfg(feature = "std")]
fn test_full_handshake() {
// The initiator's handshake writes its 64 byte elligator swift key to the buffer to send to the responder.
let mut init_message = vec![0u8; 64];
Expand Down Expand Up @@ -1007,6 +1019,7 @@ mod tests {
}

#[test]
#[cfg(feature = "std")]
fn test_decode_multiple_messages() {
let mut init_message = vec![0u8; 64];
let mut init_handshake =
Expand Down Expand Up @@ -1045,6 +1058,7 @@ mod tests {
}

#[test]
#[cfg(feature = "std")]
fn test_fuzz_decode_multiple_messages() {
let mut rng = rand::thread_rng();

Expand Down Expand Up @@ -1086,6 +1100,7 @@ mod tests {
}

#[test]
#[cfg(feature = "std")]
fn test_partial_decodings() {
let mut rng = rand::thread_rng();

Expand Down Expand Up @@ -1130,6 +1145,7 @@ mod tests {
// The rest are sourced from: https://github.com/bitcoin/bips/blob/master/bip-0324/packet_encoding_test_vectors.csv

#[test]
#[cfg(feature = "std")]
fn test_vector_1() {
let mut rng = rand::thread_rng();
let alice =
Expand Down Expand Up @@ -1164,6 +1180,7 @@ mod tests {
}

#[test]
#[cfg(feature = "std")]
fn test_vector_2() {
let alice =
SecretKey::from_str("1f9c581b35231838f0f17cf0c979835baccb7f3abbbb96ffcc318ab71e6e126f")
Expand Down Expand Up @@ -1201,6 +1218,7 @@ mod tests {
}

#[test]
#[cfg(feature = "std")]
fn test_vector_3() {
let alice =
SecretKey::from_str("0286c41cd30913db0fdff7a64ebda5c8e3e7cef10f2aebc00a7650443cf4c60d")
Expand All @@ -1226,6 +1244,7 @@ mod tests {
}

#[test]
#[cfg(feature = "std")]
fn test_vector_4() {
let alice =
SecretKey::from_str("6c77432d1fda31e9f942f8af44607e10f3ad38a65f8a4bddae823e5eff90dc38")
Expand Down Expand Up @@ -1261,6 +1280,7 @@ mod tests {
}

#[test]
#[cfg(feature = "std")]
fn test_vector_5() {
let alice =
SecretKey::from_str("a6ec25127ca1aa4cf16b20084ba1e6516baae4d32422288e9b36d8bddd2de35a")
Expand Down
Loading