From f7ada99d8231e777bde5a12b9a88fc68c04e86de Mon Sep 17 00:00:00 2001 From: Daniel Sanchez Quiros Date: Tue, 2 Apr 2024 18:00:14 +0200 Subject: [PATCH] Implement decoding tests --- nomos-da/kzgrs/src/rs.rs | 43 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/nomos-da/kzgrs/src/rs.rs b/nomos-da/kzgrs/src/rs.rs index 19f18e47..507ff727 100644 --- a/nomos-da/kzgrs/src/rs.rs +++ b/nomos-da/kzgrs/src/rs.rs @@ -1,4 +1,5 @@ use ark_bls12_381::Fr; +use ark_ff::{BigInteger, PrimeField}; use ark_poly::univariate::DensePolynomial; use ark_poly::{EvaluationDomain, Evaluations, GeneralEvaluationDomain, Polynomial}; @@ -32,3 +33,45 @@ pub fn decode( *domain, ) } + +pub fn points_to_bytes(points: &[Fr]) -> Vec { + fn point_to_buff(p: &Fr) -> impl Iterator { + p.into_bigint().to_bytes_le().into_iter().take(CHUNK_SIZE) + } + points + .iter() + .map(point_to_buff::) + .flatten() + .collect() +} + +#[cfg(test)] +mod test { + use crate::common::bytes_to_polynomial; + use crate::rs::{decode, encode, points_to_bytes}; + use ark_bls12_381::{Bls12_381, Fr}; + use ark_poly::univariate::DensePolynomial; + use ark_poly::{EvaluationDomain, GeneralEvaluationDomain}; + use ark_poly_commit::kzg10::{UniversalParams, KZG10}; + use once_cell::sync::Lazy; + use rand::{thread_rng, Fill}; + + const COEFFICIENTS_SIZE: usize = 16; + static DOMAIN: Lazy> = + Lazy::new(|| GeneralEvaluationDomain::new(COEFFICIENTS_SIZE).unwrap()); + + #[test] + fn test_encode_decode() { + let mut bytes: [u8; 310] = [0; 310]; + let mut rng = thread_rng(); + bytes.try_fill(&mut rng).unwrap(); + + let (evals, poly) = bytes_to_polynomial::<31>(&bytes, *DOMAIN).unwrap(); + + let mut encoded = encode(&poly, &evals, 2, &DOMAIN); + + let decoded = decode(10, &encoded.evals, &DOMAIN); + let decoded_bytes = points_to_bytes::<31>(&decoded.evals); + assert_eq!(decoded_bytes, bytes); + } +}