diff --git a/Cargo.lock b/Cargo.lock index 30a0e6b..62ea574 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -832,6 +832,38 @@ dependencies = [ "serde", ] +[[package]] +name = "cw-tee-mtcs" +version = "0.1.0" +source = "git+ssh://git@github.com/informalsystems/bisenzone-cw-mvp.git?branch=hu55a1n1/11-use-quartz#3382463b74411fd873a927e1d26b09c35b339753" +dependencies = [ + "cosmwasm-schema", + "cosmwasm-std", + "cw-storage-plus", + "cw2", + "hex", + "k256 0.13.3", + "quartz-cw", + "schemars", + "serde", + "thiserror", +] + +[[package]] +name = "cw2" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6c120b24fbbf5c3bedebb97f2cc85fbfa1c3287e09223428e7e597b5293c1fa" +dependencies = [ + "cosmwasm-schema", + "cosmwasm-std", + "cw-storage-plus", + "schemars", + "semver", + "serde", + "thiserror", +] + [[package]] name = "darling" version = "0.20.8" @@ -1821,6 +1853,14 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "60302e4db3a61da70c0cb7991976248362f30319e88850c487b9b95bbf059e00" +[[package]] +name = "mcmf" +version = "2.0.0" +source = "git+https://github.com/hu55a1n1/flow#cad8f3adb29d3be2177a31db0fa0e5c2a858beb0" +dependencies = [ + "cc", +] + [[package]] name = "memchr" version = "2.7.1" @@ -1853,6 +1893,20 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "mtcs" +version = "0.1.0" +source = "git+ssh://git@github.com/informalsystems/mtcs.git#b0b90c348f09c10828e321e505220a18ee891c8e" +dependencies = [ + "displaydoc", + "itertools 0.10.5", + "log", + "mcmf", + "num-traits", + "petgraph 0.6.3", + "serde", +] + [[package]] name = "mtcs-enclave" version = "0.1.0" @@ -1902,6 +1956,20 @@ dependencies = [ "winapi", ] +[[package]] +name = "num" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b05180d69e3da0e530ba2a1dae5110317e49e3b7f3d41be227dc5f92e49ee7af" +dependencies = [ + "num-bigint", + "num-complex", + "num-integer", + "num-iter", + "num-rational", + "num-traits", +] + [[package]] name = "num-bigint" version = "0.4.4" @@ -1931,6 +1999,15 @@ dependencies = [ "zeroize", ] +[[package]] +name = "num-complex" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23c6602fda94a57c990fe0df199a035d83576b496aa29f4e634a8ac6004e68a6" +dependencies = [ + "num-traits", +] + [[package]] name = "num-conv" version = "0.1.0" @@ -1968,6 +2045,18 @@ dependencies = [ "num-traits", ] +[[package]] +name = "num-rational" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0" +dependencies = [ + "autocfg", + "num-bigint", + "num-integer", + "num-traits", +] + [[package]] name = "num-traits" version = "0.2.18" @@ -2145,6 +2234,16 @@ version = "2.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" +[[package]] +name = "petgraph" +version = "0.6.3" +source = "git+https://github.com/hu55a1n1/petgraph?branch=min-cost-flow-primal-dual#3a2664a3b9d1e5b71c411a6ed497cf576c1a86ae" +dependencies = [ + "fixedbitset", + "indexmap 1.9.3", + "num", +] + [[package]] name = "petgraph" version = "0.6.4" @@ -2299,7 +2398,7 @@ dependencies = [ "log", "multimap", "once_cell", - "petgraph", + "petgraph 0.6.4", "prettyplease", "prost 0.12.3", "prost-types 0.12.3", @@ -2356,7 +2455,7 @@ dependencies = [ [[package]] name = "quartz-cw" version = "0.1.0" -source = "git+ssh://git@github.com/informalsystems/bisenzone-cw-mvp.git?branch=hu55a1n1/11-use-quartz#8f6ff80cce2160d9f0d1f7ce20c010f17c0fa88e" +source = "git+ssh://git@github.com/informalsystems/bisenzone-cw-mvp.git?branch=hu55a1n1/11-use-quartz#3382463b74411fd873a927e1d26b09c35b339753" dependencies = [ "cosmwasm-schema", "cosmwasm-std", @@ -2376,7 +2475,10 @@ dependencies = [ "clap", "color-eyre", "cosmwasm-std", + "cw-tee-mtcs", + "ecies", "k256 0.13.3", + "mtcs", "prost 0.12.3", "quartz-cw", "quartz-proto", @@ -2428,7 +2530,7 @@ dependencies = [ [[package]] name = "quartz-tee-ra" version = "0.1.0" -source = "git+ssh://git@github.com/informalsystems/bisenzone-cw-mvp.git?branch=hu55a1n1/11-use-quartz#8f6ff80cce2160d9f0d1f7ce20c010f17c0fa88e" +source = "git+ssh://git@github.com/informalsystems/bisenzone-cw-mvp.git?branch=hu55a1n1/11-use-quartz#3382463b74411fd873a927e1d26b09c35b339753" dependencies = [ "cosmwasm-schema", "cosmwasm-std", diff --git a/enclaves/quartz/Cargo.toml b/enclaves/quartz/Cargo.toml index 7ad6a09..4d88333 100644 --- a/enclaves/quartz/Cargo.toml +++ b/enclaves/quartz/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" clap = { version = "4.1.8", features = ["derive"] } color-eyre = "0.6.2" cosmwasm-std = "1.4.0" +ecies = { version = "0.2.3", default-features = false, features = ["pure"] } k256 = { version = "0.13.2", default-features = false, features = ["ecdsa", "alloc"] } prost = "0.12" rand = "0.8.5" @@ -17,6 +18,8 @@ tendermint-light-client = "0.34.0" tonic = "0.11" tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] } +cw-tee-mtcs = { git = "ssh://git@github.com/informalsystems/bisenzone-cw-mvp.git", branch = "hu55a1n1/11-use-quartz" } +mtcs = { git = "ssh://git@github.com/informalsystems/mtcs.git" } quartz-cw = { git = "ssh://git@github.com/informalsystems/bisenzone-cw-mvp.git", branch = "hu55a1n1/11-use-quartz" } quartz-proto = { path = "../../utils/quartz-proto" } quartz-relayer = { path = "../../utils/quartz-relayer" } diff --git a/enclaves/quartz/src/mtcs_server.rs b/enclaves/quartz/src/mtcs_server.rs index 7216b3e..a520085 100644 --- a/enclaves/quartz/src/mtcs_server.rs +++ b/enclaves/quartz/src/mtcs_server.rs @@ -1,6 +1,19 @@ -use std::sync::{Arc, Mutex}; +use std::{ + collections::BTreeMap, + sync::{Arc, Mutex}, +}; -use k256::ecdsa::SigningKey; +use cosmwasm_std::HexBinary; +use cw_tee_mtcs::{ + msg::execute::SubmitSetoffsMsg, + state::{RawCipherText, RawHash}, +}; +use ecies::{decrypt, encrypt}; +use k256::ecdsa::{SigningKey, VerifyingKey}; +use mtcs::{ + algo::mcmf::primal_dual::PrimalDual, impls::complex_id::ComplexIdMtcs, + obligation::SimpleObligation, prelude::DefaultMtcs, setoff::SimpleSetoff, Mtcs, +}; use tonic::{Request, Response, Result as TonicResult, Status}; use crate::{ @@ -11,15 +24,15 @@ use crate::{ #[derive(Clone, Debug)] pub struct MtcsService { sk: Arc>>, - attestor: A, + _attestor: A, } impl MtcsService where A: Attestor, { - pub fn new(sk: Arc>>, attestor: A) -> Self { - Self { sk, attestor } + pub fn new(sk: Arc>>, _attestor: A) -> Self { + Self { sk, _attestor } } } @@ -30,8 +43,41 @@ where { async fn run( &self, - _request: Request, + request: Request, ) -> TonicResult> { - todo!() + let message = request.into_inner().message; + let obligations_enc: BTreeMap = + serde_json::from_str(&message).map_err(|e| Status::invalid_argument(e.to_string()))?; + + let sk = self.sk.lock().unwrap(); + let obligations: Vec<_> = obligations_enc + .into_values() + .map(|ciphertext| { + let o = decrypt(&sk.as_ref().unwrap().to_bytes(), &ciphertext).unwrap(); + serde_json::from_slice::>(&o).unwrap() + }) + .collect(); + + let mut mtcs = ComplexIdMtcs::wrapping(DefaultMtcs::new(PrimalDual::default())); + let setoffs: Vec> = mtcs.run(obligations).unwrap(); + + let setoffs_enc: Vec = setoffs + .into_iter() + .flat_map(|so| { + let debtor_pk = VerifyingKey::from_sec1_bytes(&so.debtor).unwrap(); + let creditor_pk = VerifyingKey::from_sec1_bytes(&so.creditor).unwrap(); + + let so_ser = serde_json::to_string(&so).expect("infallible serializer"); + let so_debtor = encrypt(&debtor_pk.to_sec1_bytes(), so_ser.as_bytes()).unwrap(); + let so_creditor = encrypt(&creditor_pk.to_sec1_bytes(), so_ser.as_bytes()).unwrap(); + + [so_debtor, so_creditor] + }) + .map(Into::into) + .collect(); + + let message = serde_json::to_string(&SubmitSetoffsMsg { setoffs_enc }).unwrap(); + + Ok(Response::new(RunClearingResponse { message })) } }