feat: Add size hint for digests (#330)
Philipp Gackstatter PhilippGackstatter@users.noreply.github.com
Wed, 25 Sep 2024 12:03:31 +0200
3 files changed,
20 insertions(+),
0 deletions(-)
M
CHANGELOG.md
→
CHANGELOG.md
@@ -1,3 +1,7 @@
+## 0.10.2 (TBD) + +* Implement `get_size_hint` for `RpoDigest` and `RpxDigest` and expose constants for their serialized size (#330). + ## 0.10.1 (2024-09-13) * Added `Serializable` and `Deserializable` implementations for `PartialMmr` and `InOrderIndex` (#329).
M
src/hash/rescue/rpo/digest.rs
→
src/hash/rescue/rpo/digest.rs
@@ -19,6 +19,9 @@ #[cfg_attr(feature = "serde", serde(into = "String", try_from = "&str"))]
pub struct RpoDigest([Felt; DIGEST_SIZE]); impl RpoDigest { + /// The serialized size of the digest in bytes. + pub const SERIALIZED_SIZE: usize = DIGEST_BYTES; + pub const fn new(value: [Felt; DIGEST_SIZE]) -> Self { Self(value) }@@ -423,6 +426,10 @@ impl Serializable for RpoDigest {
fn write_into<W: ByteWriter>(&self, target: &mut W) { target.write_bytes(&self.as_bytes()); } + + fn get_size_hint(&self) -> usize { + Self::SERIALIZED_SIZE + } } impl Deserializable for RpoDigest {@@ -476,6 +483,7 @@
let mut bytes = vec![]; d1.write_into(&mut bytes); assert_eq!(DIGEST_BYTES, bytes.len()); + assert_eq!(bytes.len(), d1.get_size_hint()); let mut reader = SliceReader::new(&bytes); let d2 = RpoDigest::read_from(&mut reader).unwrap();
M
src/hash/rescue/rpx/digest.rs
→
src/hash/rescue/rpx/digest.rs
@@ -19,6 +19,9 @@ #[cfg_attr(feature = "serde", serde(into = "String", try_from = "&str"))]
pub struct RpxDigest([Felt; DIGEST_SIZE]); impl RpxDigest { + /// The serialized size of the digest in bytes. + pub const SERIALIZED_SIZE: usize = DIGEST_BYTES; + pub const fn new(value: [Felt; DIGEST_SIZE]) -> Self { Self(value) }@@ -423,6 +426,10 @@ impl Serializable for RpxDigest {
fn write_into<W: ByteWriter>(&self, target: &mut W) { target.write_bytes(&self.as_bytes()); } + + fn get_size_hint(&self) -> usize { + Self::SERIALIZED_SIZE + } } impl Deserializable for RpxDigest {@@ -476,6 +483,7 @@
let mut bytes = vec![]; d1.write_into(&mut bytes); assert_eq!(DIGEST_BYTES, bytes.len()); + assert_eq!(bytes.len(), d1.get_size_hint()); let mut reader = SliceReader::new(&bytes); let d2 = RpxDigest::read_from(&mut reader).unwrap();