1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
use std::{
    array::TryFromSliceError,
    convert::{TryFrom, TryInto},
    ops::Deref,
};

use necsim_core::{
    landscape::{IndexedLocation, Location},
    lineage::GlobalLineageReference,
};
use necsim_core_bond::PositiveF64;

#[allow(clippy::module_name_repetitions)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct SpeciesIdentity([u8; 24]);

impl Deref for SpeciesIdentity {
    type Target = [u8; 24];

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl TryFrom<&[u8]> for SpeciesIdentity {
    type Error = TryFromSliceError;

    fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
        Ok(Self(value.try_into()?))
    }
}

impl From<[u8; 24]> for SpeciesIdentity {
    fn from(value: [u8; 24]) -> Self {
        Self(value)
    }
}

impl SpeciesIdentity {
    pub fn from_speciation(origin: &IndexedLocation, time: PositiveF64) -> SpeciesIdentity {
        let location = (u64::from(origin.location().y()) << 32) | u64::from(origin.location().x());
        let index = (u64::from(origin.index()) + 1) << 16;
        let time = time.get().to_bits();

        Self::from_raw(location, index, time)
    }

    pub fn from_unspeciated(
        lineage: GlobalLineageReference,
        anchor: GlobalLineageReference,
    ) -> SpeciesIdentity {
        let lineage = unsafe { lineage.into_inner() };
        let marker = 0x0;
        let anchor = unsafe { anchor.into_inner() };

        Self::from_raw(lineage, marker, anchor)
    }

    #[allow(dead_code)]
    pub fn try_into_speciation(self) -> Result<(IndexedLocation, PositiveF64), Self> {
        let (location, index, time) = self.copy_into_raw();

        let index = index.wrapping_sub(1 << 16);

        if index & 0xFFFF_0000_0000_FFFF_u64 != 0x0 {
            return Err(self);
        }

        #[allow(clippy::cast_possible_truncation)]
        let x = (location & u64::from(u32::MAX)) as u32;
        let y = ((location >> 32) & u64::from(u32::MAX)) as u32;
        #[allow(clippy::cast_possible_truncation)]
        let i = ((index >> 16) & u64::from(u32::MAX)) as u32;

        let origin = IndexedLocation::new(Location::new(x, y), i);

        let Ok(time) = PositiveF64::new(f64::from_bits(time)) else {
            return Err(self);
        };

        Ok((origin, time))
    }

    pub fn try_into_unspeciated(
        self,
    ) -> Result<(GlobalLineageReference, GlobalLineageReference), Self> {
        let (lineage, marker, anchor) = self.copy_into_raw();

        if marker != 0x0 {
            return Err(self);
        }

        let lineage = unsafe { GlobalLineageReference::from_inner(lineage) };
        let anchor = unsafe { GlobalLineageReference::from_inner(anchor) };

        Ok((lineage, anchor))
    }

    const fn from_raw(a: u64, b: u64, c: u64) -> Self {
        let a_bytes = seahash_diffuse(a).to_le_bytes();
        let b_bytes = seahash_diffuse(b).to_le_bytes();
        let c_bytes = seahash_diffuse(c).to_le_bytes();

        // Shuffle and mix all 24 bytes of the species identity
        let lower = seahash_diffuse(u64::from_le_bytes([
            a_bytes[3], c_bytes[0], b_bytes[5], a_bytes[1], c_bytes[4], c_bytes[7], c_bytes[5],
            a_bytes[5],
        ]))
        .to_le_bytes();
        let middle = seahash_diffuse(u64::from_le_bytes([
            c_bytes[6], b_bytes[4], a_bytes[0], a_bytes[6], b_bytes[2], b_bytes[1], a_bytes[7],
            b_bytes[3],
        ]))
        .to_le_bytes();
        let upper = seahash_diffuse(u64::from_le_bytes([
            a_bytes[4], a_bytes[2], c_bytes[2], b_bytes[0], c_bytes[3], c_bytes[1], b_bytes[7],
            b_bytes[6],
        ]))
        .to_le_bytes();

        Self([
            lower[0], lower[1], lower[2], lower[3], lower[4], lower[5], lower[6], lower[7],
            middle[0], middle[1], middle[2], middle[3], middle[4], middle[5], middle[6], middle[7],
            upper[0], upper[1], upper[2], upper[3], upper[4], upper[5], upper[6], upper[7],
        ])
    }

