@@ -90,6 +90,14 @@ impl BoxedUint {
90
90
out. into ( )
91
91
}
92
92
93
+ /// Serialize this [`BoxedUint`] as big-endian without leading zeroes.
94
+ #[ inline]
95
+ pub fn to_be_bytes_trimmed_vartime ( & self ) -> Box < [ u8 ] > {
96
+ let zeroes = self . leading_zeros ( ) as usize / 8 ;
97
+
98
+ ( & self . to_be_bytes ( ) [ zeroes..] ) . into ( )
99
+ }
100
+
93
101
/// Serialize this [`BoxedUint`] as little-endian.
94
102
#[ inline]
95
103
pub fn to_le_bytes ( & self ) -> Box < [ u8 ] > {
@@ -107,6 +115,16 @@ impl BoxedUint {
107
115
out. into ( )
108
116
}
109
117
118
+ /// Serialize this [`BoxedUint`] as little-endian without trailing zeroes.
119
+ #[ inline]
120
+ pub fn to_le_bytes_trimmed_vartime ( & self ) -> Box < [ u8 ] > {
121
+ let zeroes = self . leading_zeros ( ) as usize / 8 ;
122
+
123
+ let bytes = self . to_le_bytes ( ) ;
124
+
125
+ ( & bytes[ ..bytes. len ( ) - zeroes] ) . into ( )
126
+ }
127
+
110
128
/// Create a new [`BoxedUint`] from the provided big endian hex string.
111
129
pub fn from_be_hex ( hex : & str , bits_precision : u32 ) -> CtOption < Self > {
112
130
let nlimbs = ( bits_precision / Limb :: BITS ) as usize ;
@@ -425,13 +443,64 @@ mod tests {
425
443
assert_eq ! ( bytes. as_slice( ) , & * n. to_be_bytes( ) ) ;
426
444
}
427
445
446
+ #[ test]
447
+ fn to_be_bytes_trimmed_vartime ( ) {
448
+ let bytes = hex ! ( "ff112233445566778899aabbccddeeff" ) ;
449
+ let n = BoxedUint :: from_be_slice ( & bytes, 128 ) . unwrap ( ) ;
450
+ assert_eq ! ( & bytes, & * n. to_be_bytes_trimmed_vartime( ) ) ;
451
+
452
+ let bytes = hex ! ( "00112233445566778899aabbccddeeff" ) ;
453
+ let n = BoxedUint :: from_be_slice ( & bytes, 128 ) . unwrap ( ) ;
454
+ assert_eq ! ( & bytes. as_slice( ) [ 1 ..] , & * n. to_be_bytes_trimmed_vartime( ) ) ;
455
+
456
+ let bytes: & [ u8 ] = b"" ;
457
+ let n = BoxedUint :: from_be_slice ( bytes, 128 ) . unwrap ( ) ;
458
+ assert_eq ! (
459
+ hex!( "00000000000000000000000000000000" ) ,
460
+ n. to_be_bytes( ) . as_ref( )
461
+ ) ;
462
+ assert_eq ! ( bytes, n. to_be_bytes_trimmed_vartime( ) . as_ref( ) ) ;
463
+
464
+ let bytes = hex ! ( "00012233445566778899aabbccddeeff" ) ;
465
+ let n = BoxedUint :: from_be_slice ( & bytes, 128 ) . unwrap ( ) ;
466
+ assert_eq ! ( & bytes. as_slice( ) [ 1 ..] , & * n. to_be_bytes_trimmed_vartime( ) ) ;
467
+
468
+ let bytes = hex ! ( "00000000000000000000000000000001" ) ;
469
+ let n = BoxedUint :: from_be_slice ( & bytes, 128 ) . unwrap ( ) ;
470
+ assert_eq ! ( bytes, n. to_be_bytes( ) . as_ref( ) ) ;
471
+ assert_eq ! ( & bytes. as_slice( ) [ 15 ..] , & * n. to_be_bytes_trimmed_vartime( ) ) ;
472
+ }
473
+
428
474
#[ test]
429
475
fn to_le_bytes ( ) {
430
476
let bytes = hex ! ( "ffeeddccbbaa99887766554433221100" ) ;
431
477
let n = BoxedUint :: from_le_slice ( & bytes, 128 ) . unwrap ( ) ;
432
478
assert_eq ! ( bytes. as_slice( ) , & * n. to_le_bytes( ) ) ;
433
479
}
434
480
481
+ #[ test]
482
+ fn to_le_bytes_trimmed_vartime ( ) {
483
+ let bytes = hex ! ( "ffeeddccbbaa998877665544332211ff" ) ;
484
+ let n = BoxedUint :: from_le_slice ( & bytes, 128 ) . unwrap ( ) ;
485
+ assert_eq ! ( bytes. as_slice( ) , & * n. to_le_bytes_trimmed_vartime( ) ) ;
486
+
487
+ let bytes = hex ! ( "ffeeddccbbaa99887766554433221100" ) ;
488
+ let n = BoxedUint :: from_le_slice ( & bytes, 128 ) . unwrap ( ) ;
489
+ assert_eq ! ( & bytes. as_slice( ) [ ..15 ] , & * n. to_le_bytes_trimmed_vartime( ) ) ;
490
+
491
+ let bytes = hex ! ( "ff000000000000000000000000000000" ) ;
492
+ let n = BoxedUint :: from_le_slice ( & bytes, 128 ) . unwrap ( ) ;
493
+ assert_eq ! ( & bytes. as_slice( ) [ ..1 ] , & * n. to_le_bytes_trimmed_vartime( ) ) ;
494
+
495
+ let bytes = hex ! ( "01000000000000000000000000000000" ) ;
496
+ let n = BoxedUint :: from_le_slice ( & bytes, 128 ) . unwrap ( ) ;
497
+ assert_eq ! ( & bytes. as_slice( ) [ ..1 ] , & * n. to_le_bytes_trimmed_vartime( ) ) ;
498
+
499
+ let bytes = hex ! ( "00000000000000000000000000000000" ) ;
500
+ let n = BoxedUint :: from_le_slice ( & bytes, 128 ) . unwrap ( ) ;
501
+ assert_eq ! ( b"" , & * n. to_le_bytes_trimmed_vartime( ) ) ;
502
+ }
503
+
435
504
#[ test]
436
505
fn from_str_radix_invalid ( ) {
437
506
assert_eq ! (
0 commit comments