Implemented dablob

This commit is contained in:
Daniel Sanchez Quiros 2024-04-16 18:37:58 +02:00
parent dd117f7f73
commit 5f2b441ae7
4 changed files with 58 additions and 9 deletions

View File

@ -12,9 +12,9 @@ ark-poly = "0.4.2"
kzgrs = { path = "../kzgrs" }
rand = "0.8.5"
once_cell = "1.19"
sha3 = "0.10"
[dev-dependencies]
rand = "0.8"
itertools = "0.12"
ark-ff = "0.4"
num-bigint = "0.4.4"

View File

@ -1,6 +1,11 @@
use blake2::digest::{Update, VariableOutput};
use kzgrs::Commitment;
// std
use ark_serialize::CanonicalSerialize;
use std::io::Cursor;
// crates
use blake2::digest::{Update, VariableOutput};
use sha3::{Digest, Sha3_256};
// internal
use kzgrs::Commitment;
#[derive(Clone, Eq, PartialEq, Debug)]
pub struct Chunk(pub Vec<u8>);
@ -101,18 +106,37 @@ pub fn hash_column_and_commitment<const HASH_SIZE: usize>(
column: &Column,
commitment: &Commitment,
) -> [u8; HASH_SIZE] {
use ark_serialize::CanonicalSerialize;
let mut hasher = blake2::Blake2bVar::new(HASH_SIZE)
.unwrap_or_else(|e| panic!("Blake2b should work for size {HASH_SIZE}, {e}"));
hasher.update(column.as_bytes().as_ref());
let mut buff = Cursor::new(vec![]);
commitment
.serialize_uncompressed(&mut buff)
.expect("Serialization of commitment should work");
hasher.update(buff.into_inner().as_ref());
hasher.update(commitment_to_bytes(commitment).as_ref());
hasher
.finalize_boxed()
.to_vec()
.try_into()
.unwrap_or_else(|_| panic!("Size is guaranteed by constant {HASH_SIZE:?}"))
}
pub fn build_attestation_message(
aggregated_column_commitment: &Commitment,
rows_commitments: &[Commitment],
) -> Vec<u8> {
let mut hasher = Sha3_256::new();
Digest::update(
&mut hasher,
commitment_to_bytes(aggregated_column_commitment),
);
for c in rows_commitments {
Digest::update(&mut hasher, commitment_to_bytes(c));
}
hasher.finalize().to_vec()
}
pub fn commitment_to_bytes(commitment: &Commitment) -> Vec<u8> {
use ark_serialize::CanonicalSerialize;
let mut buff = Cursor::new(vec![]);
commitment
.serialize_uncompressed(&mut buff)
.expect("Serialization of commitment should work");
buff.into_inner()
}

View File

@ -1,3 +1,4 @@
mod common;
mod encoder;
mod global;
mod verifier;

View File

@ -0,0 +1,24 @@
use crate::common::{build_attestation_message, Column};
use kzgrs::{Commitment, Proof};
use sha3::{Digest, Sha3_256};
pub struct DaBlob {
column: Column,
column_commitment: Commitment,
aggregated_column_commitment: Commitment,
aggregated_column_proof: Proof,
rows_commitments: Vec<Commitment>,
rows_proofs: Vec<Proof>,
}
impl DaBlob {
pub fn id(&self) -> Vec<u8> {
build_attestation_message(&self.aggregated_column_commitment, &self.rows_commitments)
}
pub fn column_id(&self) -> Vec<u8> {
let mut hasher = Sha3_256::new();
hasher.update(self.column.as_bytes());
hasher.finalize().as_slice().to_vec()
}
}