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(ci): add binary compatiblity test #90

Merged
merged 16 commits into from
Jun 16, 2022
Merged
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## [Unreleased]
### Added
- [\#90](https://github.com/Manta-Network/manta-rs/pull/90) Add Binary Compatibility Test for `manta-pay`
- [\#102](https://github.com/Manta-Network/manta-rs/pull/102) Add concrete parameters to `manta-parameters`

### Changed
Expand Down
18 changes: 18 additions & 0 deletions manta-accounting/src/transfer/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,3 +400,21 @@ where
pre_sender,
))
}

/// Asserts that `post` represents a valid `Transfer` verifying against `verifying_context`.
#[inline]
pub fn assert_valid_proof<C>(verifying_context: &VerifyingContext<C>, post: &TransferPost<C>)
where
C: Configuration,
<C::ProofSystem as ProofSystem>::Error: Debug,
{
assert!(
C::ProofSystem::verify(
verifying_context,
&post.generate_proof_input(),
&post.validity_proof,
)
.expect("Unable to verify proof."),
"Invalid proof.",
);
}
10 changes: 5 additions & 5 deletions manta-benchmark/benches/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
// along with manta-rs. If not, see <http://www.gnu.org/licenses/>.

use criterion::{black_box, criterion_group, criterion_main, Criterion};
use manta_benchmark::payment;
use manta_accounting::transfer::test::assert_valid_proof;
use manta_crypto::rand::{OsRng, Rand};
use manta_pay::parameters;
use manta_pay::{parameters, test::payment::prove_mint};

