Skip to content

Commit 6df53ff

Browse files
committed
hs1: remove a lot of unnecessary allocations
1 parent 82da948 commit 6df53ff

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

src/hs1.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -199,13 +199,13 @@ pub trait Subkeygen {
199199
/// Hash `M` a total of `t` times with different keys and combine the result with the stream
200200
/// cipher’s key.
201201
pub trait PRF {
202-
fn prf(&self, k: &Key, M: &Vec<u8>, N: &Vec<u8>, y: i64) -> Vec<u8>;
202+
fn prf(&self, k: &Key, M: &[u8], N: &[u8], y: i64) -> Vec<u8>;
203203
}
204204

205205
/// The hash family HS1-Hash is `(1/2^(28) + l/b2^(60))-AU` for all `M` up to `l` bytes, when
206206
/// `k_N` and `k_P` are chosen randomly and `t ≤ 4`.
207207
pub trait Hash {
208-
fn hash(&self, kN: &Vec<u32>, kP: &u64, kA: &Vec<u64>, M: &Vec<u8>) -> Vec<u8>;
208+
fn hash(&self, kN: &[u32], kP: &u64, kA: &[u64], M: &[u8]) -> Vec<u8>;
209209
}
210210

211211
/// Encrypt the message `M` using the HS1-SIV authenticated encryption cipher.
@@ -214,7 +214,7 @@ pub trait Encrypt {
214214
K: &[u8],
215215
M: &Plaintext,
216216
A: &AssociatedData,
217-
N: &Vec<u8>)
217+
N: &[u8])
218218
-> (Ciphertext, Authenticator);
219219
}
220220

@@ -225,7 +225,7 @@ pub trait Decrypt {
225225
T: &Authenticator,
226226
C: &Ciphertext,
227227
A: &AssociatedData,
228-
N: &Vec<u8>)
228+
N: &[u8])
229229
-> Result<Plaintext, Error>;
230230
}
231231