    const fn copy_into_raw(&self) -> (u64, u64, u64) {
        let lower_bytes = seahash_undiffuse(u64::from_le_bytes([
            self.0[0], self.0[1], self.0[2], self.0[3], self.0[4], self.0[5], self.0[6], self.0[7],
        ]))
        .to_le_bytes();
        let middle_bytes = seahash_undiffuse(u64::from_le_bytes([
            self.0[8], self.0[9], self.0[10], self.0[11], self.0[12], self.0[13], self.0[14],
            self.0[15],
        ]))
        .to_le_bytes();
        let upper_bytes = seahash_undiffuse(u64::from_le_bytes([
            self.0[16], self.0[17], self.0[18], self.0[19], self.0[20], self.0[21], self.0[22],
            self.0[23],
        ]))
        .to_le_bytes();

        let a = seahash_undiffuse(u64::from_le_bytes([
            middle_bytes[2],
            lower_bytes[3],
            upper_bytes[1],
            lower_bytes[0],
            upper_bytes[0],
            lower_bytes[7],
            middle_bytes[3],
            middle_bytes[6],
        ]));
        let b = seahash_undiffuse(u64::from_le_bytes([
            upper_bytes[3],
            middle_bytes[5],
            middle_bytes[4],
            middle_bytes[7],
            middle_bytes[1],
            lower_bytes[2],
            upper_bytes[7],
            upper_bytes[6],
        ]));
        let c = seahash_undiffuse(u64::from_le_bytes([
            lower_bytes[1],
            upper_bytes[5],
            upper_bytes[2],
            upper_bytes[4],
            lower_bytes[4],
            lower_bytes[6],
            middle_bytes[0],
            lower_bytes[5],
        ]));

        (a, b, c)
    }
}

const fn seahash_diffuse(mut x: u64) -> u64 {
    // SeaHash diffusion function
    // https://docs.rs/seahash/4.1.0/src/seahash/helper.rs.html#75-92

    // These are derived from the PCG RNG's round. Thanks to @Veedrac for proposing
    // this. The basic idea is that we use dynamic shifts, which are determined
    // by the input itself. The shift is chosen by the higher bits, which means
    // that changing those flips the lower bits, which scatters upwards because
    // of the multiplication.

    x = x.wrapping_add(0x9e37_79b9_7f4a_7c15);

    x = x.wrapping_mul(0x6eed_0e9d_a4d9_4a4f);

    let a = x >> 32;
    let b = x >> 60;

    x ^= a >> b;

    x = x.wrapping_mul(0x6eed_0e9d_a4d9_4a4f);

    x
}

const fn seahash_undiffuse(mut x: u64) -> u64 {
    // SeaHash undiffusion function
    // https://docs.rs/seahash/4.1.0/src/seahash/helper.rs.html#94-105

    // 0x2f72b4215a3d8caf is the modular multiplicative inverse of the constant used
    // in `diffuse`.

    x = x.wrapping_mul(0x2f72_b421_5a3d_8caf);

    let a = x >> 32;
    let b = x >> 60;

    x ^= a >> b;

    x = x.wrapping_mul(0x2f72_b421_5a3d_8caf);

    x = x.wrapping_sub(0x9e37_79b9_7f4a_7c15);

    x
}

#[cfg(test)]
mod tests {
    use rand::{rngs::StdRng, RngCore, SeedableRng};

    use necsim_core::{
        landscape::{IndexedLocation, Location},
        lineage::GlobalLineageReference,
    };
    use necsim_core_bond::PositiveF64;

    use super::SpeciesIdentity;

    #[test]
    fn test_species_identity_from_speciation() {
        let mut rng = StdRng::from_entropy();

        for _ in 0..1_000_000 {
            let x = rng.next_u32();
            let y = rng.next_u32();
            let i = rng.next_u32();
            let origin = IndexedLocation::new(Location::new(x, y), i);

            let time = loop {
                let t = f64::from_bits(rng.next_u64());

                if t.is_finite() && t > 0.0_f64 {
                    break PositiveF64::new(t).unwrap();
                }
            };

            let identity = SpeciesIdentity::from_speciation(&origin, time);

            assert_eq!(
                identity.clone().try_into_unspeciated(),
                Err(identity.clone())
            );
            assert_eq!(identity.try_into_speciation(), Ok((origin, time)));
        }
    }

    #[test]
    fn test_species_identity_from_unspeciated() {
        let mut rng = StdRng::from_entropy();

        for _ in 0..1_000_000 {
            let lineage = unsafe { GlobalLineageReference::from_inner(rng.next_u64()) };
            let anchor = unsafe { GlobalLineageReference::from_inner(rng.next_u64()) };

            let identity = SpeciesIdentity::from_unspeciated(lineage.clone(), anchor.clone());

            assert_eq!(
                identity.clone().try_into_speciation(),
                Err(identity.clone())
            );
            assert_eq!(identity.try_into_unspeciated(), Ok((lineage, anchor)));
        }
    }
}