Browse Source

* Used the std ilog2 method in merkle_stream::log2.
* Modified BlockStream::flush to return an Unsupported error.

Matthew Carr 1 year ago
parent
commit
ae4184b88b

+ 2 - 0
crates/btlib/src/accessor.rs

@@ -13,6 +13,8 @@ pub use private::Accessor;
 mod private {
 mod private {
     use super::*;
     use super::*;
 
 
+    /// Wraps a position independent stream of cipher text and exposes the positioned stream of
+    /// validated plaintext data it contains.
     pub struct Accessor<T: Size> {
     pub struct Accessor<T: Size> {
         inner: SectoredBuf<SecretStream<Cursor<T>>>,
         inner: SectoredBuf<SecretStream<Cursor<T>>>,
     }
     }

+ 6 - 14
crates/btlib/src/crypto/merkle_stream.rs

@@ -16,22 +16,14 @@ mod private {
     use std::io;
     use std::io;
     use strum::EnumDiscriminants;
     use strum::EnumDiscriminants;
 
 
-    /// Returns the base 2 logarithm of the given number. This function will return -1 when given 0, and
-    /// this is the only input for which a negative value is returned.
-    pub(super) fn log2(mut n: usize) -> isize {
-        // Is there a better implementation of this in std? I wasn't able to find an integer log2
-        // function in std, so I wrote this naive implementation.
+    /// Returns the base 2 logarithm of the given number. This function will return -1 when given 0,
+    /// and this is the only input for which a negative value is returned.
+    pub(super) fn log2(n: usize) -> isize {
         if 0 == n {
         if 0 == n {
-            return -1;
-        }
-        let num_bits = usize::BITS.try_into().unwrap();
-        for k in 0..num_bits {
-            n >>= 1;
-            if 0 == n {
-                return k;
-            }
+            -1
+        } else {
+            n.ilog2().try_into().unwrap()
         }
         }
-        num_bits
     }
     }
 
 
     /// Returns 2^x. Note that 0 is returned for any negative input.
     /// Returns 2^x. Note that 0 is returned for any negative input.

+ 4 - 1
crates/btlib/src/lib.rs

@@ -914,7 +914,10 @@ impl<T: WriteAt + Size, C: Signer + Principaled + Decrypter> WriteAt for BlockSt
     }
     }
 
 
     fn flush(&mut self) -> io::Result<()> {
     fn flush(&mut self) -> io::Result<()> {
-        self.sign_flush_meta()
+        Err(io::Error::new(
+            io::ErrorKind::Unsupported,
+            "BlockStream::flush is not supported. Use BlockStream::flush_integ",
+        ))
     }
     }
 }
 }
 
 

BIN
tools/sector_size.ods