Skip to main content

midnight_zswap/
lib.rs

1// This file is part of midnight-ledger.
2// Copyright (C) Midnight Foundation
3// SPDX-License-Identifier: Apache-2.0
4// Licensed under the Apache License, Version 2.0 (the "License");
5// You may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7// http://www.apache.org/licenses/LICENSE-2.0
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14#![deny(unreachable_pub)]
15#![deny(warnings)]
16#![allow(unused_imports)]
17
18#[macro_use]
19extern crate tracing;
20#[macro_use]
21extern crate lazy_static;
22
23pub const ZSWAP_TREE_HEIGHT: u8 = 32;
24
25pub(crate) fn ciphertext_to_field(c: &CoinCiphertext) -> transient_crypto::curve::Fr {
26    use transient_crypto::hash::{transient_commit, transient_hash};
27    transient_commit(
28        &c.ciph[..],
29        transient_hash(&[
30            transient_crypto::curve::Fr::from_le_bytes(b"midnight:zswap-ciphertext")
31                .expect("Domain sep should be in range for field"),
32            c.c.x().unwrap_or(0.into()),
33            c.c.y().unwrap_or(0.into()),
34        ]),
35    )
36}
37
38pub(crate) mod compact_slots;
39mod construct;
40pub mod error;
41pub mod keys;
42pub mod ledger;
43pub mod local;
44pub mod prove;
45mod structure;
46pub mod verify;
47
48use midnight_onchain_runtime::{ops::Op, result_mode::ResultMode};
49use storage::db::DB;
50
51pub(crate) fn filter_invalid<M: ResultMode<D>, I: Iterator<Item = Op<M, D>>, D: DB>(
52    iter: I,
53) -> impl Iterator<Item = Op<M, D>> {
54    iter.filter(|op| match op {
55        Op::Idx { path, .. } => !path.is_empty(),
56        Op::Ins { n, .. } => *n != 0,
57        _ => true,
58    })
59}
60
61pub use structure::*;