Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add size hint for digests #330

Merged
merged 4 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading