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
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
use std::{cmp::Ordering, fmt::Debug, marker::PhantomData};

use crate::{errors::IndexedMerkleTreeError, HIGHEST_ADDRESS_PLUS_ONE};
use light_concurrent_merkle_tree::{event::RawIndexedElement, light_hasher::Hasher};
use light_utils::bigint::bigint_to_be_bytes_array;
use num_bigint::BigUint;
use num_traits::Zero;
use num_traits::{CheckedAdd, CheckedSub, ToBytes, Unsigned};

#[derive(Clone, Debug, Default)]
pub struct IndexedElement<I>
where
    I: CheckedAdd + CheckedSub + Copy + Clone + PartialOrd + ToBytes + TryFrom<usize> + Unsigned,
    usize: From<I>,
{
    pub index: I,
    pub value: BigUint,
    pub next_index: I,
}

impl<I> From<RawIndexedElement<I>> for IndexedElement<I>
where
    I: CheckedAdd + CheckedSub + Copy + Clone + PartialOrd + ToBytes + TryFrom<usize> + Unsigned,
    usize: From<I>,
{
    fn from(value: RawIndexedElement<I>) -> Self {
        IndexedElement {
            index: value.index,
            value: BigUint::from_bytes_be(&value.value),
            next_index: value.next_index,
        }
    }
}

impl<I> PartialEq for IndexedElement<I>
where
    I: CheckedAdd + CheckedSub + Copy + Clone + PartialOrd + ToBytes + TryFrom<usize> + Unsigned,
    usize: From<I>,
{
    fn eq(&self, other: &Self) -> bool {
        self.value == other.value
    }
}

impl<I> Eq for IndexedElement<I>
where
    I: CheckedAdd + CheckedSub + Copy + Clone + PartialOrd + ToBytes + TryFrom<usize> + Unsigned,
    usize: From<I>,
{
}

impl<I> PartialOrd for IndexedElement<I>
where
    I: CheckedAdd + CheckedSub + Copy + Clone + PartialOrd + ToBytes + TryFrom<usize> + Unsigned,
    usize: From<I>,
{
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl<I> Ord for IndexedElement<I>
where
    I: CheckedAdd + CheckedSub + Copy + Clone + PartialOrd + ToBytes + TryFrom<usize> + Unsigned,
    usize: From<I>,
{
    fn cmp(&self, other: &Self) -> Ordering {
        self.value.cmp(&other.value)
    }
}

impl<I> IndexedElement<I>
where
    I: CheckedAdd + CheckedSub + Copy + Clone + PartialOrd + ToBytes + TryFrom<usize> + Unsigned,
    usize: From<I>,
{
    pub fn index(&self) -> usize {
        self.index.into()
    }

    pub fn next_index(&self) -> usize {
        self.next_index.into()
    }

    pub fn hash<H>(&self, next_value: &BigUint) -> Result<[u8; 32], IndexedMerkleTreeError>
    where
        H: Hasher,
    {
        let hash = H::hashv(&[
            bigint_to_be_bytes_array::<32>(&self.value)?.as_ref(),
            self.next_index.to_be_bytes().as_ref(),
            bigint_to_be_bytes_array::<32>(next_value)?.as_ref(),
        ])?;

        Ok(hash)
    }

    pub fn update_from_raw_element(&mut self, raw_element: &RawIndexedElement<I>) {
        self.index = raw_element.index;
        self.value = BigUint::from_bytes_be(&raw_element.value);
        self.next_index = raw_element.next_index;
    }
}

#[derive(Clone, Debug)]
pub struct IndexedElementBundle<I>
where
    I: CheckedAdd + CheckedSub + Copy + Clone + PartialOrd + ToBytes + TryFrom<usize> + Unsigned,
    usize: From<I>,
{
    pub new_low_element: IndexedElement<I>,
    pub new_element: IndexedElement<I>,
    pub new_element_next_value: BigUint,
}

#[derive(Clone, Debug)]
pub struct IndexedArray<H, I>
where
    H: Hasher,
    I: CheckedAdd + CheckedSub + Copy + Clone + PartialOrd + ToBytes + TryFrom<usize> + Unsigned,
    usize: From<I>,
{
    pub elements: Vec<IndexedElement<I>>,
    pub current_node_index: I,
    pub highest_element_index: I,

    _hasher: PhantomData<H>,
}

impl<H, I> Default for IndexedArray<H, I>
where
    H: Hasher,
    I: CheckedAdd + CheckedSub + Copy + Clone + PartialOrd + ToBytes + TryFrom<usize> + Unsigned,
    usize: From<I>,
{
    fn default() -> Self {
        Self {
            elements: vec![IndexedElement {
                index: I::zero(),
                value: BigUint::zero(),
                next_index: I::zero(),
            }],
            current_node_index: I::zero(),
            highest_element_index: I::zero(),
            _hasher: PhantomData,
        }
    }
}

impl<H, I> IndexedArray<H, I>
where
    H: Hasher,
    I: CheckedAdd + CheckedSub + Copy + Clone + PartialOrd + ToBytes + TryFrom<usize> + Unsigned,
    usize: From<I>,
{
    pub fn get(&self, index: usize) -> Option<&IndexedElement<I>> {
        self.elements.get(index)
    }

    pub fn len(&self) -> usize {
        self.current_node_index.into()
    }

    pub fn is_empty(&self) -> bool {
        self.current_node_index == I::zero()
    }

    pub fn iter(&self) -> IndexingArrayIter<H, I> {
        IndexingArrayIter {
            indexing_array: self,
            front: 0,
            back: self.current_node_index.into(),
        }
    }

    pub fn find_element(&self, value: &BigUint) -> Option<&IndexedElement<I>> {
        self.elements[..self.len() + 1]
            .iter()
            .find(|&node| node.value == *value)
    }

    pub fn init(&mut self) -> Result<IndexedElementBundle<I>, IndexedMerkleTreeError> {
        use num_traits::Num;
        let init_value = BigUint::from_str_radix(HIGHEST_ADDRESS_PLUS_ONE, 10)
            .map_err(|_| IndexedMerkleTreeError::IntegerOverflow)?;
        self.append(&init_value)
    }

    /// Returns the index of the low element for the given `value`, which is
    /// not yet the part of the array.
    ///
    /// Low element is the greatest element which still has lower value than
    /// the provided one.
    ///
    /// Low elements are used in non-membership proofs.
    pub fn find_low_element_index_for_nonexistent(
        &self,
        value: &BigUint,
    ) -> Result<I, IndexedMerkleTreeError> {
        // Try to find element whose next element is higher than the provided
        // value.
        for (i, node) in self.elements.iter().enumerate() {
            if node.value == *value {
                return Err(IndexedMerkleTreeError::ElementAlreadyExists);
            }
            if self.elements[node.next_index()].value > *value && node.value < *value {
                return i
                    .try_into()
                    .map_err(|_| IndexedMerkleTreeError::IntegerOverflow);
            }
        }
        // If no such element was found, it means that our value is going to be
        // the greatest in the array. This means that the currently greatest
        // element is going to be the low element of our value.
        Ok(self.highest_element_index)
    }

    /// Returns the:
    ///
    /// * Low element for the given value.
    /// * Next value for that low element.
    ///
    /// For the given `value`, which is not yet the part of the array.
    ///
    /// Low element is the greatest element which still has lower value than
    /// the provided one.
    ///
    /// Low elements are used in non-membership proofs.
    pub fn find_low_element_for_nonexistent(
        &self,
        value: &BigUint,
    ) -> Result<(IndexedElement<I>, BigUint), IndexedMerkleTreeError> {
        let low_element_index = self.find_low_element_index_for_nonexistent(value)?;
        let low_element = self.elements[usize::from(low_element_index)].clone();
        Ok((
            low_element.clone(),
            self.elements[low_element.next_index()].value.clone(),
        ))
    }

    /// Returns the index of the low element for the given `value`, which is
    /// already the part of the array.
    ///
    /// Low element is the greatest element which still has lower value than
    /// the provided one.
    ///
    /// Low elements are used in non-membership proofs.
    pub fn find_low_element_index_for_existent(
        &self,
        value: &BigUint,
    ) -> Result<I, IndexedMerkleTreeError> {
        for (i, node) in self.elements[..self.len() + 1].iter().enumerate() {
            if self.elements[usize::from(node.next_index)].value == *value {
                let i = i
                    .try_into()
                    .map_err(|_| IndexedMerkleTreeError::IntegerOverflow)?;
                return Ok(i);
            }
        }
        Err(IndexedMerkleTreeError::ElementDoesNotExist)
    }

    /// Returns the low element for the given `value`, which is already the
    /// part of the array.
    ///
    /// Low element is the greatest element which still has lower value than
    /// the provided one.
    ///
    /// Low elements are used in non-membership proofs.
    pub fn find_low_element_for_existent(
        &self,
        value: &BigUint,
    ) -> Result<IndexedElement<I>, IndexedMerkleTreeError> {
        let low_element_index = self.find_low_element_index_for_existent(value)?;
        let low_element = self.elements[usize::from(low_element_index)].clone();
        Ok(low_element)
    }

    /// Returns the hash of the given element. That hash consists of:
    ///
    /// * The value of the given element.
    /// * The `next_index` of the given element.
    /// * The value of the element pointed by `next_index`.
    pub fn hash_element(&self, index: I) -> Result<[u8; 32], IndexedMerkleTreeError> {
        let element = self
            .elements
            .get(usize::from(index))
            .ok_or(IndexedMerkleTreeError::IndexHigherThanMax)?;
        let next_element = self
            .elements
            .get(usize::from(element.next_index))
            .ok_or(IndexedMerkleTreeError::IndexHigherThanMax)?;
        let hash = H::hashv(&[
            bigint_to_be_bytes_array::<32>(&element.value)?.as_ref(),
            element.next_index.to_be_bytes().as_ref(),
            bigint_to_be_bytes_array::<32>(&next_element.value)?.as_ref(),
        ])?;
        Ok(hash)
    }

    /// Returns an updated low element and a new element, created based on the
    /// provided `low_element_index` and `value`.
    pub fn new_element_with_low_element_index(
        &self,
        low_element_index: I,
        value: &BigUint,
    ) -> Result<IndexedElementBundle<I>, IndexedMerkleTreeError> {
        let mut new_low_element = self.elements[usize::from(low_element_index)].clone();

        let new_element_index = self
            .current_node_index
            .checked_add(&I::one())
            .ok_or(IndexedMerkleTreeError::IntegerOverflow)?;
        let new_element = IndexedElement {
            index: new_element_index,
            value: value.clone(),
            next_index: new_low_element.next_index,
        };

        new_low_element.next_index = new_element_index;

        let new_element_next_value = self.elements[usize::from(new_element.next_index)]
            .value
            .clone();

        Ok(IndexedElementBundle {
            new_low_element,
            new_element,
            new_element_next_value,
        })
    }

    pub fn new_element(
        &self,
        value: &BigUint,
    ) -> Result<IndexedElementBundle<I>, IndexedMerkleTreeError> {
        let low_element_index = self.find_low_element_index_for_nonexistent(value)?;
        let element = self.new_element_with_low_element_index(low_element_index, value)?;

        Ok(element)
    }

    /// Appends the given `value` to the indexing array.
    pub fn append_with_low_element_index(
        &mut self,
        low_element_index: I,
        value: &BigUint,
    ) -> Result<IndexedElementBundle<I>, IndexedMerkleTreeError> {
        // TOD0: add length check, and add field to with tree height here

        let old_low_element = &self.elements[usize::from(low_element_index)];

        // Check that the `value` belongs to the range of `old_low_element`.
        if old_low_element.next_index == I::zero() {
            // In this case, the `old_low_element` is the greatest element.
            // The value of `new_element` needs to be greater than the value of
            // `old_low_element` (and therefore, be the greatest).
            if value <= &old_low_element.value {
                return Err(IndexedMerkleTreeError::LowElementGreaterOrEqualToNewElement);
            }
        } else {
            // The value of `new_element` needs to be greater than the value of
            // `old_low_element` (and therefore, be the greatest).
            if value <= &old_low_element.value {
                return Err(IndexedMerkleTreeError::LowElementGreaterOrEqualToNewElement);
            }
            // The value of `new_element` needs to be lower than the value of
            // next element pointed by `old_low_element`.
            if value >= &self.elements[usize::from(old_low_element.next_index)].value {
                return Err(IndexedMerkleTreeError::NewElementGreaterOrEqualToNextElement);
            }
        }

        // Create new node.
        let new_element_bundle =
            self.new_element_with_low_element_index(low_element_index, value)?;

        // If the old low element wasn't pointing to any element, it means that:
        //
        // * It used to be the highest element.
        // * Our new element, which we are appending, is going the be the
        //   highest element.
        //
        // Therefore, we need to save the new element index as the highest
        // index.
        if old_low_element.next_index == I::zero() {
            self.highest_element_index = new_element_bundle.new_element.index;
        }

        // Insert new node.
        self.current_node_index = new_element_bundle.new_element.index;
        self.elements.push(new_element_bundle.new_element.clone());

        // Update low element.
        self.elements[usize::from(low_element_index)] = new_element_bundle.new_low_element.clone();

        Ok(new_element_bundle)
    }

    pub fn append(
        &mut self,
        value: &BigUint,
    ) -> Result<IndexedElementBundle<I>, IndexedMerkleTreeError> {
        let low_element_index = self.find_low_element_index_for_nonexistent(value)?;
        self.append_with_low_element_index(low_element_index, value)
    }

    pub fn lowest(&self) -> Option<IndexedElement<I>> {
        if self.current_node_index < I::one() {
            None
        } else {
            self.elements.get(1).cloned()
        }
    }
}

pub struct IndexingArrayIter<'a, H, I>
where
    H: Hasher,
    I: CheckedAdd + CheckedSub + Copy + Clone + PartialOrd + ToBytes + TryFrom<usize> + Unsigned,
    usize: From<I>,
{
    indexing_array: &'a IndexedArray<H, I>,
    front: usize,
    back: usize,
}

impl<'a, H, I> Iterator for IndexingArrayIter<'a, H, I>
where
    H: Hasher,
    I: CheckedAdd + CheckedSub + Copy + Clone + PartialOrd + ToBytes + TryFrom<usize> + Unsigned,
    usize: From<I>,
{
    type Item = &'a IndexedElement<I>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.front <= self.back {
            let result = self.indexing_array.elements.get(self.front);
            self.front += 1;
            result
        } else {
            None
        }
    }
}

