From 6e3b2ab44d9c03fb7aa8e8b94e711c197de3d337 Mon Sep 17 00:00:00 2001 From: John Clements Date: Thu, 5 Sep 2013 09:05:26 -0700 Subject: [PATCH] move and duplicate macro defns in sha2 to make them hygienic ... it would also have been possible to add all of their dependencies, but that would have increased the already-lengthy list of parameters. Also, if we had macros that could expand into macro defns, you could stage it. This seemed like the least painful choice. --- src/libextra/crypto/sha2.rs | 62 ++++++++++++++++++++++++------------- 1 file changed, 40 insertions(+), 22 deletions(-) diff --git a/src/libextra/crypto/sha2.rs b/src/libextra/crypto/sha2.rs index 96f3e13eb22..49bbddca1db 100644 --- a/src/libextra/crypto/sha2.rs +++ b/src/libextra/crypto/sha2.rs @@ -14,28 +14,8 @@ use cryptoutil::{write_u64_be, write_u32_be, read_u64v_be, read_u32v_be, add_byt add_bytes_to_bits_tuple, FixedBuffer, FixedBuffer128, FixedBuffer64, StandardPadding}; use digest::Digest; - -// Sha-512 and Sha-256 use basically the same calculations which are implemented by these macros. -// Inlining the calculations seems to result in better generated code. -macro_rules! schedule_round( ($t:expr) => ( - W[$t] = sigma1(W[$t - 2]) + W[$t - 7] + sigma0(W[$t - 15]) + W[$t - 16]; - ) -) - -macro_rules! sha2_round( - ($A:ident, $B:ident, $C:ident, $D:ident, - $E:ident, $F:ident, $G:ident, $H:ident, $K:ident, $t:expr) => ( - { - $H += sum1($E) + ch($E, $F, $G) + $K[$t] + W[$t]; - $D += $H; - $H += sum0($A) + maj($A, $B, $C); - } - ) -) - - -// A structure that represents that state of a digest computation for the SHA-2 512 family of digest -// functions +// A structure that represents that state of a digest computation for the SHA-2 512 family +// of digest functions struct Engine512State { H0: u64, H1: u64, @@ -108,6 +88,25 @@ impl Engine512State { let mut W = [0u64, ..80]; + // Sha-512 and Sha-256 use basically the same calculations which are implemented by + // these macros. Inlining the calculations seems to result in better generated code. + macro_rules! schedule_round( ($t:expr) => ( + W[$t] = sigma1(W[$t - 2]) + W[$t - 7] + sigma0(W[$t - 15]) + W[$t - 16]; + ) + ) + + macro_rules! sha2_round( + ($A:ident, $B:ident, $C:ident, $D:ident, + $E:ident, $F:ident, $G:ident, $H:ident, $K:ident, $t:expr) => ( + { + $H += sum1($E) + ch($E, $F, $G) + $K[$t] + W[$t]; + $D += $H; + $H += sum0($A) + maj($A, $B, $C); + } + ) + ) + + read_u64v_be(W.mut_slice(0, 16), data); // Putting the message schedule inside the same loop as the round calculations allows for @@ -505,6 +504,25 @@ impl Engine256State { let mut W = [0u32, ..64]; + // Sha-512 and Sha-256 use basically the same calculations which are implemented + // by these macros. Inlining the calculations seems to result in better generated code. + macro_rules! schedule_round( ($t:expr) => ( + W[$t] = sigma1(W[$t - 2]) + W[$t - 7] + sigma0(W[$t - 15]) + W[$t - 16]; + ) + ) + + macro_rules! sha2_round( + ($A:ident, $B:ident, $C:ident, $D:ident, + $E:ident, $F:ident, $G:ident, $H:ident, $K:ident, $t:expr) => ( + { + $H += sum1($E) + ch($E, $F, $G) + $K[$t] + W[$t]; + $D += $H; + $H += sum0($A) + maj($A, $B, $C); + } + ) + ) + + read_u32v_be(W.mut_slice(0, 16), data); // Putting the message schedule inside the same loop as the round calculations allows for