Refactor evaluations into method

This commit is contained in:
Daniel Sanchez Quiros 2024-03-30 13:29:46 +01:00
parent a619bb362c
commit 38fe0e8171
1 changed files with 22 additions and 20 deletions

View File

@ -23,20 +23,12 @@ pub enum KzgRsError {
ChunkSizeTooBig(usize), ChunkSizeTooBig(usize),
} }
fn bytes_to_polynomial<const CHUNK_SIZE: usize>( fn bytes_to_evaluations<const CHUNK_SIZE: usize>(
data: &[u8], data: &[u8],
domain: GeneralEvaluationDomain<Fr>, domain: GeneralEvaluationDomain<Fr>,
) -> Result<DensePolynomial<Fr>, KzgRsError> { ) -> Evaluations<Fr> {
if CHUNK_SIZE >= 32 { assert!((data.len() % CHUNK_SIZE).is_zero());
return Err(KzgRsError::ChunkSizeTooBig(CHUNK_SIZE)); Evaluations::from_vec_and_domain(
}
if data.len() % CHUNK_SIZE != 0 {
return Err(KzgRsError::UnpaddedDataError {
expected_modulus: CHUNK_SIZE,
current_size: data.len(),
});
}
let coefficients = Evaluations::from_vec_and_domain(
data.chunks(CHUNK_SIZE) data.chunks(CHUNK_SIZE)
.map(|e| { .map(|e| {
let mut buff = [0u8; 32]; let mut buff = [0u8; 32];
@ -49,13 +41,24 @@ fn bytes_to_polynomial<const CHUNK_SIZE: usize>(
.collect(), .collect(),
domain, domain,
) )
.interpolate() }
.coeffs
.into_iter() pub fn bytes_to_polynomial<const CHUNK_SIZE: usize>(
.take(data.len() / CHUNK_SIZE); data: &[u8],
Ok(DensePolynomial::from_coefficients_vec( domain: GeneralEvaluationDomain<Fr>,
coefficients.collect(), ) -> Result<DensePolynomial<Fr>, KzgRsError> {
)) if CHUNK_SIZE >= 32 {
return Err(KzgRsError::ChunkSizeTooBig(CHUNK_SIZE));
}
if data.len() % CHUNK_SIZE != 0 {
return Err(KzgRsError::UnpaddedDataError {
expected_modulus: CHUNK_SIZE,
current_size: data.len(),
});
}
let evals = bytes_to_evaluations::<CHUNK_SIZE>(data, domain);
let coefficients = evals.interpolate();
Ok(coefficients)
} }
#[cfg(test)] #[cfg(test)]
@ -78,7 +81,6 @@ mod test {
let mut rng = thread_rng(); let mut rng = thread_rng();
bytes.try_fill(&mut rng).unwrap(); bytes.try_fill(&mut rng).unwrap();
let poly = bytes_to_polynomial::<31>(&bytes, *DOMAIN).unwrap(); let poly = bytes_to_polynomial::<31>(&bytes, *DOMAIN).unwrap();
assert_eq!(poly.degree(), N - 1);
for (i, e) in (0..100).map(|i| 2u32.pow(i)).enumerate() { for (i, e) in (0..100).map(|i| 2u32.pow(i)).enumerate() {
let eval_point = Fr::from(e); let eval_point = Fr::from(e);
let point = poly.evaluate(&eval_point); let point = poly.evaluate(&eval_point);