feat: fix tests

This commit is contained in:
2023-10-20 00:09:27 +08:00
parent 93d778bdcb
commit 2e48e5d18d
7 changed files with 180 additions and 167 deletions

View File

@@ -22,7 +22,7 @@ const CHACHA20_COUNTER_OVERFLOW: u64 = ((1 << 32) - 1) * 64;
/// # Example /// # Example
/// ///
/// ``` /// ```
/// use chacha20_poly1305_aead::encrypt; /// use chacha20_poly1305_stream::encrypt;
/// ///
/// let key = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, /// let key = [0, 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]; /// 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31];
@@ -99,9 +99,9 @@ pub fn encrypt_read<R: Read, W: Write>(key: &[u8], nonce: &[u8],
/// # Example /// # Example
/// ///
/// ``` /// ```
/// # use chacha20_poly1305_aead::DecryptError; /// # use chacha20_poly1305_stream::DecryptError;
/// # fn example() -> Result<(), DecryptError> { /// # fn example() -> Result<(), DecryptError> {
/// use chacha20_poly1305_aead::decrypt; /// use chacha20_poly1305_stream::decrypt;
/// ///
/// let key = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, /// let key = [0, 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]; /// 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31];
@@ -116,7 +116,7 @@ pub fn encrypt_read<R: Read, W: Write>(key: &[u8], nonce: &[u8],
/// // Vec implements the Write trait /// // Vec implements the Write trait
/// let mut plaintext = Vec::with_capacity(ciphertext.len()); /// let mut plaintext = Vec::with_capacity(ciphertext.len());
/// ///
/// try!(decrypt(&key, &nonce, &aad, &ciphertext, &tag, &mut plaintext)); /// decrypt(&key, &nonce, &aad, &ciphertext, &tag, &mut plaintext)?;
/// ///
/// assert_eq!(plaintext, b"hello, world"); /// assert_eq!(plaintext, b"hello, world");
/// # Ok(()) /// # Ok(())

View File

@@ -34,10 +34,17 @@ impl<T: Safe> AsBytes for [T] {
} }
unsafe impl Safe for u8 {} unsafe impl Safe for u8 {}
unsafe impl Safe for u16 {} unsafe impl Safe for u16 {}
unsafe impl Safe for u32 {} unsafe impl Safe for u32 {}
unsafe impl Safe for u64 {} unsafe impl Safe for u64 {}
unsafe impl Safe for i8 {} unsafe impl Safe for i8 {}
unsafe impl Safe for i16 {} unsafe impl Safe for i16 {}
unsafe impl Safe for i32 {} unsafe impl Safe for i32 {}
unsafe impl Safe for i64 {} unsafe impl Safe for i64 {}

View File

@@ -10,7 +10,7 @@ use crate::simd::{Vector4, u32x4};
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct ChaCha20 { pub struct ChaCha20 {
state: [u32x4; 3] state: [u32x4; 3],
} }
#[cfg_attr(feature = "clippy", allow(should_implement_trait))] #[cfg_attr(feature = "clippy", allow(should_implement_trait))]
@@ -127,7 +127,7 @@ pub fn selftest() {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use as_bytes::AsBytes; use crate::as_bytes::AsBytes;
use super::ChaCha20; use super::ChaCha20;
#[test] #[test]

View File

@@ -39,10 +39,10 @@ impl Poly1305 {
a: [0; 5], a: [0; 5],
// r &= 0x0ffffffc_0ffffffc_0ffffffc_0fffffff; // r &= 0x0ffffffc_0ffffffc_0ffffffc_0fffffff;
r: [u32_from_le(&key[ 0.. 4]) & 0x03ffffff, r: [u32_from_le(&key[0..4]) & 0x03ffffff,
u32_from_le(&key[ 3.. 7]) >> 2 & 0x03ffff03, u32_from_le(&key[3..7]) >> 2 & 0x03ffff03,
u32_from_le(&key[ 6..10]) >> 4 & 0x03ffc0ff, u32_from_le(&key[6..10]) >> 4 & 0x03ffc0ff,
u32_from_le(&key[ 9..13]) >> 6 & 0x03f03fff, u32_from_le(&key[9..13]) >> 6 & 0x03f03fff,
u32_from_le(&key[12..16]) >> 8 & 0x000fffff], u32_from_le(&key[12..16]) >> 8 & 0x000fffff],
s: [u32_from_le(&key[16..20]), s: [u32_from_le(&key[16..20]),
@@ -54,10 +54,10 @@ impl Poly1305 {
pub fn block(&mut self, msg: &[u8]) { pub fn block(&mut self, msg: &[u8]) {
assert!(msg.len() == 16); assert!(msg.len() == 16);
self.accumulate(u32_from_le(&msg[ 0.. 4]) & 0x03ffffff, self.accumulate(u32_from_le(&msg[0..4]) & 0x03ffffff,
u32_from_le(&msg[ 3.. 7]) >> 2 & 0x03ffffff, u32_from_le(&msg[3..7]) >> 2 & 0x03ffffff,
u32_from_le(&msg[ 6..10]) >> 4 & 0x03ffffff, u32_from_le(&msg[6..10]) >> 4 & 0x03ffffff,
u32_from_le(&msg[ 9..13]) >> 6 & 0x03ffffff, u32_from_le(&msg[9..13]) >> 6 & 0x03ffffff,
u32_from_le(&msg[12..16]) >> 8 | (1 << 24)); u32_from_le(&msg[12..16]) >> 8 | (1 << 24));
} }
@@ -69,10 +69,10 @@ impl Poly1305 {
buf[..msg.len()].clone_from_slice(msg); buf[..msg.len()].clone_from_slice(msg);
buf[msg.len()] = 1; buf[msg.len()] = 1;
self.accumulate(u32_from_le(&buf[ 0.. 4]) & 0x03ffffff, self.accumulate(u32_from_le(&buf[0..4]) & 0x03ffffff,
u32_from_le(&buf[ 3.. 7]) >> 2 & 0x03ffffff, u32_from_le(&buf[3..7]) >> 2 & 0x03ffffff,
u32_from_le(&buf[ 6..10]) >> 4 & 0x03ffffff, u32_from_le(&buf[6..10]) >> 4 & 0x03ffffff,
u32_from_le(&buf[ 9..13]) >> 6 & 0x03ffffff, u32_from_le(&buf[9..13]) >> 6 & 0x03ffffff,
u32_from_le(&buf[13..17])); u32_from_le(&buf[13..17]));
} }
@@ -267,7 +267,7 @@ pub fn selftest() {
0xc2, 0x2b, 0x8b, 0xaf, 0x0c, 0x01, 0x27, 0xa9]; 0xc2, 0x2b, 0x8b, 0xaf, 0x0c, 0x01, 0x27, 0xa9];
let mut state = Poly1305::new(&key); let mut state = Poly1305::new(&key);
state.block(&msg[ 0..16]); state.block(&msg[0..16]);
state.block(&msg[16..32]); state.block(&msg[16..32]);
let tag = state.last_block(&msg[32..]); let tag = state.last_block(&msg[32..]);
@@ -276,7 +276,7 @@ pub fn selftest() {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use as_bytes::AsBytes; use crate::as_bytes::AsBytes;
use super::Poly1305; use super::Poly1305;
#[test] #[test]

View File

@@ -23,9 +23,12 @@ pub trait Vector4<T>: Copy {
fn shuffle_left_2(self) -> Self; fn shuffle_left_2(self) -> Self;
fn shuffle_left_3(self) -> Self; fn shuffle_left_3(self) -> Self;
#[inline(always)] fn shuffle_right_1(self) -> Self { self.shuffle_left_3() } #[inline(always)]
#[inline(always)] fn shuffle_right_2(self) -> Self { self.shuffle_left_2() } fn shuffle_right_1(self) -> Self { self.shuffle_left_3() }
#[inline(always)] fn shuffle_right_3(self) -> Self { self.shuffle_left_1() } #[inline(always)]
fn shuffle_right_2(self) -> Self { self.shuffle_left_2() }
#[inline(always)]
fn shuffle_right_3(self) -> Self { self.shuffle_left_1() }
} }
macro_rules! impl_vector4 { macro_rules! impl_vector4 {

View File

@@ -6,7 +6,8 @@
// copied, modified, or distributed except according to those terms. // copied, modified, or distributed except according to those terms.
use crate::simdty::u32x4; use crate::simdty::u32x4;
#[cfg(feature = "simd")] use crate::simdint; #[cfg(feature = "simd")]
use crate::simdint;
use std::ops::{Add, BitXor, Shl, Shr}; use std::ops::{Add, BitXor, Shl, Shr};

View File

@@ -55,5 +55,7 @@ impl<T> Simd4<T> {
} }
unsafe impl<T: Safe> Safe for Simd4<T> {} unsafe impl<T: Safe> Safe for Simd4<T> {}
unsafe impl<T: Safe> Safe for Simd8<T> {} unsafe impl<T: Safe> Safe for Simd8<T> {}
unsafe impl<T: Safe> Safe for Simd16<T> {} unsafe impl<T: Safe> Safe for Simd16<T> {}