Skip to content

Commit 0de171f

Browse files
committed
Add Point::serialized_len
Signed-off by: Nikita Sorokovikov <nikita@dfns.co>
1 parent f5a1e7d commit 0de171f

File tree

4 files changed

+19
-1
lines changed

4 files changed

+19
-1
lines changed

generic-ec/CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## v0.4.3
2+
* Add `Point::serialized_len`
3+
14
## v0.4.2
25
* Update links, add info about our discord [#44]
36

generic-ec/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "generic-ec"
3-
version = "0.4.2"
3+
version = "0.4.3"
44
edition = "2021"
55
license = "MIT OR Apache-2.0"
66
repository = "https://github.com/LFDT-Lockness/generic-ec"

generic-ec/src/point/mod.rs

+13
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,19 @@ impl<E: Curve> Point<E> {
9797
.and_then(Self::try_from_raw)
9898
.ok_or(InvalidPoint)
9999
}
100+
101+
/// Returns size of bytes buffer that can fit a serialized point
102+
///
103+
/// `compressed` parameter has the same meaning as for [`Point::to_bytes`]; a
104+
/// buffer created with length of `Point::serialized_len(compress)` would fit
105+
/// exactly the serialization `p.to_bytes(compress)`.
106+
pub fn serialized_len(compressed: bool) -> usize {
107+
if compressed {
108+
E::CompressedPointArray::zeroes().as_ref().len()
109+
} else {
110+
E::UncompressedPointArray::zeroes().as_ref().len()
111+
}
112+
}
100113
}
101114

102115
impl<E: Curve> TryFromRaw for Point<E> {

tests/tests/curves.rs

+2
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ mod tests {
110110
let bytes_compressed = point.to_bytes(true);
111111
let bytes_uncompressed = point.to_bytes(false);
112112
assert!(bytes_compressed.len() <= bytes_uncompressed.len());
113+
assert_eq!(bytes_compressed.len(), Point::<E>::serialized_len(true));
114+
assert_eq!(bytes_uncompressed.len(), Point::<E>::serialized_len(false));
113115

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

0 commit comments

Comments
 (0)