impl<'a, H, I> DoubleEndedIterator for IndexingArrayIter<'a, H, I>
where
    H: Hasher,
    I: CheckedAdd + CheckedSub + Copy + Clone + PartialOrd + ToBytes + TryFrom<usize> + Unsigned,
    usize: From<I>,
{
    fn next_back(&mut self) -> Option<Self::Item> {
        if self.back >= self.front {
            let result = self.indexing_array.elements.get(self.back);
            self.back -= 1;
            result
        } else {
            None
        }
    }
}

#[cfg(test)]
mod test {
    use light_concurrent_merkle_tree::light_hasher::Poseidon;
    use num_bigint::{RandBigInt, ToBigUint};
    use rand::thread_rng;

    use super::*;

    #[test]
    fn test_indexed_element_cmp() {
        let mut rng = thread_rng();

        for _ in 0..1000 {
            let value = rng.gen_biguint(128);
            let element_1 = IndexedElement::<u16> {
                index: 0,
                value: value.clone(),
                next_index: 1,
            };
            let element_2 = IndexedElement::<u16> {
                index: 1,
                value,
                next_index: 2,
            };
            assert_eq!(element_1, element_2);
            assert_eq!(element_2, element_1);
            assert!(matches!(element_1.cmp(&element_2), Ordering::Equal));
            assert!(matches!(element_2.cmp(&element_1), Ordering::Equal));

            let value_higher = rng.gen_biguint(128);
            if value_higher == 0.to_biguint().unwrap() {
                continue;
            }
            let value_lower = rng.gen_biguint_below(&value_higher);
            let element_lower = IndexedElement::<u16> {
                index: 0,
                value: value_lower,
                next_index: 1,
            };
            let element_higher = IndexedElement::<u16> {
                index: 1,
                value: value_higher,
                next_index: 2,
            };
            assert_ne!(element_lower, element_higher);
            assert_ne!(element_higher, element_lower);
            assert!(matches!(element_lower.cmp(&element_higher), Ordering::Less));
            assert!(matches!(
                element_higher.cmp(&element_lower),
                Ordering::Greater
            ));
            assert!(matches!(
                element_lower.partial_cmp(&element_higher),
                Some(Ordering::Less)
            ));
            assert!(matches!(
                element_higher.partial_cmp(&element_lower),
                Some(Ordering::Greater)
            ));
        }
    }

    /// Tests the insertion of elements to the indexing array.
    #[test]
    fn test_append() {
        // The initial state of the array looks like:
        //
        // ```
        // value      = [0] [0] [0] [0] [0] [0] [0] [0]
        // next_index = [0] [0] [0] [0] [0] [0] [0] [0]
        // ```
        let mut indexed_array: IndexedArray<Poseidon, usize> = IndexedArray::default();

        let nullifier1 = 30_u32.to_biguint().unwrap();
        let bundle1 = indexed_array.new_element(&nullifier1).unwrap();
        assert!(indexed_array.find_element(&nullifier1).is_none());
        indexed_array.append(&nullifier1).unwrap();

        // After adding a new value 30, it should look like:
        //
        // ```
        // value      = [ 0] [30] [0] [0] [0] [0] [0] [0]
        // next_index = [ 1] [ 0] [0] [0] [0] [0] [0] [0]
        // ```
        //
        // Because:
        //
        // * Low element is the first node, with index 0 and value 0. There is
        //   no node with value greater as 30, so we found it as a one pointing to
        //   node 0 (which will always have value 0).
        // * The new nullifier is inserted in index 1.
        // * `next_*` fields of the low nullifier are updated to point to the new
        //   nullifier.
        assert_eq!(
            indexed_array.find_element(&nullifier1),
            Some(&bundle1.new_element),
        );
        let expected_hash = Poseidon::hashv(&[
            bigint_to_be_bytes_array::<32>(&nullifier1)
                .unwrap()
                .as_ref(),
            0_usize.to_be_bytes().as_ref(),
            bigint_to_be_bytes_array::<32>(&(0.to_biguint().unwrap()))
                .unwrap()
                .as_ref(),
        ])
        .unwrap();
        assert_eq!(indexed_array.hash_element(1).unwrap(), expected_hash);
        assert_eq!(
            indexed_array.elements[0],
            IndexedElement {
                index: 0,
                value: 0_u32.to_biguint().unwrap(),
                next_index: 1,
            },
        );
        assert_eq!(
            indexed_array.elements[1],
            IndexedElement {
                index: 1,
                value: 30_u32.to_biguint().unwrap(),
                next_index: 0,
            }
        );
        assert_eq!(
            indexed_array.iter().collect::<Vec<_>>().as_slice(),
            &[
                &IndexedElement {
                    index: 0,
                    value: 0_u32.to_biguint().unwrap(),
                    next_index: 1,
                },
                &IndexedElement {
                    index: 1,
                    value: 30_u32.to_biguint().unwrap(),
                    next_index: 0
                }
            ]
        );

        let nullifier2 = 10_u32.to_biguint().unwrap();
        let bundle2 = indexed_array.new_element(&nullifier2).unwrap();
        assert!(indexed_array.find_element(&nullifier2).is_none());
        indexed_array.append(&nullifier2).unwrap();

        // After adding an another value 10, it should look like:
        //
        // ```
        // value      = [ 0] [30] [10] [0] [0] [0] [0] [0]
        // next_index = [ 2] [ 0] [ 1] [0] [0] [0] [0] [0]
        // ```
        //
        // Because:
        //
        // * Low nullifier is still the node 0, but this time for differen reason -
        //   its `next_index` 2 contains value 30, whish is greater than 10.
        // * The new nullifier is inserted as node 2.
        // * Low nullifier is pointing to the index 1. We assign the 1st nullifier
        //   as the next nullifier of our new nullifier. Therefore, our new nullifier
        //   looks like: `[value = 10, next_index = 1]`.
        // * Low nullifier is updated to point to the new nullifier. Therefore,
        //   after update it looks like: `[value = 0, next_index = 2]`.
        // * The previously inserted nullifier, the node 1, remains unchanged.
        assert_eq!(
            indexed_array.find_element(&nullifier2),
            Some(&bundle2.new_element),
        );
        let expected_hash = Poseidon::hashv(&[
            bigint_to_be_bytes_array::<32>(&nullifier2)
                .unwrap()
                .as_ref(),
            1_usize.to_be_bytes().as_ref(),
            bigint_to_be_bytes_array::<32>(&(30.to_biguint().unwrap()))
                .unwrap()
                .as_ref(),
        ])
        .unwrap();
        assert_eq!(indexed_array.hash_element(2).unwrap(), expected_hash);
        assert_eq!(
            indexed_array.elements[0],
            IndexedElement {
                index: 0,
                value: 0_u32.to_biguint().unwrap(),
                next_index: 2,
            }
        );
        assert_eq!(
            indexed_array.elements[1],
            IndexedElement {
                index: 1,
                value: 30_u32.to_biguint().unwrap(),
                next_index: 0,
            }
        );
        assert_eq!(
            indexed_array.elements[2],
            IndexedElement {
                index: 2,
                value: 10_u32.to_biguint().unwrap(),
                next_index: 1,
            }
        );
        assert_eq!(
            indexed_array.iter().collect::<Vec<_>>().as_slice(),
            &[
                &IndexedElement {
                    index: 0,
                    value: 0_u32.to_biguint().unwrap(),
                    next_index: 2,
                },
                &IndexedElement {
                    index: 1,
                    value: 30_u32.to_biguint().unwrap(),
                    next_index: 0,
                },
                &IndexedElement {
                    index: 2,
                    value: 10_u32.to_biguint().unwrap(),
                    next_index: 1,
                }
            ]
        );

        let nullifier3 = 20_u32.to_biguint().unwrap();
        let bundle3 = indexed_array.new_element(&nullifier3).unwrap();
        assert!(indexed_array.find_element(&nullifier3).is_none());
        indexed_array.append(&nullifier3).unwrap();

        // After adding an another value 20, it should look like:
        //
        // ```
        // value      = [ 0] [30] [10] [20] [0] [0] [0] [0]
        // next_index = [ 2] [ 0] [ 3] [ 1] [0] [0] [0] [0]
        // ```
        //
        // Because:
        // * Low nullifier is the node 2.
        // * The new nullifier is inserted as node 3.
        // * Low nullifier is pointing to the node 2. We assign the 1st nullifier
        //   as the next nullifier of our new nullifier. Therefore, our new
        //   nullifier looks like:
        // * Low nullifier is updated to point to the new nullifier. Therefore,
        //   after update it looks like: `[value = 10, next_index = 3]`.
        assert_eq!(
            indexed_array.find_element(&nullifier3),
            Some(&bundle3.new_element),
        );
        let expected_hash = Poseidon::hashv(&[
            bigint_to_be_bytes_array::<32>(&nullifier3)
                .unwrap()
                .as_ref(),
            1_usize.to_be_bytes().as_ref(),
            bigint_to_be_bytes_array::<32>(&(30.to_biguint().unwrap()))
                .unwrap()
                .as_ref(),
        ])
        .unwrap();
        assert_eq!(indexed_array.hash_element(3).unwrap(), expected_hash);
        assert_eq!(
            indexed_array.elements[0],
            IndexedElement {
                index: 0,
                value: 0_u32.to_biguint().unwrap(),
                next_index: 2,
            }
        );
        assert_eq!(
            indexed_array.elements[1],
            IndexedElement {
                index: 1,
                value: 30_u32.to_biguint().unwrap(),
                next_index: 0,
            }
        );
        assert_eq!(
            indexed_array.elements[2],
            IndexedElement {
                index: 2,
                value: 10_u32.to_biguint().unwrap(),
                next_index: 3,
            }
        );
        assert_eq!(
            indexed_array.elements[3],
            IndexedElement {
                index: 3,
                value: 20_u32.to_biguint().unwrap(),
                next_index: 1,
            }
        );
        assert_eq!(
            indexed_array.iter().collect::<Vec<_>>().as_slice(),
            &[
                &IndexedElement {
                    index: 0,
                    value: 0_u32.to_biguint().unwrap(),
                    next_index: 2,
                },
                &IndexedElement {
                    index: 1,
                    value: 30_u32.to_biguint().unwrap(),
                    next_index: 0,
                },
                &IndexedElement {
                    index: 2,
                    value: 10_u32.to_biguint().unwrap(),
                    next_index: 3,
                },
                &IndexedElement {
                    index: 2,
                    value: 20_u32.to_biguint().unwrap(),
                    next_index: 1
                }
            ]
        );

        let nullifier4 = 50_u32.to_biguint().unwrap();
        let bundle4 = indexed_array.new_element(&nullifier4).unwrap();
        assert!(indexed_array.find_element(&nullifier4).is_none());
        indexed_array.append(&nullifier4).unwrap();

        // After adding an another value 50, it should look like:
        //
        // ```
        // value      = [ 0]  [30] [10] [20] [50] [0] [0] [0]
        // next_index = [ 2]  [ 4] [ 3] [ 1] [0 ] [0] [0] [0]
        // ```
        //
        // Because:
        //
        // * Low nullifier is the node 1 - there is no node with value greater
        //   than 50, so we found it as a one having 0 as the `next_value`.
        // * The new nullifier is inserted as node 4.
        // * Low nullifier is not pointing to any node. So our new nullifier
        //   is not going to point to any other node either. Therefore, the new
        //   nullifier looks like: `[value = 50, next_index = 0]`.
        // * Low nullifier is updated to point to the new nullifier. Therefore,
        //   after update it looks like: `[value = 30, next_index = 4]`.
        assert_eq!(
            indexed_array.find_element(&nullifier4),
            Some(&bundle4.new_element),
        );
        let expected_hash = Poseidon::hashv(&[
            bigint_to_be_bytes_array::<32>(&nullifier4)
                .unwrap()
                .as_ref(),
            0_usize.to_be_bytes().as_ref(),
            bigint_to_be_bytes_array::<32>(&(0.to_biguint().unwrap()))
                .unwrap()
                .as_ref(),
        ])
        .unwrap();
        assert_eq!(indexed_array.hash_element(4).unwrap(), expected_hash);
        assert_eq!(
            indexed_array.elements[0],
            IndexedElement {
                index: 0,
                value: 0_u32.to_biguint().unwrap(),
                next_index: 2,
            }
        );
        assert_eq!(
            indexed_array.elements[1],
            IndexedElement {
                index: 1,
                value: 30_u32.to_biguint().unwrap(),
                next_index: 4,
            }
        );
        assert_eq!(
            indexed_array.elements[2],
            IndexedElement {
                index: 2,
                value: 10_u32.to_biguint().unwrap(),
                next_index: 3,
            }
        );
        assert_eq!(
            indexed_array.elements[3],
            IndexedElement {
                index: 3,
                value: 20_u32.to_biguint().unwrap(),
                next_index: 1,
            }
        );
        assert_eq!(
            indexed_array.elements[4],
            IndexedElement {
                index: 4,
                value: 50_u32.to_biguint().unwrap(),
                next_index: 0,
            }
        );
        assert_eq!(
            indexed_array.iter().collect::<Vec<_>>().as_slice(),
            &[
                &IndexedElement {
                    index: 0,
                    value: 0_u32.to_biguint().unwrap(),
                    next_index: 2,
                },
                &IndexedElement {
                    index: 1,
                    value: 30_u32.to_biguint().unwrap(),
                    next_index: 4,
                },
                &IndexedElement {
                    index: 2,
                    value: 10_u32.to_biguint().unwrap(),
                    next_index: 3,
                },
                &IndexedElement {
                    index: 3,
                    value: 20_u32.to_biguint().unwrap(),
                    next_index: 1,
                },
                &IndexedElement {
                    index: 4,
                    value: 50_u32.to_biguint().unwrap(),
                    next_index: 0,
                }
            ]
        );
    }

    #[test]
    fn test_append_with_low_element_index() {
        // The initial state of the array looks like:
        //
        // ```
        // value      = [0] [0] [0] [0] [0] [0] [0] [0]
        // next_index = [0] [0] [0] [0] [0] [0] [0] [0]
        // ```
        let mut indexing_array: IndexedArray<Poseidon, usize> = IndexedArray::default();

        let low_element_index = 0;
        let nullifier1 = 30_u32.to_biguint().unwrap();
        indexing_array
            .append_with_low_element_index(low_element_index, &nullifier1)
            .unwrap();

        // After adding a new value 30, it should look like:
        //
        // ```
        // value      = [ 0] [30] [0] [0] [0] [0] [0] [0]
        // next_index = [ 1] [ 0] [0] [0] [0] [0] [0] [0]
        // ```
        //
        // Because:
        //
        // * Low element is the first node, with index 0 and value 0. There is
        //   no node with value greater as 30, so we found it as a one pointing to
        //   node 0 (which will always have value 0).
        // * The new nullifier is inserted in index 1.
        // * `next_*` fields of the low nullifier are updated to point to the new
        //   nullifier.
        assert_eq!(
            indexing_array.elements[0],
            IndexedElement {
                index: 0,
                value: 0_u32.to_biguint().unwrap(),
                next_index: 1,
            },
        );
        assert_eq!(
            indexing_array.elements[1],
            IndexedElement {
                index: 1,
                value: 30_u32.to_biguint().unwrap(),
                next_index: 0,
            }
        );

        let low_element_index = 0;
        let nullifier2 = 10_u32.to_biguint().unwrap();
        indexing_array
            .append_with_low_element_index(low_element_index, &nullifier2)
            .unwrap();

        // After adding an another value 10, it should look like:
        //
        // ```
        // value      = [ 0] [30] [10] [0] [0] [0] [0] [0]
        // next_index = [ 2] [ 0] [ 1] [0] [0] [0] [0] [0]
        // ```
        //
        // Because:
        //
        // * Low nullifier is still the node 0, but this time for differen reason -
        //   its `next_index` 2 contains value 30, whish is greater than 10.
        // * The new nullifier is inserted as node 2.
        // * Low nullifier is pointing to the index 1. We assign the 1st nullifier
        //   as the next nullifier of our new nullifier. Therefore, our new nullifier
        //   looks like: `[value = 10, next_index = 1]`.
        // * Low nullifier is updated to point to the new nullifier. Therefore,
        //   after update it looks like: `[value = 0, next_index = 2]`.
        // * The previously inserted nullifier, the node 1, remains unchanged.
        assert_eq!(
            indexing_array.elements[0],
            IndexedElement {
                index: 0,
                value: 0_u32.to_biguint().unwrap(),
                next_index: 2,
            }
        );
        assert_eq!(
            indexing_array.elements[1],
            IndexedElement {
                index: 1,
                value: 30_u32.to_biguint().unwrap(),
                next_index: 0,
            }
        );
        assert_eq!(
            indexing_array.elements[2],
            IndexedElement {
                index: 2,
                value: 10_u32.to_biguint().unwrap(),
                next_index: 1,
            }
        );

        let low_element_index = 2;
        let nullifier3 = 20_u32.to_biguint().unwrap();
        indexing_array
            .append_with_low_element_index(low_element_index, &nullifier3)
            .unwrap();

        // After adding an another value 20, it should look like:
        //
        // ```
        // value      = [ 0] [30] [10] [20] [0] [0] [0] [0]
        // next_index = [ 2] [ 0] [ 3] [ 1] [0] [0] [0] [0]
        // ```
        //
        // Because:
        // * Low nullifier is the node 2.
        // * The new nullifier is inserted as node 3.
        // * Low nullifier is pointing to the node 2. We assign the 1st nullifier
        //   as the next nullifier of our new nullifier. Therefore, our new
        //   nullifier looks like:
        // * Low nullifier is updated to point to the new nullifier. Therefore,
        //   after update it looks like: `[value = 10, next_index = 3]`.
        assert_eq!(
            indexing_array.elements[0],
            IndexedElement {
                index: 0,
                value: 0_u32.to_biguint().unwrap(),
                next_index: 2,
            }
        );
        assert_eq!(
            indexing_array.elements[1],
            IndexedElement {
                index: 1,
                value: 30_u32.to_biguint().unwrap(),
                next_index: 0,
            }
        );
        assert_eq!(
            indexing_array.elements[2],
            IndexedElement {
                index: 2,
                value: 10_u32.to_biguint().unwrap(),
                next_index: 3,
            }
        );
        assert_eq!(
            indexing_array.elements[3],
            IndexedElement {
                index: 3,
                value: 20_u32.to_biguint().unwrap(),
                next_index: 1,
            }
        );

        let low_element_index = 1;
        let nullifier4 = 50_u32.to_biguint().unwrap();
        indexing_array
            .append_with_low_element_index(low_element_index, &nullifier4)
            .unwrap();

        // After adding an another value 50, it should look like:
        //
        // ```
        // value      = [ 0]  [30] [10] [20] [50] [0] [0] [0]
        // next_index = [ 2]  [ 4] [ 3] [ 1] [0 ] [0] [0] [0]
        // ```
        //
        // Because:
        //
        // * Low nullifier is the node 1 - there is no node with value greater
        //   than 50, so we found it as a one having 0 as the `next_value`.
        // * The new nullifier is inserted as node 4.
        // * Low nullifier is not pointing to any node. So our new nullifier
        //   is not going to point to any other node either. Therefore, the new
        //   nullifier looks like: `[value = 50, next_index = 0]`.
        // * Low nullifier is updated to point to the new nullifier. Therefore,
        //   after update it looks like: `[value = 30, next_index = 4]`.
        assert_eq!(
            indexing_array.elements[0],
            IndexedElement {
                index: 0,
                value: 0_u32.to_biguint().unwrap(),
                next_index: 2,
            }
        );
        assert_eq!(
            indexing_array.elements[1],
            IndexedElement {
                index: 1,
                value: 30_u32.to_biguint().unwrap(),
                next_index: 4,
            }
        );
        assert_eq!(
            indexing_array.elements[2],
            IndexedElement {
                index: 2,
                value: 10_u32.to_biguint().unwrap(),
                next_index: 3,
            }
        );
        assert_eq!(
            indexing_array.elements[3],
            IndexedElement {
                index: 3,
                value: 20_u32.to_biguint().unwrap(),
                next_index: 1,
            }
        );
        assert_eq!(
            indexing_array.elements[4],
            IndexedElement {
                index: 4,
                value: 50_u32.to_biguint().unwrap(),
                next_index: 0,
            }
        );
    }

    /// Tries to violate the integrity of the array by pointing to invalid low
    /// nullifiers. Tests whether the range check works correctly and disallows
    /// the invalid appends from happening.
    #[test]
    fn test_append_with_low_element_index_invalid() {
        // The initial state of the array looks like:
        //
        // ```
        // value      = [0] [0] [0] [0] [0] [0] [0] [0]
        // next_index = [0] [0] [0] [0] [0] [0] [0] [0]
        // ```
        let mut indexing_array: IndexedArray<Poseidon, usize> = IndexedArray::default();

        // Append nullifier 30. The low nullifier is at index 0. The array
        // should look like:
        //
        // ```
        // value      = [ 0] [30] [0] [0] [0] [0] [0] [0]
        // next_index = [ 1] [ 0] [0] [0] [0] [0] [0] [0]
        // ```
        let low_element_index = 0;
        let nullifier1 = 30_u32.to_biguint().unwrap();
        indexing_array
            .append_with_low_element_index(low_element_index, &nullifier1)
            .unwrap();

        // Try appending nullifier 20, while pointing to index 1 as low
        // nullifier.
        // Therefore, the new element is lower than the supposed low element.
        let low_element_index = 1;
        let nullifier2 = 20_u32.to_biguint().unwrap();
        assert!(matches!(
            indexing_array.append_with_low_element_index(low_element_index, &nullifier2),
            Err(IndexedMerkleTreeError::LowElementGreaterOrEqualToNewElement)
        ));

        // Try appending nullifier 50, while pointing to index 0 as low
        // nullifier.
        // Therefore, the new element is greater than next element.
        let low_element_index = 0;
        let nullifier2 = 50_u32.to_biguint().unwrap();
        assert!(matches!(
            indexing_array.append_with_low_element_index(low_element_index, &nullifier2),
            Err(IndexedMerkleTreeError::NewElementGreaterOrEqualToNextElement),
        ));

        // Append nullifier 50 correctly, with 0 as low nullifier. The array
        // should look like:
        //
        // ```
        // value      = [ 0] [30] [50] [0] [0] [0] [0] [0]
        // next_index = [ 1] [ 2] [ 0] [0] [0] [0] [0] [0]
        // ```
        let low_element_index = 1;
        let nullifier2 = 50_u32.to_biguint().unwrap();
        indexing_array
            .append_with_low_element_index(low_element_index, &nullifier2)
            .unwrap();

        // Try appending nullifier 40, while pointint to index 2 (value 50) as
        // low nullifier.
        // Therefore, the pointed low element is greater than the new element.
        let low_element_index = 2;
        let nullifier3 = 40_u32.to_biguint().unwrap();
        assert!(matches!(
            indexing_array.append_with_low_element_index(low_element_index, &nullifier3),
            Err(IndexedMerkleTreeError::LowElementGreaterOrEqualToNewElement)
        ));
    }

