diff --git a/Cargo.toml b/Cargo.toml index ddbe2a01..cc1f3f3f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,6 +3,7 @@ members = [ "crypto/transcript", "crypto/dalek-ff-group", + "crypto/tables", "crypto/multiexp", "crypto/dleq", diff --git a/crypto/tables/Cargo.toml b/crypto/tables/Cargo.toml new file mode 100644 index 00000000..f5de44ff --- /dev/null +++ b/crypto/tables/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "tables" +version = "0.1.0" +description = "Efficient multiplication tables for ff/group" +license = "MIT" +repository = "https://github.com/serai-dex/serai" +authors = ["Luke Parker "] +keywords = ["ff", "group"] +edition = "2021" + +[dependencies] +ff = "0.12" +group = "0.12" + +[dev-dependencies] +k256 = { version = "0.11", features = ["bits"] } diff --git a/crypto/tables/LICENSE b/crypto/tables/LICENSE new file mode 100644 index 00000000..f05b748b --- /dev/null +++ b/crypto/tables/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022 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/tables/src/lib.rs b/crypto/tables/src/lib.rs new file mode 100644 index 00000000..ed214ad2 --- /dev/null +++ b/crypto/tables/src/lib.rs @@ -0,0 +1,59 @@ +use std::{any::TypeId, mem, sync::Once, cell::Cell, boxed::Box, collections::HashMap}; + +use group::Group; + +struct Tables(Cell>>, Once); +static mut TABLES: Tables = Tables(Cell::new(mem::MaybeUninit::uninit()), Once::new()); + +pub struct Table(Vec); + +fn acquire() -> &'static mut HashMap { + unsafe { + TABLES.1.call_once(|| { + TABLES.0.set(mem::MaybeUninit::new(HashMap::new())); + }); + &mut (*(*TABLES.0.as_ptr()).as_mut_ptr()) + } +} + +/// This should ONLY be called via the generate_table macro. It is solely public to make said +/// macro work +#[doc(hidden)] +pub fn __unsafe_add_table(table: Table) { + let tables = acquire(); + if tables.contains_key(&TypeId::of::()) { + return; + } + + let ptr = std::ptr::addr_of!(*Box::leak(Box::new(table))); + unsafe { + tables.insert(TypeId::of::(), mem::transmute::<*const Table, *const ()>(ptr)); + } +} + +macro_rules! generate_table { + ($G: ident) => { + __unsafe_add_table(Table(vec![$G::generator()])); + }; +} + +/// Returns a table usable for fast multiplication. This will panic if the table was not registered +/// via the generate_table macro +pub fn generator_table() -> &'static Table { + generate_table!(G); + unsafe { + acquire() + .get(&TypeId::of::()) + .map(|arc| &*mem::transmute::<*const (), *const Table>(*arc)) + .unwrap() + } +} + +#[test] +fn test_static() { + use k256::ProjectivePoint; + let table: &'static Table = generator_table::(); + dbg!("Read"); + dbg!(table.0.len()); + dbg!(&table.0); +}