|
| 1 | +# pylint: disable=global-statement |
| 2 | +# support for computing discrete logs, with a cache so they're never recomputed |
| 3 | + |
| 4 | +import asyncio |
| 5 | +from typing import Dict, Tuple |
| 6 | + |
| 7 | +from electionguard.singleton import Singleton |
| 8 | + |
| 9 | +from .group import G, ElementModP, ONE_MOD_P, mult_p, int_to_p_unchecked |
| 10 | + |
| 11 | +DLOG_CACHE = Dict[ElementModP, int] |
| 12 | +DLOG_MAX = 100_000_000 |
| 13 | +"""The max number to calculate. This value is used to stop a race condition.""" |
| 14 | + |
| 15 | + |
| 16 | +def discrete_log(element: ElementModP, cache: DLOG_CACHE) -> Tuple[int, DLOG_CACHE]: |
| 17 | + """ |
| 18 | + Computes the discrete log (base g, mod p) of the given element, |
| 19 | + with internal caching of results. Should run efficiently when called |
| 20 | + multiple times when the exponent is at most in the single-digit millions. |
| 21 | + Performance will degrade if it's much larger. |
| 22 | +
|
| 23 | + For the best possible performance, |
| 24 | + pre-compute the discrete log of a number you expect to have the biggest |
| 25 | + exponent you'll ever see. After that, the cache will be fully loaded, |
| 26 | + and every call will be nothing more than a dictionary lookup. |
| 27 | + """ |
| 28 | + |
| 29 | + if element in cache: |
| 30 | + return (cache[element], cache) |
| 31 | + |
| 32 | + cache = compute_discrete_log_cache(element, cache) |
| 33 | + return (cache[element], cache) |
| 34 | + |
| 35 | + |
| 36 | +async def discrete_log_async( |
| 37 | + element: ElementModP, |
| 38 | + cache: DLOG_CACHE, |
| 39 | + mutex: asyncio.Lock = asyncio.Lock(), |
| 40 | +) -> Tuple[int, DLOG_CACHE]: |
| 41 | + """ |
| 42 | + Computes the discrete log (base g, mod p) of the given element, |
| 43 | + with internal caching of results. Should run efficiently when called |
| 44 | + multiple times when the exponent is at most in the single-digit millions. |
| 45 | + Performance will degrade if it's much larger. |
| 46 | +
|
| 47 | + Note: *this function is thread-safe*. For the best possible performance, |
| 48 | + pre-compute the discrete log of a number you expect to have the biggest |
| 49 | + exponent you'll ever see. After that, the cache will be fully loaded, |
| 50 | + and every call will be nothing more than a dictionary lookup. |
| 51 | + """ |
| 52 | + if element in cache: |
| 53 | + return (cache[element], cache) |
| 54 | + |
| 55 | + async with mutex: |
| 56 | + if element in cache: |
| 57 | + return (cache[element], cache) |
| 58 | + |
| 59 | + cache = compute_discrete_log_cache(element, cache) |
| 60 | + return (cache[element], cache) |
| 61 | + |
| 62 | + |
| 63 | +def compute_discrete_log_cache( |
| 64 | + element: ElementModP, cache: DLOG_CACHE |
| 65 | +) -> Dict[ElementModP, int]: |
| 66 | + """ |
| 67 | + Compute a discrete log cache up to the specified element. |
| 68 | + """ |
| 69 | + if not cache: |
| 70 | + cache = {ONE_MOD_P: 0} |
| 71 | + max_element = list(cache)[-1] |
| 72 | + exponent = cache[max_element] |
| 73 | + |
| 74 | + g = int_to_p_unchecked(G) |
| 75 | + while element != max_element: |
| 76 | + exponent = exponent + 1 |
| 77 | + if exponent > DLOG_MAX: |
| 78 | + raise ValueError("size is larger than max.") |
| 79 | + max_element = mult_p(g, max_element) |
| 80 | + cache[max_element] = exponent |
| 81 | + print(f"max: {max_element}, exp: {exponent}") |
| 82 | + return cache |
| 83 | + |
| 84 | + |
| 85 | +class DiscreteLog(Singleton): |
| 86 | + """ |
| 87 | + A class instance of the discrete log that includes a cache. |
| 88 | + """ |
| 89 | + |
| 90 | + _cache: DLOG_CACHE = {ONE_MOD_P: 0} |
| 91 | + _mutex = asyncio.Lock() |
| 92 | + |
| 93 | + def discrete_log(self, element: ElementModP) -> int: |
| 94 | + (result, cache) = discrete_log(element, self._cache) |
| 95 | + self._cache = cache |
| 96 | + return result |
| 97 | + |
| 98 | + async def discrete_log_async(self, element: ElementModP) -> int: |
| 99 | + (result, cache) = await discrete_log_async(element, self._cache, self._mutex) |
| 100 | + self._cache = cache |
| 101 | + return result |
0 commit comments