Skip to content

Commit

Permalink
Merge pull request #47 from nyonson/come-ci-matrix
Browse files Browse the repository at this point in the history
Add some more matrix to CI
  • Loading branch information
nyonson authored Apr 24, 2024
2 parents 974be01 + 3e7bbba commit 30c35d0
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 72 deletions.
60 changes: 51 additions & 9 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ on:
pull_request:

jobs:
build-and-test:
# Proxy application is not as strict as protocol library.
proxy:
runs-on: ubuntu-latest
strategy:
matrix:
toolchain:
- stable
- beta
- nightly
toolchain: [stable, beta, nightly]
steps:
- uses: actions/checkout@v3
- name: Update Toolchain
Expand All @@ -23,14 +21,58 @@ jobs:
rustup component add --toolchain ${{ matrix.toolchain }} rustfmt
rustup component add --toolchain ${{ matrix.toolchain }} clippy
rustup update ${{ matrix.toolchain }}
- name: Lint
run: |
cargo clippy --package bip324-proxy --all-targets
- name: Format
run: |
cargo fmt --package bip324-proxy -- --check
- name: Build
run: cargo build --all --verbose
run: |
cargo build --package bip324-proxy --verbose
cargo build --all --verbose --no-default-features
cargo build --all --verbose --all-features
- name: Test
run: |
cargo test --package bip324-proxy --verbose
protocol:
runs-on: ubuntu-latest
strategy:
matrix:
# Minumum Supported Rust Version (MSRV) is 1.56.1 but
# having some weird issues with the memchr dependency.
toolchain: [stable, beta, nightly]
steps:
- uses: actions/checkout@v3
- name: Update Toolchain
run: |
rustup default ${{ matrix.toolchain }}
rustup component add --toolchain ${{ matrix.toolchain }} rustfmt
rustup component add --toolchain ${{ matrix.toolchain }} clippy
rustup update ${{ matrix.toolchain }}
- name: Lint
run: cargo clippy --all --all-targets
run: |
cargo clippy --package bip324 --all-targets
- name: Format
run: cargo fmt --all -- --check
run: |
cargo fmt --package bip324 -- --check
- name: Build
# Build with default features, all, and none.
# Then build with specific feature sub-sets.
run: |
cargo build --package bip324 --verbose
cargo build --package bip324 --verbose --all-features
cargo build --package bip324 --verbose --no-default-features
cargo build --package bip324 --verbose --no-default-features --features alloc
- name: Test
run: cargo test --all --verbose
# Test with default features, all, and none.
# Then test with specific feature sub-sets.
run: |
cargo test --package bip324 --verbose
cargo test --package bip324 --verbose --all-features
cargo test --package bip324 --verbose --no-default-features
cargo test --package bip324 --verbose --no-default-features --features alloc
- name: Check No Standard Library Support
run: |
rustup target add --toolchain ${{ matrix.toolchain }} thumbv7m-none-eabi
Expand Down
37 changes: 4 additions & 33 deletions Cargo.lock

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

4 changes: 1 addition & 3 deletions protocol/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ rust-version = "1.56.1"

[features]
default = ["std"]
std = ["alloc", "secp256k1/std", "rand/std", "rand/std_rng"]
std = ["alloc", "bitcoin/std", "rand/std", "rand/std_rng"]
alloc = []

[dependencies]
secp256k1 = { version="0.29.0", default-features = false}
rand = { version = "0.8.0", default-features = false }
bitcoin = { version = "0.31.2", default-features = false, features = ["no-std"] }
bitcoin_hashes = { version = "0.14.0", default-features = false }

[dev-dependencies]
hex = { package = "hex-conservative", version = "0.2.0" }
Expand Down
1 change: 1 addition & 0 deletions protocol/src/chacha20poly1305.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ impl ChaCha20Poly1305 {
}