fn prove(c: &mut Criterion) {
let mut group = c.benchmark_group("bench");
Expand All @@ -27,7 +27,7 @@ fn prove(c: &mut Criterion) {
group.bench_function("mint prove", |b| {
let asset = black_box(rng.gen());
b.iter(|| {
payment::prove_mint(
prove_mint(
&proving_context.mint,
&parameters,
&utxo_accumulator_model,
Expand All @@ -43,7 +43,7 @@ fn verify(c: &mut Criterion) {
let (proving_context, verifying_context, parameters, utxo_accumulator_model) =
parameters::generate().unwrap();
let mut rng = OsRng;
let mint = black_box(payment::prove_mint(
let mint = black_box(prove_mint(
&proving_context.mint,
&parameters,
&utxo_accumulator_model,
Expand All @@ -52,7 +52,7 @@ fn verify(c: &mut Criterion) {
));
group.bench_function("mint verify", |b| {
b.iter(|| {
payment::assert_valid_proof(&verifying_context.mint, &mint);
assert_valid_proof(&verifying_context.mint, &mint);
})
});
}
Expand Down
8 changes: 4 additions & 4 deletions manta-benchmark/benches/private_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@
// along with manta-rs. If not, see <http://www.gnu.org/licenses/>.

use criterion::{black_box, criterion_group, criterion_main, Criterion};
use manta_benchmark::payment::{self, assert_valid_proof};
use manta_accounting::transfer::test::assert_valid_proof;
use manta_crypto::rand::OsRng;
use manta_pay::parameters;
use manta_pay::{parameters, test::payment::prove_private_transfer};

fn prove(c: &mut Criterion) {
let mut group = c.benchmark_group("bench");
let mut rng = OsRng;
let (proving_context, _, parameters, utxo_accumulator_model) = parameters::generate().unwrap();
group.bench_function("private transfer prove", |b| {
b.iter(|| {
let _ = payment::prove_private_transfer(
prove_private_transfer(
&proving_context,
&parameters,
&utxo_accumulator_model,
Expand All @@ -40,7 +40,7 @@ fn verify(c: &mut Criterion) {
let mut rng = OsRng;
let (proving_context, verifying_context, parameters, utxo_accumulator_model) =
parameters::generate().unwrap();
let private_transfer = black_box(payment::prove_private_transfer(
let private_transfer = black_box(prove_private_transfer(
&proving_context,
&parameters,
&utxo_accumulator_model,
Expand Down
8 changes: 4 additions & 4 deletions manta-benchmark/benches/reclaim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@
// along with manta-rs. If not, see <http://www.gnu.org/licenses/>.

use criterion::{black_box, criterion_group, criterion_main, Criterion};
use manta_benchmark::payment::{self, assert_valid_proof};
use manta_accounting::transfer::test::assert_valid_proof;
use manta_crypto::rand::OsRng;
use manta_pay::parameters;
use manta_pay::{parameters, test::payment::prove_reclaim};

fn prove(c: &mut Criterion) {
let mut group = c.benchmark_group("bench");
let mut rng = OsRng;
let (proving_context, _, parameters, utxo_accumulator_model) = parameters::generate().unwrap();
group.bench_function("reclaim prove", |b| {
b.iter(|| {
let _ = payment::prove_reclaim(
prove_reclaim(
&proving_context,
&parameters,
&utxo_accumulator_model,
Expand All @@ -40,7 +40,7 @@ fn verify(c: &mut Criterion) {
let mut rng = OsRng;
let (proving_context, verifying_context, parameters, utxo_accumulator_model) =
parameters::generate().unwrap();
let reclaim = black_box(payment::prove_reclaim(
let reclaim = black_box(prove_reclaim(
&proving_context,
&parameters,
&utxo_accumulator_model,
Expand Down
5 changes: 2 additions & 3 deletions manta-benchmark/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,17 @@
// You should have received a copy of the GNU General Public License
// along with manta-rs. If not, see <http://www.gnu.org/licenses/>.

use crate::payment::assert_valid_proof;
use manta_accounting::transfer::test::assert_valid_proof;
use manta_crypto::rand::{OsRng, Rand};
use manta_pay::{
config::{
MultiProvingContext, MultiVerifyingContext, Parameters, TransferPost, UtxoAccumulatorModel,
},
parameters,
test::payment,
};
use wasm_bindgen::prelude::wasm_bindgen;

pub mod payment;

#[wasm_bindgen]
#[derive(Clone, Debug)]
pub struct Context {
Expand Down
6 changes: 4 additions & 2 deletions manta-pay/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ simulation = [
std = ["manta-accounting/std", "manta-util/std"]

# Testing Frameworks
test = ["manta-accounting/test", "manta-crypto/test"]
test = ["anyhow", "manta-accounting/test", "manta-crypto/test", "manta-parameters/download", "tempfile"]

# Wallet
wallet = ["bip32", "manta-crypto/getrandom", "std"]
Expand All @@ -104,6 +104,7 @@ websocket = [

[dependencies]
aes-gcm = { version = "0.9.4", default-features = false, features = ["aes", "alloc"] }
anyhow = { version = "1.0.57", optional = true, default-features = false }
ark-bls12-381 = { version = "0.3.0", optional = true, default-features = false, features = ["curve"] }
ark-ec = { version = "0.3.0", optional = true, default-features = false }
ark-ed-on-bls12-381 = { version = "0.3.0", optional = true, default-features = false, features = ["r1cs"] }
Expand All @@ -123,6 +124,7 @@ futures = { version = "0.3.21", optional = true, default-features = false }
indexmap = { version = "1.8.2", optional = true, default-features = false }
manta-accounting = { path = "../manta-accounting", default-features = false }
manta-crypto = { path = "../manta-crypto", default-features = false }
manta-parameters = { path = "../manta-parameters", optional = true, default-features = false }
manta-util = { path = "../manta-util", default-features = false }
parking_lot = { version = "0.12.1", optional = true, default-features = false }
rand_chacha = { version = "0.3.1", default-features = false }
Expand All @@ -131,6 +133,7 @@ reqwest = { version = "0.11.9", optional = true, default-features = false, featu
scale-codec = { package = "parity-scale-codec", version = "3.1.2", optional = true, default-features = false, features = ["derive", "max-encoded-len"] }
scale-info = { version = "2.1.2", optional = true, default-features = false, features = ["derive"] }
serde_json = { version = "1.0.79", optional = true, default-features = false, features = ["alloc"] }
tempfile = { version = "3.3.0", optional = true, default-features = false }
tide = { version = "0.16.0", optional = true, default-features = false, features = ["h1-server"] }
tokio = { version = "1.18.2", optional = true, default-features = false }
tokio-tungstenite = { version = "0.17.1", optional = true, default-features = false, features = ["native-tls"] }
Expand All @@ -140,4 +143,3 @@ ws_stream_wasm = { version = "0.7.3", optional = true, default-features = false
[dev-dependencies]
manta-crypto = { path = "../manta-crypto", default-features = false, features = ["getrandom"] }
manta-pay = { path = ".", default-features = false, features = ["groth16", "scale", "scale-std", "std", "test"] }
tempfile = { version = "3.3.0", default-features = false }
7 changes: 4 additions & 3 deletions manta-pay/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@

extern crate alloc;

#[cfg(test)]
mod test;

pub mod crypto;
pub mod util;

Expand All @@ -48,3 +45,7 @@ pub mod signer;
#[cfg(all(feature = "groth16", feature = "simulation"))]
#[cfg_attr(doc_cfg, doc(cfg(all(feature = "groth16", feature = "simulation"))))]
pub mod simulation;

#[cfg(any(test, feature = "test"))]
#[cfg_attr(doc_cfg, doc(cfg(feature = "test")))]
pub mod test;
142 changes: 142 additions & 0 deletions manta-pay/src/test/compatibility.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// Copyright 2019-2022 Manta Network.
// This file is part of manta-rs.
//
// manta-rs 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-rs 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-rs. If not, see <http://www.gnu.org/licenses/>.

//! Manta Pay UTXO Binary Compatibility
//! Checks if the current circuit implementation is compatible with precomputed parameters.

use crate::{
config::{
MultiProvingContext, MultiVerifyingContext, NoteEncryptionScheme, Parameters,
ProvingContext, UtxoAccumulatorModel, UtxoCommitmentScheme, VerifyingContext,
VoidNumberCommitmentScheme,
},
test::payment::{prove_mint, prove_private_transfer, prove_reclaim},
};
use anyhow::Result;
use manta_accounting::transfer::test::assert_valid_proof;
use manta_crypto::rand::{OsRng, Rand};
use manta_util::codec::{Decode, IoReader};
use std::{fs::File, path::Path};

/// Loads parameters from the `manta-parameters`, using `directory` as a temporary directory to store files.
#[inline]
fn load_parameters(
directory: &Path,
) -> Result<(
MultiProvingContext,
MultiVerifyingContext,
Parameters,
UtxoAccumulatorModel,
)> {
println!("Loading parameters...");
let mint_path = directory.join("mint.dat");
manta_parameters::pay::testnet::proving::Mint::download(&mint_path)?;
let private_transfer_path = directory.join("private-transfer.dat");
manta_parameters::pay::testnet::proving::PrivateTransfer::download(&private_transfer_path)?;
let reclaim_path = directory.join("reclaim.dat");
manta_parameters::pay::testnet::proving::Reclaim::download(&reclaim_path)?;
println!("mint_path: {:?}", mint_path);
let proving_context = MultiProvingContext {
mint: ProvingContext::decode(IoReader(File::open(mint_path)?))
.expect("Unable to decode MINT proving context."),
private_transfer: ProvingContext::decode(IoReader(File::open(private_transfer_path)?))
.expect("Unable to decode PRIVATE_TRANSFER proving context."),
reclaim: ProvingContext::decode(IoReader(File::open(reclaim_path)?))
.expect("Unable to decode RECLAIM proving context."),
};
let verifying_context = MultiVerifyingContext {
mint: VerifyingContext::decode(
manta_parameters::pay::testnet::verifying::Mint::get()
.expect("Checksum did not match."),
)
.expect("Unable to decode MINT verifying context."),
private_transfer: VerifyingContext::decode(
manta_parameters::pay::testnet::verifying::PrivateTransfer::get()
.expect("Checksum did not match."),
)
.expect("Unable to decode PRIVATE_TRANSFER verifying context."),
reclaim: VerifyingContext::decode(
manta_parameters::pay::testnet::verifying::Reclaim::get()
.expect("Checksum did not match."),
)
.expect("Unable to decode RECLAIM verifying context."),
};
let parameters = Parameters {
note_encryption_scheme: NoteEncryptionScheme::decode(
manta_parameters::pay::testnet::parameters::NoteEncryptionScheme::get()
.expect("Checksum did not match."),
)
.expect("Unable to decode NOTE_ENCRYPTION_SCHEME parameters."),
utxo_commitment: UtxoCommitmentScheme::decode(
manta_parameters::pay::testnet::parameters::UtxoCommitmentScheme::get()
.expect("Checksum did not match."),
)
.expect("Unable to decode UTXO_COMMITMENT_SCHEME parameters."),
void_number_commitment: VoidNumberCommitmentScheme::decode(
manta_parameters::pay::testnet::parameters::VoidNumberCommitmentScheme::get()
.expect("Checksum did not match."),
)
.expect("Unable to decode VOID_NUMBER_COMMITMENT_SCHEME parameters."),
};
println!("Loading parameters Done.");
Ok((
proving_context,
verifying_context,
parameters,
UtxoAccumulatorModel::decode(
manta_parameters::pay::testnet::parameters::UtxoAccumulatorModel::get()
.expect("Checksum did not match."),
)
.expect("Unable to decode UTXO_ACCUMULATOR_MODEL."),
))
}

/// Tests that the circuit is compatible with the current known parameters in `manta-parameters`.
#[test]
fn compatibility() {
let directory = tempfile::tempdir().expect("Unable to generate temporary test directory.");
let mut rng = OsRng;
let (proving_context, verifying_context, parameters, utxo_accumulator_model) =
load_parameters(directory.path()).expect("Failed to load parameters");
assert_valid_proof(
&verifying_context.mint,
&prove_mint(
&proving_context.mint,
&parameters,
&utxo_accumulator_model,
rng.gen(),
&mut rng,
),
);
assert_valid_proof(
&verifying_context.private_transfer,
&prove_private_transfer(
&proving_context,
&parameters,
&utxo_accumulator_model,
&mut rng,
),
);
assert_valid_proof(
&verifying_context.reclaim,
&prove_reclaim(
&proving_context,
&parameters,
&utxo_accumulator_model,
&mut rng,
),
);
}
8 changes: 8 additions & 0 deletions manta-pay/src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,12 @@
// #[cfg_attr(doc_cfg, doc(cfg(feature = "simulation")))]
// pub mod simulation;

#[cfg(test)]
pub mod compatibility;

#[cfg(test)]
pub mod transfer;

#[cfg(feature = "groth16")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "groth16")))]
pub mod payment;
Loading