Implement MTCS clearing run

This commit is contained in:
hu55a1n1 2024-02-29 05:17:52 -08:00
parent c38bbb9006
commit f961c2fdbc
3 changed files with 161 additions and 10 deletions

108
Cargo.lock generated
View file

@ -832,6 +832,38 @@ dependencies = [
"serde", "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]] [[package]]
name = "darling" name = "darling"
version = "0.20.8" version = "0.20.8"
@ -1821,6 +1853,14 @@ version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60302e4db3a61da70c0cb7991976248362f30319e88850c487b9b95bbf059e00" checksum = "60302e4db3a61da70c0cb7991976248362f30319e88850c487b9b95bbf059e00"
[[package]]
name = "mcmf"
version = "2.0.0"
source = "git+https://github.com/hu55a1n1/flow#cad8f3adb29d3be2177a31db0fa0e5c2a858beb0"
dependencies = [
"cc",
]
[[package]] [[package]]
name = "memchr" name = "memchr"
version = "2.7.1" version = "2.7.1"
@ -1853,6 +1893,20 @@ dependencies = [
"windows-sys 0.48.0", "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]] [[package]]
name = "mtcs-enclave" name = "mtcs-enclave"
version = "0.1.0" version = "0.1.0"
@ -1902,6 +1956,20 @@ dependencies = [
"winapi", "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]] [[package]]
name = "num-bigint" name = "num-bigint"
version = "0.4.4" version = "0.4.4"
@ -1931,6 +1999,15 @@ dependencies = [
"zeroize", "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]] [[package]]
name = "num-conv" name = "num-conv"
version = "0.1.0" version = "0.1.0"
@ -1968,6 +2045,18 @@ dependencies = [
"num-traits", "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]] [[package]]
name = "num-traits" name = "num-traits"
version = "0.2.18" version = "0.2.18"
@ -2145,6 +2234,16 @@ version = "2.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" 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]] [[package]]
name = "petgraph" name = "petgraph"
version = "0.6.4" version = "0.6.4"
@ -2299,7 +2398,7 @@ dependencies = [
"log", "log",
"multimap", "multimap",
"once_cell", "once_cell",
"petgraph", "petgraph 0.6.4",
"prettyplease", "prettyplease",
"prost 0.12.3", "prost 0.12.3",
"prost-types 0.12.3", "prost-types 0.12.3",
@ -2356,7 +2455,7 @@ dependencies = [
[[package]] [[package]]
name = "quartz-cw" name = "quartz-cw"
version = "0.1.0" 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 = [ dependencies = [
"cosmwasm-schema", "cosmwasm-schema",
"cosmwasm-std", "cosmwasm-std",
@ -2376,7 +2475,10 @@ dependencies = [
"clap", "clap",
"color-eyre", "color-eyre",
"cosmwasm-std", "cosmwasm-std",
"cw-tee-mtcs",
"ecies",
"k256 0.13.3", "k256 0.13.3",
"mtcs",
"prost 0.12.3", "prost 0.12.3",
"quartz-cw", "quartz-cw",
"quartz-proto", "quartz-proto",
@ -2428,7 +2530,7 @@ dependencies = [
[[package]] [[package]]
name = "quartz-tee-ra" name = "quartz-tee-ra"
version = "0.1.0" 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 = [ dependencies = [
"cosmwasm-schema", "cosmwasm-schema",
"cosmwasm-std", "cosmwasm-std",

View file

@ -7,6 +7,7 @@ edition = "2021"
clap = { version = "4.1.8", features = ["derive"] } clap = { version = "4.1.8", features = ["derive"] }
color-eyre = "0.6.2" color-eyre = "0.6.2"
cosmwasm-std = "1.4.0" 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"] } k256 = { version = "0.13.2", default-features = false, features = ["ecdsa", "alloc"] }
prost = "0.12" prost = "0.12"
rand = "0.8.5" rand = "0.8.5"
@ -17,6 +18,8 @@ tendermint-light-client = "0.34.0"
tonic = "0.11" tonic = "0.11"
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] } 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-cw = { git = "ssh://git@github.com/informalsystems/bisenzone-cw-mvp.git", branch = "hu55a1n1/11-use-quartz" }
quartz-proto = { path = "../../utils/quartz-proto" } quartz-proto = { path = "../../utils/quartz-proto" }
quartz-relayer = { path = "../../utils/quartz-relayer" } quartz-relayer = { path = "../../utils/quartz-relayer" }

View file

@ -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 tonic::{Request, Response, Result as TonicResult, Status};
use crate::{ use crate::{
@ -11,15 +24,15 @@ use crate::{
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct MtcsService<A> { pub struct MtcsService<A> {
sk: Arc<Mutex<Option<SigningKey>>>, sk: Arc<Mutex<Option<SigningKey>>>,
attestor: A, _attestor: A,
} }
impl<A> MtcsService<A> impl<A> MtcsService<A>
where where
A: Attestor, A: Attestor,
{ {
pub fn new(sk: Arc<Mutex<Option<SigningKey>>>, attestor: A) -> Self { pub fn new(sk: Arc<Mutex<Option<SigningKey>>>, _attestor: A) -> Self {
Self { sk, attestor } Self { sk, _attestor }
} }
} }
@ -30,8 +43,41 @@ where
{ {
async fn run( async fn run(
&self, &self,
_request: Request<RunClearingRequest>, request: Request<RunClearingRequest>,
) -> TonicResult<Response<RunClearingResponse>> { ) -> TonicResult<Response<RunClearingResponse>> {
todo!() let message = request.into_inner().message;
let obligations_enc: BTreeMap<RawHash, RawCipherText> =
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::<SimpleObligation<HexBinary, i64>>(&o).unwrap()
})
.collect();
let mut mtcs = ComplexIdMtcs::wrapping(DefaultMtcs::new(PrimalDual::default()));
let setoffs: Vec<SimpleSetoff<HexBinary, i64>> = mtcs.run(obligations).unwrap();
let setoffs_enc: Vec<HexBinary> = 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 }))
} }
} }