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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
/*
* Tequel-rs: High-Density 384-bit Cryptographic Hash Engine
* Copyright (C) 2026 Gabriel Xavier (dotxav)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*/
#[cfg(target_arch = "x86_64")]
use std::arch::x86_64::*;
use core::arch::x86_64::_mm_prefetch;
use core::arch::x86_64::_MM_HINT_T0;
use crate::avx2_inline::{ add, loadu, or, rota_lf, rota_rg, setone_i32, setzero, xor, horiz_add_avx2 };
use std::hint::black_box;
macro_rules! teq_direct {
($ss:ident, $ss1:expr, $lv:expr, $lr:expr, $ymm_a1:ident) => {
$ss = add($ss, $ymm_a1);
$ss = or(rota_lf::<$lv>($ss), rota_rg::<$lr>($ss));
$ss = xor($ss1, $ss);
};
}
use zeroize::{Zeroize, ZeroizeOnDrop};
/// ```TequelHash``` provides hash functions, custom iterations and salt. <br><br>
#[derive(Debug, Zeroize, ZeroizeOnDrop, Clone, PartialEq, Eq)]
pub struct TequelHash {
pub states: [u32; 12],
pub salt: String,
pub iterations: u32
}
impl TequelHash {
pub fn new() -> Self {
Self {
states: [
0x1A2B3C4D, 0x5E6F7A8B, 0x9C0D1E2F, 0x31415926,
0x27182818, 0xDEADBEEF, 0xCAFEBABE, 0x80808080,
0xABCDEF01, 0x456789AB, 0xFEDCBA98, 0x01234567
],
salt: "".to_string(),
iterations: 30
}
}
pub fn with_salt(mut self, salt: &str) -> Self {
self.salt = salt.to_string();
self
}
pub fn with_iteration(mut self, value: u32) -> Self{
self.iterations = value;
self
}
/// Generates a unique 384-bit hexadecimal hash from the input data.
///
/// This function is the core of the Tequel engine, utilizing **SIMD/AVX2** /// instructions to process data in 256-bit blocks. It is designed for
/// high-speed performance and maximum bit diffusion.
///
/// # Performance
/// By leveraging hardware acceleration, `tqlhash` achieves significantly lower
/// latency compared to scalar implementations, making it ideal for
/// large-scale data integrity checks and real-time obfuscation.
///
/// # Determinism
/// The algorithm is strictly deterministic. Providing the same input bytes
/// will always yield the exact same hexadecimal string.
///
/// # Arguments
/// * `input` - The raw data bytes (`&[u8]`) to be hashed.
///
/// # Returns
/// A 96-character hexadecimal `String` (12 x 32-bit internal states).
///
/// # Example
/// ```rust
/// use tequel::hash::TequelHash;
///
/// let mut tequel = TequelHash::new();
/// let data = b"secret_data";
///
/// let hash_a = tequel.tqlhash(data);
/// let hash_b = tequel.tqlhash(data);
///
/// assert_eq!(hash_a, hash_b);
/// println!("Hash: {}", hash_a);
/// ```
pub fn tqlhash(&mut self, input: &[u8]) -> String {
self.states = [
0x107912FA, 0x220952EA, 0x3320212A, 0x4324312F,
0x5320212A, 0x9E3779B1, 0x85EBCA6B, 0xAD35744D,
0xCC2912FA, 0xEE0952EA, 0x1120212A, 0x2224312F,
];
const HEX_CHARS: &[u8; 16] = b"0123456789abcdef";
let mut s0 = unsafe { setzero() };
let mut s1 = unsafe { setzero() };
let mut s2 = unsafe { setzero() };
let mut s3 = unsafe { setzero() };
let mut s4 = unsafe { setzero() };
let mut s5 = unsafe { setzero() };
let mut s6 = unsafe { setzero() };
let mut s7 = unsafe { setzero() };
let mut s8 = unsafe { setzero() };
let mut s9 = unsafe { setzero() };
let mut s10 = unsafe { setzero() };
let mut s11 = unsafe { setzero() };
let mut chunks = input.chunks_exact(256);
for chunk in chunks.by_ref() {
unsafe {
_mm_prefetch(chunk.as_ptr().add(256) as *const i8, _MM_HINT_T0);
let bl_a = &chunk[..64];
let bl_b = &chunk[64..128];
let bl_c = &chunk[128..];
let bl_d = &chunk[..256];
let ymm_a1 = loadu(bl_a.as_ptr() as *const __m256i);
let ymm_a2 = xor(loadu(bl_a.as_ptr().add(32) as *const __m256i), setone_i32(0x517CC1B7));
teq_direct!(s0, s1, 7, 25, ymm_a1);
teq_direct!(s1, s2, 31, 28, ymm_a2);
teq_direct!(s2, s3, 25, 7, ymm_a1);
teq_direct!(s3, s4, 23, 9, ymm_a2);
teq_direct!(s4, s5, 13, 19, ymm_a1);
teq_direct!(s5, s6, 29, 3, ymm_a2);
teq_direct!(s6, s7, 19, 13, ymm_a1);
teq_direct!(s7, s8, 17, 15, ymm_a2);
teq_direct!(s8, s9, 11, 21, ymm_a1);
teq_direct!(s9, s10, 5, 27, ymm_a2);
teq_direct!(s10, s11, 3, 29, ymm_a1);
teq_direct!(s11, s0, 2, 30, ymm_a2);
let ymm_b1 = loadu(bl_b.as_ptr() as *const __m256i);
let ymm_b2 = xor(loadu(bl_b.as_ptr().add(32) as *const __m256i), setone_i32(0x517CC1B7));
teq_direct!(s0, s1, 7, 25, ymm_b1);
teq_direct!(s1, s2, 31, 28, ymm_b2);
teq_direct!(s2, s3, 25, 7, ymm_b1);
teq_direct!(s3, s4, 23, 9, ymm_b2);
teq_direct!(s4, s5, 13, 19, ymm_b1);
teq_direct!(s5, s6, 29, 3, ymm_b2);
teq_direct!(s6, s7, 19, 13, ymm_b1);
teq_direct!(s7, s8, 17, 15, ymm_b2);
teq_direct!(s8, s9, 11, 21, ymm_b1);
teq_direct!(s9, s10, 5, 27, ymm_b2);
teq_direct!(s10, s11, 3, 29, ymm_b1);
teq_direct!(s11, s0, 2, 30, ymm_b2);
let ymm_c1 = loadu(bl_c.as_ptr() as *const __m256i);
let ymm_c2 = xor(loadu(bl_c.as_ptr().add(32) as *const __m256i), setone_i32(0x517CC1B7));
teq_direct!(s0, s1, 7, 25, ymm_c1);
teq_direct!(s1, s2, 31, 28, ymm_c2);
teq_direct!(s2, s3, 25, 7, ymm_c1);
teq_direct!(s3, s4, 23, 9, ymm_c2);
teq_direct!(s4, s5, 13, 19, ymm_c1);
teq_direct!(s5, s6, 29, 3, ymm_c2);
teq_direct!(s6, s7, 19, 13, ymm_c1);
teq_direct!(s7, s8, 17, 15, ymm_c2);
teq_direct!(s8, s9, 11, 21, ymm_c1);
teq_direct!(s9, s10, 5, 27, ymm_c2);
teq_direct!(s10, s11, 3, 29, ymm_c1);
teq_direct!(s11, s0, 2, 30, ymm_c2);
let ymm_d1 = loadu(bl_d.as_ptr() as *const __m256i);
let ymm_d2 = xor(loadu(bl_d.as_ptr().add(32) as *const __m256i), setone_i32(0x517CC1B7));
teq_direct!(s0, s1, 7, 25, ymm_d1);
teq_direct!(s1, s2, 31, 28, ymm_d2);
teq_direct!(s2, s3, 25, 7, ymm_d1);
teq_direct!(s3, s4, 23, 9, ymm_d2);
teq_direct!(s4, s5, 13, 19, ymm_d1);
teq_direct!(s5, s6, 29, 3, ymm_d2);
teq_direct!(s6, s7, 19, 13, ymm_d1);
teq_direct!(s7, s8, 17, 15, ymm_d2);
teq_direct!(s8, s9, 11, 21, ymm_d1);
teq_direct!(s9, s10, 5, 27, ymm_d2);
teq_direct!(s10, s11, 3, 29, ymm_d1);
teq_direct!(s11, s0, 2, 30, ymm_d2);
s0 = xor(s0, s11);
}
}
unsafe {
self.states[0] = self.states[0] .wrapping_add(horiz_add_avx2(s0));
self.states[1] = self.states[1] .wrapping_add(horiz_add_avx2(s1));
self.states[2] = self.states[2] .wrapping_add(horiz_add_avx2(s2));
self.states[3] = self.states[3] .wrapping_add(horiz_add_avx2(s3));
self.states[4] = self.states[4] .wrapping_add(horiz_add_avx2(s4));
self.states[5] = self.states[5] .wrapping_add(horiz_add_avx2(s5));
self.states[6] = self.states[6] .wrapping_add(horiz_add_avx2(s6));
self.states[7] = self.states[7] .wrapping_add(horiz_add_avx2(s7));
self.states[8] = self.states[8] .wrapping_add(horiz_add_avx2(s8));
self.states[9] = self.states[9] .wrapping_add(horiz_add_avx2(s9));
self.states[10] = self.states[10].wrapping_add(horiz_add_avx2(s10));
self.states[11] = self.states[11].wrapping_add(horiz_add_avx2(s11));
}
let remainder_128 = chunks.remainder();
let mut chunks_64 = remainder_128.chunks_exact(64);
for chunk in chunks_64.by_ref() {
unsafe {
let ymm_a1 = loadu(chunk.as_ptr() as *const __m256i);
let ymm_a2 = xor(loadu(chunk.as_ptr().add(32) as *const __m256i), setone_i32(0x517CC1B7));
teq_direct!(s0, s1, 7, 25, ymm_a1);
teq_direct!(s1, s2, 31, 28, ymm_a2);
teq_direct!(s2, s3, 25, 7, ymm_a1);
teq_direct!(s3, s4, 23, 9, ymm_a2);
teq_direct!(s4, s5, 13, 19, ymm_a1);
teq_direct!(s5, s6, 29, 3, ymm_a2);
teq_direct!(s6, s7, 19, 13, ymm_a1);
teq_direct!(s7, s8, 17, 15, ymm_a2);
teq_direct!(s8, s9, 11, 21, ymm_a1);
teq_direct!(s9, s10, 5, 27, ymm_a2);
teq_direct!(s10, s11, 3, 29, ymm_a1);
teq_direct!(s11, s0, 2, 30, ymm_a2);
}
}
let final_remainder = chunks_64.remainder();
for (idx, &byte) in final_remainder.iter().enumerate() {
let pos = idx % 12;
self.states[pos] = self.states[pos].wrapping_add((byte as u32) ^ 0x9E3779B1);
}
self.apply_final_mixer_64();
let mut hex_buffer = vec![0u8; 96];
for (i, &s) in self.states.iter().enumerate() {
let bytes = s.to_be_bytes();
for (j, &byte) in bytes.iter().enumerate() {
let offset = (i * 8) + (j * 2);
hex_buffer[offset] = HEX_CHARS[(byte >> 4) as usize];
hex_buffer[offset + 1] = HEX_CHARS[(byte & 0x0f) as usize];
}
}
unsafe { String::from_utf8_unchecked(hex_buffer) }
}
/// Generates a unique 384-bit hexadecimal hash from the input data.
///
/// This function is the core of the Tequel engine, utilizing **SIMD/AVX2** /// instructions to process data in 256-bit blocks. It is designed for
/// high-speed performance and maximum bit diffusion.
///
/// # Performance
/// By leveraging hardware acceleration, `tqlhash_raw` achieves significantly lower
/// latency compared to scalar implementations, making it ideal for
/// large-scale data integrity checks and real-time obfuscation.
///
/// # Determinism
/// The algorithm is strictly deterministic. Providing the same input bytes
/// will always yield the exact same hexadecimal string.
///
/// # Arguments
/// * `input` - The raw data bytes (`&[u8]`).
///
/// # Returns
/// A 32-bit list `[u8; 32]`
///
/// # Example
/// ```rust
/// use tequel::hash::TequelHash;
///
/// let mut tequel = TequelHash::new();
/// let data = b"secret_data";
///
/// let bytes_a = tequel.tqlhash_raw(data);
/// let bytes_b = tequel.tqlhash_raw(data);
///
/// assert_eq!(bytes_a, bytes_b);
/// println!("bytes: {:?}", bytes_a);
/// ```
pub fn tqlhash_raw(&mut self, input: &[u8]) -> [u8; 48] {
self.states = [
0x107912FA, 0x220952EA, 0x3320212A, 0x4324312F,
0x5320212A, 0x9E3779B1, 0x85EBCA6B, 0xAD35744D,
0xCC2912FA, 0xEE0952EA, 0x1120212A, 0x2224312F,
];
let mut s0 = unsafe { setzero() };
let mut s1 = unsafe { setzero() };
let mut s2 = unsafe { setzero() };
let mut s3 = unsafe { setzero() };
let mut s4 = unsafe { setzero() };
let mut s5 = unsafe { setzero() };
let mut s6 = unsafe { setzero() };
let mut s7 = unsafe { setzero() };
let mut s8 = unsafe { setzero() };
let mut s9 = unsafe { setzero() };
let mut s10 = unsafe { setzero() };
let mut s11 = unsafe { setzero() };
let mut chunks = input.chunks_exact(128);
for chunk in chunks.by_ref() {
unsafe {
_mm_prefetch(chunk.as_ptr().add(256) as *const i8, _MM_HINT_T0);
let bl_a = &chunk[..64];
let bl_b = &chunk[64..128];
let bl_c = &chunk[128..];
let bl_d = &chunk[..256];
let ymm_a1 = loadu(bl_a.as_ptr() as *const __m256i);
let ymm_a2 = xor(loadu(bl_a.as_ptr().add(32) as *const __m256i), setone_i32(0x517CC1B7));
teq_direct!(s0, s1, 7, 25, ymm_a1);
teq_direct!(s1, s2, 31, 28, ymm_a2);
teq_direct!(s2, s3, 25, 7, ymm_a1);
teq_direct!(s3, s4, 23, 9, ymm_a2);
teq_direct!(s4, s5, 13, 19, ymm_a1);
teq_direct!(s5, s6, 29, 3, ymm_a2);
teq_direct!(s6, s7, 19, 13, ymm_a1);
teq_direct!(s7, s8, 17, 15, ymm_a2);
teq_direct!(s8, s9, 11, 21, ymm_a1);
teq_direct!(s9, s10, 5, 27, ymm_a2);
teq_direct!(s10, s11, 3, 29, ymm_a1);
teq_direct!(s11, s0, 2, 30, ymm_a2);
let ymm_b1 = loadu(bl_b.as_ptr() as *const __m256i);
let ymm_b2 = xor(loadu(bl_b.as_ptr().add(32) as *const __m256i), setone_i32(0x517CC1B7));
teq_direct!(s0, s1, 7, 25, ymm_b1);
teq_direct!(s1, s2, 31, 28, ymm_b2);
teq_direct!(s2, s3, 25, 7, ymm_b1);
teq_direct!(s3, s4, 23, 9, ymm_b2);
teq_direct!(s4, s5, 13, 19, ymm_b1);
teq_direct!(s5, s6, 29, 3, ymm_b2);
teq_direct!(s6, s7, 19, 13, ymm_b1);
teq_direct!(s7, s8, 17, 15, ymm_b2);
teq_direct!(s8, s9, 11, 21, ymm_b1);
teq_direct!(s9, s10, 5, 27, ymm_b2);
teq_direct!(s10, s11, 3, 29, ymm_b1);
teq_direct!(s11, s0, 2, 30, ymm_b2);
let ymm_c1 = loadu(bl_c.as_ptr() as *const __m256i);
let ymm_c2 = xor(loadu(bl_c.as_ptr().add(32) as *const __m256i), setone_i32(0x517CC1B7));
teq_direct!(s0, s1, 7, 25, ymm_c1);
teq_direct!(s1, s2, 31, 28, ymm_c2);
teq_direct!(s2, s3, 25, 7, ymm_c1);
teq_direct!(s3, s4, 23, 9, ymm_c2);
teq_direct!(s4, s5, 13, 19, ymm_c1);
teq_direct!(s5, s6, 29, 3, ymm_c2);
teq_direct!(s6, s7, 19, 13, ymm_c1);
teq_direct!(s7, s8, 17, 15, ymm_c2);
teq_direct!(s8, s9, 11, 21, ymm_c1);
teq_direct!(s9, s10, 5, 27, ymm_c2);
teq_direct!(s10, s11, 3, 29, ymm_c1);
teq_direct!(s11, s0, 2, 30, ymm_c2);
let ymm_d1 = loadu(bl_d.as_ptr() as *const __m256i);
let ymm_d2 = xor(loadu(bl_d.as_ptr().add(32) as *const __m256i), setone_i32(0x517CC1B7));
teq_direct!(s0, s1, 7, 25, ymm_d1);
teq_direct!(s1, s2, 31, 28, ymm_d2);
teq_direct!(s2, s3, 25, 7, ymm_d1);
teq_direct!(s3, s4, 23, 9, ymm_d2);
teq_direct!(s4, s5, 13, 19, ymm_d1);
teq_direct!(s5, s6, 29, 3, ymm_d2);
teq_direct!(s6, s7, 19, 13, ymm_d1);
teq_direct!(s7, s8, 17, 15, ymm_d2);
teq_direct!(s8, s9, 11, 21, ymm_d1);
teq_direct!(s9, s10, 5, 27, ymm_d2);
teq_direct!(s10, s11, 3, 29, ymm_d1);
teq_direct!(s11, s0, 2, 30, ymm_d2);
s0 = xor(s0, s11);
}
}
unsafe {
self.states[0] = self.states[0] .wrapping_add(horiz_add_avx2(s0));
self.states[1] = self.states[1] .wrapping_add(horiz_add_avx2(s1));
self.states[2] = self.states[2] .wrapping_add(horiz_add_avx2(s2));
self.states[3] = self.states[3] .wrapping_add(horiz_add_avx2(s3));
self.states[4] = self.states[4] .wrapping_add(horiz_add_avx2(s4));
self.states[5] = self.states[5] .wrapping_add(horiz_add_avx2(s5));
self.states[6] = self.states[6] .wrapping_add(horiz_add_avx2(s6));
self.states[7] = self.states[7] .wrapping_add(horiz_add_avx2(s7));
self.states[8] = self.states[8] .wrapping_add(horiz_add_avx2(s8));
self.states[9] = self.states[9] .wrapping_add(horiz_add_avx2(s9));
self.states[10] = self.states[10].wrapping_add(horiz_add_avx2(s10));
self.states[11] = self.states[11].wrapping_add(horiz_add_avx2(s11));
}
let remainder_128 = chunks.remainder();
let mut chunks_64 = remainder_128.chunks_exact(64);
for chunk in chunks_64.by_ref() {
unsafe {
let ymm_a1 = loadu(chunk.as_ptr() as *const __m256i);
let ymm_a2 = xor(loadu(chunk.as_ptr().add(32) as *const __m256i), setone_i32(0x517CC1B7));
teq_direct!(s0, s1, 7, 25, ymm_a1);
teq_direct!(s1, s2, 31, 28, ymm_a2);
teq_direct!(s2, s3, 25, 7, ymm_a1);
teq_direct!(s3, s4, 23, 9, ymm_a2);
teq_direct!(s4, s5, 13, 19, ymm_a1);
teq_direct!(s5, s6, 29, 3, ymm_a2);
teq_direct!(s6, s7, 19, 13, ymm_a1);
teq_direct!(s7, s8, 17, 15, ymm_a2);
teq_direct!(s8, s9, 11, 21, ymm_a1);
teq_direct!(s9, s10, 5, 27, ymm_a2);
teq_direct!(s10, s11, 3, 29, ymm_a1);
teq_direct!(s11, s0, 2, 30, ymm_a2);
}
}
let final_remainder = chunks_64.remainder();
for (idx, &byte) in final_remainder.iter().enumerate() {
let pos = idx % 12;
self.states[pos] = self.states[pos].wrapping_add((byte as u32) ^ 0x9E3779B1);
}
self.apply_final_mixer_64();
let mut bytes = [0u8; 48];
for (i, &val) in self.states.iter().enumerate() {
let b = val.to_be_bytes();
bytes[i*4 .. i*4+4].copy_from_slice(&b);
}
bytes
}
/// Verifies if a given hash matches the original input data.
///
/// This is a convenience function that re-hashes the provided `input`
/// and performs a comparison against the existing `hash` string.
///
/// # Security
/// The verification process leverages the TQL-11 SIMD engine to ensure
/// high-speed integrity checks. It is ideal for verifying file integrity
/// or checking stored credentials.
///
/// # Arguments
/// * `hash` - The pre-computed hexadecimal hash string to be verified.
/// * `input` - The raw bytes (`&[u8]`) of the data to check.
///
/// # Returns
/// Returns `true` if the re-computed hash matches the provided one, `false` otherwise.
///
/// # Example
/// ```rust
/// use tequel::hash::TequelHash;
///
/// let mut tequel = TequelHash::new();
/// let data = b"secret_message";
/// let hash = tequel.tqlhash(data);
///
/// if tequel.isv_tqlhash(&hash, data) {
/// println!("Integrity verified: VALID!");
/// } else {
/// println!("Integrity compromised: NOT VALID!");
/// }
/// ```
pub fn isv_tqlhash(&mut self, hash: &String, input: &[u8]) -> bool {
let new_hash = self.tqlhash(input);
let a = new_hash.as_bytes();
let b = hash.as_bytes();
if a.len() != b.len() {
return false;
}
let mut result = 0u8;
for i in 0..a.len() {
result |= black_box(a[i] ^ b[i]);
}
result == 0
}
pub fn isv_tqlhash_raw(&mut self, hash: &[u8; 48], input: &[u8]) -> bool {
let a_bh = self.tqlhash_raw(input);
let mut result = 0u8;
for i in 0..48 {
result |= black_box(a_bh[i] ^ hash[i]);
}
result == 0
}
/// Derives a high-entropy cryptographic key from a password and a salt.
///
/// This function implements a **Key Derivation Function (KDF)** powered by the TQL-11 engine.
/// It utilizes a "Key Stretching" mechanism to make brute-force and dictionary attacks
/// computationally expensive.
///
/// # Architecture
/// The process is **SIMD-accelerated (AVX2)**, ensuring that the computational cost
/// remains high for attackers (who must replicate the intensive TQL-11 rounds) while
/// staying efficient for legitimate local use. Every iteration triggers a non-linear
/// mutation with a validated 51% avalanche diffusion.
///
/// # Arguments
/// * `password` - The raw bytes of the master password (e.g., from user input).
/// * `salt` - A unique, random value used to prevent Rainbow Table attacks.
/// * `iterations` - The number of hashing rounds. Higher values increase resistance
/// against GPU-accelerated cracking (Recommended: >1000).
///
/// # Returns
/// A 384-bit hexadecimal `String` representing the derived cryptographic key.
///
/// # Example
/// ```rust
/// use tequel::hash::TequelHash;
///
/// fn main() {
/// let mut teq = TequelHash::new();
/// let key = teq.derive_key("master_password_123", 2048);
/// println!("Derived Key: {:?}", key);
/// }
/// ```
pub fn derive_key(&mut self, password: &str, iterations: u32) -> [u8; 32] {
self.iterations = if iterations > 0 { iterations } else { 30 };
let mut derived = format!("{}{}{}", self.salt, password, self.salt);
for i in 0..self.iterations {
let hash_hex = self.tqlhash(derived.as_bytes());
derived = format!("{}{}{}", i, hash_hex, self.salt);
}
let final_hash = self.tqlhash(derived.as_bytes());
let bytes = hex::decode(&final_hash).expect("Error in key closing");
let mut key = [0u8; 32];
key.copy_from_slice(&bytes[0..32]);
key
}
fn apply_final_mixer_64(&mut self) {
for r in 0..64 {
for i in 0..12 {
let prev = if i == 0 { 11 } else { i - 1 };
let next = (i + 1) % 12;
self.states[i] = self.states[i]
.wrapping_add(self.states[prev])
.rotate_left(((r % 31) as u32) + 1);
self.states[next] ^= self.states[i].wrapping_mul(0xAD35744D);
}
}
}
}