Skip to content

Commit 9c2fdf1

Browse files
committed
Add type annotations to method/function signatures.
1 parent bb605db commit 9c2fdf1

File tree

1 file changed

+27
-21
lines changed

1 file changed

+27
-21
lines changed

src/tinybook/tinybook.py

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
`protocol <https://eprint.iacr.org/2023/1740>`__.
55
"""
66
from __future__ import annotations
7-
from typing import List
7+
from typing import Optional, Dict, List, Tuple, Sequence, Iterable
88
import doctest
9+
from modulo import modulo
910
import tinynmc
1011

1112
class node:
@@ -27,8 +28,8 @@ class node:
2728
>>> preprocess(nodes, prices=16)
2829
2930
A requests must be submitted for the opportunity to submit an order. The
30-
clients can create :obj:`request` instances for this purpose. Below, the
31-
two clients each create their request.
31+
clients can create :obj:`request` instances for this purpose. Below, two
32+
clients each create their request.
3233
3334
>>> request_ask = request.ask()
3435
>>> request_bid = request.bid()
@@ -74,7 +75,7 @@ class node:
7475
>>> reveal(shares) is None
7576
True
7677
"""
77-
def __init__(self):
78+
def __init__(self: node):
7879
"""
7980
Create a node instance and initialize its private attributes.
8081
"""
@@ -83,9 +84,9 @@ def __init__(self):
8384
self._nodes: List[tinynmc.node] = None
8485

8586
def masks( # pylint: disable=redefined-outer-name
86-
self,
87-
request
88-
):
87+
self: node,
88+
request: Iterable[Tuple[int, int]]
89+
) -> List[Dict[Tuple[int, int], modulo]]:
8990
"""
9091
Return masks for a given request.
9192
@@ -96,27 +97,27 @@ def masks( # pylint: disable=redefined-outer-name
9697
for i in range(self._prices)
9798
]
9899

99-
def outcome(self, ask, bid):
100+
def outcome(self: node, ask: Sequence[order], bid: Sequence[order]) -> List[modulo]:
100101
"""
101102
Perform computation to determine a share of the overall workflow
102103
outcome that represents the bid-ask spread.
103104
104105
:param votes: Sequence of masked orders.
105106
"""
106-
orders = [ask, bid]
107-
prices = len(ask)
107+
orders: List[Sequence[order]] = [ask, bid]
108+
prices: int = len(ask)
108109
return [ # pylint: disable=unsubscriptable-object
109110
self._nodes[i].compute(self._signature, [order[i] for order in orders])
110111
for i in range(prices)
111112
]
112113

113-
class request(list):
114+
class request(List[Tuple[int, int]]):
114115
"""
115116
Data structure for representing a request to submit an order. A request can
116117
be submitted to each node to obtain corresponding masks for an order.
117118
"""
118119
@staticmethod
119-
def ask():
120+
def ask() -> request:
120121
"""
121122
Create a request to submit an ask order.
122123
@@ -126,7 +127,7 @@ def ask():
126127
return request([(0, 0), (1, 0)])
127128

128129
@staticmethod
129-
def bid():
130+
def bid() -> request:
130131
"""
131132
Create a request to submit a bid order.
132133
@@ -156,13 +157,18 @@ class order(list):
156157
>>> isinstance(order(masks, price), order)
157158
True
158159
"""
159-
def __init__(self, masks, price):
160+
def __init__(
161+
self: order,
162+
masks: List[List[Dict[Tuple[int, int], modulo]]],
163+
price: int
164+
):
160165
"""
161166
Create a masked order that can be broadcast to nodes.
162167
"""
163-
prices = len(masks[0])
168+
prices: int = len(masks[0])
164169
for i in range(prices):
165170
masks_i = [mask[i] for mask in masks]
171+
166172
coordinate_to_value = {}
167173
kind = list(masks_i[0].keys())[0][1]
168174
for key in masks_i[0]:
@@ -172,7 +178,7 @@ def __init__(self, masks, price):
172178

173179
self.append(tinynmc.masked_factors(coordinate_to_value, masks_i))
174180

175-
def preprocess(nodes, prices):
181+
def preprocess(nodes: Sequence[node], prices: int):
176182
"""
177183
Simulate a preprocessing workflow among the supplied nodes for a workflow
178184
that supports the specified number of distinct prices (where prices are
@@ -188,7 +194,7 @@ def preprocess(nodes, prices):
188194
>>> preprocess(nodes, prices=16)
189195
"""
190196
# pylint: disable=protected-access
191-
signature = [2, 1, 1]
197+
signature: List[int] = [2, 1, 1]
192198

193199
for node_ in nodes:
194200
node_._signature = signature
@@ -198,7 +204,7 @@ def preprocess(nodes, prices):
198204
for i in range(prices):
199205
tinynmc.preprocess(signature, [node_._nodes[i] for node_ in nodes])
200206

201-
def reveal(shares):
207+
def reveal(shares: List[List[modulo]]) -> Optional[range]:
202208
"""
203209
Reconstruct the overall workflow outcome (representing the bid-ask spread)
204210
from the shares obtained from each node.
@@ -238,8 +244,8 @@ def reveal(shares):
238244
>>> reveal(shares)
239245
range(1, 3)
240246
"""
241-
prices = len(shares[0])
242-
result = [
247+
prices: int = len(shares[0])
248+
result: List[int] = [
243249
int(sum(share[i] for share in shares) + 2) - 1
244250
for i in range(prices)
245251
]
@@ -248,7 +254,7 @@ def reveal(shares):
248254
return None
249255

250256
result.append(0)
251-
ask = result.index(1)
257+
ask: int = result.index(1)
252258
return range(ask, ask + result[ask + 1:].index(0) + 1)
253259

254260
if __name__ == '__main__':

0 commit comments

Comments
 (0)