4
4
//!
5
5
//! [BCH]: <https://en.wikipedia.org/wiki/BCH_code>
6
6
7
- #[ cfg( all( feature = "alloc" , not( feature = "std" ) , not ( test ) ) ) ]
7
+ #[ cfg( all( feature = "alloc" , not( feature = "std" ) ) ) ]
8
8
use alloc:: vec:: Vec ;
9
- #[ cfg( feature = "alloc" ) ]
10
- use core:: fmt;
11
- #[ cfg( feature = "alloc" ) ]
12
9
use core:: marker:: PhantomData ;
13
- use core:: { mem, ops} ;
10
+ use core:: { fmt , mem, ops} ;
14
11
15
- #[ cfg( feature = "alloc" ) ]
16
12
use super :: Polynomial ;
17
13
use crate :: primitives:: hrp:: Hrp ;
18
- #[ cfg( feature = "alloc" ) ]
19
- use crate :: Fe1024 ;
20
- use crate :: Fe32 ;
14
+ use crate :: { Fe1024 , Fe32 } ;
21
15
22
16
/// Trait defining a particular checksum.
23
17
///
@@ -169,18 +163,16 @@ pub trait Checksum {
169
163
/// to compute the values yourself. The reason is that the specific values
170
164
/// used depend on the representation of extension fields, which may differ
171
165
/// between implementations (and between specifications) of your BCH code.
172
- #[ cfg( feature = "alloc" ) ]
173
166
pub struct PrintImpl < ' a , ExtField = Fe1024 > {
174
167
name : & ' a str ,
175
- generator : & ' a [ Fe32 ] ,
168
+ generator : Polynomial < Fe32 > ,
176
169
target : & ' a [ Fe32 ] ,
177
170
bit_len : usize ,
178
171
hex_width : usize ,
179
172
midstate_repr : & ' static str ,
180
173
phantom : PhantomData < ExtField > ,
181
174
}
182
175
183
- #[ cfg( feature = "alloc" ) ]
184
176
impl < ' a , ExtField > PrintImpl < ' a , ExtField > {
185
177
/// Constructor for an object to print an impl-block for the [`Checksum`] trait.
186
178
///
@@ -225,7 +217,7 @@ impl<'a, ExtField> PrintImpl<'a, ExtField> {
225
217
// End sanity checks.
226
218
PrintImpl {
227
219
name,
228
- generator,
220
+ generator : Polynomial :: with_monic_leading_term ( generator ) ,
229
221
target,
230
222
bit_len,
231
223
hex_width,
@@ -235,23 +227,16 @@ impl<'a, ExtField> PrintImpl<'a, ExtField> {
235
227
}
236
228
}
237
229
238
- #[ cfg( feature = "alloc" ) ]
239
230
impl < ' a , ExtField > fmt:: Display for PrintImpl < ' a , ExtField >
240
231
where
241
232
ExtField : super :: Bech32Field + super :: ExtensionField < BaseField = Fe32 > ,
242
233
{
243
234
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
244
235
// Generator polynomial as a polynomial over GF1024
245
- let gen_poly = {
246
- let mut v = Vec :: with_capacity ( self . generator . len ( ) + 1 ) ;
247
- v. push ( ExtField :: ONE ) ;
248
- v. extend ( self . generator . iter ( ) . cloned ( ) . map ( ExtField :: from) ) ;
249
- Polynomial :: new ( v)
250
- } ;
251
- let ( gen, length, exponents) = gen_poly. bch_generator_primitive_element ( ) ;
236
+ let ( gen, length, exponents) = self . generator . bch_generator_primitive_element :: < ExtField > ( ) ;
252
237
253
238
write ! ( f, "// Code block generated by Checksum::print_impl polynomial " ) ?;
254
- for fe in self . generator {
239
+ for fe in self . generator . as_inner ( ) {
255
240
write ! ( f, "{}" , fe) ?;
256
241
}
257
242
write ! ( f, " target " ) ?;
@@ -278,9 +263,9 @@ where
278
263
) ?;
279
264
f. write_str ( "\n " ) ?;
280
265
writeln ! ( f, " const CODE_LENGTH: usize = {};" , length) ?;
281
- writeln ! ( f, " const CHECKSUM_LENGTH: usize = {};" , gen_poly . degree( ) ) ?;
266
+ writeln ! ( f, " const CHECKSUM_LENGTH: usize = {};" , self . generator . degree( ) ) ?;
282
267
writeln ! ( f, " const GENERATOR_SH: [{}; 5] = [" , self . midstate_repr) ?;
283
- let mut gen5 = self . generator . to_vec ( ) ;
268
+ let mut gen5 = self . generator . clone ( ) . into_inner ( ) ;
284
269
for _ in 0 ..5 {
285
270
let gen_packed = u128:: pack ( gen5. iter ( ) . copied ( ) . map ( From :: from) ) ;
286
271
writeln ! ( f, " 0x{:0width$x}," , gen_packed, width = self . hex_width) ?;
0 commit comments