#[cfg(test)]
#[cfg(feature = "alloc")]
mod tests {
use super::*;
use alloc::vec::Vec;
Expand Down
1 change: 1 addition & 0 deletions protocol/src/chacha20poly1305/chacha20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ fn keystream_at_slice(
}

#[cfg(test)]
#[cfg(feature = "alloc")]
mod tests {
use super::*;
use alloc::vec::Vec;
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 @@ -255,6 +255,7 @@ fn _print_acc(num: &[u32; 5]) {
}

#[cfg(test)]
#[cfg(feature = "alloc")]
mod tests {
use super::*;
use alloc::vec::Vec;
Expand Down
5 changes: 3 additions & 2 deletions protocol/src/hkdf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! Implementation based on RFC5869, but the interface is scoped
//! to BIP324's requirements.
use bitcoin_hashes::{Hash, HashEngine, Hmac, HmacEngine};
use bitcoin::hashes::{Hash, HashEngine, Hmac, HmacEngine};
use core::fmt;

// Output keying material max length multiple.
Expand Down Expand Up @@ -86,10 +86,11 @@ impl<T: Hash> Hkdf<T> {
}

#[cfg(test)]
#[cfg(feature = "alloc")]
mod tests {
use super::*;
use alloc::vec::Vec;
use bitcoin_hashes::sha256;
use bitcoin::hashes::sha256;
use hex::prelude::*;

#[test]
Expand Down
48 changes: 28 additions & 20 deletions protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,23 @@ mod hkdf;
use core::fmt;

pub use bitcoin::Network;
use bitcoin_hashes::sha256;

#[cfg(feature = "alloc")]
use alloc::vec;
#[cfg(feature = "alloc")]
use alloc::vec::Vec;

use bitcoin::{
hashes::sha256,
secp256k1::{
self,
ellswift::{ElligatorSwift, ElligatorSwiftParty},
PublicKey, Secp256k1, SecretKey, Signing,
},
};
use fschacha20poly1305::{FSChaCha20, FSChaCha20Poly1305};
use hkdf::Hkdf;
use rand::Rng;
use secp256k1::{
ellswift::{ElligatorSwift, ElligatorSwiftParty},
ffi::types::AlignedType,
PublicKey, Secp256k1, SecretKey,
};

/// Number of bytes for the decoy flag on a packet.
const DECOY_BYTES: usize = 1;
Expand Down Expand Up @@ -133,12 +135,14 @@ pub enum Role {
}

/// A message or decoy packet from a connected peer.
#[cfg(feature = "alloc")]
#[derive(Clone, Debug)]
pub struct ReceivedMessage {
/// A message to handle or `None` if the peer sent a decoy and the message may be safely ignored.
pub message: Option<Vec<u8>>,
}

#[cfg(feature = "alloc")]
impl ReceivedMessage {
pub fn new(msg_bytes: &[u8]) -> Result<Self, Error> {
let header = msg_bytes.first().ok_or(Error::MessageLengthTooSmall)?;
Expand Down Expand Up @@ -444,16 +448,17 @@ impl PacketHandler {
}

fn gen_key(rng: &mut impl Rng) -> Result<SecretKey, Error> {
let mut buffer: Vec<u8> = vec![0; 32];
let mut buffer = [0u8; 32];
rng.fill(&mut buffer[..]);
let sk = SecretKey::from_slice(&buffer)?;
Ok(sk)
}

fn new_elligator_swift(sk: SecretKey) -> Result<ElligatorSwift, Error> {
let mut buf_ful = vec![AlignedType::zeroed(); Secp256k1::preallocate_size()];
let curve = Secp256k1::preallocated_new(&mut buf_ful)?;
let pk = PublicKey::from_secret_key(&curve, &sk);
fn new_elligator_swift<C: Signing>(
sk: SecretKey,
curve: &Secp256k1<C>,
) -> Result<ElligatorSwift, Error> {
let pk = PublicKey::from_secret_key(curve, &sk);
Ok(ElligatorSwift::from_pubkey(pk))
}

Expand Down Expand Up @@ -562,17 +567,19 @@ impl<'a> Handshake<'a> {
buffer: &mut [u8],
) -> Result<Self, Error> {
let mut rng = rand::thread_rng();
Self::new_with_rng(network, role, garbage, buffer, &mut rng)
let curve = Secp256k1::signing_only();
Self::new_with_rng(network, role, garbage, buffer, &mut rng, &curve)
}

/// Initialize a V2 transport handshake with a peer.
///
/// # Arguments
///
/// * `network` - The bitcoin network which both peers operate on.
/// * `garbage` - Optional garbage to send in handshake.
/// * `buffer` - Message buffer to send to peer which will include initial materials for handshake + garbage.
/// * `rng` - Supplied Random Number Generator.
/// - `network` - The bitcoin network which both peers operate on.
/// - `garbage` - Optional garbage to send in handshake.
/// - `buffer` - Message buffer to send to peer which will include initial materials for handshake + garbage.
/// - `rng` - Supplied Random Number Generator.
/// - `curve` - Supplied secp256k1 context.
///
/// # Returns
///
Expand All @@ -581,15 +588,16 @@ impl<'a> Handshake<'a> {
/// # Errors
///
/// Fails if their was an error generating the keypair.
pub fn new_with_rng(
pub fn new_with_rng<C: Signing>(
network: Network,
role: Role,
garbage: Option<&'a [u8]>,
buffer: &mut [u8],
rng: &mut impl Rng,
curve: &Secp256k1<C>,
) -> Result<Self, Error> {
let sk = gen_key(rng)?;
let es = new_elligator_swift(sk)?;
let es = new_elligator_swift(sk, curve)?;
let point = EcdhPoint {
secret_key: sk,
elligator_swift: es,
Expand All @@ -615,8 +623,8 @@ impl<'a> Handshake<'a> {
///
/// # Arguments
///
/// * `their_elliswift` - The key material of the remote peer.
/// * `response` - Buffer to write response for remote peer which includes the garbage terminator and version packet.
/// - `their_elliswift` - The key material of the remote peer.
/// - `response` - Buffer to write response for remote peer which includes the garbage terminator and version packet.
pub fn complete_materials(
&mut self,
their_elliswift: [u8; 64],
Expand Down
6 changes: 3 additions & 3 deletions protocol/tests/round_trips.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use bip324::{Handshake, Role};
use bitcoin::Network;

#[test]
#[cfg(feature = "std")]
fn hello_world_happy_path() {
use bip324::{Handshake, Role};
use bitcoin::Network;

let mut init_message = vec![0u8; 64];
let mut init_handshake =
Handshake::new(Network::Bitcoin, Role::Initiator, None, &mut init_message).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions proxy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ readme = "README.md"
rust-version = "1.56.1"

[dependencies]
bitcoin = "0.31.1"
tokio = { version = "1.36.0", features = ["full"] }
bitcoin = { version = "0.31.2", default-features = false, features = ["no-std"] }
tokio = { version = "1.37.0", features = ["full"] }
bytes = "1.6.0"
hex = { package = "hex-conservative", version = "0.2.0" }
bip324 = { path = "../protocol", version = "0.2.0" }

0 comments on commit 30c35d0

Please sign in to comment.