Skip to content

Commit bbddd98

Browse files
committed
fix: repair merge discrepancy and adjust for linting/style
1 parent f35e0fb commit bbddd98

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed

src/nilql/nilql.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,33 +24,34 @@
2424
_PLAINTEXT_STRING_BUFFER_LEN_MAX = 4096
2525
"""Maximum length of plaintext string values that can be encrypted."""
2626

27-
_Hash = hashlib.sha512
27+
_HASH = hashlib.sha512
2828
"""Hash function used for HKDF and matching."""
2929

30-
3130
def _hkdf_extract(salt: bytes, input_key: bytes) -> bytes:
3231
"""
3332
Extracts a pseudorandom key (PRK) using HMAC with the given salt and input key material.
34-
If the salt is empty, a zero-filled byte string of the same length as the hash function's digest size is used.
33+
If the salt is empty, a zero-filled byte string of the same length as the hash function's
34+
digest size is used.
3535
"""
3636
if len(salt) == 0:
37-
salt = bytes([0] * _Hash().digest_size)
38-
return hmac.new(salt, input_key, _Hash).digest()
37+
salt = bytes([0] * _HASH().digest_size)
38+
return hmac.new(salt, input_key, _HASH).digest()
3939

4040
def _hkdf_expand(pseudo_random_key: bytes, info: bytes, length: int) -> bytes:
4141
"""
42-
Expands the pseudo_random_key into an output key material (OKM) of the desired length using HMAC-based expansion.
42+
Expands the pseudo_random_key into an output key material (OKM) of the desired length using
43+
HMAC-based expansion.
4344
"""
44-
t = b""
45-
okm = b""
45+
t = b''
46+
okm = b''
4647
i = 0
4748
while len(okm) < length:
4849
i += 1
49-
t = hmac.new(pseudo_random_key, t + info + bytes([i]), _Hash).digest()
50+
t = hmac.new(pseudo_random_key, t + info + bytes([i]), _HASH).digest()
5051
okm += t
5152
return okm[:length]
5253

53-
def _hkdf(length: int, input_key: bytes, salt: bytes = b"", info: bytes = b"") -> bytes:
54+
def _hkdf(length: int, input_key: bytes, salt: bytes = b'', info: bytes = b'') -> bytes:
5455
"""
5556
Extract a pseudorandom key of `length` from `input_key` and optionally `salt` and `info`.
5657
"""
@@ -63,7 +64,7 @@ def _random_bytes(length: int, seed: Optional[bytes] = None, salt: Optional[byte
6364
the seed if one is supplied).
6465
"""
6566
if seed is not None:
66-
return _hkdf(length, seed, b"" if salt is None else salt)
67+
return _hkdf(length, seed, b'' if salt is None else salt)
6768

6869
return secrets.token_bytes(length)
6970

@@ -93,7 +94,7 @@ def _random_int(
9394
integer = None
9495
index = 0
9596
while integer is None or integer > range_:
96-
bytes_ = bytearray(_random_bytes(8, seed, index.to_bytes(8, 'little')))
97+
bytes_ = bytearray(_random_bytes(8, seed, index.to_bytes(64, 'little')))
9798
index += 1
9899
bytes_[4] &= 1
99100
bytes_[5] &= 0
@@ -260,14 +261,18 @@ def generate(
260261
'seed-based derivation of summation-compatible keys ' +
261262
'is not supported for single-node clusters'
262263
)
263-
secret_key['material'] = pailliers.secret(2048)
264+
secret_key['material'] = pailliers.secret(256)
264265
else:
265266
# Multiplicative masks to be used on the shares for each node.
266267
secret_key['material'] = [
267268
_random_int(
268269
1,
269270
_SECRET_SHARED_SIGNED_INTEGER_MODULUS - 1,
270-
_seeds(seed, i) if seed is not None else None
271+
(
272+
_random_bytes(64, seed, i.to_bytes(64, 'little'))
273+
if seed is not None else
274+
None
275+
)
271276
)
272277
for i in range(len(secret_key['cluster']['nodes']))
273278
]
@@ -560,7 +565,7 @@ def encrypt(
560565

561566
# Encrypt (i.e., hash) a value for matching.
562567
if key['operations'].get('match'):
563-
ciphertext = _pack(_Hash(key['material'] + buffer).digest())
568+
ciphertext = _pack(_HASH(key['material'] + buffer).digest())
564569

565570
# For multiple-node clusters, prepare the same ciphertext for each.
566571
if len(key['cluster']['nodes']) > 1:

test/test_nilql.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,12 @@ def test_key_from_seed_for_store_with_multiple_nodes(self):
148148
sk_from_seed = nilql.SecretKey.generate({'nodes': [{}, {}, {}]}, {'store': True}, SEED)
149149
self.assertEqual(
150150
to_hash_base64(sk_from_seed['material']),
151-
'UEoI836rNUBdCixoavnwlPEVqAe2wrPxj+UkVpJPPo0='
151+
'2bW6BLeeCTqsCqrijSkBBPGjDb/gzjtGnFZt0nsZP8w='
152152
)
153153
sk = nilql.SecretKey.generate({'nodes': [{}, {}, {}]}, {'store': True})
154154
self.assertNotEqual(
155155
to_hash_base64(sk['material']),
156-
'UEoI836rNUBdCixoavnwlPEVqAe2wrPxj+UkVpJPPo0='
156+
'2bW6BLeeCTqsCqrijSkBBPGjDb/gzjtGnFZt0nsZP8w='
157157
)
158158

159159
def test_key_from_seed_for_match_with_single_node(self):
@@ -193,12 +193,12 @@ def test_key_from_seed_for_sum_with_multiple_nodes(self):
193193
sk_from_seed = nilql.SecretKey.generate({'nodes': [{}, {}, {}]}, {'sum': True}, SEED)
194194
self.assertEqual(
195195
to_hash_base64(sk_from_seed['material']),
196-
'l3O25x9CYiiA+XXTNPoT4WylTOXjeWj4GmoSoOPpZHo='
196+
'L8RiHNq2EUgt/fDOoUw9QK2NISeUkAkhxHHIPoHPZ84='
197197
)
198198
sk = nilql.SecretKey.generate({'nodes': [{}, {}, {}]}, {'sum': True})
199199
self.assertNotEqual(
200200
to_hash_base64(sk['material']),
201-
'l3O25x9CYiiA+XXTNPoT4WylTOXjeWj4GmoSoOPpZHo='
201+
'L8RiHNq2EUgt/fDOoUw9QK2NISeUkAkhxHHIPoHPZ84='
202202
)
203203

204204
class TestKeysError(TestCase):

0 commit comments

Comments
 (0)