Browse Source

feat: Add size hint for digests (#330)

main
Philipp Gackstatter 6 months ago
committed by GitHub
parent
commit
a924ac6b81
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
3 changed files with 20 additions and 0 deletions
  1. +4
    -0
      CHANGELOG.md
  2. +8
    -0
      src/hash/rescue/rpo/digest.rs
  3. +8
    -0
      src/hash/rescue/rpx/digest.rs

+ 4
- 0
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) ## 0.10.1 (2024-09-13)
* Added `Serializable` and `Deserializable` implementations for `PartialMmr` and `InOrderIndex` (#329). * Added `Serializable` and `Deserializable` implementations for `PartialMmr` and `InOrderIndex` (#329).

+ 8
- 0
src/hash/rescue/rpo/digest.rs

@ -19,6 +19,9 @@ use crate::{
pub struct RpoDigest([Felt; DIGEST_SIZE]); pub struct RpoDigest([Felt; DIGEST_SIZE]);
impl RpoDigest { 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 { pub const fn new(value: [Felt; DIGEST_SIZE]) -> Self {
Self(value) Self(value)
} }
@ -423,6 +426,10 @@ impl Serializable for RpoDigest {
fn write_into<W: ByteWriter>(&self, target: &mut W) { fn write_into<W: ByteWriter>(&self, target: &mut W) {
target.write_bytes(&self.as_bytes()); target.write_bytes(&self.as_bytes());
} }
fn get_size_hint(&self) -> usize {
Self::SERIALIZED_SIZE
}
} }
impl Deserializable for RpoDigest { impl Deserializable for RpoDigest {
@ -476,6 +483,7 @@ mod tests {
let mut bytes = vec![]; let mut bytes = vec![];
d1.write_into(&mut bytes); d1.write_into(&mut bytes);
assert_eq!(DIGEST_BYTES, bytes.len()); assert_eq!(DIGEST_BYTES, bytes.len());
assert_eq!(bytes.len(), d1.get_size_hint());
let mut reader = SliceReader::new(&bytes); let mut reader = SliceReader::new(&bytes);
let d2 = RpoDigest::read_from(&mut reader).unwrap(); let d2 = RpoDigest::read_from(&mut reader).unwrap();

+ 8
- 0
src/hash/rescue/rpx/digest.rs

@ -19,6 +19,9 @@ use crate::{
pub struct RpxDigest([Felt; DIGEST_SIZE]); pub struct RpxDigest([Felt; DIGEST_SIZE]);
impl RpxDigest { 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 { pub const fn new(value: [Felt; DIGEST_SIZE]) -> Self {
Self(value) Self(value)
} }
@ -423,6 +426,10 @@ impl Serializable for RpxDigest {
fn write_into<W: ByteWriter>(&self, target: &mut W) { fn write_into<W: ByteWriter>(&self, target: &mut W) {
target.write_bytes(&self.as_bytes()); target.write_bytes(&self.as_bytes());
} }
fn get_size_hint(&self) -> usize {
Self::SERIALIZED_SIZE
}
} }
impl Deserializable for RpxDigest { impl Deserializable for RpxDigest {
@ -476,6 +483,7 @@ mod tests {
let mut bytes = vec![]; let mut bytes = vec![];
d1.write_into(&mut bytes); d1.write_into(&mut bytes);
assert_eq!(DIGEST_BYTES, bytes.len()); assert_eq!(DIGEST_BYTES, bytes.len());
assert_eq!(bytes.len(), d1.get_size_hint());
let mut reader = SliceReader::new(&bytes); let mut reader = SliceReader::new(&bytes);
let d2 = RpxDigest::read_from(&mut reader).unwrap(); let d2 = RpxDigest::read_from(&mut reader).unwrap();

Loading…
Cancel
Save