Skip to content

Add common hash sizes From impls#28

Open
parazyd wants to merge 1 commit intooconnor663:masterfrom
parazyd:impl-common
Open

Add common hash sizes From impls#28
parazyd wants to merge 1 commit intooconnor663:masterfrom
parazyd:impl-common

Conversation

@parazyd
Copy link
Copy Markdown

@parazyd parazyd commented Aug 28, 2023

blake2s:
* From<[u8; 16]>

blake2b:
* From<[u8; 16]>
* From<[u8; 32]>

Both introduce an intermediate buffer in the implementation, but this is a small tradeoff that allows usage of some common hash sizes more idiomatically for the users of this library.

blake2s:
    * From<[u8; 16]>

blake2b:
    * From<[u8; 16]>
    * From<[u8; 32]>

Both introduce an intermediate buffer in the implementation, but
this is a small tradeoff that allows usage of some common hash
sizes more idiomatically for the users of this library.
@parazyd
Copy link
Copy Markdown
Author

parazyd commented Feb 10, 2025

@oconnor663 Any plans to check this out?

@oconnor663
Copy link
Copy Markdown
Owner

Thanks, this looks like a good idea. What would you think about doing it generically, something like this:

diff --git a/blake2b/src/lib.rs b/blake2b/src/lib.rs
index 6c4f92f..aa58385 100644
--- a/blake2b/src/lib.rs
+++ b/blake2b/src/lib.rs
@@ -616,17 +616,20 @@ fn bytes_to_hex(bytes: &[u8]) -> HexString {
     s
 }
 
-impl From<[u8; OUTBYTES]> for Hash {
-    fn from(bytes: [u8; OUTBYTES]) -> Self {
+impl<const N: usize> From<[u8; N]> for Hash {
+    fn from(bytes: [u8; N]) -> Self {
+        assert!(N <= OUTBYTES, "Hash is at most {OUTBYTES} bytes");
+        let mut buf = [0u8; OUTBYTES];
+        buf[0..N].copy_from_slice(&bytes);
         Self {
-            bytes,
-            len: OUTBYTES as u8,
+            bytes: buf,
+            len: N as u8,
         }
     }
 }
 
-impl From<&[u8; OUTBYTES]> for Hash {
-    fn from(bytes: &[u8; OUTBYTES]) -> Self {
+impl<const N: usize> From<&[u8; N]> for Hash {
+    fn from(bytes: &[u8; N]) -> Self {
         Self::from(*bytes)
     }
 }
diff --git a/blake2s/src/lib.rs b/blake2s/src/lib.rs
index f845e96..c2852b4 100644
--- a/blake2s/src/lib.rs
+++ b/blake2s/src/lib.rs
@@ -608,17 +608,20 @@ fn bytes_to_hex(bytes: &[u8]) -> HexString {
     s
 }
 
-impl From<[u8; OUTBYTES]> for Hash {
-    fn from(bytes: [u8; OUTBYTES]) -> Self {
+impl<const N: usize> From<[u8; N]> for Hash {
+    fn from(bytes: [u8; N]) -> Self {
+        assert!(N <= OUTBYTES, "Hash is at most {OUTBYTES} bytes");
+        let mut buf = [0u8; OUTBYTES];
+        buf[0..N].copy_from_slice(&bytes);
         Self {
-            bytes,
-            len: OUTBYTES as u8,
+            bytes: buf,
+            len: N as u8,
         }
     }
 }
 
-impl From<&[u8; OUTBYTES]> for Hash {
-    fn from(bytes: &[u8; OUTBYTES]) -> Self {
+impl<const N: usize> From<&[u8; N]> for Hash {
+    fn from(bytes: &[u8; N]) -> Self {
         Self::from(*bytes)
     }
 }

@parazyd
Copy link
Copy Markdown
Author

parazyd commented Feb 17, 2025

I mean yeah, that looks like a good solution as well. Do you want me to update the pull request this way?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants