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

Add Point::serialized_len #45

Closed
wants to merge 1 commit into from
Closed
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
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
Loading