Skip to content

Commit

Permalink
Merge pull request #46 from LFDT-Lockness/serialized-point
Browse files Browse the repository at this point in the history
Add `Point::serialized_len`
  • Loading branch information
maurges authored Oct 2, 2024
2 parents f5a1e7d + f7bdc3a commit 9d7153d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
3 changes: 3 additions & 0 deletions generic-ec/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## v0.4.3
* Add `Point::serialized_len`

## v0.4.2
* Update links, add info about our discord [#44]

Expand Down
2 changes: 1 addition & 1 deletion generic-ec/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "generic-ec"
version = "0.4.2"
version = "0.4.3"
edition = "2021"
license = "MIT OR Apache-2.0"
repository = "https://github.com/LFDT-Lockness/generic-ec"
Expand Down
13 changes: 13 additions & 0 deletions generic-ec/src/point/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,19 @@ impl<E: Curve> Point<E> {
.and_then(Self::try_from_raw)
.ok_or(InvalidPoint)
}

/// Returns size of bytes buffer that can fit a serialized point
///
/// `compressed` parameter has the same meaning as for [`Point::to_bytes`]; a
/// buffer created with length of `Point::serialized_len(compress)` would fit
/// exactly the serialization `p.to_bytes(compress)`.
pub fn serialized_len(compressed: bool) -> usize {
if compressed {
E::CompressedPointArray::zeroes().as_ref().len()
} else {
E::UncompressedPointArray::zeroes().as_ref().len()
}
}
}

impl<E: Curve> TryFromRaw for Point<E> {
Expand Down
2 changes: 2 additions & 0 deletions tests/tests/curves.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ mod tests {
let bytes_compressed = point.to_bytes(true);
let bytes_uncompressed = point.to_bytes(false);
assert!(bytes_compressed.len() <= bytes_uncompressed.len());
assert_eq!(bytes_compressed.len(), Point::<E>::serialized_len(true));
assert_eq!(bytes_uncompressed.len(), Point::<E>::serialized_len(false));

let p1 = Point::<E>::from_bytes(&bytes_compressed).unwrap();
let p2 = Point::<E>::from_bytes(&bytes_uncompressed).unwrap();
Expand Down

0 comments on commit 9d7153d

Please sign in to comment.