Skip to content

Commit d8808fb

Browse files
author
Martin Vrachev
committed
SuccinctRoles: add zero padding to bins
Add zero padding to bin names inside SuccinctRoles. Zero padding ensures that the bin names always have the same length. This characteristic is implied in the example given by TAP 15 where the third bin is named "alice.hbd-03". For context read TAP 15: https://github.com/theupdateframework/taps/blob/master/tap15.md Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
1 parent 9259ced commit d8808fb

1 file changed

Lines changed: 17 additions & 6 deletions

File tree

tuf/api/metadata.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,12 +1443,9 @@ class SuccinctRoles(Role):
14431443
in the graph.
14441444
14451445
The total number of bins is 2 to the power of the passed ``bit_length``.
1446-
Targets are assigned to bins by casting the left-most ``bit_length`` of
1447-
bits of the file path hash digest to int, using it as bin index between 0
1448-
and ``2**bit_length - 1``.
14491446
1450-
Bin names are the concatenation of the passed ``name_prefix`` and a hex
1451-
representation of the bin index between separated by a hyphen.
1447+
Bin names are the concatenation of the passed ``name_prefix`` and a
1448+
zero-padded hex representation of the bin index separated by a hyphen.
14521449
14531450
The passed ``keyids`` and ``threshold`` is used for each bin, and each bin
14541451
is 'terminating'.
@@ -1485,6 +1482,14 @@ def __init__(
14851482
self.bit_length = bit_length
14861483
self.name_prefix = name_prefix
14871484

1485+
# Calculate the suffix_len value based on the total number of bins in
1486+
# hex. If bit_length = 8 then number_of_bins = 256 or 100 in hex
1487+
# and suffix_len = 3 meaning the third bin will have a suffix of "003"
1488+
self.number_of_bins = 2**bit_length
1489+
# suffix_len is calculated based on "number_of_bins - 1" as the name
1490+
# of the last bin contains the number "number_of_bins -1" as a suffix.
1491+
self.suffix_len = len(f"{self.number_of_bins-1:x}")
1492+
14881493
def __eq__(self, other: Any) -> bool:
14891494
if not isinstance(other, SuccinctRoles):
14901495
return False
@@ -1522,6 +1527,10 @@ def get_role_for_target(self, target_filepath: str) -> str:
15221527
"""Calculates the name of the delegated role responsible for
15231528
``target_filepath``.
15241529
1530+
The target at path ``target_filepath`` is assigned to a bin by casting
1531+
the left-most ``bit_length`` of bits of the file path hash digest to
1532+
int, using it as bin index between 0 and ``2**bit_length - 1``.
1533+
15251534
Args:
15261535
target_filepath: URL path to a target file, relative to a base
15271536
targets URL.
@@ -1535,7 +1544,9 @@ def get_role_for_target(self, target_filepath: str) -> str:
15351544
# bit_length bits that we care about.
15361545
shift_value = 32 - self.bit_length
15371546
bin_number = int.from_bytes(hash_bytes, byteorder="big") >> shift_value
1538-
return f"{self.name_prefix}-{bin_number}"
1547+
# Add zero padding if necessary and cast to hex the suffix.
1548+
suffix = f"{bin_number:0{self.suffix_len}x}"
1549+
return f"{self.name_prefix}-{suffix}"
15391550

15401551

15411552
class Delegations:

0 commit comments

Comments
 (0)