@@ -4,9 +4,6 @@ use super::funarr::*;
44
55use std:: fmt:: Formatter ;
66
7- // This is required due to some hax-lib inconsistencies with versus without `cfg(hax)`.
8- #[ cfg( hax) ]
9- use hax_lib:: { int, ToInt } ;
107
118// TODO: this module uses `u128/i128` as mathematic integers. We should use `hax_lib::int` or bigint.
129
@@ -20,12 +17,10 @@ use hax_lib::{int, ToInt};
2017/// The [`Debug`] implementation for `BitVec` pretty-prints the bits in groups of eight,
2118/// making the bit pattern more human-readable. The type also implements indexing,
2219/// allowing for easy access to individual bits.
23- #[ hax_lib:: fstar:: before( "noeq" ) ]
2420#[ derive( Copy , Clone , Eq , PartialEq ) ]
2521pub struct BitVec < const N : u64 > ( FunArray < N , Bit > ) ;
2622
27- /// Pretty prints a bit slice by group of 8
28- #[ hax_lib:: exclude]
23+ /// Pretty prints a bit slice by group of 8#[hax_lib::exclude]
2924fn bit_slice_to_string ( bits : & [ Bit ] ) -> String {
3025 bits. iter ( )
3126 . map ( |bit| match bit {
@@ -41,24 +36,23 @@ fn bit_slice_to_string(bits: &[Bit]) -> String {
4136 . into ( )
4237}
4338
44- # [ hax_lib :: exclude ]
39+
4540impl < const N : u64 > core:: fmt:: Debug for BitVec < N > {
4641 fn fmt ( & self , f : & mut Formatter < ' _ > ) -> Result < ( ) , std:: fmt:: Error > {
4742 write ! ( f, "{}" , bit_slice_to_string( & self . 0 . as_vec( ) ) )
4843 }
4944}
5045
51- # [ hax_lib :: attributes ]
46+
5247impl < const N : u64 > core:: ops:: Index < u64 > for BitVec < N > {
5348 type Output = Bit ;
54- #[ requires( index < N ) ]
5549 fn index ( & self , index : u64 ) -> & Self :: Output {
5650 self . 0 . get ( index)
5751 }
5852}
5953
6054/// Convert a bit slice into an unsigned number.
61- # [ hax_lib :: exclude ]
55+
6256fn u128_int_from_bit_slice ( bits : & [ Bit ] ) -> u128 {
6357 bits. iter ( )
6458 . enumerate ( )
@@ -67,7 +61,6 @@ fn u128_int_from_bit_slice(bits: &[Bit]) -> u128 {
6761}
6862
6963/// Convert a bit slice into a machine integer of type `T`.
70- #[ hax_lib:: exclude]
7164fn int_from_bit_slice < T : TryFrom < i128 > + MachineInteger + Copy > ( bits : & [ Bit ] ) -> T {
7265 debug_assert ! ( bits. len( ) <= T :: bits( ) as usize ) ;
7366 let result = if T :: SIGNED {
@@ -87,27 +80,6 @@ fn int_from_bit_slice<T: TryFrom<i128> + MachineInteger + Copy>(bits: &[Bit]) ->
8780 } ;
8881 n
8982}
90-
91- macro_rules! impl_pointwise {
92- ( $n: literal, $( $i: literal) * ) => {
93- impl BitVec <$n> {
94- pub fn pointwise( self ) -> Self {
95- Self :: from_fn( |i| match i {
96- $( $i => self [ $i] , ) *
97- _ => unreachable!( ) ,
98- } )
99- }
100- }
101- } ;
102- }
103-
104- impl_pointwise ! ( 128 , 0 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 ) ;
105- impl_pointwise ! ( 256 , 0 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 ) ;
106-
107- /// An F* attribute that indiquates a rewritting lemma should be applied
108- pub const REWRITE_RULE : ( ) = { } ;
109-
110- #[ hax_lib:: exclude]
11183impl < const N : u64 > BitVec < N > {
11284 /// Constructor for BitVec. `BitVec::<N>::from_fn` constructs a bitvector out of a function that takes usizes smaller than `N` and produces bits.
11385 pub fn from_fn < F : Fn ( u64 ) -> Bit > ( f : F ) -> Self {
@@ -148,26 +120,21 @@ impl<const N: u64> BitVec<N> {
148120 }
149121}
150122
151- # [ hax_lib :: attributes ]
123+
152124impl < const N : u64 > BitVec < N > {
153- # [ hax_lib :: requires ( CHUNK > 0 && CHUNK . to_int ( ) * SHIFTS . to_int ( ) == N . to_int ( ) ) ]
125+
154126 pub fn chunked_shift < const CHUNK : u64 , const SHIFTS : u64 > (
155127 self ,
156128 shl : FunArray < SHIFTS , i128 > ,
157129 ) -> BitVec < N > {
158- # [ hax_lib :: requires ( CHUNK > 0 && CHUNK . to_int ( ) * SHIFTS . to_int ( ) == N . to_int ( ) ) ]
130+
159131 fn chunked_shift < const N : u64 , const CHUNK : u64 , const SHIFTS : u64 > (
160132 bitvec : BitVec < N > ,
161133 shl : FunArray < SHIFTS , i128 > ,
162134 ) -> BitVec < N > {
163135 BitVec :: from_fn ( |i| {
164136 let nth_bit = i % CHUNK ;
165137 let nth_chunk = i / CHUNK ;
166- hax_lib:: assert_prop!( nth_chunk. to_int( ) <= SHIFTS . to_int( ) - int!( 1 ) ) ;
167- hax_lib:: assert_prop!(
168- nth_chunk. to_int( ) * CHUNK . to_int( )
169- <= ( SHIFTS . to_int( ) - int!( 1 ) ) * CHUNK . to_int( )
170- ) ;
171138 let shift: i128 = if nth_chunk < SHIFTS {
172139 shl[ nth_chunk]
173140 } else {
@@ -176,10 +143,6 @@ impl<const N: u64> BitVec<N> {
176143 let local_index = ( nth_bit as i128 ) . wrapping_sub ( shift) ;
177144 if local_index < CHUNK as i128 && local_index >= 0 {
178145 let local_index = local_index as u64 ;
179- hax_lib:: assert_prop!(
180- nth_chunk. to_int( ) * CHUNK . to_int( ) + local_index. to_int( )
181- < SHIFTS . to_int( ) * CHUNK . to_int( )
182- ) ;
183146 bitvec[ nth_chunk * CHUNK + local_index]
184147 } else {
185148 Bit :: Zero
@@ -215,7 +178,6 @@ pub mod int_vec_interp {
215178 pub type $name = FunArray <$m, $ty>;
216179 pastey:: paste! {
217180 const _: ( ) = {
218- #[ hax_lib:: opaque]
219181 impl BitVec <$n> {
220182 #[ doc = concat!( "Conversion from " , stringify!( $ty) , " vectors of size " , stringify!( $m) , "to bit vectors of size " , stringify!( $n) ) ]
221183 pub fn [ < from_ $name >] ( iv: $name) -> BitVec <$n> {
@@ -250,26 +212,6 @@ pub mod int_vec_interp {
250212 FunArray :: from_fn( |_| value)
251213 }
252214 }
253-
254-
255-
256- #[ doc = concat!( "Lemma that asserts that applying " , stringify!( BitVec :: <$n>:: from) , " and then " , stringify!( $name:: from) , " is the identity." ) ]
257- #[ hax_lib:: fstar:: before( "[@@ $SIMPLIFICATION_LEMMA ]" ) ]
258- #[ hax_lib:: opaque]
259- #[ hax_lib:: lemma]
260- // #[hax_lib::fstar::smt_pat($name::from(BitVec::<$n>::from(x)))]
261- pub fn lemma_cancel_iv( x: $name) -> Proof <{
262- hax_lib:: eq( BitVec :: [ < to_ $name >] ( BitVec :: [ <from_ $name>] ( x) ) , x)
263- } > { }
264- #[ doc = concat!( "Lemma that asserts that applying " , stringify!( $name:: from) , " and then " , stringify!( BitVec :: <$n>:: from) , " is the identity." ) ]
265- #[ hax_lib:: fstar:: before( "[@@ $SIMPLIFICATION_LEMMA ]" ) ]
266- #[ hax_lib:: opaque]
267- #[ hax_lib:: lemma]
268- // #[hax_lib::fstar::smt_pat(BitVec::<$n>::from($name::from(x)))]
269- pub fn lemma_cancel_bv( x: BitVec <$n>) -> Proof <{
270- hax_lib:: eq( BitVec :: [ < from_ $name >] ( BitVec :: [ <to_ $name>] ( x) ) , x)
271- // hax_lib::eq(BitVec::<$n>::from($name::from(x)), x)
272- } > { }
273215 } ;
274216 }
275217 ) *
0 commit comments