@@ -22,17 +22,20 @@ pub fn parent_index(node_index: usize) -> usize {
22
22
23
23
// The list of values is completed repeating the last value to a power of two length
24
24
pub fn complete_until_power_of_two < T : Clone > ( values : & mut Vec < T > ) -> Vec < T > {
25
- if values. len ( ) == 1 {
26
- return values. clone ( ) ; // Return immediately if there is only one element.
27
- }
28
25
while !is_power_of_two ( values. len ( ) ) {
29
26
values. push ( values[ values. len ( ) - 1 ] . clone ( ) ) ;
30
27
}
31
28
values. to_vec ( )
32
29
}
33
30
34
- pub fn is_power_of_two ( x : usize ) -> bool {
35
- ( x != 0 ) && ( ( x & ( x - 1 ) ) == 0 )
31
+ // ! NOTE !
32
+ // We in this function we say 2^0 = 1 is not a power of two when it is infact true. We
33
+ // need this to maintain that the smallest tree at two leaves counts and we could not find
34
+ // a name that wasn't overly verbose. In the case of a single value being present in a leaf node
35
+ // this indicates we pad to next power of two (i.e. 2). The function is private and is only used
36
+ // to ensure the tree has a power of 2 number of leaves.
37
+ fn is_power_of_two ( x : usize ) -> bool {
38
+ ( x > 1 ) && ( ( x & ( x - 1 ) ) == 0 )
36
39
}
37
40
38
41
// ! CAUTION !
@@ -109,6 +112,19 @@ mod tests {
109
112
}
110
113
}
111
114
115
+ #[ test]
116
+ // expected |2|2|
117
+ fn complete_the_length_of_one_field_element_to_be_a_power_of_two ( ) {
118
+ let mut values: Vec < FE > = vec ! [ FE :: new( 2 ) ] ;
119
+ let hashed_leaves = complete_until_power_of_two ( & mut values) ;
120
+
121
+ let mut expected_leaves = vec ! [ FE :: new( 2 ) ] ;
122
+ expected_leaves. extend ( [ FE :: new ( 2 ) ] ) ;
123
+ assert_eq ! ( hashed_leaves. len( ) , 2 ) ;
124
+ assert_eq ! ( hashed_leaves[ 0 ] , expected_leaves[ 0 ] ) ;
125
+ assert_eq ! ( hashed_leaves[ 1 ] , expected_leaves[ 1 ] ) ;
126
+ }
127
+
112
128
const ROOT : usize = 0 ;
113
129
114
130
#[ test]
0 commit comments