Stub code for #222

This commit is contained in:
Luke Parker 2023-01-14 02:59:43 -05:00
parent be05e0dd47
commit 6ed5592d22
No known key found for this signature in database
5 changed files with 107 additions and 0 deletions

7
Cargo.lock generated
View file

@ -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"

View file

@ -7,6 +7,7 @@ members = [
"crypto/ff-group-tests",
"crypto/dalek-ff-group",
"crypto/ed448",
"crypto/dynamic-generator",
"crypto/ciphersuite",
"crypto/multiexp",

View file

@ -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 <lukeparker5132@gmail.com>"]
keywords = ["ff", "group", "generator"]
edition = "2021"
[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]
[dependencies]
group = "0.12"

View file

@ -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.

View file

@ -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<HashMap<ThreadId, HashMap<TypeId, Vec<Box<dyn Any>>>>>;
static mut GENERATORS: RwLock<Stack> = RwLock::new(None);
fn stack<G: Group>(map: &mut Stack) -> &mut Vec<Box<dyn Any>> {
map
.get_or_insert_with(HashMap::new)
.entry(thread::current().id())
.or_insert(HashMap::new())
.entry(TypeId::of::<G>())
.or_insert(Vec::new())
}
#[doc(hidden)]
pub fn push<G: Group>(generator: G) {
stack::<G>(&mut unsafe { GENERATORS.write() }.unwrap()).push(Box::new(generator));
}
#[doc(hidden)]
pub fn pop<G: Group>() {
stack::<G>(&mut unsafe { GENERATORS.write() }.unwrap()).pop().unwrap();
}
#[doc(hidden)]
fn get<G: Group>() {
stack::<G>(&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<Scalar = Self::F> + 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<G: Group> { _phantom: PhantomData<G> }
impl DynamicGenerator {}
*/