From 38fe0e81716c5899e630e95f7fd00a6d97ddc154 Mon Sep 17 00:00:00 2001 From: Daniel Sanchez Quiros Date: Sat, 30 Mar 2024 13:29:46 +0100 Subject: [PATCH] Refactor evaluations into method --- nomos-da/kzgrs/src/common.rs | 42 +++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/nomos-da/kzgrs/src/common.rs b/nomos-da/kzgrs/src/common.rs index f6033f63..94300ac9 100644 --- a/nomos-da/kzgrs/src/common.rs +++ b/nomos-da/kzgrs/src/common.rs @@ -23,20 +23,12 @@ pub enum KzgRsError { ChunkSizeTooBig(usize), } -fn bytes_to_polynomial( +fn bytes_to_evaluations( data: &[u8], domain: GeneralEvaluationDomain, -) -> Result, 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 coefficients = Evaluations::from_vec_and_domain( +) -> Evaluations { + assert!((data.len() % CHUNK_SIZE).is_zero()); + Evaluations::from_vec_and_domain( data.chunks(CHUNK_SIZE) .map(|e| { let mut buff = [0u8; 32]; @@ -49,13 +41,24 @@ fn bytes_to_polynomial( .collect(), domain, ) - .interpolate() - .coeffs - .into_iter() - .take(data.len() / CHUNK_SIZE); - Ok(DensePolynomial::from_coefficients_vec( - coefficients.collect(), - )) +} + +pub fn bytes_to_polynomial( + data: &[u8], + domain: GeneralEvaluationDomain, +) -> Result, 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::(data, domain); + let coefficients = evals.interpolate(); + Ok(coefficients) } #[cfg(test)] @@ -78,7 +81,6 @@ mod test { let mut rng = thread_rng(); bytes.try_fill(&mut rng).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() { let eval_point = Fr::from(e); let point = poly.evaluate(&eval_point);