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 a method to serialize the whole VerifiableSecretSharingSharingCommitment<C>. #870

Open
StackOverflowExcept1on opened this issue Feb 11, 2025 · 0 comments

Comments

@StackOverflowExcept1on
Copy link
Contributor

StackOverflowExcept1on commented Feb 11, 2025

Currently it is only possible to get Vec<Vec<u8>>, although CoefficientCommitment will be serialized in <C::Group as Group>::Serialization. Implementing method that provides Vec<u8> should not be problem since Serialization has fixed length.

/// Returns serialized coefficient commitments
pub fn serialize(&self) -> Result<Vec<Vec<u8>>, Error<C>> {
self.0
.iter()
.map(|cc| cc.serialize())
.collect::<Result<_, Error<C>>>()
}

Something like this. I'll probably open PR later.

#[cfg(feature = "codec")]
impl<C> parity_scale_codec::Encode for VerifiableSecretSharingCommitment<C>
where
    C: Ciphersuite,
{
    fn encode(&self) -> Vec<u8> {
        let tmp = self
            .serialize()
            .expect("Could not serialize `VerifiableSecretSharingCommitment<C>`")
            .concat();
        let compact_len = parity_scale_codec::Compact(tmp.len() as u32);

        let mut output = Vec::with_capacity(compact_len.size_hint() + tmp.len());

        compact_len.encode_to(&mut output);
        output.extend(tmp);

        output
    }
}

#[cfg(feature = "codec")]
impl<C> parity_scale_codec::Decode for VerifiableSecretSharingCommitment<C>
where
    C: Ciphersuite,
{
    fn decode<I: parity_scale_codec::Input>(
        input: &mut I,
    ) -> Result<Self, parity_scale_codec::Error> {
        let input: Vec<u8> = parity_scale_codec::Decode::decode(input)?;

        let generator = <C::Group>::generator();
        let serialization =
            <C::Group>::serialize(&generator).expect("serializing the generator always works");
        let serialization_len = serialization.as_ref().len();

        const ERROR_MSG: &str = "Could not decode `VerifiableSecretSharingCommitment<C>`";

        let chunks_exact = input.chunks_exact(serialization_len);

        if !chunks_exact.remainder().is_empty() {
            return Err(ERROR_MSG.into());
        }

        let serialized_coefficient_commitments: Vec<Vec<u8>> =
            chunks_exact.map(Vec::from).collect();

        Self::deserialize(serialized_coefficient_commitments).map_err(|_| ERROR_MSG.into())
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

No branches or pull requests

1 participant