File tree 2 files changed +12
-8
lines changed
2 files changed +12
-8
lines changed Original file line number Diff line number Diff line change @@ -45,12 +45,14 @@ impl<E: Environment> FromBits for Field<E> {
45
45
// Return the field.
46
46
Ok ( Field { field : E :: Field :: from_bigint ( field) . ok_or_else ( || anyhow ! ( "Invalid field from bits" ) ) ? } )
47
47
} else {
48
- // Construct the sanitized list of bits, resizing up if necessary.
49
- let mut bits_le = bits_le. iter ( ) . take ( size_in_bits) . cloned ( ) . collect :: < Vec < _ > > ( ) ;
50
- bits_le. resize ( size_in_bits, false ) ;
48
+ // Construct the sanitized list of bits padded with `false`
49
+ let mut sanitized_bits = vec ! [ false ; size_in_bits] ;
50
+ // Note: This is safe, because we just checked that the length of bits isn't bigger
51
+ // than `size_in_data_bits` which is equal to `size_in_bits - 1`.
52
+ sanitized_bits[ ..num_bits] . copy_from_slice ( bits_le) ;
51
53
52
54
// Recover the native field.
53
- let field = E :: Field :: from_bigint ( E :: BigInteger :: from_bits_le ( & bits_le ) ?)
55
+ let field = E :: Field :: from_bigint ( E :: BigInteger :: from_bits_le ( & sanitized_bits ) ?)
54
56
. ok_or_else ( || anyhow ! ( "Invalid field from bits" ) ) ?;
55
57
56
58
// Return the field.
Original file line number Diff line number Diff line change @@ -48,12 +48,14 @@ impl<E: Environment> FromBits for Scalar<E> {
48
48
// Return the scalar.
49
49
Ok ( Scalar { scalar : E :: Scalar :: from_bigint ( scalar) . ok_or_else ( || anyhow ! ( "Invalid scalar from bits" ) ) ? } )
50
50
} else {
51
- // Construct the sanitized list of bits, resizing up if necessary.
52
- let mut bits_le = bits_le. iter ( ) . take ( size_in_bits) . cloned ( ) . collect :: < Vec < _ > > ( ) ;
53
- bits_le. resize ( size_in_bits, false ) ;
51
+ // Construct the sanitized list of bits padded with `false`
52
+ let mut sanitized_bits = vec ! [ false ; size_in_bits] ;
53
+ // Note: This is safe, because we just checked that the length of bits isn't bigger
54
+ // than `size_in_data_bits` which is equal to `size_in_bits - 1`.
55
+ sanitized_bits[ ..num_bits] . copy_from_slice ( bits_le) ;
54
56
55
57
// Recover the native scalar.
56
- let scalar = E :: Scalar :: from_bigint ( E :: BigInteger :: from_bits_le ( & bits_le ) ?)
58
+ let scalar = E :: Scalar :: from_bigint ( E :: BigInteger :: from_bits_le ( & sanitized_bits ) ?)
57
59
. ok_or_else ( || anyhow ! ( "Invalid scalar from bits" ) ) ?;
58
60
59
61
// Return the scalar.
You can’t perform that action at this time.
0 commit comments