Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix query_proof RPC #514

Merged
merged 2 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions e2e/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,48 @@ pub mod tests {
let dim = Dimensions::new(rows, cols).unwrap();
let res = verify(&pp, dim, &commitment, &dcell).unwrap();
assert!(res);

// Fetch & verify extended cell
let cell = Cell { row: 1, col: 1 };
let cells = Cells::try_from(vec![cell.clone()]).unwrap();

// RPC call
let actual_proof: Vec<GDataProof> =
client.rpc_methods().query_proof(cells, block_hash).await?;
let actual_proof: Vec<u8> = actual_proof
.iter()
.map(|(raw_scalar, g_proof)| {
let mut scalar_bytes = [0u8; 32];
raw_scalar.to_big_endian(&mut scalar_bytes);
let proof_bytes: Vec<u8> = Vec::from(*g_proof);

[proof_bytes, scalar_bytes.to_vec()]
.into_iter()
.flatten()
.collect::<Vec<u8>>()
})
.flatten()
.collect();
assert_eq!(actual_proof.len(), 80);

let (commitment, rows, cols) = match &submitted_block.header().extension {
HeaderExtension::V3(ext) => (
ext.commitment.commitment.clone(),
ext.commitment.rows,
ext.commitment.cols,
),
};

let mut content = [0u8; 80];
content.copy_from_slice(&actual_proof);
let commitment: [u8; 48] = commitment[48..96].try_into().unwrap();
let dcell = kate_recovery::data::Cell {
Copy link
Collaborator

@fmiguelgarcia fmiguelgarcia Apr 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like we can refactor this Cell because we are encoding the scalar to raw bytes some lines above, and then it is decoded inside verify function.
Maybe it could be a new PR after release.
In general, we should avoid any Vec<u8> from encoding/serializing in favor of an specific type like GDataProof.
att. @ToufeeqP @jakubcech

position: kate_recovery::matrix::Position { row: 1, col: 1 },
content,
};
let dim = Dimensions::new(rows, cols).unwrap();
let res = verify(&pp, dim, &commitment, &dcell).unwrap();
assert!(res);
Ok(())
}

Expand Down
2 changes: 2 additions & 0 deletions pallets/dactr/src/kate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ pub enum Error {
MissingCell { row: u32, col: u32 },
#[error("MultiProof error")]
Proof,
#[error("Failed to extend columns")]
ColumnExtension,
}

impl From<TryFromIntError> for Error {
Expand Down
6 changes: 5 additions & 1 deletion pallets/dactr/src/kate/hosted_kate.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::{AppExtrinsic, AppId, BlockLength, Error, GDataProof, GProof, GRawScalar, GRow, Seed};
use avail_core::{BlockLengthColumns, BlockLengthRows};
use core::num::NonZeroU16;
use frame_system::header_builder::MIN_WIDTH;
#[cfg(feature = "std")]
use kate::{
Expand Down Expand Up @@ -85,7 +86,10 @@ pub trait HostedKate {
) -> Result<Vec<GDataProof>, Error> {
let srs = SRS.get_or_init(multiproof_params);
let (max_width, max_height) = to_width_height(&block_len);
let grid = EGrid::from_extrinsics(extrinsics, MIN_WIDTH, max_width, max_height, seed)?;
let grid = EGrid::from_extrinsics(extrinsics, MIN_WIDTH, max_width, max_height, seed)?
.extend_columns(NonZeroU16::new(2).expect("2>0"))
.map_err(|_| Error::ColumnExtension)?;

let poly = grid.make_polynomial_grid()?;

let proofs = cells
Expand Down
Loading