From 6ed5592d22e31054b2bc8dfa92d827251ff8237f Mon Sep 17 00:00:00 2001 From: Luke Parker Date: Sat, 14 Jan 2023 02:59:43 -0500 Subject: [PATCH] Stub code for #222 --- Cargo.lock | 7 ++++ Cargo.toml | 1 + crypto/dynamic-generator/Cargo.toml | 16 ++++++++ crypto/dynamic-generator/LICENSE | 21 ++++++++++ crypto/dynamic-generator/src/lib.rs | 62 +++++++++++++++++++++++++++++ 5 files changed, 107 insertions(+) create mode 100644 crypto/dynamic-generator/Cargo.toml create mode 100644 crypto/dynamic-generator/LICENSE create mode 100644 crypto/dynamic-generator/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 16bdcacb..683212f2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1707,6 +1707,13 @@ version = "1.0.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c9b0705efd4599c15a38151f4721f7bc388306f61084d3bfd50bd07fbca5cb60" +[[package]] +name = "dynamic-generator" +version = "0.1.0" +dependencies = [ + "group", +] + [[package]] name = "ecdsa" version = "0.14.8" diff --git a/Cargo.toml b/Cargo.toml index 7cdec0c1..75616bd2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ members = [ "crypto/ff-group-tests", "crypto/dalek-ff-group", "crypto/ed448", + "crypto/dynamic-generator", "crypto/ciphersuite", "crypto/multiexp", diff --git a/crypto/dynamic-generator/Cargo.toml b/crypto/dynamic-generator/Cargo.toml new file mode 100644 index 00000000..27884ba0 --- /dev/null +++ b/crypto/dynamic-generator/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "dynamic-generator" +version = "0.1.0" +description = "Provide dynamic generators for curves implementing the Group API" +license = "MIT" +repository = "https://github.com/serai-dex/serai/tree/develop/crypto/custom-generator" +authors = ["Luke Parker "] +keywords = ["ff", "group", "generator"] +edition = "2021" + +[package.metadata.docs.rs] +all-features = true +rustdoc-args = ["--cfg", "docsrs"] + +[dependencies] +group = "0.12" diff --git a/crypto/dynamic-generator/LICENSE b/crypto/dynamic-generator/LICENSE new file mode 100644 index 00000000..e6bff13c --- /dev/null +++ b/crypto/dynamic-generator/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 Luke Parker + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/crypto/dynamic-generator/src/lib.rs b/crypto/dynamic-generator/src/lib.rs new file mode 100644 index 00000000..3e7fad4f --- /dev/null +++ b/crypto/dynamic-generator/src/lib.rs @@ -0,0 +1,62 @@ +#![cfg_attr(docsrs, feature(doc_cfg))] +#![cfg_attr(docsrs, feature(doc_auto_cfg))] + +use core::any::{TypeId, Any}; + +use std::{ + thread::{self, ThreadId}, + sync::RwLock, + collections::HashMap, +}; + +use group::Group; + +type Stack = Option>>>>; +static mut GENERATORS: RwLock = RwLock::new(None); + +fn stack(map: &mut Stack) -> &mut Vec> { + map + .get_or_insert_with(HashMap::new) + .entry(thread::current().id()) + .or_insert(HashMap::new()) + .entry(TypeId::of::()) + .or_insert(Vec::new()) +} + +#[doc(hidden)] +pub fn push(generator: G) { + stack::(&mut unsafe { GENERATORS.write() }.unwrap()).push(Box::new(generator)); +} + +#[doc(hidden)] +pub fn pop() { + stack::(&mut unsafe { GENERATORS.write() }.unwrap()).pop().unwrap(); +} + +#[doc(hidden)] +fn get() { + stack::(&mut unsafe { GENERATORS.write() }.unwrap()).get(0).unwrap_or(G::generator()); +} + +macro_rules! dynamic_generator { + ($Name: ident, $Base: ident, $generator: expr) => { + #[derive(Clone, Copy, PartialEq, Eq, Debug)] + struct $Name($Base); + } +} + +// type G: Group + GroupOps + PrimeGroup + Zeroize + ConstantTimeEq; +macro_rules! complete_dynamic_generator { + ($Name: ident, $Base: ident, $generator: expr) => { + #[derive(Clone, Copy, PartialEq, Eq, Debug, ConstantTimeEq, Zeroize)] + struct $Name($Base); + impl Group for DynamicGenerator {} + impl GroupEncoding for DynamicGenerator {} + impl PrimeGroup for DynamicGenerator {} + } +} + +/* +struct DynamicGenerator { _phantom: PhantomData } +impl DynamicGenerator {} +*/