@@ -39,6 +39,20 @@ impl BoxedUint {
39
39
Ok ( ret)
40
40
}
41
41
42
+ /// Create a new [`BoxedUint`] from the provided big endian bytes, automatically selecting its
43
+ /// precision based on the size of the input.
44
+ ///
45
+ /// This method is variable-time with respect to all subsequent operations since it chooses the
46
+ /// limb count based on the input size, and is therefore only suitable for public inputs.
47
+ ///
48
+ /// When working with secret values, use [`BoxedUint::from_be_slice`].
49
+ pub fn from_be_slice_vartime ( bytes : & [ u8 ] ) -> Self {
50
+ let bits_precision = ( bytes. len ( ) as u32 ) . saturating_mul ( 8 ) ;
51
+
52
+ // TODO(tarcieri): avoid panic
53
+ Self :: from_be_slice ( bytes, bits_precision) . expect ( "precision should be large enough" )
54
+ }
55
+
42
56
/// Create a new [`BoxedUint`] from the provided little endian bytes.
43
57
///
44
58
/// The `bits_precision` argument represents the precision of the resulting integer, which is
@@ -72,6 +86,20 @@ impl BoxedUint {
72
86
Ok ( ret)
73
87
}
74
88
89
+ /// Create a new [`BoxedUint`] from the provided little endian bytes, automatically selecting
90
+ /// its precision based on the size of the input.
91
+ ///
92
+ /// This method is variable-time with respect to all subsequent operations since it chooses the
93
+ /// limb count based on the input size, and is therefore only suitable for public inputs.
94
+ ///
95
+ /// When working with secret values, use [`BoxedUint::from_le_slice`].
96
+ pub fn from_le_slice_vartime ( bytes : & [ u8 ] ) -> Self {
97
+ let bits_precision = ( bytes. len ( ) as u32 ) . saturating_mul ( 8 ) ;
98
+
99
+ // TODO(tarcieri): avoid panic
100
+ Self :: from_le_slice ( bytes, bits_precision) . expect ( "precision should be large enough" )
101
+ }
102
+
75
103
/// Serialize this [`BoxedUint`] as big-endian.
76
104
#[ inline]
77
105
pub fn to_be_bytes ( & self ) -> Box < [ u8 ] > {
@@ -343,6 +371,15 @@ mod tests {
343
371
) ;
344
372
}
345
373
374
+ #[ test]
375
+ fn from_be_slice_vartime ( ) {
376
+ let bytes = hex ! (
377
+ "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111F"
378
+ ) ;
379
+ let uint = BoxedUint :: from_be_slice_vartime ( & bytes) ;
380
+ assert_eq ! ( & * uint. to_be_bytes_trimmed_vartime( ) , bytes. as_slice( ) ) ;
381
+ }
382
+
346
383
#[ test]
347
384
#[ cfg( target_pointer_width = "32" ) ]
348
385
fn from_le_slice_eq ( ) {
@@ -436,6 +473,15 @@ mod tests {
436
473
) ;
437
474
}
438
475
476
+ #[ test]
477
+ fn from_le_slice_vartime ( ) {
478
+ let bytes = hex ! (
479
+ "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111F"
480
+ ) ;
481
+ let uint = BoxedUint :: from_le_slice_vartime ( & bytes) ;
482
+ assert_eq ! ( & * uint. to_le_bytes_trimmed_vartime( ) , bytes. as_slice( ) ) ;
483
+ }
484
+
439
485
#[ test]
440
486
fn to_be_bytes ( ) {
441
487
let bytes = hex ! ( "00112233445566778899aabbccddeeff" ) ;
0 commit comments