File: djbec.py

package info (click to toggle)
python-axolotl 0.2.3-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 592 kB
  • sloc: python: 2,962; makefile: 3
file content (65 lines) | stat: -rw-r--r-- 1,487 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# -*- coding: utf-8 -*-

import struct

from .ec import ECPublicKey, ECPrivateKey
from ..util.byteutil import ByteUtil


class DjbECPublicKey(ECPublicKey):

    def __init__(self, publicKey):
        self.publicKey = publicKey

    def serialize(self):
        from .curve import Curve

        combined = ByteUtil.combine([Curve.DJB_TYPE], self.publicKey)
        return bytes(combined)

    def getType(self):
        from .curve import Curve

        return Curve.DJB_TYPE

    def getPublicKey(self):
        return self.publicKey

    def __eq__(self, other):
        return self.publicKey == other.getPublicKey()

    def __lt__(self, other):
        myVal = struct.unpack(">8i", self.publicKey)
        otherVal = struct.unpack(">8i", other.getPublicKey())

        return myVal < otherVal

    def __cmp__(self, other):
        myVal = struct.unpack(">8i", self.publicKey)
        otherVal = struct.unpack(">8i", other.getPublicKey())

        if myVal < otherVal:
            return -1
        elif myVal == otherVal:
            return 0
        else:
            return 1


class DjbECPrivateKey(ECPrivateKey):
    def __init__(self, privateKey):
        self.privateKey = privateKey

    def getType(self):
        from .curve import Curve

        return Curve.DJB_TYPE

    def getPrivateKey(self):
        return self.privateKey

    def serialize(self):
        return self.privateKey

    def __eq__(self, other):
        return self.privateKey == other.getPrivateKey()