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

blob: make the Blob struct more rust-like #14

Open
samlaf opened this issue Nov 22, 2024 · 0 comments
Open

blob: make the Blob struct more rust-like #14

samlaf opened this issue Nov 22, 2024 · 0 comments
Labels
breaking breaking api change

Comments

@samlaf
Copy link
Contributor

samlaf commented Nov 22, 2024

The Blob struct is written in a very dynamic-typed way, and doesn't make good use of rust's type system.

pub struct Blob {
    blob_data: Vec<u8>,
    is_padded: bool,
    length_after_padding: usize,
}

It would be cleaner to separate it into

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Blob {
    data: Vec<u8>,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct BlobPadded {
    data: Vec<u8>,
}

Benefits:

  1. don't need extra "dynamic" state fields like is_padded and length_after_padding, you can just call blob.len() and blobPadded.len()
  2. much cleaner API with explicit rust-like conversions between them (from/into/try_into/etc)
  3. don't need to throw dynamic errors for things that could easily be caught at compile time, like to_polynomial which would only be implemented on BlobPadded.
    pub fn to_polynomial(&self, form: PolynomialFormat) -> Result<Polynomial, BlobError> {
        if !self.is_padded {
            Err(BlobError::NotPaddedError)
        } else {
            let fr_vec = helpers::to_fr_array(&self.blob_data);
            let poly = Polynomial::new(&fr_vec, self.length_after_padding, form)
                .map_err(|err| BlobError::GenericError(err.to_string()))?;
            Ok(poly)
        }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking breaking api change
Projects
None yet
Development

No branches or pull requests

1 participant