pub(crate) mod random;
mod sip;
use core::fmt::Pointer;
use core::marker;
use marker::PhantomData;
pub use sip::DaemonicSipHasher13;
pub use sip::SipHasher;
pub trait DaemonicHashable {
fn declare_hashable<GLASS: DaemonicHasher<GLASS> + Glass<GLASS>>(&self, state: &mut GLASS);
fn declare_hashable_slice<GLASS: DaemonicHasher<GLASS> + Glass<GLASS>>(
data: &[Self],
state: &mut GLASS,
) where
Self: Sized,
{
for piece in data {
piece.declare_hashable(state)
}
}
}
pub(crate) mod macros {
pub macro DaemonicHash($item:item) {
}
}
use crate::daemonic::glass::daemonic_system_call::DaemonicSystemCall;
use crate::daemonic::glass::{
Annotation, Glass, GlassStable, Observation, ObservationTier, Severity, Temporal,
};
use crate::daemonic::identity::TransitionCause::Observation as TransitionObservation;
use crate::daemonic::topology::TOPOLOGY_ANCHOR;
use crate::daemonic::{SemanticAnchor, TopologyAnchor, TopologySegment};
use crate::{AXIOM_OFFSET, DaemonicError, const_daemonic_hash};
#[doc(inline)]
pub use macros::DaemonicHash;
pub trait DaemonicHasher<GLASS: Glass<GLASS>> {
#[must_use]
fn finish(&self) -> u64;
fn write(&mut self, bytes: &[u8]);
#[inline]
fn write_u8(&mut self, i: u8) {
self.write(&[i])
}
#[inline]
fn write_u16(&mut self, i: u16) {
self.write(&i.to_ne_bytes())
}
#[inline]
fn write_u32(&mut self, i: u32) {
self.write(&i.to_ne_bytes())
}
#[inline]
fn write_u64(&mut self, i: u64) {
self.write(&i.to_ne_bytes())
}
#[inline]
fn write_u128(&mut self, i: u128) {
self.write(&i.to_ne_bytes())
}
#[inline]
fn write_usize(&mut self, i: usize) {
self.write(&i.to_ne_bytes())
}
#[inline]
fn write_i8(&mut self, i: i8) {
self.write_u8(i as u8)
}
#[inline]
fn write_i16(&mut self, i: i16) {
self.write_u16(i as u16)
}
#[inline]
fn write_i32(&mut self, i: i32) {
self.write_u32(i as u32)
}
#[inline]
fn write_i64(&mut self, i: i64) {
self.write_u64(i as u64)
}
#[inline]
fn write_i128(&mut self, i: i128) {
self.write_u128(i as u128)
}
#[inline]
fn write_isize(&mut self, i: isize) {
self.write_usize(i as usize)
}
#[inline]
fn write_length_prefix(&mut self, len: usize) {
self.write_usize(len);
}
#[inline]
fn write_str(&mut self, s: &str) {
self.write(s.as_bytes());
self.write_u8(0xff);
}
}
impl<GLASS: Glass<GLASS> + Glass<dyn DaemonicHasher<GLASS>> + DaemonicHasher<GLASS>>
DaemonicHasher<GLASS> for &mut GLASS
where
Self: Sized,
{
fn finish(&self) -> u64 {
(**self).finish()
}
fn write(&mut self, bytes: &[u8]) {
(**self).write(bytes)
}
fn write_u8(&mut self, i: u8) {
(**self).write_u8(i)
}
fn write_u16(&mut self, i: u16) {
(**self).write_u16(i)
}
fn write_u32(&mut self, i: u32) {
(**self).write_u32(i)
}
fn write_u64(&mut self, i: u64) {
(**self).write_u64(i)
}
fn write_u128(&mut self, i: u128) {
(**self).write_u128(i)
}
fn write_usize(&mut self, i: usize) {
(**self).write_usize(i)
}
fn write_i8(&mut self, i: i8) {
(**self).write_i8(i)
}
fn write_i16(&mut self, i: i16) {
(**self).write_i16(i)
}
fn write_i32(&mut self, i: i32) {
(**self).write_i32(i)
}
fn write_i64(&mut self, i: i64) {
(**self).write_i64(i)
}
fn write_i128(&mut self, i: i128) {
(**self).write_i128(i)
}
fn write_isize(&mut self, i: isize) {
(**self).write_isize(i)
}
fn write_length_prefix(&mut self, len: usize) {
(**self).write_length_prefix(len)
}
fn write_str(&mut self, s: &str) {
(**self).write_str(s)
}
}
pub trait BuildHasher<GLASS: Glass<GLASS>> {
type Hasher: DaemonicHasher<GLASS>;
fn build_hasher(&self) -> Self::Hasher;
fn hash_one<T: DaemonicHashable>(&self, x: T) -> u64
where
Self: Sized,
Self::Hasher: DaemonicHasher<GLASS>,
<Self as BuildHasher<GLASS>>::Hasher: DaemonicHasher<<Self as BuildHasher<GLASS>>::Hasher>,
<Self as BuildHasher<GLASS>>::Hasher: Glass<<Self as BuildHasher<GLASS>>::Hasher>,
{
let mut hasher = self.build_hasher();
x.declare_hashable(&mut hasher);
<<Self as BuildHasher<GLASS>>::Hasher as DaemonicHasher<GLASS>>::finish(&hasher)
}
}
#[derive(Debug)]
pub struct BuildHasherDefault<GLASS>(PhantomData<fn() -> GLASS>);
impl<GLASS> BuildHasherDefault<GLASS> {
pub const fn new() -> Self {
BuildHasherDefault(PhantomData)
}
}
unsafe impl<GLASS> DaemonicSystemCall for BuildHasherDefault<GLASS> {}
static BUILD_HASHER_DEFAULT_TOPOLOGY_ANCH0R: TopologySegment = TopologySegment {
label: "Daemonic::DaemonicHasher::BuildHasherDefault",
hash: const_daemonic_hash(
"Daemonic::DaemonicHasher::BuildHasherDefault".as_bytes(),
AXIOM_OFFSET,
),
crypto_id: const_daemonic_hash(
"Daemonic::DaemonicHasher::BuildHasherDefault".as_bytes(),
TOPOLOGY_ANCHOR,
),
depth: 2u16,
};
impl<GLASS> Glass<BuildHasherDefault<GLASS>> for BuildHasherDefault<GLASS> {
type Anchor = TopologySegment;
fn position(&self) -> &TopologySegment {
&BUILD_HASHER_DEFAULT_TOPOLOGY_ANCH0R
}
fn severity(&self) -> Severity {
Severity::Unknown
}
fn payload(&self) -> Option<&BuildHasherDefault<GLASS>> {
Some(&self)
}
fn into_payload(self) -> Option<BuildHasherDefault<GLASS>>
where
Self: Sized,
{
Some(self)
}
}
impl<GLASS: Default + DaemonicHasher<GLASS> + Glass<GLASS>> BuildHasher<GLASS>
for BuildHasherDefault<GLASS>
{
type Hasher = GLASS;
fn build_hasher(&self) -> GLASS {
GLASS::default()
}
}
impl<GLASS> Clone for BuildHasherDefault<GLASS> {
fn clone(&self) -> BuildHasherDefault<GLASS> {
BuildHasherDefault(PhantomData)
}
}
impl<GLASS> Default for BuildHasherDefault<GLASS> {
fn default() -> BuildHasherDefault<GLASS> {
Self::new()
}
}
impl<GLASS> PartialEq for BuildHasherDefault<GLASS> {
fn eq(&self, _other: &BuildHasherDefault<GLASS>) -> bool {
true
}
}
impl<GLASS> Eq for BuildHasherDefault<GLASS> {}
mod impls {
use super::*;
use core::hash::Hash;
use core::ptr::Pointee;
macro_rules! impl_write {
($(($ty:ident, $meth:ident),)*) => {$(
impl DaemonicHashable for $ty {
#[inline]
fn declare_hashable<GLASS: DaemonicHasher<GLASS> + Glass<GLASS>>(&self, state: &mut GLASS) {
state.$meth(*self)
}
#[inline]
fn declare_hashable_slice<GLASS: DaemonicHasher<GLASS> + Glass<GLASS>>(data: &[$ty], state: &mut GLASS) {
let newlen = size_of_val(data);
let ptr = data.as_ptr() as *const u8;
#[allow(unsafe_code)]
state.write(unsafe { core::slice::from_raw_parts(ptr, newlen) })
}
}
)*}
}
impl_write! {
(u8, write_u8),
(u16, write_u16),
(u32, write_u32),
(u64, write_u64),
(usize, write_usize),
(i8, write_i8),
(i16, write_i16),
(i32, write_i32),
(i64, write_i64),
(isize, write_isize),
(u128, write_u128),
(i128, write_i128),
}
impl DaemonicHashable for bool {
#[inline]
fn declare_hashable<GLASS: DaemonicHasher<GLASS> + Glass<GLASS>>(&self, state: &mut GLASS) {
state.write_u8(*self as u8)
}
}
impl DaemonicHashable for char {
#[inline]
fn declare_hashable<GLASS: DaemonicHasher<GLASS> + Glass<GLASS>>(&self, state: &mut GLASS) {
state.write_u32(*self as u32)
}
}
impl DaemonicHashable for str {
#[inline]
fn declare_hashable<GLASS: DaemonicHasher<GLASS> + Glass<GLASS>>(&self, state: &mut GLASS) {
state.write_str(self);
}
}
impl DaemonicHashable for ! {
#[inline]
fn declare_hashable<GLASS: DaemonicHasher<GLASS> + Glass<GLASS>>(&self, _: &mut GLASS) {
*self
}
}
macro_rules! impl_hash_tuple {
() => (
impl DaemonicHashable for () {
#[inline]
fn declare_hashable<GLASS: DaemonicHasher<GLASS> + Glass<GLASS>>(&self, _state: &mut GLASS) {}
}
);
( $($name:ident)+) => (
maybe_tuple_doc! {
$($name)+
@#[doc = r" An attribute had to go here else it fucking dies and doesnt explain why"]
impl<$($name: DaemonicHashable),+> DaemonicHashable for ($($name,)+) where last_type!($($name,)+): ?Sized {
#[allow(non_snake_case)]
#[inline]
fn declare_hashable<GLASS: DaemonicHasher<GLASS> + Glass<GLASS>>(&self, state: &mut GLASS) {
let ($(ref $name,)+) = *self;
$($name.declare_hashable(state);)+
}
}
}
);
}
macro_rules! maybe_tuple_doc {
($a:ident @ #[$meta:meta] $item:item) => {
#[doc = "This trait is implemented for tuples up to twelve items long."]
#[$meta]
$item
};
($a:ident $($rest_a:ident)+ @ #[$meta:meta] $item:item) => {
#[doc(hidden)]
#[$meta]
$item
};
}
macro_rules! last_type {
($a:ident,) => { $a };
($a:ident, $($rest_a:ident,)+) => { last_type!($($rest_a,)+) };
}
impl_hash_tuple! {}
impl_hash_tuple! { T }
impl_hash_tuple! { T B }
impl_hash_tuple! { T B C }
impl_hash_tuple! { T B C D }
impl_hash_tuple! { T B C D E }
impl_hash_tuple! { T B C D E F }
impl_hash_tuple! { T B C D E F G }
impl_hash_tuple! { T B C D E F G H }
impl_hash_tuple! { T B C D E F G H I }
impl_hash_tuple! { T B C D E F G H I J }
impl_hash_tuple! { T B C D E F G H I J K }
impl_hash_tuple! { T B C D E F G H I J K L }
impl<T: DaemonicHashable> DaemonicHashable for [T] {
#[inline]
fn declare_hashable<GLASS: DaemonicHasher<GLASS> + Glass<GLASS> + Pointee>(&self, state: &mut GLASS) {
state.write_length_prefix(self.len());
DaemonicHashable::declare_hashable_slice(self, state)
}
}
impl<T: ?Sized + DaemonicHashable> DaemonicHashable for &T {
#[inline]
fn declare_hashable<GLASS: DaemonicHasher<GLASS> + Glass<GLASS> + Pointee>(&self, state: &mut GLASS) {
(**self).declare_hashable(state);
}
}
impl<T: ?Sized + DaemonicHashable> DaemonicHashable for &mut T {
#[inline]
fn declare_hashable<GLASS: DaemonicHasher<GLASS> + Glass<GLASS> + Pointee>(&self, state: &mut GLASS) {
(**self).declare_hashable(state);
}
}
impl<T: ?Sized> DaemonicHashable for *const T
where *const T: Pointee + DaemonicHashable
{
#[inline]
fn declare_hashable<GLASS: DaemonicHasher<GLASS> + Glass<GLASS> + Pointee>(&self, state: &mut GLASS) {
todo!("fix this")
}
}
impl<T: ?Sized> DaemonicHashable for *mut T
where *mut T: Pointee + DaemonicHashable
{
#[inline]
fn declare_hashable<GLASS: DaemonicHasher<GLASS> + Glass<GLASS> + Pointee>(&self, state: &mut GLASS) {
todo!("fix this")
}
}
}