From 2a21945f1e1e971d5b57815795b5af96c53050a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Lindstr=C3=B8m?= Date: Thu, 5 Dec 2024 14:09:20 +0100 Subject: [PATCH] Add length of uncompressed G1 element as a constant --- fastcrypto/src/groups/bls12381.rs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/fastcrypto/src/groups/bls12381.rs b/fastcrypto/src/groups/bls12381.rs index 5e6810da0..6067c53e0 100644 --- a/fastcrypto/src/groups/bls12381.rs +++ b/fastcrypto/src/groups/bls12381.rs @@ -60,6 +60,7 @@ pub const G1_ELEMENT_BYTE_LENGTH: usize = 48; pub const G2_ELEMENT_BYTE_LENGTH: usize = 96; pub const GT_ELEMENT_BYTE_LENGTH: usize = 576; pub const FP_BYTE_LENGTH: usize = 48; +pub const G1_ELEMENT_UNCOMPRESSED_BYTE_LENGTH: usize = 96; impl Add for G1Element { type Output = Self; @@ -382,18 +383,20 @@ impl TryFrom<&G1ElementUncompressed> for G1Element { } } -impl ToFromByteArray<{ 2 * G1_ELEMENT_BYTE_LENGTH }> for G1ElementUncompressed { - fn from_byte_array(bytes: &[u8; 2 * G1_ELEMENT_BYTE_LENGTH]) -> FastCryptoResult { +impl ToFromByteArray for G1ElementUncompressed { + fn from_byte_array( + bytes: &[u8; G1_ELEMENT_UNCOMPRESSED_BYTE_LENGTH], + ) -> FastCryptoResult { let uncompressed = Self::from_trusted_byte_array(*bytes); - // TODO: Refactor the validity check + // Use the validity check from G1Element::try_from if G1Element::try_from(&uncompressed).is_err() { return Err(InvalidInput); } Ok(uncompressed) } - fn to_byte_array(&self) -> [u8; 2 * G1_ELEMENT_BYTE_LENGTH] { + fn to_byte_array(&self) -> [u8; G1_ELEMENT_UNCOMPRESSED_BYTE_LENGTH] { self.0 } } @@ -405,12 +408,12 @@ impl G1ElementUncompressed { /// The input is not validated so it should come from a trusted source. /// /// See [the blst docs](https://github.com/supranational/blst/tree/master?tab=readme-ov-file#serialization-format) for details about the uncompressed serialization format. - pub fn from_trusted_byte_array(bytes: [u8; 2 * G1_ELEMENT_BYTE_LENGTH]) -> Self { + pub fn from_trusted_byte_array(bytes: [u8; G1_ELEMENT_UNCOMPRESSED_BYTE_LENGTH]) -> Self { Self(bytes) } /// Get the byte array representation of this element. - pub fn into_byte_array(self) -> [u8; 2 * G1_ELEMENT_BYTE_LENGTH] { + pub fn into_byte_array(self) -> [u8; G1_ELEMENT_UNCOMPRESSED_BYTE_LENGTH] { self.0 }