Add from_bytes() and to_bytes() to CryptoNum, and do a basic implementation of from_bytes().

This commit is contained in:
2019-07-23 21:05:20 -07:00
parent aff88eb2f0
commit 203c23e277
2 changed files with 49 additions and 1 deletions

View File

@@ -20,5 +20,17 @@ pub trait CryptoNum {
/// Test if the given bit is zero, where bits are numbered in
/// least-significant order (0 is the LSB, etc.).
fn testbit(&self, bit: usize) -> bool;
/// Convert a slice into a CryptoNum, assuming big-endian memory layout.
/// If you pass in a slice bigger than the bit size of the numeric type,
/// this will assume that the number is in the first `n` bits of the
/// memory layout. If you pass in a smaller buffer, it will use the bits
/// available as the low `n` bits of the number.
fn from_bytes(&self, bytes: &[u8]) -> Self;
/// Write the cryptonum into the provide slice. If the provided slice
/// is greater than or equal to `n` bits in length, `to_bytes` will
/// write to the first `n` bits. If the slice is less than `n` bits
/// in length, `to_bytes` will write the lower-order bits that fit
/// into the provided slice.
fn to_bytes(&self, bytes: &mut [u8]);
}