diff options
| author | J08nY | 2020-02-11 20:44:45 +0100 |
|---|---|---|
| committer | J08nY | 2020-02-11 20:44:45 +0100 |
| commit | 11bd56b296f1620932f098a6037f0807e7f6616f (patch) | |
| tree | 2a791114a710ab49af523cf1ba2144646ef9ad90 /pyecsca/ec/key_generation.py | |
| parent | 4e2bd346baf2db39391deb49e9bdb9d89f94101a (diff) | |
| download | pyecsca-11bd56b296f1620932f098a6037f0807e7f6616f.tar.gz pyecsca-11bd56b296f1620932f098a6037f0807e7f6616f.tar.zst pyecsca-11bd56b296f1620932f098a6037f0807e7f6616f.zip | |
Diffstat (limited to 'pyecsca/ec/key_generation.py')
| -rw-r--r-- | pyecsca/ec/key_generation.py | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/pyecsca/ec/key_generation.py b/pyecsca/ec/key_generation.py new file mode 100644 index 0000000..0813dbd --- /dev/null +++ b/pyecsca/ec/key_generation.py @@ -0,0 +1,44 @@ +from typing import Tuple + +from public import public + +from .context import Action +from .mod import Mod +from .mult import ScalarMultiplier +from .params import DomainParameters +from .point import Point + + +@public +class KeygenAction(Action): + """A key generation.""" + params: DomainParameters + + def __init__(self, params: DomainParameters): + super().__init__() + self.params = params + + def __repr__(self): + return f"{self.__class__.__name__}({self.params})" + + +@public +class KeyGeneration(object): + """Key generator.""" + mult: ScalarMultiplier + params: DomainParameters + affine: bool + + def __init__(self, mult: ScalarMultiplier, params: DomainParameters, affine: bool = False): + self.mult = mult + self.params = params + self.mult.init(self.params, self.params.generator) + self.affine = affine + + def generate(self) -> Tuple[Mod, Point]: + with KeygenAction(self.params): + privkey = Mod.random(self.params.order) + pubkey = self.mult.multiply(privkey.x) + if self.affine: + pubkey = pubkey.to_affine() + return privkey, pubkey |
