Skip to content

Commit 0150373

Browse files
Fix/discrete log async (#131)
* fixed async issue when no event loop * clean up comments Co-authored-by: Matt Wilhelm <github@addressxception.com>
1 parent 463bf0c commit 0150373

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

src/electionguard/dlog.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
# support for computing discrete logs, with a cache so they're never recomputed
22

33
import asyncio
4-
from typing import Dict
4+
from typing import Dict, Optional
55

66
from .group import G, ElementModP, ONE_MOD_P, mult_p, int_to_p_unchecked
77

88
__dlog_cache: Dict[ElementModP, int] = {ONE_MOD_P: 0}
99
__dlog_max_elem = ONE_MOD_P
1010
__dlog_max_exp = 0
11-
__dlog_lock = asyncio.Lock()
11+
12+
__dlog_lock: Optional[asyncio.Lock] = None
1213

1314

1415
def discrete_log(e: ElementModP) -> int:
@@ -38,6 +39,10 @@ async def __discrete_log_internal(e: ElementModP) -> int:
3839
global __dlog_max_exp
3940
global __dlog_lock
4041

42+
if __dlog_lock is None:
43+
# Initialize the lock on on first function call per process
44+
__dlog_lock = asyncio.Lock()
45+
4146
async with __dlog_lock:
4247
g = int_to_p_unchecked(G)
4348
while e != __dlog_max_elem:

0 commit comments

Comments
 (0)