about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJohn Clements <clements@racket-lang.org>2013-09-05 09:05:26 -0700
committerJohn Clements <clements@racket-lang.org>2013-09-06 09:28:45 -0700
commit6e3b2ab44d9c03fb7aa8e8b94e711c197de3d337 (patch)
tree662c7f9afbf5fa1eb028ee1a948f9262c3adcef5
parentd39cec65b025ad4c6de50e778ffd1177279b5b3d (diff)
downloadrust-6e3b2ab44d9c03fb7aa8e8b94e711c197de3d337.tar.gz
rust-6e3b2ab44d9c03fb7aa8e8b94e711c197de3d337.zip
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.
-rw-r--r--src/libextra/crypto/sha2.rs62
1 files 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