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

feat(fraud): Add fraud proof trait and byzantine encoding fraud #32

Merged
merged 7 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 10 additions & 9 deletions types/src/byzantine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,13 @@ impl FraudProof for BadEncodingFraudProof {

// verify that Merkle proofs correspond to particular shares.
for share in &self.shares {
let share_data = share.leaf.share.to_array();
share
.proof
.verify_range(&root, &[share_data], share.leaf.namespace.into())
.verify_range(
&root,
&[share.leaf.share.as_ref()],
share.leaf.namespace.into(),
)
.map_err(Error::RangeProofError)?;
}

Expand All @@ -131,24 +134,22 @@ struct NmtLeaf {
}

impl NmtLeaf {
pub fn new(mut bytes: Vec<u8>) -> Result<Self> {
pub fn new(bytes: Vec<u8>) -> Result<Self> {
if bytes.len() != appconsts::SHARE_SIZE + NS_SIZE {
return Err(Error::InvalidNmtLeafSize(bytes.len()));
}

let namespace = Namespace::from_raw(&bytes[..NS_SIZE])?;
bytes.drain(..NS_SIZE);
let (namespace, share) = bytes.split_at(NS_SIZE);

Ok(Self {
namespace,
share: Share::new(bytes)?,
namespace: Namespace::from_raw(namespace)?,
share: Share::new(share)?,
})
}

pub fn to_vec(&self) -> Vec<u8> {
let mut bytes = self.namespace.as_bytes().to_vec();
bytes.extend_from_slice(self.share.namespace.as_bytes());
bytes.extend_from_slice(&self.share.data);
bytes.extend_from_slice(self.share.as_ref());
bytes
}
}
Expand Down
4 changes: 4 additions & 0 deletions types/src/nmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ impl Namespace {
Ok(Namespace(nmt_rs::NamespaceId(bytes)))
}

pub(crate) fn new_unchecked(bytes: [u8; NS_SIZE]) -> Self {
zvolin marked this conversation as resolved.
Show resolved Hide resolved
Namespace(nmt_rs::NamespaceId(bytes))
}

pub const fn const_v0(id: [u8; NS_ID_V0_SIZE]) -> Self {
let mut bytes = [0u8; NS_SIZE];
let start = NS_SIZE - NS_ID_V0_SIZE;
Expand Down
53 changes: 27 additions & 26 deletions types/src/share.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,36 +33,37 @@ pub struct NamespacedRow {
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(try_from = "RawShare", into = "RawShare")]
pub struct Share {
pub namespace: Namespace,
pub data: Vec<u8>,
pub data: [u8; appconsts::SHARE_SIZE],
}

impl Share {
pub fn new(mut bytes: Vec<u8>) -> Result<Self> {
if bytes.len() != appconsts::SHARE_SIZE {
return Err(Error::InvalidShareSize(bytes.len()));
pub fn new(data: &[u8]) -> Result<Self> {
if data.len() != appconsts::SHARE_SIZE {
return Err(Error::InvalidShareSize(data.len()));
}

let namespace = Namespace::from_raw(&bytes[..NS_SIZE])?;
bytes.drain(..NS_SIZE);
Namespace::from_raw(&data[..NS_SIZE])?;
zvolin marked this conversation as resolved.
Show resolved Hide resolved

Ok(Share {
namespace,
data: bytes,
data: data.try_into().unwrap(),
})
}

pub fn namespace(&self) -> Namespace {
Namespace::new_unchecked(self.data[..NS_SIZE].try_into().unwrap())
}

pub fn data(&self) -> &[u8] {
&self.data[NS_SIZE..]
}

pub fn to_vec(&self) -> Vec<u8> {
let mut bytes = self.namespace.as_bytes().to_vec();
bytes.extend_from_slice(&self.data);
bytes
self.as_ref().to_vec()
}
}

pub fn to_array(&self) -> [u8; appconsts::SHARE_SIZE] {
let mut out = [0; appconsts::SHARE_SIZE];
out[..NS_SIZE].copy_from_slice(self.namespace.as_bytes());
out[NS_SIZE..].copy_from_slice(&self.data);
out
impl AsRef<[u8]> for Share {
fn as_ref(&self) -> &[u8] {
&self.data
}
}

Expand Down Expand Up @@ -103,7 +104,7 @@ impl TryFrom<RawShare> for Share {
type Error = Error;

fn try_from(value: RawShare) -> Result<Self, Self::Error> {
Share::new(value.data)
Share::new(&value.data)
}
}

Expand All @@ -124,7 +125,7 @@ impl TryFrom<RawRow> for NamespacedRow {
let shares = value
.shares
.into_iter()
.map(Share::new)
.map(|bytes| Share::new(&bytes))
.collect::<Result<Vec<_>>>()?;

let proof: NamespaceProof = value
Expand Down Expand Up @@ -157,13 +158,13 @@ mod tests {

#[test]
fn share_should_have_correct_len() {
Share::new(vec![0; 0]).unwrap_err();
Share::new(vec![0; 100]).unwrap_err();
Share::new(vec![0; appconsts::SHARE_SIZE - 1]).unwrap_err();
Share::new(vec![0; appconsts::SHARE_SIZE + 1]).unwrap_err();
Share::new(vec![0; 2 * appconsts::SHARE_SIZE]).unwrap_err();
Share::new(&[0; 0]).unwrap_err();
Share::new(&[0; 100]).unwrap_err();
Share::new(&[0; appconsts::SHARE_SIZE - 1]).unwrap_err();
Share::new(&[0; appconsts::SHARE_SIZE + 1]).unwrap_err();
Share::new(&[0; 2 * appconsts::SHARE_SIZE]).unwrap_err();

Share::new(vec![0; appconsts::SHARE_SIZE]).unwrap();
Share::new(&vec![0; appconsts::SHARE_SIZE]).unwrap();
}

#[test]
Expand Down