@@ -564,6 +564,8 @@ const IBE_HEADER: [u8; 8] = [b'I', b'C', b' ', b'I', b'B', b'E', 0x00, 0x01];
564564
565565const IBE_HEADER_BYTES : usize = IBE_HEADER . len ( ) ;
566566
567+ const IBE_OVERHEAD : usize = IBE_HEADER_BYTES + IBE_SEED_BYTES + G2AFFINE_BYTES ;
568+
567569#[ derive( Clone , Debug , Eq , PartialEq ) ]
568570/// An IBE (identity based encryption) ciphertext
569571pub struct IbeCiphertext {
@@ -599,8 +601,7 @@ impl IbeDomainSep {
599601impl IbeCiphertext {
600602 /// Serialize this IBE ciphertext
601603 pub fn serialize ( & self ) -> Vec < u8 > {
602- let mut output =
603- Vec :: with_capacity ( self . header . len ( ) + G2AFFINE_BYTES + IBE_SEED_BYTES + self . c3 . len ( ) ) ;
604+ let mut output = Vec :: with_capacity ( IBE_OVERHEAD + self . c3 . len ( ) ) ;
604605
605606 output. extend_from_slice ( & self . header ) ;
606607 output. extend_from_slice ( & self . c1 . to_compressed ( ) ) ;
@@ -614,7 +615,7 @@ impl IbeCiphertext {
614615 ///
615616 /// Returns Err if the encoding is not valid
616617 pub fn deserialize ( bytes : & [ u8 ] ) -> Result < Self , String > {
617- if bytes. len ( ) < IBE_HEADER_BYTES + G2AFFINE_BYTES + IBE_SEED_BYTES {
618+ if bytes. len ( ) < IBE_OVERHEAD {
618619 return Err ( "IbeCiphertext too short to be valid" . to_string ( ) ) ;
619620 }
620621
@@ -637,7 +638,6 @@ impl IbeCiphertext {
637638 }
638639
639640 fn hash_to_mask ( header : & [ u8 ] , seed : & [ u8 ; IBE_SEED_BYTES ] , msg : & [ u8 ] ) -> Scalar {
640-
641641 /*
642642 It would have been better to instead use the SHA-256 of the message instead of the
643643 message directly, since that would avoid having to allocate an extra buffer of
@@ -763,6 +763,23 @@ impl IbeCiphertext {
763763 Err ( "decryption failed" . to_string ( ) )
764764 }
765765 }
766+
767+ /// Helper function for determining size of the IBE ciphertext
768+ pub fn ciphertext_size ( ptext_len : usize ) -> usize {
769+ return ptext_len + IBE_OVERHEAD ;
770+ }
771+
772+ /// Helper function for determining size of the IBE plaintext
773+ ///
774+ /// Returns Err if the indicated ctext_len would be a ciphertext
775+ /// that is not possibly valid (due to missing required elements)
776+ pub fn plaintext_size ( ctext_len : usize ) -> Result < usize , ( ) > {
777+ if ctext_len < IBE_OVERHEAD {
778+ return Err ( ( ) ) ;
779+ } else {
780+ return Ok ( ctext_len - IBE_OVERHEAD ) ;
781+ }
782+ }
766783}
767784
768785/// Verify an augmented BLS signature
0 commit comments