diff --git a/Makefile b/Makefile index 074b444eb..f85df16eb 100644 --- a/Makefile +++ b/Makefile @@ -49,7 +49,7 @@ $(BINS): token_lists contracts $(PROOFS_BIN) $(BINDEPS) RUSTFLAGS="$(RUSTFLAGS)" $(CARGO) build --all-features --release --package $@ cp -f target/release/$@ $@ -check: token_lists zkas $(PROOFS_BIN) +check: token_lists zkas $(PROOFS_BIN) contracts RUSTFLAGS="$(RUSTFLAGS)" $(CARGO) hack check --release --feature-powerset --all fix: token_lists zkas $(PROOFS_BIN) @@ -62,8 +62,9 @@ rustdoc: token_lists zkas RUSTFLAGS="$(RUSTFLAGS)" $(CARGO) doc --release --workspace --all-features \ --no-deps --document-private-items -test: token_lists zkas $(PROOFS_BIN) +test: token_lists zkas $(PROOFS_BIN) contracts RUSTFLAGS="$(RUSTFLAGS)" $(CARGO) test --release --all-features --all + $(MAKE) -c src/contract/money test test-dao: zkas $(MAKE) -C example/dao diff --git a/src/sdk/src/crypto/merkle_node.rs b/src/sdk/src/crypto/merkle_node.rs index 8c7e3582c..be3f33378 100644 --- a/src/sdk/src/crypto/merkle_node.rs +++ b/src/sdk/src/crypto/merkle_node.rs @@ -158,3 +158,32 @@ impl Hashable for MerkleNode { EMPTY_ROOTS[::from(altitude)] } } + +#[cfg(test)] +mod tests { + use super::*; + + use halo2_proofs::arithmetic::Field; + use incrementalmerkletree::Tree; + use pasta_curves::pallas; + use rand::rngs::OsRng; + + #[test] + fn bridgetree_checkpoints() { + const MAX_CHECKPOINTS: usize = 100; + let mut tree = MerkleTree::new(MAX_CHECKPOINTS); + let mut roots = vec![]; + + for _ in 0..MAX_CHECKPOINTS { + let leaf = MerkleNode::from(pallas::Base::random(&mut OsRng)); + tree.append(&leaf); + roots.push(tree.root(0).unwrap()); + tree.checkpoint(); + } + + for root in roots.iter().rev() { + tree.rewind(); + assert!(root == &tree.root(0).unwrap()); + } + } +} diff --git a/tests/arithmetic_proof.rs b/tests/arithmetic_proof.rs deleted file mode 100644 index ca499a4d0..000000000 --- a/tests/arithmetic_proof.rs +++ /dev/null @@ -1,84 +0,0 @@ -/* This file is part of DarkFi (https://dark.fi) - * - * Copyright (C) 2020-2022 Dyne.org foundation - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program 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 Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -use halo2_proofs::circuit::Value; -use pasta_curves::pallas; -use rand::rngs::OsRng; -use simplelog::{ColorChoice, Config, LevelFilter, TermLogger, TerminalMode}; - -use darkfi::{ - zk::{ - proof::{ProvingKey, VerifyingKey}, - vm::{Witness, ZkCircuit}, - vm_stack::empty_witnesses, - Proof, - }, - zkas::decoder::ZkBinary, - Result, -}; - -#[test] -fn arithmetic_proof() -> Result<()> { - TermLogger::init(LevelFilter::Debug, Config::default(), TerminalMode::Mixed, ColorChoice::Auto) - .unwrap(); - - /* ANCHOR: main */ - let bincode = include_bytes!("../proof/arithmetic.zk.bin"); - let zkbin = ZkBinary::decode(bincode)?; - - // ====== - // Prover - // ====== - - // Witness values - let a = pallas::Base::from(42); - let b = pallas::Base::from(69); - let y_0 = pallas::Base::from(0); // Here we will compare a > b, which is false (0) - let y_1 = pallas::Base::from(1); // Here we will compare b > a, which is true (1) - - let prover_witnesses = vec![Witness::Base(Value::known(a)), Witness::Base(Value::known(b))]; - - // Create the public inputs - let sum = a + b; - let product = a * b; - let difference = a - b; - - let public_inputs = vec![sum, product, difference, y_0, y_1]; - - // Create the circuit - let circuit = ZkCircuit::new(prover_witnesses, zkbin.clone()); - - let proving_key = ProvingKey::build(13, &circuit); - let proof = Proof::create(&proving_key, &[circuit], &public_inputs, &mut OsRng)?; - - // ======== - // Verifier - // ======== - - // Construct empty witnesses - let verifier_witnesses = empty_witnesses(&zkbin); - - // Create the circuit - let circuit = ZkCircuit::new(verifier_witnesses, zkbin); - - let verifying_key = VerifyingKey::build(13, &circuit); - proof.verify(&verifying_key, &public_inputs)?; - /* ANCHOR_END: main */ - - Ok(()) -} diff --git a/tests/bridgetree_checkpoints.rs b/tests/bridgetree_checkpoints.rs deleted file mode 100644 index 737719fb0..000000000 --- a/tests/bridgetree_checkpoints.rs +++ /dev/null @@ -1,44 +0,0 @@ -/* This file is part of DarkFi (https://dark.fi) - * - * Copyright (C) 2020-2022 Dyne.org foundation - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program 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 Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -use darkfi_sdk::{ - crypto::{MerkleNode, MerkleTree}, - incrementalmerkletree::Tree, -}; -use halo2_proofs::arithmetic::Field; -use pasta_curves::pallas; -use rand::rngs::OsRng; - -#[test] -fn bridgetree_checkpoints() { - const MAX_CHECKPOINTS: usize = 100; - let mut tree = MerkleTree::new(MAX_CHECKPOINTS); - let mut roots = vec![]; - - for _ in 0..MAX_CHECKPOINTS { - let leaf = MerkleNode::from(pallas::Base::random(&mut OsRng)); - tree.append(&leaf); - roots.push(tree.root(0).unwrap()); - tree.checkpoint(); - } - - for root in roots.iter().rev() { - tree.rewind(); - assert!(root == &tree.root(0).unwrap()); - } -} diff --git a/tests/burn_proof.rs b/tests/burn_proof.rs deleted file mode 100644 index d5001a1c9..000000000 --- a/tests/burn_proof.rs +++ /dev/null @@ -1,153 +0,0 @@ -/* This file is part of DarkFi (https://dark.fi) - * - * Copyright (C) 2020-2022 Dyne.org foundation - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program 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 Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -use darkfi_sdk::{ - crypto::{ - pedersen::{pedersen_commitment_base, pedersen_commitment_u64}, - poseidon_hash, MerkleNode, Nullifier, PublicKey, SecretKey, - }, - incrementalmerkletree::{bridgetree::BridgeTree, Tree}, - pasta::{ - arithmetic::CurveAffine, - group::{ff::Field, Curve}, - pallas, - }, -}; -use halo2_gadgets::poseidon::primitives as poseidon; -use halo2_proofs::circuit::Value; -use rand::rngs::OsRng; - -use darkfi::{ - zk::{ - proof::{ProvingKey, VerifyingKey}, - vm::{Witness, ZkCircuit}, - vm_stack::empty_witnesses, - Proof, - }, - zkas::decoder::ZkBinary, - Result, -}; - -#[test] -fn burn_proof() -> Result<()> { - /* ANCHOR: main */ - let bincode = include_bytes!("../proof/burn.zk.bin"); - let zkbin = ZkBinary::decode(bincode)?; - - // ====== - // Prover - // ====== - - // Witness values - let value = 42; - let token_id = pallas::Base::random(&mut OsRng); - let value_blind = pallas::Scalar::random(&mut OsRng); - let token_blind = pallas::Scalar::random(&mut OsRng); - let serial = pallas::Base::random(&mut OsRng); - let coin_blind = pallas::Base::random(&mut OsRng); - let secret = SecretKey::random(&mut OsRng); - let sig_secret = SecretKey::random(&mut OsRng); - - // Build the coin - let coin2 = { - let (pub_x, pub_y) = PublicKey::from_secret(secret).xy(); - let messages = [pub_x, pub_y, pallas::Base::from(value), token_id, serial, coin_blind]; - - poseidon::Hash::<_, poseidon::P128Pow5T3, poseidon::ConstantLength<6>, 3, 2>::init() - .hash(messages) - }; - - // Fill the merkle tree with some random coins that we want to witness, - // and also add the above coin. - let mut tree = BridgeTree::::new(100); - let coin0 = pallas::Base::random(&mut OsRng); - let coin1 = pallas::Base::random(&mut OsRng); - let coin3 = pallas::Base::random(&mut OsRng); - - tree.append(&MerkleNode::from(coin0)); - tree.witness(); - tree.append(&MerkleNode::from(coin1)); - tree.append(&MerkleNode::from(coin2)); - let leaf_pos = tree.witness().unwrap(); - tree.append(&MerkleNode::from(coin3)); - tree.witness(); - - let root = tree.root(0).unwrap(); - let merkle_path = tree.authentication_path(leaf_pos, &root).unwrap(); - let leaf_pos: u64 = leaf_pos.into(); - - let prover_witnesses = vec![ - Witness::Base(Value::known(secret.inner())), - Witness::Base(Value::known(serial)), - Witness::Base(Value::known(pallas::Base::from(value))), - Witness::Base(Value::known(token_id)), - Witness::Base(Value::known(coin_blind)), - Witness::Scalar(Value::known(value_blind)), - Witness::Scalar(Value::known(token_blind)), - Witness::Uint32(Value::known(leaf_pos.try_into().unwrap())), - Witness::MerklePath(Value::known(merkle_path.try_into().unwrap())), - Witness::Base(Value::known(sig_secret.inner())), - ]; - - // Create the public inputs - let nullifier = Nullifier::from(poseidon_hash::<2>([secret.inner(), serial])); - - let value_commit = pedersen_commitment_u64(value, value_blind); - let value_coords = value_commit.to_affine().coordinates().unwrap(); - - let token_commit = pedersen_commitment_base(token_id, token_blind); - let token_coords = token_commit.to_affine().coordinates().unwrap(); - - let sig_pubkey = PublicKey::from_secret(sig_secret); - let (sig_x, sig_y) = sig_pubkey.xy(); - - let merkle_root = tree.root(0).unwrap(); - - let public_inputs = vec![ - nullifier.inner(), - *value_coords.x(), - *value_coords.y(), - *token_coords.x(), - *token_coords.y(), - merkle_root.inner(), - sig_x, - sig_y, - ]; - - // Create the circuit - let circuit = ZkCircuit::new(prover_witnesses, zkbin.clone()); - - let proving_key = ProvingKey::build(13, &circuit); - let proof = Proof::create(&proving_key, &[circuit], &public_inputs, &mut OsRng)?; - - // ======== - // Verifier - // ======== - - // Construct empty witnesses - let verifier_witnesses = empty_witnesses(&zkbin); - - // Create the circuit - let circuit = ZkCircuit::new(verifier_witnesses, zkbin); - - let verifying_key = VerifyingKey::build(13, &circuit); - proof.verify(&verifying_key, &public_inputs)?; - /* ANCHOR_END: main */ - - Ok(()) -} diff --git a/tests/data/erc20tokenlisttest.json b/tests/data/erc20tokenlisttest.json deleted file mode 100644 index 925a7949f..000000000 --- a/tests/data/erc20tokenlisttest.json +++ /dev/null @@ -1,34 +0,0 @@ -{ - "name": "CoinGecko", - "logoURI": "https://www.coingecko.com/assets/thumbnail-007177f3eca19695592f0b8b0eabbdae282b54154e1be912285c9034ea6cbaf2.png", - "keywords": [ - "defi" - ], - "timestamp": "2021-10-25T10:07:01.891+00:00", - "tokens": [ - { - "chainId": 1, - "address": "0xdac17f958d2ee523a2206206994597c13d831ec7", - "name": "Tether", - "symbol": "USDT", - "decimals": 6, - "logoURI": "https://assets.coingecko.com/coins/images/325/thumb/Tether-logo.png?1598003707" - }, - { - "chainId": 1, - "address": "0xeb4c2781e4eba804ce9a9803c67d0893436bb27d", - "name": "renBTC", - "symbol": "RENBTC", - "decimals": 8, - "logoURI": "https://assets.coingecko.com/coins/images/11370/thumb/Bitcoin.jpg?1628072791" - }, - { - "chainId": 1, - "address": "0x2260fac5e5542a773aa44fbcfedf7c193bc2c599", - "name": "Wrapped Bitcoin", - "symbol": "WBTC", - "decimals": 8, - "logoURI": "https://assets.coingecko.com/coins/images/7598/thumb/wrapped_bitcoin_wbtc.png?1548822744" - } - ] -} diff --git a/tests/data/solanatokenlisttest.json b/tests/data/solanatokenlisttest.json deleted file mode 100644 index 1fc1374da..000000000 --- a/tests/data/solanatokenlisttest.json +++ /dev/null @@ -1,88 +0,0 @@ -{ - "tokens": [ - { - "chainId": 101, - "address": "HDLRMKW1FDz2q5Zg778CZx26UgrtnqpUDkNNJHhmVUFr", - "symbol": "MILLI", - "name": "MILLIONSY", - "decimals": 9, - "logoURI": "https://github.com/millionsy/token-list/blob/main/assets/mainnet/HDLRMKW1FDz2q5Zg778CZx26UgrtnqpUDkNNJHhmVUFr/logo.png", - "tags": [ - "Solana tokenized", - "Solana Community token" - ], - "extensions": { - "website": "https://www.millionsy.io/", - "telegram": "https://t.me/MILLIONSYofficialchat", - "twitter": "https://twitter.com/MILLIONSYio" - } - }, - { - "chainId": 101, - "address": "99pifp4v4qQNk3irTHpmAEEzgKfs3ahLE7iFKEqfyxPj", - "symbol": "ZI", - "name": "ZI (The Z Institute Token)", - "decimals": 6, - "logoURI": "https://raw.githubusercontent.com/solana-labs/token-list/main/assets/mainnet/99pifp4v4qQNk3irTHpmAEEzgKfs3ahLE7iFKEqfyxPj/logo.png", - "tags": [ - "utility-token" - ], - "extensions": { - "website": "https://zinstitute.net/", - "twitter": "https://twitter.com/the_z_institute" - } - }, - { - "chainId": 101, - "address": "FYfQ9uaRaYvRiaEGUmct45F9WKam3BYXArTrotnTNFXF", - "symbol": "SOLA", - "name": "Sola Token", - "decimals": 9, - "logoURI": "https://raw.githubusercontent.com/solana-labs/token-list/main/assets/mainnet/FYfQ9uaRaYvRiaEGUmct45F9WKam3BYXArTrotnTNFXF/logo.png", - "tags": [ - "Solana tokenized", - "Solana Community token" - ], - "extensions": { - "website": "https://solatoken.net/", - "telegram": "https://t.me/solatokennet", - "twitter": "https://twitter.com/EcoSolana", - "serumV3Usdc": "4RZ27tjRnSwrtRqsJxDEgsERnDKFs7yx6Ra3HsJvkboy", - "coingeckoId": "sola-token" - } - }, - { - "chainId": 101, - "address": "So11111111111111111111111111111111111111112", - "symbol": "SOL", - "name": "Wrapped SOL", - "decimals": 9, - "logoURI": "https://raw.githubusercontent.com/solana-labs/token-list/main/assets/mainnet/So11111111111111111111111111111111111111112/logo.png", - "tags": [], - "extensions": { - "website": "https://solana.com/", - "serumV3Usdc": "9wFFyRfZBsuAha4YcuxcXLKwMxJR43S7fPfQLusDBzvT", - "serumV3Usdt": "HWHvQhFmJB3NUcu1aihKmrKegfVxBEHzwVX6yZCKEsi1", - "coingeckoId": "solana" - } - }, - { - "chainId": 101, - "address": "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v", - "symbol": "USDC", - "name": "USD Coin", - "decimals": 6, - "logoURI": "https://raw.githubusercontent.com/solana-labs/token-list/main/assets/mainnet/EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v/logo.png", - "tags": [ - "stablecoin" - ], - "extensions": { - "website": "https://www.centre.io/", - "coingeckoId": "usd-coin", - "serumV3Usdt": "77quYg4MGneUdjgXCunt9GgM1usmrxKY31twEy3WHwcS" - } - } - ] -} - - diff --git a/tests/mint_proof.rs b/tests/mint_proof.rs deleted file mode 100644 index 1c0416d76..000000000 --- a/tests/mint_proof.rs +++ /dev/null @@ -1,111 +0,0 @@ -/* This file is part of DarkFi (https://dark.fi) - * - * Copyright (C) 2020-2022 Dyne.org foundation - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program 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 Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -use darkfi_sdk::{ - crypto::{ - pedersen::{pedersen_commitment_base, pedersen_commitment_u64}, - PublicKey, SecretKey, - }, - pasta::{ - arithmetic::CurveAffine, - group::{ff::Field, Curve}, - pallas, - }, -}; -use halo2_gadgets::poseidon::primitives as poseidon; -use halo2_proofs::circuit::Value; -use rand::rngs::OsRng; - -use darkfi::{ - zk::{ - proof::{ProvingKey, VerifyingKey}, - vm::{Witness, ZkCircuit}, - vm_stack::empty_witnesses, - Proof, - }, - zkas::decoder::ZkBinary, - Result, -}; - -#[test] -fn mint_proof() -> Result<()> { - /* ANCHOR: main */ - let bincode = include_bytes!("../proof/mint.zk.bin"); - let zkbin = ZkBinary::decode(bincode)?; - - // ====== - // Prover - // ====== - - // Witness values - let value = 42; - let token_id = pallas::Base::random(&mut OsRng); - let value_blind = pallas::Scalar::random(&mut OsRng); - let token_blind = pallas::Scalar::random(&mut OsRng); - let serial = pallas::Base::random(&mut OsRng); - let coin_blind = pallas::Base::random(&mut OsRng); - let public_key = PublicKey::from_secret(SecretKey::random(&mut OsRng)); - let (pub_x, pub_y) = public_key.xy(); - - let prover_witnesses = vec![ - Witness::Base(Value::known(pub_x)), - Witness::Base(Value::known(pub_y)), - Witness::Base(Value::known(pallas::Base::from(value))), - Witness::Base(Value::known(token_id)), - Witness::Base(Value::known(serial)), - Witness::Base(Value::known(coin_blind)), - Witness::Scalar(Value::known(value_blind)), - Witness::Scalar(Value::known(token_blind)), - ]; - - // Create the public inputs - let msgs = [pub_x, pub_y, pallas::Base::from(value), token_id, serial, coin_blind]; - let coin = poseidon::Hash::<_, poseidon::P128Pow5T3, poseidon::ConstantLength<6>, 3, 2>::init() - .hash(msgs); - - let value_commit = pedersen_commitment_u64(value, value_blind); - let value_coords = value_commit.to_affine().coordinates().unwrap(); - - let token_commit = pedersen_commitment_base(token_id, token_blind); - let token_coords = token_commit.to_affine().coordinates().unwrap(); - - let public_inputs = - vec![coin, *value_coords.x(), *value_coords.y(), *token_coords.x(), *token_coords.y()]; - - // Create the circuit - let circuit = ZkCircuit::new(prover_witnesses, zkbin.clone()); - - let proving_key = ProvingKey::build(13, &circuit); - let proof = Proof::create(&proving_key, &[circuit], &public_inputs, &mut OsRng)?; - - // ======== - // Verifier - // ======== - - // Construct empty witnesses - let verifier_witnesses = empty_witnesses(&zkbin); - - // Create the circuit - let circuit = ZkCircuit::new(verifier_witnesses, zkbin); - - let verifying_key = VerifyingKey::build(13, &circuit); - proof.verify(&verifying_key, &public_inputs)?; - /* ANCHOR_END: main */ - - Ok(()) -}