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

Add SDK Data Files and Rust Library #3

Merged
merged 23 commits into from
Feb 8, 2022
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
059b3f4
wip: start moving to new sdk system
bhgomes Jan 19, 2022
587def7
wip: add temporary proving and verifying keys
bhgomes Jan 20, 2022
55270ad
feat: add compile-time maps for assets
bhgomes Jan 20, 2022
ca97d02
feat: add checksums to Rust library
bhgomes Jan 23, 2022
e456e13
chore: update data
bhgomes Jan 23, 2022
94c06fc
chore: bump license year
bhgomes Jan 23, 2022
dae0005
feat: add download framework
bhgomes Jan 24, 2022
8fe6150
fix: add tests for GitHub downloads
bhgomes Jan 24, 2022
f057efe
fix: update parameters to uncompressed form
bhgomes Jan 24, 2022
f9d4240
fix: add more logging in test
bhgomes Jan 24, 2022
3458a73
fix: remove nightly Result feature
bhgomes Jan 25, 2022
2634559
fix: remove unnecessary dependencies
bhgomes Jan 25, 2022
cc6086e
chore: bump deps and optimize library
bhgomes Jan 26, 2022
07d7901
feat: remove old asset data and improve checksum support
bhgomes Jan 31, 2022
0259bce
feat: add data getter method which does automatic checksum validation
bhgomes Jan 31, 2022
3a4378d
fix: improve error message for checksum mismatch
bhgomes Jan 31, 2022
b556f6c
fix: add data to checksum error branch
bhgomes Jan 31, 2022
5189d21
wip: include data directory in Cargo.toml
bhgomes Jan 31, 2022
8658aa2
wip: remove files from git-lfs
bhgomes Jan 31, 2022
559cafc
wip: add attributes file back
bhgomes Jan 31, 2022
f9251a5
fix: move files out of Git-LFS
bhgomes Jan 31, 2022
cc29c47
fix: add legacy proving keys
bhgomes Feb 8, 2022
7060401
feat: use main branch for downloading
bhgomes Feb 8, 2022
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
10 changes: 10 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
root = true

[*.rs]
charset=utf-8
end_of_line=lf
insert_final_newline=true
max_line_length=100
tab_width=4
trim_trailing_whitespace=true

1 change: 0 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
*.bin filter=lfs diff=lfs merge=lfs -text
10 changes: 1 addition & 9 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,2 @@
# Generated by Cargo
# will have compiled files and executables
/target/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
/target
Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk
2 changes: 2 additions & 0 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
imports_granularity="Crate"
license_template_path = "FILE_TEMPLATE"
52 changes: 52 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
[package]
name = "manta-sdk"
edition = "2021"
version = "0.4.0"
authors = ["Manta Network <[email protected]>"]
readme = "README.md"
license-file = "LICENSE"
repository = "https://github.com/Manta-Network/sdk"
homepage = "https://github.com/Manta-Network"
documentation = "https://github.com/Manta-Network/sdk"
categories = [""]
keywords = [""]
description = "Manta Network SDK"
include = ["LICENSE", "README.md", "data", "src/lib.rs"]
publish = false

[package.metadata.docs.rs]
# To build locally:
# RUSTDOCFLAGS="--cfg doc_cfg" cargo +nightly doc --all-features --open
all-features = true
rustdoc-args = ["--cfg", "doc_cfg"]

[badges]
is-it-maintained-issue-resolution = { repository = "Manta-Network/sdk" }
is-it-maintained-open-issues = { repository = "Manta-Network/sdk" }
maintenance = { status = "actively-developed" }

[features]
# Enable Allocation
alloc = []

# Enable Standard Library
std = ["alloc"]

# Download Data from GitHub
download = ["anyhow/std", "attohttpc", "std"]

[dependencies]
anyhow = { version = "1.0.53", default-features = false, optional = true }
attohttpc = { version = "0.18.0", optional = true }
blake3 = { version = "1.3.1", default-features = false }

