Skip to content

Commit 49daf42

Browse files
committed
Improved some methods; fixed some comments
1 parent 6984eee commit 49daf42

File tree

3 files changed

+19
-22
lines changed

3 files changed

+19
-22
lines changed

tvl/crypto/hash.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Copyright 2023 TropicSquare
22
# SPDX-License-Identifier: Apache-2.0
33

4+
# type: ignore
5+
46
import crcmod.predefined
57

68
_crc16 = crcmod.predefined.mkPredefinedCrcFun("crc-16-buypass")

tvl/crypto/keccak.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from itertools import chain, product
66
from math import log2
77
from operator import xor
8-
from typing import List, Protocol, Reversible, Sequence
8+
from typing import List, Literal, Protocol, Reversible, Sequence
99

1010
from ..utils import chunked
1111
from .conversion import bit, bitlist_to_bytes, ints_to_bitlist
@@ -95,14 +95,13 @@ def __call__(self, s: List[List[int]]) -> None:
9595

9696

9797
class keccak_p:
98-
def __init__(self, b: int, nr: int) -> None:
98+
def __init__(self, b: Literal[25, 50, 100, 200, 400, 800, 1600], nr: int) -> None:
9999
"""Keccak-p permutation function as defined in NIST.FIPS.202 3.3
100100
101101
Args:
102-
b (int): width of the permutation
102+
b (Literal[25, 50, 100, 200, 400, 800, 1600]): width of the permutation
103103
nr (int): number of rounds
104104
"""
105-
assert b in {25, 50, 100, 200, 400, 800, 1600}
106105
self.b = b
107106
self.nr = nr
108107
self._W = self.b // 25
@@ -140,11 +139,11 @@ def __call__(self, s: List[List[int]]) -> None:
140139
self.round(s, ir)
141140

142141

143-
def keccak_f(b: int):
142+
def keccak_f(b: Literal[25, 50, 100, 200, 400, 800, 1600]):
144143
"""Keccak-f permutation function as defined in NIST.FIPS.202 3.4
145144
146145
Args:
147-
b (int): width of the permutation
146+
b (Literal[25, 50, 100, 200, 400, 800, 1600]): width of the permutation
148147
149148
Returns:
150149
the permutation function keccak-f
@@ -289,11 +288,11 @@ def pad10s1(x: int, m: int) -> List[bit]:
289288
return [bit(1)] + [bit(0)] * j + [bit(1)]
290289

291290

292-
def keccak(b: int):
293-
"""KECCAK sponge as defined in NIST.FIPS.202 6.2
291+
def keccak(b: Literal[25, 50, 100, 200, 400, 800, 1600]):
292+
"""KECCAK sponge as defined in NIST.FIPS.202 5.2
294293
295294
Args:
296-
b (int): width of the permutation
295+
b (Literal[25, 50, 100, 200, 400, 800, 1600]): width of the permutation
297296
"""
298297

299298
def call(r: int):
@@ -303,7 +302,7 @@ def call(r: int):
303302

304303

305304
def keccak_c(c: int):
306-
"""KECCAK[c] sponge as defined in NIST.FIPS.202 6.2
305+
"""KECCAK[c] sponge as defined in NIST.FIPS.202 5.2
307306
308307
Args:
309308
c (int): capacity

tvl/messages/message.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,8 @@ class _MetaMessage(type):
4646
"""
4747
Metaclass of the Message class.
4848
49-
Make sure the field names are unique and move the DataField objects to the
50-
class field _FIELD_MODELS so they can used during the instantiation of
51-
a new Message object.
49+
Make sure the field names are unique
50+
and add parameters to the DataField objects
5251
"""
5352

5453
def __new__(
@@ -101,19 +100,16 @@ def specs(cls) -> List[Tuple[str, Type[DataField[Any]], Params]]:
101100
return sorted(_get_specs(cls), key=lambda x: x[2].priority)
102101

103102
def __init__(self, **kwargs: Any) -> None:
104-
for name, type_, params_ in self.specs():
105-
if (value := kwargs.get(name)) is None:
106-
value = params_.default
107-
setattr(self, name, type_(value=value, params=params_))
103+
for name, type_, params in self.specs():
104+
value = kwargs.get(name, params.default)
105+
setattr(self, name, type_(value=value, params=params))
108106

109107
def __eq__(self, __other: Any) -> bool:
110108
if __other is self:
111109
return True
112-
try:
113-
other_to_bytes = __other.to_bytes()
114-
except AttributeError:
115-
return NotImplemented
116-
return other_to_bytes == self.to_bytes()
110+
if isinstance(__other, BaseMessage):
111+
return __other.to_bytes() == self.to_bytes()
112+
return NotImplemented
117113

118114
def __iter__(self) -> Iterator[Tuple[str, DataField[Any]]]:
119115
yield from self.__dict__.items()

0 commit comments

Comments
 (0)