    /// Tests whether `find_*_for_existent` elements return `None` when a
    /// nonexistent is provided.
    #[test]
    fn test_find_low_element_for_existent_element() {
        let mut indexed_array: IndexedArray<Poseidon, usize> = IndexedArray::default();

        // Append nullifiers 40 and 20.
        let low_element_index = 0;
        let nullifier_1 = 40_u32.to_biguint().unwrap();
        indexed_array
            .append_with_low_element_index(low_element_index, &nullifier_1)
            .unwrap();
        let low_element_index = 0;
        let nullifier_2 = 20_u32.to_biguint().unwrap();
        indexed_array
            .append_with_low_element_index(low_element_index, &nullifier_2)
            .unwrap();

        // Try finding a low element for nonexistent nullifier 30.
        let nonexistent_nullifier = 30_u32.to_biguint().unwrap();
        // `*_existent` methods should fail.
        let res = indexed_array.find_low_element_index_for_existent(&nonexistent_nullifier);
        assert!(matches!(
            res,
            Err(IndexedMerkleTreeError::ElementDoesNotExist)
        ));
        let res = indexed_array.find_low_element_for_existent(&nonexistent_nullifier);
        assert!(matches!(
            res,
            Err(IndexedMerkleTreeError::ElementDoesNotExist)
        ));
        // `*_nonexistent` methods should succeed.
        let low_element_index = indexed_array
            .find_low_element_index_for_nonexistent(&nonexistent_nullifier)
            .unwrap();
        assert_eq!(low_element_index, 2);
        let low_element = indexed_array
            .find_low_element_for_nonexistent(&nonexistent_nullifier)
            .unwrap();
        assert_eq!(
            low_element,
            (
                IndexedElement::<usize> {
                    index: 2,
                    value: 20_u32.to_biguint().unwrap(),
                    next_index: 1,
                },
                40_u32.to_biguint().unwrap(),
            )
        );

        // Try finding a low element of existent nullifier 40.
        // `_existent` methods should succeed.
        let low_element_index = indexed_array
            .find_low_element_index_for_existent(&nullifier_1)
            .unwrap();
        assert_eq!(low_element_index, 2);
        let low_element = indexed_array
            .find_low_element_for_existent(&nullifier_1)
            .unwrap();
        assert_eq!(
            low_element,
            IndexedElement::<usize> {
                index: 2,
                value: 20_u32.to_biguint().unwrap(),
                next_index: 1,
            },
        );
        // `*_nonexistent` methods should fail.
        let res = indexed_array.find_low_element_index_for_nonexistent(&nullifier_1);
        assert!(matches!(
            res,
            Err(IndexedMerkleTreeError::ElementAlreadyExists)
        ));
        let res = indexed_array.find_low_element_for_nonexistent(&nullifier_1);
        assert!(matches!(
            res,
            Err(IndexedMerkleTreeError::ElementAlreadyExists)
        ));
    }
}