1 /// Implementation of Gimli digester.
2 module tern.digest.gimli;
3 
4 import tern.digest;
5 import tern.algorithm;
6 import tern.serialization;
7 
8 /**
9  * Implementation of Gimli digester.
10  *
11  * Gimli is a cryptographic permutation designed for speed and security. It operates
12  * by applying a series of operations to the input data, including bitwise XOR, addition,
13  * and rotation.
14  *
15  * Example:
16  * ```d
17  * import tern.digest.gimli;
18  * 
19  * ubyte[] data = [1, 2, 3, 4, 5];
20  * auto hashValue = Gimli.hash(data);
21  * ```
22  */
23 public static @digester class Gimli
24 {
25 public:
26 static:
27 pure:
28     /**
29      * Computes the Gimli hash digest of the given data.
30      *
31      * Params:
32      *  data = The input byte array for which the Gimli hash is to be computed.
33      *
34      * Returns:
35      *  A byte array representing the computed Gimli hash digest.
36      */
37     ubyte[] hash(ubyte[] data) 
38     {
39         sachp(data, 128);
40         
41         foreach (ref block; data.portionTo!(uint[4]))
42         {
43             foreach (r; 0..24)
44             {
45                 for (uint col = 0; col < 4; ++col)
46                     block[col] ^= block[col] >>> 24;
47 
48                 block[0] += block[1];
49                 block[1] = block[1] << 9 | block[1] >>> 23;
50                 block[2] += block[3];
51                 block[3] = block[3] << 9 | block[3] >>> 23;
52                 block[1] ^= block[0];
53                 block[3] ^= block[2];
54                 block[0] += block[3];
55                 block[3] = block[3] << 2 | block[3] >>> 30;
56                 block[2] += block[1];
57                 block[1] = block[1] << 2 | block[1] >>> 30;
58             }
59         }
60         return data;
61     }
62 }