Skip to content

Commit

Permalink
linting + improv Trevisan from_params code
Browse files Browse the repository at this point in the history
  • Loading branch information
cforeman-cqc committed Dec 24, 2023
1 parent 87909bb commit 6c83ea8
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 43 deletions.
4 changes: 2 additions & 2 deletions cryptomite/circulant.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ def from_params(
The Circulant extractor.
"""
if log2_error >= 0:
raise Exception('Cannot extract with these parameters.'
'log2_error must be < 0.')
raise Exception('''Cannot extract with these parameters.
log2_error must be < 0.''')
input_length = closest_prime((input_length1 + input_length2)//2)
if input_length1 > input_length - 1:
min_entropy1 -= input_length1 - input_length
Expand Down
6 changes: 3 additions & 3 deletions cryptomite/dodis.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def from_params(
given the initial lengths and min-entropies of the input sources
and generate the associated extractor.
The input_length must be prime with primitive root 2, else
The input_length must be prime with primitive root 2, else
the code will chose a valid input_length choice and adjust the
other parameters accordingly.
The min entropy inputs are a lower bound on the
Expand Down Expand Up @@ -99,8 +99,8 @@ def from_params(
The Dodis extractor.
"""
if log2_error >= 0:
raise Exception('Cannot extract with these parameters.'
'log2_error must be < 0.')
raise Exception('''Cannot extract with these parameters.
log2_error must be < 0.''')
input_length = closest_na_set((input_length1 + input_length2)//2)
if input_length1 > input_length:
min_entropy1 -= input_length1 - input_length
Expand Down
20 changes: 10 additions & 10 deletions cryptomite/toeplitz.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ def from_params(
"""
Calculate a valid input and output size for this extractor,
given the initial lengths and min-entropies of the input sources
and generate the associated extractor.
and generate the associated extractor.
The min entropy inputs are a lower bound on the :term:`min-entropy`
of the related input string.
The min entropy inputs are a lower bound on the
:term:`min-entropy` of the related input string.
Parameters
----------
Expand All @@ -95,12 +95,12 @@ def from_params(
The Toeplitz extractor.
"""
if log2_error > 0:
raise Exception('Cannot extract with these parameters.'
'log2_error must be < 0.')
raise Exception('''Cannot extract with these parameters.
log2_error must be < 0.''')
if input_length2 <= input_length1:
raise Exception('Cannot extract with these parameters.'
'Increase the seed length (input_length2).'
'The seed must be longer than the input.')
raise Exception('''Cannot extract with these parameters.
Increase the seed length (input_length2).
The seed must be longer than the input.''')
if min_entropy2 >= input_length2:
output_length = min_entropy1 + 2 * log2_error
if input_length2 >= output_length + input_length1 - 1:
Expand Down Expand Up @@ -134,8 +134,8 @@ def from_params(
while input_length2 < output_length + input_length1 - 1:
output_length -= 1
if output_length <= 0:
raise Exception('Cannot extract with these parameters. '
'''Increase min_entropy1 and/or min_entropy2
raise Exception('''Cannot extract with these parameters.
Increase min_entropy1 and/or min_entropy2
and/or log2_error.''')
if verbose:
print('Min entropy1: ', min_entropy1,
Expand Down
50 changes: 22 additions & 28 deletions cryptomite/trevisan.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

__all__ = ['Trevisan']

from math import ceil, log, log2, exp, floor
from math import ceil, exp, floor, log, log2

from cryptomite import _cryptomite

Expand Down Expand Up @@ -72,10 +72,11 @@ def from_params(
given the initial lengths and min-entropies of the input sources
and generate the associated extractor.
*** Note: at present, this function (for Trevisan) only supports when
input_length2 = min_entropy2, i.e. when the seed is uniform. ***
The min entropy inputs are a lower bound on the :term:`min-entropy`
of the related input string.
*** Note: at present, this function (for Trevisan) only supports
when input_length2 = min_entropy2, i.e. when the seed is
uniform. ***
The min entropy inputs are a lower bound on the
:term:`min-entropy` of the related input string.
Parameters
----------
Expand All @@ -101,11 +102,11 @@ def from_params(
The Trevisan extractor.
"""
if log2_error > 0:
raise Exception('Cannot extract with these parameters.'
'log2_error must be < 0.')
raise Exception('''Cannot extract with these parameters.
log2_error must be < 0.''')
if min_entropy2 != input_length2:
raise Exception('Cannot calcuate with these parameters.'
'Set min_entropy2 = input_length2.')
raise Exception('''Cannot calcuate with these parameters.
Set min_entropy2 = input_length2.''')
r = 2 * exp(1)
m = min_entropy1 + 4 * log2_error - 6
output_length = floor(min_entropy1 + 4 * log2_error
Expand All @@ -114,26 +115,19 @@ def from_params(
- 2 * log2_error + 2 * log2(2 * output_length))
a = max(1, ceil((log(output_length - r) - log(t - r))
/ (log(r) - log(r-1))))
if input_length2 >= 4 * a * t**2:
input_length2 = 4 * a * t**2
if input_length2 < 4 * a * t**2:
raise Exception('Cannot extract with these parameters.'
'Increase input_length2.')
if markov_q_proof:
if min_entropy2 >= input_length2:
m = (1/7) * (min_entropy1 + 6 - 6 * log2(3) +
12 * log2_error)
output_length = floor((1/7) * (min_entropy1 + 6 - 6 * log2(3) +
12 * log2_error - 4 * log2(m)))
t = 2 * ceil(log2(input_length1) + 1
- 2 * log2_error + 2 * log2(2 * output_length))
a = max(1, ceil((log(output_length - r) - log(t - r))
/ (log(r) - log(r-1))))
if input_length2 >= 4 * a * t**2:
input_length2 = 4 * a * t**2
if input_length2 < 4 * a * t**2:
raise Exception('Cannot extract with these parameters.'
'Increase input_length2.')
m = (1/7) * (min_entropy1 + 6 - 6 * log2(3) +
12 * log2_error)
output_length = floor((1/7) * (min_entropy1 + 6 - 6 * log2(3) +
12 * log2_error - 12 * log2(m)))
t = 2 * ceil(log2(input_length1) + 1
- 2 * log2_error + 2 * log2(2 * output_length))
a = max(1, ceil((log(output_length - r) - log(t - r))
/ (log(r) - log(r-1))))
if input_length2 < 4 * a * t**2:
raise Exception('''Cannot extract with these parameters.
Increase input_length2.''')
input_length2 = 4 * a * t**2
if verbose:
print('Min entropy1: ', min_entropy1,
'Min entropy2: ', min_entropy2,
Expand Down

0 comments on commit 6c83ea8

Please sign in to comment.