Skip to content

Commit

Permalink
feat: Add size hint for digests (#330)
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippGackstatter authored Sep 25, 2024
1 parent e34900c commit a924ac6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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).
Expand Down
8 changes: 8 additions & 0 deletions src/hash/rescue/rpo/digest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ use crate::{
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)
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -476,6 +483,7 @@ mod tests {
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();
Expand Down
8 changes: 8 additions & 0 deletions src/hash/rescue/rpx/digest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ use crate::{
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)
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -476,6 +483,7 @@ mod tests {
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();
Expand Down

0 comments on commit a924ac6

Please sign in to comment.