@@ -340,13 +340,13 @@ impl Subkeygen for HS1 {
340340
// XXX Ugh… the .. syntax all over the place in this section is horribly unreadable.
341341
Key {
342342
S: take32(&output[..chachaLen]),
343-
N: toInts4(&output[chachaLen..][..nhLen].to_vec()).unwrap(),
344-
P: toInts8(&output[chachaLen + nhLen..][..polyLen].to_vec())
343+
N: toInts4(&output[chachaLen..][..nhLen]).unwrap(),
344+
P: toInts8(&output[chachaLen + nhLen..][..polyLen])
345345
.unwrap()
346346
.iter()
347347
.map(|x| *x % 2u64.pow(60))
348348
.collect(),
349-
A: toInts8(&output[chachaLen + nhLen + polyLen..][..asuLen].to_vec()).unwrap(),
349+
A: toInts8(&output[chachaLen + nhLen + polyLen..][..asuLen]).unwrap(),
350350
}
351351
}
352352
}
@@ -397,7 +397,7 @@ impl Subkeygen for HS1 {
397397
/// 092, 230, 175, 079, 064, 119, 249, 143])
398398
/// ```
399399
impl PRF for HS1 {
400-
fn prf(&self, k: &Key, M: &Vec<u8>, N: &Vec<u8>, y: i64) -> Vec<u8> {
400+
fn prf(&self, k: &Key, M: &[u8], N: &[u8], y: i64) -> Vec<u8> {
401401
assert_eq!(k.S.len(), 32);
402402
assert_eq!(N.len(), 12);
403403
assert!(0i64 < y);
@@ -409,9 +409,9 @@ impl PRF for HS1 {
409409

410410
// 1. `A_i = HS1-Hash[b,t](kN[4i, b/4], kP[i], kA[3i, 3], M) for each 0 ≤ i < t`
411411
for i in 0..self.parameters.t {
412-
let n: Vec<u32> = subsl(&*k.N, i as usize * 4, self.parameters.b as usize / 4).to_vec();
412+
let n: &[u32] = subsl(&*k.N, i as usize * 4, self.parameters.b as usize / 4);
413413
let p: u64 = k.P[i as usize];
414-
let a: Vec<u64> = subsl(&*k.A, i as usize * 3, 3).to_vec();
414+
let a: &[u64] = subsl(&*k.A, i as usize * 3, 3);
415415

416416
// Concatenate A_i (either 4 or 8 bytes) into the hashed input for combination with the
417417
// keystream:
@@ -432,7 +432,7 @@ impl PRF for HS1 {
432432
// 2. `Y = ChaCha[r](pad(32, A_0 || A_1 || … || A_(t-1)) ⊕ kS), 0, N, 0^y)`
433433
xor_keystream(&mut key, &pad(32, &A), &k.S[..]);
434434
ChaCha20::new(&k.S, &N[..], Some(self.parameters.r as i8)).process(&key[..], &mut Y[..]);
435-
Y.to_vec()
435+
Y
436436
}
437437
}
438438

@@ -479,7 +479,7 @@ impl PRF for HS1 {
479479
/// 6. if (t ≤ 4) Y = toStr(8, h)
480480
/// 7. else Y = toStr(4, (kA[0] + kA[1] × (h mod 2^32) + kA[2] × (h div 2 ^32)) div 2^32)
481481
impl Hash for HS1 {
482-
fn hash(&self, kN: &Vec<u32>, kP: &u64, kA: &Vec<u64>, M: &Vec<u8>) -> Vec<u8> {
482+
fn hash(&self, kN: &[u32], kP: &u64, kA: &[u64], M: &[u8]) -> Vec<u8> {
483483
let n: u32;
484484
let Mi: Chunks<u8>;
485485
let mut Y: Vec<u8>;
@@ -495,7 +495,7 @@ impl Hash for HS1 {
495495

496496
// 3. m_i = toInts(4, pad(16, M_i)) for each 1 ≤ i ≤ n.
497497
for (_, chunk) in Mi.enumerate() {
498-
let mi: Vec<u32> = toInts4(&pad(16, &chunk.to_vec())).unwrap();
498+
let mi: Vec<u32> = toInts4(&pad(16, &chunk)).unwrap();
499499
// 4. a_i = NH(kN, m_i) mod 2^60 + (|M_i| mod 16) for each 1 ≤ i ≤ n.
500500
a.push(NH(kN, &mi) + BigInt::from_u8(self.parameters.b % 16u8).unwrap());
501501
}
@@ -551,7 +551,7 @@ impl Encrypt for HS1 {
551551
K: &[u8],
552552
M: &Plaintext,
553553
A: &AssociatedData,
554-
N: &Vec<u8>)
554+
N: &[u8])
555555
-> (Ciphertext, Authenticator) {
556556
assert!(N.len() == 12);
557557

@@ -617,7 +617,7 @@ impl Decrypt for HS1 {
617617
T: &Authenticator,
618618
C: &Ciphertext,
619619
A: &AssociatedData,
620-
N: &Vec<u8>)
620+
N: &[u8])
621621
-> Result<Plaintext, Error> {
622622
assert!(T.len() == self.parameters.l as usize);
623623
assert!(N.len() == 12);
@@ -677,7 +677,7 @@ impl Decrypt for HS1 {
677677
///
678678
/// assert_eq!(NH(&v1, &v2).to_u64().unwrap(), 162501409595406698u64);
679679
/// ```
680-
pub fn NH(v1: &Vec<u32>, v2: &Vec<u32>) -> BigInt {
680+
pub fn NH(v1: &[u32], v2: &[u32]) -> BigInt {
681681
let mut sum: BigInt = BigInt::from_usize(0).unwrap();
682682
let m: BigInt = BigInt::from_u64(2u64.pow(60)).unwrap();
683683
let bn1: Vec<BigInt> = v1.iter().map(|x| x.to_bigint().unwrap()).collect();
@@ -721,8 +721,8 @@ pub enum ConversionError {
721721
///
722722
/// assert_eq!(padded, [0x41, 0x42, 0x43, 0x00, 0x00]);
723723
/// ```
724-
fn pad(multiple: usize, input: &Vec<u8>) -> Vec<u8> {
725-
let mut padded: Vec<u8> = input.clone();
724+
fn pad(multiple: usize, input: &[u8]) -> Vec<u8> {
725+
let mut padded: Vec<u8> = input.to_vec();
726726

727727
while (padded.len() % multiple) > 0 {
728728
padded.push(0x00);
@@ -782,7 +782,7 @@ pub fn toStr<'a>(n: isize, x: &'a usize) -> Vec<u8> {
782782
/// let yet_another: Vec<u32> = toInts4(&vec![255, 255, 255, 255]).unwrap();
783783
/// assert!(yet_another[0] == 4294967295u32);
784784
/// ```
785-
pub fn toInts4(S: &Vec<u8>) -> Result<Vec<u32>, ConversionError> {
785+
pub fn toInts4(S: &[u8]) -> Result<Vec<u32>, ConversionError> {
786786
if S.len() % 4 != 0 {
787787
println!("The length of S in bytes must be some multiple of 4.");
788788
return Err(ConversionError::StrToInt);
@@ -819,7 +819,7 @@ pub fn toInts4(S: &Vec<u8>) -> Result<Vec<u32>, ConversionError> {
819819
/// let yet_another: Vec<u64> = toInts8(&vec![255, 255, 255, 255, 255, 255, 255, 255]).unwrap();
820820
/// assert!(yet_another[0] == 18446744073709551615u64);
821821
/// ```
822-
pub fn toInts8(S: &Vec<u8>) -> Result<Vec<u64>, ConversionError> {
822+
pub fn toInts8(S: &[u8]) -> Result<Vec<u64>, ConversionError> {
823823
if S.len() % 8 != 0 {
824824
println!("The length of S in bytes must be some multiple of 8.");
825825
return Err(ConversionError::StrToInt);

0 commit comments

Comments
 (0)