[dev-dependencies]
hex = { version = "0.4.3", default-features = false, features = ["std"] }
tempfile = { version = "3.3.0", default-features = false }
walkdir = { version = "2.3.2", default-features = false }

[build-dependencies]
anyhow = { version = "1.0.52", default-features = false, features = ["std"] }
blake3 = { version = "1.3.1", default-features = false, features = ["std"] }
hex = { version = "0.4.3", default-features = false, features = ["std"] }
walkdir = { version = "2.3.2", default-features = false }
15 changes: 15 additions & 0 deletions FILE_TEMPLATE
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2019-2022 Manta Network.
// This file is part of manta-sdk.
//
// manta-sdk is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// manta-sdk is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with manta-sdk. If not, see <http://www.gnu.org/licenses/>.
29 changes: 27 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,27 @@
# sdk
Manta Network SDK
# Manta Network SDK

## Get Started

This SDK represents the parts of the Manta protocols which depend on real-world data or concrete public parameters. To build on top of of the Manta protocols, be sure to visit [`manta-rs`](https://github.com/manta-network/manta-rs) for actual libraries and implementations of the protocols.

The SDK is comprised of a bunch of data files, either as raw binary data or JSON, and an accompanying Rust library which has access to these data files at compile-time. See the [`build.rs`](./build.rs) file for more on how those data files are parsed into Rust. Some data files are too large to package into a Rust library and so are left as stand-alone files. For these files, a [`BLAKE3`](https://github.com/BLAKE3-team/BLAKE3) digest is offered in the Rust library as a checksum.

## Checksums

For checksums we use [`BLAKE3`](https://github.com/BLAKE3-team/BLAKE3). Install the `b3sum` command with

```sh
cargo install b3sum
```

to compute the checksums for yourself. The checksums for the [`data`](./data/) directory are stored in [`data.checkfile`](./data.checkfile) which is created by the following command:

```sh
b3sum data/**/* 2>/dev/null > data.checkfile
```

To check that the checkfile is up-to-date use the following command:

```sh
b3sum --check data.checkfile
```
131 changes: 131 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// Copyright 2019-2022 Manta Network.
// This file is part of manta-sdk.
//
// manta-sdk is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// manta-sdk is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with manta-sdk. If not, see <http://www.gnu.org/licenses/>.

//! Manta SDK Build Script

use anyhow::{anyhow, bail, ensure, Result};
use hex::FromHex;
use std::{
collections::HashMap,
env,
fs::{self, OpenOptions},
io::{BufRead, BufReader},
path::{Path, PathBuf},
};

/// Returns the parent of `path` which should exist relative to `OUT_DIR`.
#[inline]
fn parent(path: &Path) -> Result<&Path> {
path.parent().ok_or(anyhow!(
"The parent should be in the subtree of the `OUT_DIR` directory."
))
}

/// Checksum
type Checksum = [u8; 32];

/// Checksum Map
type ChecksumMap = HashMap<PathBuf, Checksum>;

/// Parses the checkfile at `path` producing a [`ChecksumMap`] for all the files in the data
/// directory.
#[inline]
fn parse_checkfile<P>(path: P) -> Result<ChecksumMap>
where
P: AsRef<Path>,
{
let file = OpenOptions::new().read(true).open(path)?;
let mut checksums = ChecksumMap::new();
for line in BufReader::new(file).lines() {
let line = line?;
let mut iter = line.split(" ");
match (iter.next(), iter.next(), iter.next()) {
(Some(checksum), Some(path), None) => {
checksums.insert(path.into(), Checksum::from_hex(checksum)?);
}
_ => bail!("Invalid checkfile line: {:?}", line),
}
}
Ok(checksums)
}

/// Gets the checksum from the `checksums` map for `path` returning an error if it was not found.
#[inline]
fn get_checksum<P>(checksums: &ChecksumMap, path: P) -> Result<Checksum>
where
P: AsRef<Path>,
{
let path = path.as_ref();
checksums
.get(path)
.ok_or_else(|| anyhow!("Unable to get checksum for path: {:?}", path))
.map(move |c| *c)
}

/// Writes the `checksum` to `path` returning an error if the write failed.
#[inline]
fn write_checksum<P>(path: P, checksum: Checksum) -> Result<()>
where
P: AsRef<Path>,
{
Ok(fs::write(
path.as_ref().with_extension("checksum"),
checksum,
)?)
}

/// Compiles raw data files by copying them to the `out_dir` directory to be interpreted as blobs.
#[inline]
fn compile_dat(source: &Path, out_dir: &Path, checksums: &ChecksumMap) -> Result<()> {
let checksum = get_checksum(checksums, source)?;
let data = fs::read(source)?;
let found_checksum = blake3::hash(&data);
ensure!(
found_checksum == checksum,
"Checksum did not match for {:?}. Expected: {:?}, Found: {:?}. Data: {:?}",
source,
hex::encode(checksum),
found_checksum,
data,
);
let target = out_dir.join(source);
fs::create_dir_all(parent(&target)?)?;
fs::copy(source, &target)?;
write_checksum(target, checksum)?;
Ok(())
}

/// Loads all the files from `data` into the `OUT_DIR` directory for inclusion into the library.
#[inline]
fn main() -> Result<()> {
println!("cargo:rerun-if-changed=data");
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
let checksums = parse_checkfile("data.checkfile")?;
for file in walkdir::WalkDir::new("data") {
let file = file?;
let path = file.path();
if !path.is_dir() {
match path.extension() {
Some(extension) => match extension.to_str() {
Some("dat") => compile_dat(path, &out_dir, &checksums)?,
_ => bail!("Unsupported data file extension."),
},
_ => bail!("All data files must have an extension."),
}
}
}
Ok(())
}
10 changes: 10 additions & 0 deletions data.checkfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
df87563f5a6366a341f4dd1796ecd7eec85354e0e8e0bb9e2a8cdb33be3a31e9 data/pay/testnet/parameters/key-agreement.dat
348d7e0072ffdb56fd19d6115c89a3c9172639c5167e8a7e905dc8a575e4f17d data/pay/testnet/parameters/utxo-commitment-scheme.dat
05e6b3b6a0d6d6debe458b6f7ddaefa51a4d38608024a98fc4dedf5ee8b68e99 data/pay/testnet/parameters/utxo-set-parameters.dat
9dcf5d51f6ffbcda46eaaab64e7350095465f8cc774f8276b46889c46479a2e4 data/pay/testnet/parameters/void-number-hash-function.dat
9748336897c8c98764e5701717871e39a5794e61bbc9e150c613a0ed7024986a data/pay/testnet/proving/mint.dat
d5d73fd62ff0f7bf8945f34e27b1471c0445e9835313ebb689be80a8ea3e7cee data/pay/testnet/proving/private-transfer.dat
22999c822392afd18992dedeb849ae0ccb0bf76399f60fc64a24dff2592ed763 data/pay/testnet/proving/reclaim.dat
2624fa19b9193eec55d83f72b090cd08623c351382c02add861e0626ac59de5f data/pay/testnet/verifying/mint.dat
c90cd08e2ed765b35888e6dc93c963919ed4cbf66db891c1b45d3eb5ec594d29 data/pay/testnet/verifying/private-transfer.dat
69726f8db5b9bf9c5c4592d473c741fff08a909bdfa4c8fc0508c207c9a4ad3d data/pay/testnet/verifying/reclaim.dat
1 change: 1 addition & 0 deletions data/pay/testnet/parameters/key-agreement.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.B���04����1�tLo�'5'�����ȝQ<Om
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added data/pay/testnet/proving/mint.dat
Binary file not shown.
Binary file added data/pay/testnet/proving/private-transfer.dat
Binary file not shown.
Binary file added data/pay/testnet/proving/reclaim.dat
Binary file not shown.
Binary file added data/pay/testnet/verifying/mint.dat
Binary file not shown.
Binary file added data/pay/testnet/verifying/private-transfer.dat
Binary file not shown.
Binary file added data/pay/testnet/verifying/reclaim.dat
Binary file not shown.
Loading