Skip to content
Snippets Groups Projects
Verified Commit 9a8acbf7 authored by STEVAN Antoine's avatar STEVAN Antoine :crab:
Browse files

do the thing lol

parent 537aac2e
No related branches found
No related tags found
No related merge requests found
use ark_ff::PrimeField;
use ark_poly::DenseUVPolynomial;
use ark_std::ops::Div;
use rs_merkle::algorithms::Sha256;
use rs_merkle::Hasher;
use tracing::{debug, info};
......@@ -20,13 +21,37 @@ pub struct Block<F: PrimeField, H: Hasher> {
pub shard: fec::Shard<F>,
proof: MerkleProof<H>,
commit: FridaCommitment<F, H>,
position: usize,
}
pub fn encode<F: PrimeField>(bytes: &[u8], k: usize, n: usize) -> Vec<fec::Shard<F>> {
debug!("splitting bytes into rows");
let elements: Vec<F> = algebra::split_data_into_field_elements(bytes, k);
let rows = elements.chunks(k).map(|c| c.to_vec()).collect::<Vec<_>>();
info!(
"data is composed of {} rows and {} elements",
rows.len(),
elements.len()
);
let hash = Sha256::hash(bytes).to_vec();
let w = F::get_root_of_unity(n as u64).unwrap();
rows.into_iter()
.enumerate()
.map(|(i, r)| fec::Shard {
k: k as u32,
linear_combination: (0..k).map(|j| w.pow([(i + j) as u64])).collect(),
hash: hash.clone(),
data: to_evaluations(r, n),
size: bytes.len(),
})
.collect::<Vec<_>>()
}
pub fn prove<const N: usize, F: PrimeField, H: Hasher, P>(
bytes: &[u8],
shards: Vec<fec::Shard<F>>,
k: usize,
domain_size: usize,
blowup_factor: usize,
remainder_plus_one: usize,
nb_queries: usize,
......@@ -36,24 +61,8 @@ where
for<'a, 'b> &'a P: Div<&'b P, Output = P>,
<H as rs_merkle::Hasher>::Hash: AsRef<[u8]>,
{
info!("encoding and proving {} bytes", bytes.len());
debug!("splitting bytes into rows");
let elements: Vec<F> = algebra::split_data_into_field_elements(bytes, k);
let rows = elements.chunks(k).map(|c| c.to_vec()).collect::<Vec<_>>();
info!(
"data is composed of {} rows and {} elements",
rows.len(),
elements.len()
);
let evaluations = rows
.into_iter()
.map(|r| to_evaluations(r, domain_size))
.collect::<Vec<_>>();
let builder = FridaBuilder::<F, H>::new::<N, _>(
&evaluations,
&shards.iter().map(|s| s.data.clone()).collect::<Vec<_>>(),
FriChallenger::<H>::default(),
blowup_factor,
remainder_plus_one,
......@@ -64,29 +73,18 @@ where
Ok(shards
.iter()
.map(|s| {
// TODO: compute true position
let position = 0;
let _pos = s
.linear_combination
.iter()
.filter(|x| !x.is_zero())
.cloned()
.collect::<Vec<F>>()
.first()
.unwrap();
Block {
shard: s.clone(),
proof: builder.prove_shards(&[position]),
commit: commit.clone(),
}
.enumerate()
.map(|(i, s)| Block {
shard: s.clone(),
proof: builder.prove_shards(&[i]),
commit: commit.clone(),
position: i,
})
.collect())
}
pub fn verify<const N: usize, F: PrimeField, H: Hasher, P>(
block: Block<F, H>,
k: usize,
domain_size: usize,
nb_queries: usize,
) -> Result<(), KomodoError>
......@@ -97,15 +95,17 @@ where
{
block
.commit
.verify::<N, _>(FriChallenger::<H>::default(), nb_queries, k, domain_size)
.verify::<N, _>(
FriChallenger::<H>::default(),
nb_queries,
block.shard.k as usize,
domain_size,
)
.unwrap();
// TODO: compute true position
let position = 0;
assert!(block.proof.verify(
block.commit.tree_root(),
&[position],
&[block.position],
&[H::hash_item(&block.shard)],
domain_size,
));
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment