From 2b34669d3353d4b451489b9e9f57e2258e8016f2 Mon Sep 17 00:00:00 2001 From: Daniel Sanchez Quiros Date: Wed, 17 Apr 2024 13:04:36 +0200 Subject: [PATCH] Implement verifier new Implement verifier check column --- nomos-da/kzgrs-backend/Cargo.toml | 9 ++-- nomos-da/kzgrs-backend/src/verifier.rs | 75 +++++++++++++++++++++++++- 2 files changed, 79 insertions(+), 5 deletions(-) diff --git a/nomos-da/kzgrs-backend/Cargo.toml b/nomos-da/kzgrs-backend/Cargo.toml index d5cb4281..52b6855a 100644 --- a/nomos-da/kzgrs-backend/Cargo.toml +++ b/nomos-da/kzgrs-backend/Cargo.toml @@ -6,15 +6,18 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -blake2 = "0.10" +ark-ff = "0.4" ark-serialize = "0.4.2" ark-poly = "0.4.2" +blake2 = "0.10" +blst = "0.3.11" +itertools = "0.12" kzgrs = { path = "../kzgrs" } +num-bigint = "0.4.4" rand = "0.8.5" once_cell = "1.19" sha3 = "0.10" [dev-dependencies] rand = "0.8" -itertools = "0.12" -ark-ff = "0.4" + diff --git a/nomos-da/kzgrs-backend/src/verifier.rs b/nomos-da/kzgrs-backend/src/verifier.rs index 821bfd76..65092926 100644 --- a/nomos-da/kzgrs-backend/src/verifier.rs +++ b/nomos-da/kzgrs-backend/src/verifier.rs @@ -1,7 +1,21 @@ -use crate::common::{build_attestation_message, Column}; -use kzgrs::{Commitment, Proof}; +// std + +// crates +use blst::min_sig::{PublicKey, SecretKey, Signature}; +use itertools::Itertools; +use num_bigint::BigUint; use sha3::{Digest, Sha3_256}; +// internal +use crate::common::{build_attestation_message, hash_column_and_commitment, Column}; +use crate::encoder::DaEncoderParams; +use crate::global::{DOMAIN, GLOBAL_PARAMETERS}; +use kzgrs::common::field_element_from_bytes_le; +use kzgrs::{ + bytes_to_polynomial, commit_polynomial, verify_element_proof, Commitment, FieldElement, Proof, + BYTES_PER_FIELD_ELEMENT, +}; + pub struct DaBlob { column: Column, column_commitment: Commitment, @@ -22,3 +36,60 @@ impl DaBlob { hasher.finalize().as_slice().to_vec() } } + +pub struct DaVerifier { + // TODO: substitute this for an abstraction to sign things over + sk: SecretKey, + index: usize, +} + +impl DaVerifier { + pub fn new(sk: SecretKey, nodes_public_keys: &[PublicKey]) -> Self { + // TODO: `is_sorted` is experimental, and by contract `nodes_public_keys` should be shorted + // but not sure how we could enforce it here without re-sorting anyway. + // assert!(nodes_public_keys.is_sorted()); + let self_pk = sk.sk_to_pk(); + let (index, _) = nodes_public_keys + .iter() + .find_position(|&pk| pk == &self_pk) + .expect("Self pk should be registered"); + Self { sk, index } + } + + fn verify_column( + column: &Column, + column_commitment: &Commitment, + aggregated_column_commitment: &Commitment, + aggregated_column_proof: &Proof, + index: usize, + ) -> bool { + // 1. compute commitment for column + let Ok((_, polynomial)) = + bytes_to_polynomial::(column.as_bytes().as_slice(), *DOMAIN) + else { + return false; + }; + let Ok(computed_column_commitment) = commit_polynomial(&polynomial, &GLOBAL_PARAMETERS) + else { + return false; + }; + // 2. if computed column commitment != column commitment, fail + if &computed_column_commitment != column_commitment { + return false; + } + // 3. compute column hash + let column_hash = hash_column_and_commitment::< + { DaEncoderParams::MAX_BLS12_381_ENCODING_CHUNK_SIZE }, + >(column, column_commitment); + // 4. check proof with commitment and proof over the aggregated column commitment + let element = field_element_from_bytes_le(column_hash.as_slice()); + verify_element_proof( + index, + &element, + aggregated_column_commitment, + aggregated_column_proof, + *DOMAIN, + &GLOBAL_PARAMETERS, + ) + } +}