From 4baae88a1c2f9da97be4ea0e772e057d758312db Mon Sep 17 00:00:00 2001 From: Daniel Sanchez Quiros Date: Tue, 2 Apr 2024 17:35:25 +0200 Subject: [PATCH] Implement rs encode/decode --- nomos-da/kzgrs/src/common.rs | 2 -- nomos-da/kzgrs/src/rs.rs | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/nomos-da/kzgrs/src/common.rs b/nomos-da/kzgrs/src/common.rs index bc23eaa1..9f6636d9 100644 --- a/nomos-da/kzgrs/src/common.rs +++ b/nomos-da/kzgrs/src/common.rs @@ -5,8 +5,6 @@ use ark_ff::{BigInteger256, PrimeField, Zero}; use ark_poly::domain::general::GeneralEvaluationDomain; use ark_poly::evaluations::univariate::Evaluations; use ark_poly::univariate::DensePolynomial; -use num_bigint; -use num_traits::ops::bytes::FromBytes; use thiserror::Error; // internal diff --git a/nomos-da/kzgrs/src/rs.rs b/nomos-da/kzgrs/src/rs.rs index e69de29b..19f18e47 100644 --- a/nomos-da/kzgrs/src/rs.rs +++ b/nomos-da/kzgrs/src/rs.rs @@ -0,0 +1,34 @@ +use ark_bls12_381::Fr; +use ark_poly::univariate::DensePolynomial; +use ark_poly::{EvaluationDomain, Evaluations, GeneralEvaluationDomain, Polynomial}; + +pub fn encode( + polynomial: &DensePolynomial, + evaluations: &Evaluations, + factor: usize, + domain: &GeneralEvaluationDomain, +) -> Evaluations { + assert!(factor > 1); + Evaluations::from_vec_and_domain( + (0..evaluations.evals.len() * factor) + .map(|i| polynomial.evaluate(&domain.element(i))) + .collect(), + *domain, + ) +} + +pub fn decode( + original_chunks_len: usize, + points: &[Fr], + domain: &GeneralEvaluationDomain, +) -> Evaluations { + let evals = Evaluations::::from_vec_and_domain(points.to_vec(), *domain); + let coeffs = evals.interpolate(); + + Evaluations::from_vec_and_domain( + (0..original_chunks_len) + .map(|i| coeffs.evaluate(&domain.element(i))) + .collect(), + *domain, + ) +}