Skip to content

Commit

Permalink
Merge pull request #44 from DaviRain-Su/no-std-cleanup
Browse files Browse the repository at this point in the history
Fix cargo protoc error
  • Loading branch information
ethanfrey authored Jul 21, 2021
2 parents 51fb73d + f78ae0a commit 5a0206f
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 243 deletions.
244 changes: 52 additions & 192 deletions rust/Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ members = ["codegen"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
prost = { version = "0.7.0", default-features = false, features = ["prost-derive"] }
prost = { version = "0.8.0", default-features = false, features = ["prost-derive"] }
bytes = { version = "1.0.1", default-features = false }
hex = { version = "0.4.3", default-features = false, features = [ "alloc" ] }
sha2 = { version = "0.9.3", default-features = false }
Expand Down
6 changes: 3 additions & 3 deletions rust/codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
prost = "0.5"
prost-build = "0.5"
bytes = "0.4.12"
prost = "0.8.0"
prost-build = "0.8.0"
bytes = "1.0.1"
12 changes: 6 additions & 6 deletions rust/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub fn verify_membership(

// if let Some(ics23::commitment_proof::Proof::Exist(ex)) = &proof.proof {
if let Some(ex) = get_exist_proof(proof, key) {
let valid = verify_existence(&ex, spec, root, key, value);
let valid = verify_existence(ex, spec, root, key, value);
valid.is_ok()
} else {
false
Expand Down Expand Up @@ -58,7 +58,7 @@ pub fn verify_non_membership(
}

if let Some(non) = get_nonexist_proof(proof, key) {
let valid = verify_non_existence(&non, spec, root, key);
let valid = verify_non_existence(non, spec, root, key);
valid.is_ok()
} else {
false
Expand Down Expand Up @@ -397,18 +397,18 @@ mod tests {
data: &RefData,
) -> Result<()> {
if let Some(value) = &data.value {
let valid = super::verify_membership(&proof, spec, &data.root, &data.key, &value);
let valid = super::verify_membership(proof, spec, &data.root, &data.key, value);
ensure!(valid, "invalid test vector");
let mut items = BTreeMap::new();
items.insert(data.key.as_slice(), value.as_slice());
let valid = super::verify_batch_membership(&proof, spec, &data.root, items);
let valid = super::verify_batch_membership(proof, spec, &data.root, items);
ensure!(valid, "invalid test vector");
Ok(())
} else {
let valid = super::verify_non_membership(&proof, spec, &data.root, &data.key);
let valid = super::verify_non_membership(proof, spec, &data.root, &data.key);
ensure!(valid, "invalid test vector");
let keys = &[data.key.as_slice()];
let valid = super::verify_batch_non_membership(&proof, spec, &data.root, keys);
let valid = super::verify_batch_non_membership(proof, spec, &data.root, keys);
ensure!(valid, "invalid test vector");
Ok(())
}
Expand Down
17 changes: 8 additions & 9 deletions rust/src/compress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ use crate::helpers::Result;
use crate::ics23;

pub fn is_compressed(proof: &ics23::CommitmentProof) -> bool {
if let Some(ics23::commitment_proof::Proof::Compressed(_)) = &proof.proof {
true
} else {
false
}
matches!(
&proof.proof,
Some(ics23::commitment_proof::Proof::Compressed(_))
)
}

pub fn compress(proof: &ics23::CommitmentProof) -> Result<ics23::CommitmentProof> {
Expand All @@ -38,7 +37,7 @@ pub fn compress_batch(proof: &ics23::BatchProof) -> Result<ics23::CommitmentProo
for entry in &proof.entries {
let centry = match &entry.proof {
Some(ics23::batch_entry::Proof::Exist(ex)) => {
let exist = compress_exist(&ex, &mut lookup, &mut registry)?;
let exist = compress_exist(ex, &mut lookup, &mut registry)?;
ics23::CompressedBatchEntry {
proof: Some(ics23::compressed_batch_entry::Proof::Exist(exist)),
}
Expand Down Expand Up @@ -116,14 +115,14 @@ pub fn decompress_batch(proof: &ics23::CompressedBatchProof) -> Result<ics23::Co
.map(|cent| -> Result<ics23::BatchEntry> {
match &cent.proof {
Some(ics23::compressed_batch_entry::Proof::Exist(ex)) => {
let exist = decompress_exist(&ex, &lookup);
let exist = decompress_exist(ex, lookup);
Ok(ics23::BatchEntry {
proof: Some(ics23::batch_entry::Proof::Exist(exist)),
})
}
Some(ics23::compressed_batch_entry::Proof::Nonexist(non)) => {
let left = non.left.clone().map(|l| decompress_exist(&l, &lookup));
let right = non.right.clone().map(|r| decompress_exist(&r, &lookup));
let left = non.left.clone().map(|l| decompress_exist(&l, lookup));
let right = non.right.clone().map(|r| decompress_exist(&r, lookup));
let nonexist = ics23::NonExistenceProof {
key: non.key.clone(),
left,
Expand Down
57 changes: 30 additions & 27 deletions rust/src/ics23.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
///length-prefix the data before hashing it.
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct ExistenceProof {
#[prost(bytes, tag = "1")]
pub key: alloc::vec::Vec<u8>,
#[prost(bytes, tag = "2")]
pub value: alloc::vec::Vec<u8>,
#[prost(bytes = "vec", tag = "1")]
pub key: ::prost::alloc::vec::Vec<u8>,
#[prost(bytes = "vec", tag = "2")]
pub value: ::prost::alloc::vec::Vec<u8>,
#[prost(message, optional, tag = "3")]
pub leaf: ::core::option::Option<LeafOp>,
#[prost(message, repeated, tag = "4")]
pub path: ::alloc::vec::Vec<InnerOp>,
pub path: ::prost::alloc::vec::Vec<InnerOp>,
}
///
///NonExistenceProof takes a proof of two neighbors, one left of the desired key,
Expand All @@ -36,8 +36,8 @@ pub struct ExistenceProof {
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct NonExistenceProof {
/// TODO: remove this as unnecessary??? we prove a range
#[prost(bytes, tag = "1")]
pub key: alloc::vec::Vec<u8>,
#[prost(bytes = "vec", tag = "1")]
pub key: ::prost::alloc::vec::Vec<u8>,
#[prost(message, optional, tag = "2")]
pub left: ::core::option::Option<ExistenceProof>,
#[prost(message, optional, tag = "3")]
Expand All @@ -50,6 +50,7 @@ pub struct CommitmentProof {
#[prost(oneof = "commitment_proof::Proof", tags = "1, 2, 3, 4")]
pub proof: ::core::option::Option<commitment_proof::Proof>,
}
/// Nested message and enum types in `CommitmentProof`.
pub mod commitment_proof {
#[derive(Clone, PartialEq, ::prost::Oneof)]
pub enum Proof {
Expand Down Expand Up @@ -90,8 +91,8 @@ pub struct LeafOp {
pub length: i32,
/// prefix is a fixed bytes that may optionally be included at the beginning to differentiate
/// a leaf node from an inner node.
#[prost(bytes, tag = "5")]
pub prefix: alloc::vec::Vec<u8>,
#[prost(bytes = "vec", tag = "5")]
pub prefix: ::prost::alloc::vec::Vec<u8>,
}
///*
///InnerOp represents a merkle-proof step that is not a leaf.
Expand All @@ -113,10 +114,10 @@ pub struct LeafOp {
pub struct InnerOp {
#[prost(enumeration = "HashOp", tag = "1")]
pub hash: i32,
#[prost(bytes, tag = "2")]
pub prefix: alloc::vec::Vec<u8>,
#[prost(bytes, tag = "3")]
pub suffix: alloc::vec::Vec<u8>,
#[prost(bytes = "vec", tag = "2")]
pub prefix: ::prost::alloc::vec::Vec<u8>,
#[prost(bytes = "vec", tag = "3")]
pub suffix: ::prost::alloc::vec::Vec<u8>,
}
///*
///ProofSpec defines what the expected parameters are for a given proof type.
Expand Down Expand Up @@ -159,16 +160,16 @@ pub struct InnerSpec {
/// iavl tree is [0, 1] (left then right)
/// merk is [0, 2, 1] (left, right, here)
#[prost(int32, repeated, tag = "1")]
pub child_order: ::alloc::vec::Vec<i32>,
pub child_order: ::prost::alloc::vec::Vec<i32>,
#[prost(int32, tag = "2")]
pub child_size: i32,
#[prost(int32, tag = "3")]
pub min_prefix_length: i32,
#[prost(int32, tag = "4")]
pub max_prefix_length: i32,
/// empty child is the prehash image that is used when one child is nil (eg. 20 bytes of 0)
#[prost(bytes, tag = "5")]
pub empty_child: alloc::vec::Vec<u8>,
#[prost(bytes = "vec", tag = "5")]
pub empty_child: ::prost::alloc::vec::Vec<u8>,
/// hash is the algorithm that must be used for each InnerOp
#[prost(enumeration = "HashOp", tag = "6")]
pub hash: i32,
Expand All @@ -178,14 +179,15 @@ pub struct InnerSpec {
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct BatchProof {
#[prost(message, repeated, tag = "1")]
pub entries: ::alloc::vec::Vec<BatchEntry>,
pub entries: ::prost::alloc::vec::Vec<BatchEntry>,
}
/// Use BatchEntry not CommitmentProof, to avoid recursion
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct BatchEntry {
#[prost(oneof = "batch_entry::Proof", tags = "1, 2")]
pub proof: ::core::option::Option<batch_entry::Proof>,
}
/// Nested message and enum types in `BatchEntry`.
pub mod batch_entry {
#[derive(Clone, PartialEq, ::prost::Oneof)]
pub enum Proof {
Expand All @@ -200,16 +202,17 @@ pub mod batch_entry {
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct CompressedBatchProof {
#[prost(message, repeated, tag = "1")]
pub entries: ::alloc::vec::Vec<CompressedBatchEntry>,
pub entries: ::prost::alloc::vec::Vec<CompressedBatchEntry>,
#[prost(message, repeated, tag = "2")]
pub lookup_inners: ::alloc::vec::Vec<InnerOp>,
pub lookup_inners: ::prost::alloc::vec::Vec<InnerOp>,
}
/// Use BatchEntry not CommitmentProof, to avoid recursion
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct CompressedBatchEntry {
#[prost(oneof = "compressed_batch_entry::Proof", tags = "1, 2")]
pub proof: ::core::option::Option<compressed_batch_entry::Proof>,
}
/// Nested message and enum types in `CompressedBatchEntry`.
pub mod compressed_batch_entry {
#[derive(Clone, PartialEq, ::prost::Oneof)]
pub enum Proof {
Expand All @@ -221,21 +224,21 @@ pub mod compressed_batch_entry {
}
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct CompressedExistenceProof {
#[prost(bytes, tag = "1")]
pub key: alloc::vec::Vec<u8>,
#[prost(bytes, tag = "2")]
pub value: alloc::vec::Vec<u8>,
#[prost(bytes = "vec", tag = "1")]
pub key: ::prost::alloc::vec::Vec<u8>,
#[prost(bytes = "vec", tag = "2")]
pub value: ::prost::alloc::vec::Vec<u8>,
#[prost(message, optional, tag = "3")]
pub leaf: ::core::option::Option<LeafOp>,
/// these are indexes into the lookup_inners table in CompressedBatchProof
#[prost(int32, repeated, tag = "4")]
pub path: ::alloc::vec::Vec<i32>,
pub path: ::prost::alloc::vec::Vec<i32>,
}
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct CompressedNonExistenceProof {
/// TODO: remove this as unnecessary??? we prove a range
#[prost(bytes, tag = "1")]
pub key: alloc::vec::Vec<u8>,
#[prost(bytes = "vec", tag = "1")]
pub key: ::prost::alloc::vec::Vec<u8>,
#[prost(message, optional, tag = "2")]
pub left: ::core::option::Option<CompressedExistenceProof>,
#[prost(message, optional, tag = "3")]
Expand All @@ -252,7 +255,7 @@ pub enum HashOp {
Ripemd160 = 4,
/// ripemd160(sha256(x))
Bitcoin = 5,
Sha512_256 = 6,
Sha512256 = 6,
}
///*
///LengthOp defines how to process the key and value of the LeafOp
Expand Down
4 changes: 2 additions & 2 deletions rust/src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fn do_hash(hash: HashOp, data: &[u8]) -> Hash {
HashOp::Bitcoin => {
Hash::from(Ripemd160::digest(Sha256::digest(data).as_slice()).as_slice())
}
HashOp::Sha512_256 => Hash::from(Sha512Trunc256::digest(data).as_slice()),
HashOp::Sha512256 => Hash::from(Sha512Trunc256::digest(data).as_slice()),
}
}

Expand Down Expand Up @@ -111,7 +111,7 @@ mod tests {
"bitcoin hash fails"
);

let hash = do_hash(HashOp::Sha512_256, b"food");
let hash = do_hash(HashOp::Sha512256, b"food");
assert!(
hash == decode("5b3a452a6acbf1fc1e553a40c501585d5bd3cca176d562e0a0e19a3c43804e88"),
"sha512/256 hash fails"
Expand Down
6 changes: 3 additions & 3 deletions rust/src/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub fn verify_existence(
ensure!(proof.key == key, "Provided key doesn't match proof");
ensure!(proof.value == value, "Provided value doesn't match proof");

let calc = calculate_existence_root(&proof)?;
let calc = calculate_existence_root(proof)?;
ensure!(calc == root, "Root hash doesn't match");
Ok(())
}
Expand All @@ -34,11 +34,11 @@ pub fn verify_non_existence(
key: &[u8],
) -> Result<()> {
if let Some(left) = &proof.left {
verify_existence(&left, spec, root, &left.key, &left.value)?;
verify_existence(left, spec, root, &left.key, &left.value)?;
ensure!(key > left.key.as_slice(), "left key isn't before key");
}
if let Some(right) = &proof.right {
verify_existence(&right, spec, root, &right.key, &right.value)?;
verify_existence(right, spec, root, &right.key, &right.value)?;
ensure!(key < right.key.as_slice(), "right key isn't after key");
}

Expand Down

0 comments on commit 5a0206f

Please sign in to comment.