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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
macro_rules! shake_api {
    (
        name: $name:ident,
        wc: $wc:ty,
        ds: $ds:literal,
        init: $init:ident, heap: $heap:expr, devid: $devId:expr,
        update: $update:ident,
        finalize: $finalize:ident,
        free: $free:ident,
        copy: $copy:ident $(,)?
    ) => {
        #[doc = concat!("The `", stringify!($name), "` hasher.")]
        #[doc = ""]
        #[doc = "# Example"]
        #[doc = ""]
        #[doc = "```"]
        #[doc = concat!("use wolf_crypto::hash::", stringify!($name), ";")]
        #[doc = ""]
        #[doc = concat!("let mut hasher = ", stringify!($name), "::new().unwrap();")]
        #[doc = ""]
        #[doc = "let input = b\"hello world\";"]
        #[doc = "assert!(hasher.try_update(input.as_slice()).is_ok());"]
        #[doc = ""]
        #[doc = "let finalized = hasher.try_finalize::<64>().unwrap();"]
        #[doc = "assert_ne!(finalized.as_slice(), input.as_slice());"]
        #[doc = "assert_eq!(finalized.len(), 64);"]
        #[doc = "```"]
        #[repr(transparent)]
        pub struct $name {
            inner: $wc
        }

        impl $name {
            #[doc = concat!("Create a new `", stringify!($name), "` instance.")]
            #[doc = ""]
            #[doc = "# Errors"]
            #[doc = ""]
            #[doc = concat!(
                "If the underlying initialization function fails (`", stringify!($init), "`)"
            )]
            #[doc = ""]
            #[doc = "# Example"]
            #[doc = ""]
            #[doc = "```"]
            #[doc = concat!("use wolf_crypto::hash::", stringify!($name), ";")]
            #[doc = ""]
            #[doc = concat!("let mut hasher = ", stringify!($name), "::new().unwrap();")]
            #[doc = ""]
            #[doc = "let input = b\"hello world\";"]
            #[doc = "assert!(hasher.try_update(input.as_slice()).is_ok());"]
            #[doc = ""]
            #[doc = "let finalized = hasher.try_finalize::<32>()"]
            #[doc = "    .unwrap();"]
            #[doc = "assert_eq!(finalized.len(), 32);"]
            #[doc = "assert_ne!(finalized.as_slice(), input.as_slice());"]
            #[doc = "```"]
            pub fn new() -> Result<Self, $crate::error::Unspecified> {
                unsafe {
                    let mut res = $crate::opaque_res::Res::new();
                    let mut inner = ::core::mem::MaybeUninit::<$wc>::uninit();

                    res.ensure_0($init(inner.as_mut_ptr(), $heap, $devId));

                    res.unit_err_with(|| Self { inner: inner.assume_init() })
                }
            }

            #[doc = concat!(
                "Update the underlying `", stringify!($wc), "` instance, without performing any ",
                "safety checks."
            )]
            #[doc = ""]
            #[doc = "# Safety"]
            #[doc = ""]
            #[doc = "The length of data is casted to a 32 bit unsigned integer without checking "]
            #[doc = "for overflows. While it is incredibly unlikely that this overflow will ever"]
            #[doc = "take place, it is not impossible. Thus this function is marked unsafe."]
            #[doc = ""]
            #[doc = "# Arguments"]
            #[doc = ""]
            #[doc = "* `data` - The slice to update the underlying hasher state with."]
            #[doc = ""]
            #[doc = "# Returns"]
            #[doc = ""]
            #[doc = "This function returns the result of the operation."]
            #[doc = ""]
            #[doc = "# Example"]
            #[doc = ""]
            #[doc = "```"]
            #[doc = concat!("use wolf_crypto::hash::", stringify!($name), ";")]
            #[doc = ""]
            #[doc = concat!("let mut hasher = ", stringify!($name), "::new().unwrap();")]
            #[doc = ""]
            #[doc = "let input = b\"hello world\";"]
            #[doc = "// SAFETY: The length of `hello world` is 11, which"]
            #[doc = "// cannot overflow even an 8 bit integer."]
            #[doc = "let res = unsafe {"]
            #[doc = "    hasher.update_unchecked(input.as_slice())"]
            #[doc = "};"]
            #[doc = "assert!(res.is_ok());"]
            #[doc = ""]
            #[doc = "let finalized = hasher.finalize_default().unwrap();"]
            #[doc = "assert_ne!(finalized.as_slice(), input.as_slice());"]
            #[doc = "```"]
            #[inline]
            pub unsafe fn update_unchecked(&mut self, data: &[u8]) -> $crate::opaque_res::Res {
                let mut res = $crate::opaque_res::Res::new();

                res.ensure_0($update(
                    ::core::ptr::addr_of_mut!(self.inner),
                    data.as_ptr(),
                    data.len() as u32
                ));

                res
            }

            #[doc = concat!("Update the underlying `", stringify!($wc), "` instance.")]
            #[doc = ""]
            #[doc = "# Arguments"]
            #[doc = ""]
            #[doc = "* `data` - The slice to update the underlying hasher state with."]
            #[doc = ""]
            #[doc = "# Returns"]
            #[doc = ""]
            #[doc = "This function returns the result of the operation."]
            #[doc = ""]
            #[doc = "# Errors"]
            #[doc = ""]
            #[doc = "- If the length of `data` cannot be safely casted to a `u32`."]
            #[doc = concat!("- If the underlying `", stringify!($update), "` function fails.")]
            #[doc = ""]
            #[doc = "# Example"]
            #[doc = ""]
            #[doc = "```"]
            #[doc = concat!("use wolf_crypto::hash::", stringify!($name), ";")]
            #[doc = ""]
            #[doc = concat!("let mut hasher = ", stringify!($name), "::new().unwrap();")]
            #[doc = ""]
            #[doc = "let input = b\"hello world\";"]
            #[doc = "assert!(hasher.try_update(input.as_slice()).is_ok());"]
            #[doc = ""]
            #[doc = "let finalized = hasher.finalize_default().unwrap();"]
            #[doc = "assert_ne!(finalized.as_slice(), input.as_slice());"]
            #[doc = concat!("assert_eq!(finalized.len(), ", stringify!($ds), ");")]
            #[doc = "```"]
            #[doc = ""]
            #[doc = "**Note**: if the size of the `data` is known at compile time, see "]
            #[doc = "[`update_sized`] for a slight optimization as the safety checks are done at "]
            #[doc = "compilation time."]
            #[doc = ""]
            #[doc = "[`update_sized`]: Self::update_sized"]
            #[inline]
            pub fn try_update(&mut self, data: &[u8]) -> $crate::opaque_res::Res {
                if !$crate::can_cast_u32(data.len()) {
                    return $crate::opaque_res::Res::ERR;
                }

                unsafe { self.update_unchecked(data) }
            }

            #[doc = concat!(
                "Update the underlying `", stringify!($wc), "` instance, with the safety checks ",
                "performed at compilation time."
            )]
            #[doc = ""]
            #[doc = "# Arguments"]
            #[doc = ""]
            #[doc = "* `data` - The slice to update the underlying hasher state with."]
            #[doc = ""]
            #[doc = "# Returns"]
            #[doc = ""]
            #[doc = "This function returns the result of the operation."]
            #[doc = ""]
            #[doc = "# Errors"]
            #[doc = ""]
            #[doc = "- If the length of `data` cannot be safely casted to a `u32`."]
            #[doc = concat!("- If the underlying `", stringify!($update), "` function fails.")]
            #[doc = ""]
            #[doc = "# Example"]
            #[doc = ""]
            #[doc = "```"]
            #[doc = concat!("use wolf_crypto::hash::", stringify!($name), ";")]
            #[doc = ""]
            #[doc = concat!("let mut hasher = ", stringify!($name), "::new().unwrap();")]
            #[doc = ""]
            #[doc = "let input = b\"hello world\";"]
            #[doc = "assert!(hasher.update_sized(input).is_ok());"]
            #[doc = ""]
            #[doc = "let finalized = hasher.finalize_default().unwrap();"]
            #[doc = "assert_ne!(finalized.as_slice(), input.as_slice());"]
            #[doc = concat!("assert_eq!(finalized.len(), ", stringify!($ds), ");")]
            #[doc = "```"]
            #[doc = ""]
            #[doc = "**Note**: if the size of the `data` is not known at compile time, see "]
            #[doc = "[`try_update`] for more flexibility."]
            #[doc = ""]
            #[doc = "[`try_update`]: Self::try_update"]
            #[inline]
            pub fn update_sized<const C: usize>(&mut self, data: &[u8; C]) -> $crate::opaque_res::Res {
                if !$crate::const_can_cast_u32::<{ C }>() {
                    return $crate::opaque_res::Res::ERR;
                }

                unsafe { self.update_unchecked(data) }
            }

            #[doc = concat!(
                "Update the underlying `", stringify!($wc), "`, panicking under any failure."
            )]
            #[doc = ""]
            #[doc = "# Arguments"]
            #[doc = ""]
            #[doc = "* `data` - The slice to update the underlying hasher state with."]
            #[doc = ""]
            #[doc = "# Panics"]
            #[doc = ""]
            #[doc = "- If the length of `data` cannot be safely casted to a `u32`."]
            #[doc = concat!("- If the underlying `", stringify!($update), "` function fails.")]
            #[doc = ""]
            #[doc = "If a `panic` under any failure is not acceptable for your use case, which "]
            #[doc = "generally is true, please consider using [`try_update`]."]
            #[doc = ""]
            #[doc = "# Example"]
            #[doc = ""]
            #[doc = "```"]
            #[doc = concat!("use wolf_crypto::hash::", stringify!($name), ";")]
            #[doc = ""]
            #[doc = concat!("let mut hasher = ", stringify!($name), "::new().unwrap();")]
            #[doc = ""]
            #[doc = "let input = b\"hello world\";"]
            #[doc = "hasher.update(input.as_slice());"]
            #[doc = ""]
            #[doc = "let finalized = hasher.try_finalize::<64>().unwrap();"]
            #[doc = "assert_ne!(finalized.as_slice(), input.as_slice());"]
            #[doc = "assert_eq!(finalized.len(), 64);"]
            #[doc = "```"]
            #[doc = ""]
            #[doc = "[`try_update`]: Self::try_update"]
            #[cfg(feature = "panic-api")]
            #[track_caller]
            pub fn update(&mut self, data: &[u8]) {
                self.try_update(data).unit_err(())
                    .expect(concat!("Failed to update hash in `", stringify!($name), "`"))
            }

            #[doc = concat!(
                "Calls the `", stringify!($finalize), "` function, finalizing the extensible ",
                "hashing of data and resetting the underlying `", stringify!($wc), "` instance's ",
                "state without performing any safety checks on the `output` buffer size."
            )]
            #[doc = ""]
            #[doc = "# Safety"]
            #[doc = ""]
            #[doc = "The length of `output` is casted to a 32 bit unsigned integer without checking"]
            #[doc = "for overflows. While it is incredibly unlikely that this overflow will ever"]
            #[doc = "take place, it is not impossible. Thus this function is marked unsafe."]
            #[doc = ""]
            #[doc = "# Arguments"]
            #[doc = ""]
            #[doc = "* `output` - The buffer to store the variable-length output digest. Its length must be manually verified."]
            #[doc = ""]
            #[doc = "# Errors"]
            #[doc = ""]
            #[doc = "If the underlying finalize function fails, the returned result will contain an error."]
            #[doc = ""]
            #[doc = "# Example"]
            #[doc = ""]
            #[doc = "```"]
            #[doc = concat!("use wolf_crypto::hash::", stringify!($name), ";")]
            #[doc = concat!("let mut hasher = ", stringify!($name), "::new().unwrap();")]
            #[doc = "# let input = b\"hello world\";"]
            #[doc = "# assert!(hasher.update_sized(input).is_ok());"]
            #[doc = ""]
            #[doc = "// Use the hasher ..."]
            #[doc = ""]
            #[doc = "let mut output = [0u8; 64];"]
            #[doc = "unsafe {"]
            #[doc = "    let res = hasher.finalize_unchecked(&mut output);"]
            #[doc = "    assert!(res.is_ok());"]
            #[doc = "}"]
            #[doc = "```"]
            #[doc = ""]
            #[doc = "**Note**: Prefer using [`finalize_into`] or [`finalize_into_sized`] where possible to "]
            #[doc = "benefit from safety checks."]
            #[doc = ""]
            #[doc = "[`finalize_into`]: Self::finalize_into"]
            #[doc = "[`finalize_into_sized`]: Self::finalize_into_sized"]
            pub unsafe fn finalize_unchecked(&mut self, output: &mut [u8]) -> $crate::opaque_res::Res {
                let mut res = $crate::opaque_res::Res::new();
                let len = output.len() as u32;

                res.ensure_0($finalize(
                    ::core::ptr::addr_of_mut!(self.inner),
                    output.as_mut_ptr(),
                    len
                ));

                res
            }

            #[doc = concat!(
                "Calls the `", stringify!($finalize), "` function, finalizing the extensible ",
                "hashing of data and resetting the underlying `", stringify!($wc), "` instance's ",
                "state."
            )]
            #[doc = ""]
            #[doc = "# Arguments"]
            #[doc = ""]
            #[doc = "* `output` - The buffer to store the variable-length output digest."]
            #[doc = ""]
            #[doc = "# Errors"]
            #[doc = ""]
            #[doc = "- If the size of `output` exceeds what can be represented as a `u32`."]
            #[doc = "- If the underlying finalize function fails."]
            #[doc = ""]
            #[doc = "# Example"]
            #[doc = ""]
            #[doc = "```"]
            #[doc = concat!("use wolf_crypto::hash::", stringify!($name), ";")]
            #[doc = concat!("let mut hasher = ", stringify!($name), "::new().unwrap();")]
            #[doc = "# let input = b\"hello world\";"]
            #[doc = "# assert!(hasher.update_sized(input).is_ok());"]
            #[doc = ""]
            #[doc = "// Use the hasher ..."]
            #[doc = ""]
            #[doc = "let mut output = [0u8; 64];"]
            #[doc = "let res = hasher.finalize_into(output.as_mut_slice());"]
            #[doc = "assert!(res.is_ok());"]
            #[doc = "```"]
            #[doc = ""]
            #[doc = "**Note**: If the size of the `output` slice is known at compile time, see "]
            #[doc = "[`finalize_into_sized`] for a slight optimization."]
            #[doc = ""]
            #[doc = "[`finalize_into_sized`]: Self::finalize_into_sized"]
            #[inline]
            pub fn finalize_into(&mut self, output: &mut [u8]) -> $crate::opaque_res::Res {
                if !$crate::can_cast_u32(output.len()) { return $crate::opaque_res::Res::ERR }
                unsafe { self.finalize_unchecked(output) }
            }

            #[doc = concat!(
                "Calls the `", stringify!($finalize), "` function, finalizing the extensible ",
                "hashing of data and resetting the underlying `", stringify!($wc), "` instance's ",
                "state, with the safety checks performed at compilation time."
            )]
            #[doc = ""]
            #[doc = "# Arguments"]
            #[doc = ""]
            #[doc = "* `output` - The buffer to store the variable-length output digest."]
            #[doc = ""]
            #[doc = "# Errors"]
            #[doc = ""]
            #[doc = "- If the size of `output` exceeds what can be represented as a `u32`."]
            #[doc = "- If the underlying finalize function fails."]
            #[doc = ""]
            #[doc = "# Example"]
            #[doc = ""]
            #[doc = "```"]
            #[doc = concat!("use wolf_crypto::hash::", stringify!($name), ";")]
            #[doc = concat!("let mut hasher = ", stringify!($name), "::new().unwrap();")]
            #[doc = "# let input = b\"hello world\";"]
            #[doc = "# assert!(hasher.update_sized(input).is_ok());"]
            #[doc = ""]
            #[doc = "// Use the hasher ..."]
            #[doc = ""]
            #[doc = "let mut output = [0u8; 64];"]
            #[doc = "let res = hasher.finalize_into_sized(&mut output);"]
            #[doc = "assert!(res.is_ok());"]
            #[doc = "```"]
            #[doc = ""]
            #[doc = "**Note**: If the size of the output buffer is not known at compilation time, "]
            #[doc = "see [`finalize_into`] for greater flexibility."]
            #[doc = ""]
            #[doc = "[`finalize_into`]: Self::finalize_into"]
            #[inline]
            pub fn finalize_into_sized<const C: usize>(&mut self, output: &mut [u8; C]) -> $crate::opaque_res::Res {
                if !$crate::const_can_cast_u32::<{ C }>() { return $crate::opaque_res::Res::ERR }
                unsafe { self.finalize_unchecked(output) }
            }

            #[doc = concat!(
                "Calls the `", stringify!($finalize), "` function, finalizing the extensible ",
                "hashing of data and resetting the underlying `", stringify!($wc), "` instance's ",
                "state, returning a buffer of the specified output size."
            )]
            #[doc = ""]
            #[doc = "# Returns"]
            #[doc = ""]
            #[doc = "On success, this returns the output digest of the given size."]
            #[doc = ""]
            #[doc = "# Errors"]
            #[doc = ""]
            #[doc = "If the underlying finalize function fails."]
            #[doc = ""]
            #[doc = "# Example"]
            #[doc = ""]
            #[doc = "```"]
            #[doc = concat!("use wolf_crypto::hash::", stringify!($name), ";")]
            #[doc = concat!("let mut hasher = ", stringify!($name), "::new().unwrap();")]
            #[doc = "# let input = b\"hello world\";"]
            #[doc = "# assert!(hasher.update_sized(input).is_ok());"]
            #[doc = ""]
            #[doc = "// Use the hasher ..."]
            #[doc = ""]
            #[doc = "let res = hasher.try_finalize::<64>().unwrap();"]
            #[doc = "assert_ne!(res.as_slice(), input.as_slice());"]
            #[doc = "```"]
            #[inline]
            pub fn try_finalize<const C: usize>(&mut self) -> Result<[u8; C], $crate::error::Unspecified> {
                let mut buf = [0u8; C];
                self.finalize_into_sized(&mut buf).unit_err(buf)
            }

            #[doc = concat!(
                "Calls the `", stringify!($finalize), "` function, finalizing the extensible ",
                "hashing of data and resetting the underlying `", stringify!($wc), "` instance's ",
                "state with the default digest size of `", stringify!($ds), "` bytes."
            )]
            #[doc = ""]
            #[doc = "# Returns"]
            #[doc = ""]
            #[doc = "On success, this returns the default output digest."]
            #[doc = ""]
            #[doc = "# Errors"]
            #[doc = ""]
            #[doc = "If the underlying finalize function fails."]
            #[doc = ""]
            #[doc = "# Example"]
            #[doc = ""]
            #[doc = "```"]
            #[doc = concat!("use wolf_crypto::hash::", stringify!($name), ";")]
            #[doc = concat!("let mut hasher = ", stringify!($name), "::new().unwrap();")]
            #[doc = "# let input = b\"hello world\";"]
            #[doc = "# assert!(hasher.update_sized(input).is_ok());"]
            #[doc = ""]
            #[doc = "// Use the hasher ..."]
            #[doc = ""]
            #[doc = "let res = hasher.finalize_default().unwrap();"]
            #[doc = "assert_ne!(res.as_slice(), input.as_slice());"]
            #[doc = "```"]
            #[inline]
            pub fn finalize_default(&mut self) -> Result<[u8; $ds], $crate::error::Unspecified> {
                self.try_finalize::<{ $ds }>()
            }

            #[doc = concat!(
                "Calls the `", stringify!($finalize), "` function, finalizing the extensible ",
                "hashing of data and resetting the underlying `", stringify!($wc),
                "` instance's state."
            )]
            #[doc = ""]
            #[doc = "# Panics"]
            #[doc = ""]
            #[doc = "If the underlying finalize function fails. If panicking is not acceptable for your "]
            #[doc = "use case, see [`try_finalize`] or [`finalize_into`] instead."]
            #[doc = ""]
            #[doc = "# Example"]
            #[doc = ""]
            #[doc = "```"]
            #[doc = concat!("use wolf_crypto::hash::", stringify!($name), ";")]
            #[doc = concat!("let mut hasher = ", stringify!($name), "::new().unwrap();")]
            #[doc = "# let input = b\"hello world\";"]
            #[doc = "# assert!(hasher.update_sized(input).is_ok());"]
            #[doc = ""]
            #[doc = "// Use the hasher ..."]
            #[doc = ""]
            #[doc = "let res = hasher.try_finalize::<24>().unwrap();"]
            #[doc = "assert_ne!(res.as_slice(), input.as_slice());"]
            #[doc = "```"]
            #[doc = ""]
            #[doc = "[`try_finalize`]: Self::try_finalize"]
            #[doc = "[`finalize_into`]: Self::finalize_into"]
            #[cfg(feature = "panic-api")]
            #[track_caller]
            pub fn finalize<const C: usize>(&mut self) -> [u8; C] {
                self.try_finalize::<{ C }>().expect(concat!(
                    "Failed to finalize in `", stringify!($name), "`"
                ))
            }
        }

        // SAFETY:
        // All methods which mutate the underlying state require a mutable reference,
        // the only way to obtain a mutable reference across thread boundaries is via
        // synchronization or unsafe in Rust (which then would be the user's responsibility).
        unsafe impl Send for $name {}

        // SAFETY:
        // There is no providing of interior mutability, all methods which mutate the underlying
        // state require a mutable reference, thus making this safe to mark `Sync`.
        unsafe impl Sync for $name {}

        impl Drop for $name {
            #[doc = concat!(
                "Calls the `", stringify!($free), "` function, cleaning up after itself."
            )]
            #[inline]
            fn drop(&mut self) {
                unsafe { $free(::core::ptr::addr_of_mut!(self.inner)) }
            }
        }

        copy_impl! {
            name: $name,
            wc: $wc,
            copy: $copy,
            finalize_func: finalize_default
        }

        #[cfg(test)]
        mod unit_tests {
            use super::*;

            #[test]
            fn test_new() {
                let hasher = $name::new();
                assert!(hasher.is_ok());
            }

            #[test]
            fn test_update_single_finalize() {
                let mut hasher = $name::new().unwrap();
                let input = b"hello world";
                assert!(hasher.try_update(input).is_ok());

                let mut output = [0u8; 64];
                assert!(hasher.finalize_into(&mut output).is_ok());
                assert_eq!(output.len(), 64);
            }

            #[test]
            fn test_multiple_updates_before_finalize() {
                let mut hasher = $name::new().unwrap();
                let inputs = [b"part1", b"part2", b"part3"];
                for input in inputs {
                    assert!(hasher.try_update(input).is_ok());
                }

                let mut output = [0u8; 64];
                assert!(hasher.finalize_into(&mut output).is_ok());
                assert_eq!(output.len(), 64);
            }

            #[test]
            fn test_empty_input() {
                let mut hasher = $name::new().unwrap();
                let input: &[u8] = &[];
                assert!(hasher.try_update(input).is_ok());

                let mut output = [0u8; 64];
                assert!(hasher.finalize_into(&mut output).is_ok());
                assert_eq!(output.len(), 64);
            }

            #[test]
            fn test_large_input() {
                let mut hasher = $name::new().unwrap();
                let input = vec![0u8; 10 * 1024 * 1024]; // 10 MB
                assert!(hasher.try_update(&input).is_ok());

                let mut output = [0u8; 64];
                assert!(hasher.finalize_into(&mut output).is_ok());
                assert_eq!(output.len(), 64);
            }

            #[test]
            fn test_finalize_into_sized_exact() {
                let mut hasher = $name::new().unwrap();
                let input = b"exact sized finalize";
                assert!(hasher.try_update(input).is_ok());

                let mut output = [0u8; $ds];
                assert!(hasher.finalize_into_sized(&mut output).is_ok());
                assert_eq!(output.len(), $ds);
            }

            #[test]
            fn test_finalize_default() {
                let mut hasher = $name::new().unwrap();
                let input = b"default finalize test";
                assert!(hasher.try_update(input).is_ok());

                let output = hasher.finalize_default();
                assert!(output.is_ok());
                assert_eq!(output.unwrap().len(), $ds);
            }

            #[test]
            fn test_try_finalize_variable_size() {
                let mut hasher = $name::new().unwrap();
                let input = b"variable size finalize test";
                assert!(hasher.try_update(input).is_ok());

                let res = hasher.try_finalize::<128>();
                assert!(res.is_ok());
                assert_eq!(res.unwrap().len(), 128);
            }

            #[test]
            fn test_update_sized() {
                let mut hasher = $name::new().unwrap();
                let input: &[u8; 5] = b"hello";
                assert!(hasher.update_sized(input).is_ok());

                let mut output = [0u8; 64];
                assert!(hasher.finalize_into(&mut output).is_ok());
                assert_eq!(output.len(), 64);
            }

            #[test]
            fn test_finalize_after_reset() {
                let mut hasher = $name::new().unwrap();
                let input1 = b"first input";
                let input2 = b"second input";

                assert!(hasher.try_update(input1).is_ok());
                let mut output1 = [0u8; 64];
                assert!(hasher.finalize_into(&mut output1).is_ok());

                assert!(hasher.try_update(input2).is_ok());
                let mut output2 = [0u8; 64];
                assert!(hasher.finalize_into(&mut output2).is_ok());

                assert_ne!(output1, output2);
            }

            #[test]
            fn test_finalize_default_no_input() {
                let mut hasher = $name::new().unwrap();
                let output = hasher.finalize_default();
                assert!(output.is_ok());
                assert_eq!(output.unwrap().len(), $ds);
            }

            #[test]
            fn test_update_10_mb() {
                let mut hasher = $name::new().unwrap();
                let input = vec![0u8; 10_000_000]; // 10 MB
                assert!(hasher.try_update(&input).is_ok());

                let mut output = [0u8; 64];
                assert!(hasher.finalize_into_sized(&mut output).is_ok());
                assert_eq!(output.len(), 64);
            }

            #[test]
            fn test_finalize_into_sized_ds_size() {
                let mut hasher = $name::new().unwrap();
                let input = b"exact ds size finalize test";
                assert!(hasher.try_update(input).is_ok());

                let mut output = [0u8; $ds];
                assert!(hasher.finalize_into_sized(&mut output).is_ok());
                assert_eq!(output.len(), $ds);
            }

            #[test]
            fn test_finalize_into_large_output_buffer() {
                let mut hasher = $name::new().unwrap();
                let input = b"large output buffer finalize test";
                assert!(hasher.try_update(input).is_ok());

                let mut output = vec![0u8; 10_000]; // 10 KB
                assert!(hasher.finalize_into(&mut output).is_ok());
                assert_eq!(output.len(), 10_000);
            }

            #[test]
            fn test_finalize_after_empty_input() {
                let mut hasher = $name::new().unwrap();
                let input: &[u8] = &[];
                assert!(hasher.try_update(input).is_ok());

                let mut output = [0u8; 64];
                assert!(hasher.finalize_into(&mut output).is_ok());

                let mut output2 = [0u8; 64];
                assert!(hasher.finalize_into(&mut output2).is_ok());

                assert_eq!(output, output2);
            }

            #[test]
            fn test_finalize_into_exact_zero_length() {
                let mut hasher = $name::new().unwrap();
                let input = b"zero length output buffer test";
                assert!(hasher.try_update(input).is_ok());

                let mut output: [u8; 0] = [];
                assert!(hasher.finalize_into_sized(&mut output).is_ok());
            }

            #[test]
            fn test_finalize_default_multiple_no_updates() {
                let mut hasher = $name::new().unwrap();

                let output1 = hasher.finalize_default();
                assert!(output1.is_ok());

                let output2 = hasher.finalize_default();
                assert!(output2.is_ok());

                assert_eq!(output1.unwrap().as_slice(), output2.unwrap().as_slice());
            }

            #[test]
            fn test_update_after_finalize() {
                let mut hasher = $name::new().unwrap();
                let input1 = b"input before finalize";
                let input2 = b"input after finalize";

                assert!(hasher.try_update(input1).is_ok());
                let mut output1 = [0u8; 64];
                assert!(hasher.finalize_into(&mut output1).is_ok());

                assert!(hasher.try_update(input2).is_ok());
                let mut output2 = [0u8; 64];
                assert!(hasher.finalize_into(&mut output2).is_ok());

                assert_ne!(output1, output2);
            }

            #[test]
            fn test_finalize_with_multiple_threads() {
                use std::sync::{Arc, Mutex};
                use std::thread;

                let hasher = Arc::new(Mutex::new($name::new().unwrap()));
                let input = b"multithreaded finalize test";

                let handles: Vec<_> = (0..10).map(|_| {
                    let hasher_clone = Arc::clone(&hasher);
                    let input_clone = input.clone();
                    thread::spawn(move || {
                        let mut hasher = hasher_clone.lock().unwrap();
                        assert!(hasher.try_update(&input_clone).is_ok());
                        let mut output = [0u8; 64];
                        assert!(hasher.finalize_into(&mut output).is_ok());
                        output
                    })
                }).collect();

                for handle in handles {
                    let output = handle.join().expect("Thread panicked");
                    assert_eq!(output.len(), 64);
                }
            }

            #[test]
            fn test_finalize_with_various_output_sizes() {
                let mut hasher = $name::new().unwrap();
                let input = b"various output sizes test";
                assert!(hasher.try_update(input).is_ok());

                let sizes = [16, 32, 64, 128, 256];
                for &size in &sizes {
                    let mut output = vec![0u8; size];
                    assert!(hasher.finalize_into(&mut output).is_ok());
                    assert_eq!(output.len(), size);
                }
            }

            #[test]
            fn test_finalize_into_exact_max_size() {
                let mut hasher = $name::new().unwrap();
                let input = b"max size finalize test";
                assert!(hasher.try_update(input).is_ok());

                const MAX_SIZE: usize = 10_000;
                let mut output = [0u8; MAX_SIZE];
                assert!(hasher.finalize_into_sized(&mut output).is_ok());
                assert_eq!(output.len(), MAX_SIZE);
            }

            #[test]
            fn test_finalize_into_exact_after_multiple_updates() {
                let mut hasher = $name::new().unwrap();
                let inputs = [b"update1", b"update2", b"update3"];
                for input in inputs {
                    assert!(hasher.try_update(input).is_ok());
                }

                let mut output = [0u8; $ds];
                assert!(hasher.finalize_into_sized(&mut output).is_ok());
                assert_eq!(output.len(), $ds);
            }

            #[test]
            fn test_finalize_default_reset_behavior() {
                let mut hasher = $name::new().unwrap();
                let input = b"multiple finalize calls test";
                assert!(hasher.try_update(input).is_ok());

                let output1 = hasher.finalize_default();
                assert!(output1.is_ok());

                let output2 = hasher.finalize_default();
                assert!(output2.is_ok());

                assert_ne!(output1.unwrap().as_slice(), output2.unwrap().as_slice());
            }

            #[test]
            fn test_finalize_into_overlapping_mut_slices() {
                let mut hasher = $name::new().unwrap();
                let input = b"overlapping mutable slices test";
                assert!(hasher.try_update(input).is_ok());

                let mut buffer = [0u8; 128];
                let (left, right) = buffer.split_at_mut(64);
                assert!(hasher.finalize_into(left).is_ok());
                assert!(hasher.finalize_into(right).is_ok());
            }

            #[test]
            fn test_finalize_with_different_output_sizes_sequentially() {
                let mut hasher = $name::new().unwrap();
                let input = b"sequential different output sizes test";
                assert!(hasher.try_update(input).is_ok());

                let mut output1 = [0u8; 32];
                assert!(hasher.finalize_into(&mut output1).is_ok());
                assert_eq!(output1.len(), 32);

                let mut output2 = [0u8; 64];
                assert!(hasher.finalize_into(&mut output2).is_ok());
                assert_eq!(output2.len(), 64);
            }

            #[test]
            fn test_finalize_after_multiple_large_inputs() {
                let mut hasher = $name::new().unwrap();
                let inputs = [
                    vec![0u8; 5_000_000],
                    vec![1u8; 10_000_000],
                    vec![2u8; 15_000_000],
                ];

                for input in &inputs {
                    assert!(hasher.try_update(input).is_ok());
                    let mut output = [0u8; 64];
                    assert!(hasher.finalize_into(&mut output).is_ok());
                }
            }

            #[test]
            fn test_finalize_into_exact_multiple_exact_sizes() {
                let mut hasher = $name::new().unwrap();
                let inputs = [b"exact size A", b"exact size B", b"exact size C"];
                for input in inputs {
                    assert!(hasher.try_update(input).is_ok());
                    let mut output = [0u8; $ds];
                    assert!(hasher.finalize_into_sized(&mut output).is_ok());
                }
            }

            #[test]
            fn test_finalize_with_random_output_sizes() {
                let mut hasher = $name::new().unwrap();
                let input = b"random output sizes test";
                assert!(hasher.try_update(input).is_ok());

                let sizes = [15, 64, 100, 255, 1024];
                for &size in &sizes {
                    let mut output = vec![0u8; size];
                    assert!(hasher.finalize_into(&mut output).is_ok());
                    assert_eq!(output.len(), size);
                }
            }

            #[test]
            fn test_finalize_into_exact_varying_exact_sizes() {
                let mut hasher = $name::new().unwrap();
                let input = b"varying exact sizes finalize test";
                assert!(hasher.try_update(input).is_ok());

                let sizes = [32, 64, 128, 256];
                for size in sizes {
                    let mut output = vec![0u8; size];
                    assert!(hasher.finalize_into(output.as_mut_slice()).is_ok());
                    assert_eq!(output.len(), size);
                }
            }

            #[test]
            fn test_finalize_after_finalize_with_empty_input() {
                let mut hasher = $name::new().unwrap();

                let mut output1 = [0u8; 64];
                assert!(hasher.finalize_into(&mut output1).is_ok());

                let mut output2 = [0u8; 64];
                assert!(hasher.finalize_into(&mut output2).is_ok());

                assert_eq!(output1, output2);
            }

            #[test]
            fn test_finalize_into_exact_with_different_sizes() {
                let mut hasher = $name::new().unwrap();
                let inputs = [b"exact size test1", b"exact size test2"];

                for input in inputs {
                    assert!(hasher.try_update(input).is_ok());
                    let mut output = [0u8; $ds];
                    assert!(hasher.finalize_into_sized(&mut output).is_ok());
                }
            }

            #[test]
            fn test_finalize_with_unicode_input() {
                let mut hasher = $name::new().unwrap();
                let input = "こんにちは世界".as_bytes(); // "Hello, World" in Japanese
                assert!(hasher.try_update(input).is_ok());

                let mut output = [0u8; 64];
                assert!(hasher.finalize_into(&mut output).is_ok());
                assert_eq!(output.len(), 64);
            }

            #[test]
            fn test_finalize_with_repeated_updates() {
                let mut hasher = $name::new().unwrap();
                let input = b"repeated updates test";
                for _ in 0..1000 {
                    assert!(hasher.try_update(input).is_ok());
                }

                let mut output = [0u8; 64];
                assert!(hasher.finalize_into(&mut output).is_ok());
                assert_eq!(output.len(), 64);
            }

            #[test]
            fn test_finalize_into_sized_after_multiple_updates() {
                let mut hasher = $name::new().unwrap();
                let inputs = [b"update1", b"update2", b"update3"];
                for input in inputs {
                    assert!(hasher.try_update(input).is_ok());
                }

                let mut output = [0u8; $ds];
                assert!(hasher.finalize_into_sized(&mut output).is_ok());
                assert_eq!(output.len(), $ds);
            }

            #[test]
            fn test_finalize_with_random_updates_and_finalizes() {
                use rand::Rng;

                let mut hasher = $name::new().unwrap();
                let mut rng = rand::thread_rng();

                for _ in 0..100 {
                    let size = rng.gen_range(0..1024);
                    let input: Vec<u8> = (0..size).map(|_| rng.gen()).collect();
                    assert!(hasher.try_update(&input).is_ok());

                    let output_size = rng.gen_range(1..2048);
                    let mut output = vec![0u8; output_size];
                    assert!(hasher.finalize_into(&mut output).is_ok());
                    assert_eq!(output.len(), output_size);
                }
            }

            #[test]
            fn test_finalize_after_large_input_multiple_times() {
                let mut hasher = $name::new().unwrap();
                let input = vec![3u8; 20_000_000]; // 20 MB
                assert!(hasher.try_update(&input).is_ok());

                for _ in 0..10 {
                    let mut output = [0u8; 64];
                    assert!(hasher.finalize_into(&mut output).is_ok());
                }
            }

            #[test]
            fn test_finalize_with_special_characters_input() {
                let mut hasher = $name::new().unwrap();
                let input = b"sp3c!@l ch@r@ct3rs #test";
                assert!(hasher.try_update(input).is_ok());

                let mut output = [0u8; 64];
                assert!(hasher.finalize_into(&mut output).is_ok());
                assert_eq!(output.len(), 64);
            }

            #[test]
            fn test_finalize_after_finalizing_with_large_input() {
                let mut hasher = $name::new().unwrap();
                let input = vec![4u8; 50_000_000]; // 50 MB
                assert!(hasher.try_update(&input).is_ok());

                let mut output = [0u8; 64];
                assert!(hasher.finalize_into(&mut output).is_ok());

                // Finalize again without updates
                let mut output2 = [0u8; 64];
                assert!(hasher.finalize_into(&mut output2).is_ok());

                // Outputs should be different
                assert_ne!(output, output2);
            }

            #[test]
            fn test_finalize_with_multiple_different_inputs() {
                let mut hasher = $name::new().unwrap();
                let inputs = [
                    b"input one".as_slice(),
                    b"input two".as_slice(),
                    b"input three".as_slice(),
                    b"input four".as_slice(),
                    b"input five".as_slice(),
                ];

                for input in &inputs {
                    assert!(hasher.try_update(input).is_ok());
                }

                let mut output = [0u8; 64];
                assert!(hasher.finalize_into(&mut output).is_ok());
                assert_eq!(output.len(), 64);
            }
        }

        #[cfg(test)]
        mod property_tests {
            use super::*;
            use digest::{ExtendableOutput, ExtendableOutputReset, XofReader, Update};
            use sha3::$name as RcShake;
            use proptest::prelude::*;

            fn get_rc_hasher() -> RcShake {
                RcShake::default()
            }

            proptest! {
                #![proptest_config(ProptestConfig::with_cases(1000))]

                #[test]
                fn prop_single_update(
                    input in any::<Vec<u8>>(),
                    output_size in 1..2048usize
                ) {
                    let mut wolf = $name::new().unwrap();
                    let mut rc = get_rc_hasher();

                    // Update both hashers
                    assert!(wolf.try_update(&input).is_ok());
                    rc.update(&input);

                    // Finalize both hashers
                    let mut wolf_output = vec![0u8; output_size];
                    assert!(wolf.finalize_into(&mut wolf_output).is_ok());

                    let mut rc_output = vec![0u8; output_size];
                    rc.finalize_xof().read(&mut rc_output);

                    // Compare outputs
                    prop_assert_eq!(wolf_output, rc_output);
                }

                #[test]
                fn prop_multiple_updates(
                    inputs in proptest::collection::vec(any::<Vec<u8>>(), 0..100),
                    output_size in 1..2048usize
                ) {
                    let mut wolf = $name::new().unwrap();
                    let mut rc = get_rc_hasher();

                    for input in &inputs {
                        assert!(wolf.try_update(input).is_ok());
                        rc.update(input);
                    }

                    let mut wolf_output = vec![0u8; output_size];
                    assert!(wolf.finalize_into(&mut wolf_output).is_ok());

                    let mut rc_output = vec![0u8; output_size];
                    rc.finalize_xof().read(&mut rc_output);

                    // Compare outputs
                    prop_assert_eq!(wolf_output, rc_output);
                }

                #[test]
                fn prop_finalize_idempotent(
                    input in any::<Vec<u8>>(),
                    output_size in 1..2048usize
                ) {
                    let mut wolf = $name::new().unwrap();
                    let mut rc = get_rc_hasher();

                    assert!(wolf.try_update(&input).is_ok());
                    rc.update(&input);

                    let mut wolf_output1 = vec![0u8; output_size];
                    assert!(wolf.finalize_into(&mut wolf_output1).is_ok());

                    let mut rc_output1 = vec![0u8; output_size];
                    rc.finalize_xof_reset().read(&mut rc_output1);

                    let mut wolf_output2 = vec![0u8; output_size];
                    assert!(wolf.finalize_into(&mut wolf_output2).is_ok());

                    let mut rc_output2 = vec![0u8; output_size];
                    rc.finalize_xof().read(&mut rc_output2);

                    // Compare first outputs
                    prop_assert_eq!(wolf_output1, rc_output1);

                    // Compare second outputs (hash of empty input)
                    prop_assert_eq!(wolf_output2, rc_output2);
                }

                #[test]
                fn prop_zero_length_input(
                    output_size in 1..2048usize
                ) {
                    let mut wolf = $name::new().unwrap();
                    let mut rc = get_rc_hasher();

                    let input: Vec<u8> = vec![];
                    assert!(wolf.try_update(&input).is_ok());
                    rc.update(&input);

                    let mut wolf_output = vec![0u8; output_size];
                    assert!(wolf.finalize_into(&mut wolf_output).is_ok());

                    let mut rc_output = vec![0u8; output_size];
                    rc.finalize_xof().read(&mut rc_output);

                    // Compare outputs
                    prop_assert_eq!(wolf_output, rc_output);
                }

                #[test]
                fn prop_variable_output_sizes(
                    input in any::<Vec<u8>>(),
                    sizes in proptest::collection::vec(1..2048usize, 1..10)
                ) {
                    let mut wolf = $name::new().unwrap();
                    let mut rc = get_rc_hasher();

                    // Update both hashers
                    assert!(wolf.try_update(&input).is_ok());
                    rc.update(&input);

                    for &size in &sizes {
                        let mut wolf_output = vec![0u8; size];
                        assert!(wolf.finalize_into(&mut wolf_output).is_ok());

                        let mut rc_output = vec![0u8; size];
                        rc.finalize_xof_reset().read(&mut rc_output);

                        // Compare outputs
                        prop_assert_eq!(wolf_output, rc_output);
                    }
                }
            }
        }
    };
}