Skip to content

Commit

Permalink
They're not really predicate methods when the intention is to raise a…
Browse files Browse the repository at this point in the history
…nd fail fast.
  • Loading branch information
grempe committed Apr 13, 2016
1 parent 5dad23e commit 8865a5a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
28 changes: 14 additions & 14 deletions lib/tss/combiner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ def combine
when 'sample'
@shares = shares.sample(threshold)
when 'combinations'
share_combinations_mode_allowed?(hash_id)
share_combinations_out_of_bounds?(shares, threshold)
share_combinations_mode_allowed!(hash_id)
share_combinations_out_of_bounds!(shares, threshold)
end
end

Expand All @@ -132,7 +132,7 @@ def combine
bytestring.unpack('C*') unless bytestring.nil?
end.compact

shares_bytes_have_valid_indexes?(shares_bytes)
shares_bytes_have_valid_indexes!(shares_bytes)

if select_by == 'combinations'
# Build an Array of all possible `threshold` size combinations.
Expand Down Expand Up @@ -222,15 +222,15 @@ def valid_header?(header)
header[:share_len].is_a?(Integer)
end

def shares_have_same_bytesize?(shares)
def shares_have_same_bytesize!(shares)
shares.each do |s|
unless s.bytesize == shares.first.bytesize
raise TSS::ArgumentError, 'invalid shares, different byte lengths'
end
end
end

def shares_have_valid_headers?(shares)
def shares_have_valid_headers!(shares)
fh = Util.extract_share_header(shares.first)
shares.each do |s|
h = Util.extract_share_header(s)
Expand All @@ -240,29 +240,29 @@ def shares_have_valid_headers?(shares)
end
end

def shares_have_expected_length?(shares)
def shares_have_expected_length!(shares)
shares.each do |s|
unless s.bytesize > Splitter::SHARE_HEADER_STRUCT.size + 1
raise TSS::ArgumentError, 'invalid shares, too short'
end
end
end

def shares_meet_threshold_min?(shares)
def shares_meet_threshold_min!(shares)
fh = Util.extract_share_header(shares.first)
unless shares.size >= fh[:threshold]
raise TSS::ArgumentError, 'invalid shares, fewer than threshold'
end
end

def validate_all_shares(shares)
shares_have_valid_headers?(shares)
shares_have_same_bytesize?(shares)
shares_have_expected_length?(shares)
shares_meet_threshold_min?(shares)
shares_have_valid_headers!(shares)
shares_have_same_bytesize!(shares)
shares_have_expected_length!(shares)
shares_meet_threshold_min!(shares)
end

def shares_bytes_have_valid_indexes?(shares_bytes)
def shares_bytes_have_valid_indexes!(shares_bytes)
u = shares_bytes.collect do |s|
raise TSS::ArgumentError, 'invalid shares, no index' if s[0].blank?
raise TSS::ArgumentError, 'invalid shares, zero index' if s[0] == 0
Expand All @@ -274,13 +274,13 @@ def shares_bytes_have_valid_indexes?(shares_bytes)
end
end

def share_combinations_mode_allowed?(hash_id)
def share_combinations_mode_allowed!(hash_id)
unless Hasher.codes_without_none.include?(hash_id)
raise TSS::ArgumentError, 'invalid options, combinations mode can only be used with hashed shares.'
end
end

def share_combinations_out_of_bounds?(shares, threshold, max_combinations = 1_000_000)
def share_combinations_out_of_bounds!(shares, threshold, max_combinations = 1_000_000)
# Raise if the number of combinations is too high.
# If this is not checked, the number of combinations can quickly grow into
# numbers that cannot be calculated before the end of the universe.
Expand Down
16 changes: 8 additions & 8 deletions lib/tss/splitter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ class Splitter < Dry::Types::Struct
# `TSS::Error` or `TSS::ArgumentError` exceptions if anything has gone wrong.
#
def split
secret_has_acceptable_encoding(secret)
secret_does_not_begin_with_padding_char(secret)
num_shares_not_less_than_threshold(threshold, num_shares)
secret_has_acceptable_encoding!(secret)
secret_does_not_begin_with_padding_char!(secret)
num_shares_not_less_than_threshold!(threshold, num_shares)

# RTSS : Combine the secret with a hash digest before splitting. On recombine
# the two will be separated again and the hash used to validate the
Expand All @@ -102,7 +102,7 @@ def split
hashed_secret = Hasher.byte_array(hash_alg, secret)
secret_bytes = Util.utf8_to_bytes(padded_secret) + hashed_secret

secret_bytes_is_smaller_than_max_size(secret_bytes)
secret_bytes_is_smaller_than_max_size!(secret_bytes)

# For each share, a distinct Share Index is generated. Each Share
# Index is an octet other than the all-zero octet. All of the Share
Expand Down Expand Up @@ -156,25 +156,25 @@ def split

private

def secret_has_acceptable_encoding(secret)
def secret_has_acceptable_encoding!(secret)
unless secret.encoding.name == 'UTF-8' || secret.encoding.name == 'US-ASCII'
raise TSS::ArgumentError, "invalid secret, must be a UTF-8 or US-ASCII encoded String not '#{secret.encoding.name}'"
end
end

def secret_does_not_begin_with_padding_char(secret)
def secret_does_not_begin_with_padding_char!(secret)
if secret.slice(0) == "\u001F"
raise TSS::ArgumentError, 'invalid secret, first byte of secret is the reserved left-pad character (\u001F)'
end
end

def num_shares_not_less_than_threshold(threshold, num_shares)
def num_shares_not_less_than_threshold!(threshold, num_shares)
if num_shares < threshold
raise TSS::ArgumentError, "invalid num_shares, must be >= threshold (#{threshold})"
end
end

def secret_bytes_is_smaller_than_max_size(secret_bytes)
def secret_bytes_is_smaller_than_max_size!(secret_bytes)
if secret_bytes.size >= 65_535
raise TSS::ArgumentError, 'invalid secret, combined padded secret and hash are too large'
end
Expand Down

0 comments on commit 8865a5a

Please sign in to comment.