Пример #1
0
def main():
    for input_name, input_dict in tio.get_all_inputs():
        result = []

        curve = create_curve(**input_dict)

        for task_type, *task_args in input_dict['tasks']:
            if task_type == 'у':
                x, y, k = task_args

                result.append(curve.multiply(k, Point(x, y)))
            if task_type == 'с':
                x1, y1, x2, y2 = task_args

                result.append(curve.add(Point(x1, y1), Point(x2, y2)))

        tio.write_result(input_name, result)
Пример #2
0
def test_ecdh():
    curve = Curve(65, -65, 3077783)
    G = Point(1, 1, curve)

    alice = ECDH(curve, G)
    bob = ECDH(curve, G)

    alice.setSecretMultiplicand(13)
    bob.setSecretMultiplicand(79)

    # Alice -> Bob
    bob.receiveECDHMessage(alice.getECDHMessage())
    print("Alice -> Bob", alice.getECDHMessage())

    # Bob -> Alice
    alice.receiveECDHMessage(bob.getECDHMessage())
    print("Bob -> Alice", bob.getECDHMessage())

    print("Alice: K=", alice.getCommonKey())
    print("Bob: K=", bob.getCommonKey())
Пример #3
0
from curve import Curve, Point

#ECDH - Elliptic Curve Diffie Hellman
curve1 = Curve(-1, 3, 7)
G = Point(2, 4, curve1)

#Alice
a = 5
kxa = a * G

#Bob
b = 2
kxb = b * G

# Key computation for Alice
ka = kxb * a
print(ka)

# Key computation for Bob
kb = kxa * b
print(kb)
Пример #4
0
# Take leftmost bits of hash, bit length of group order n
# Select random int from [1, n-1]
# calculate curve pt of random int * generator pt
# calculate modular inverse of x coordinate: r = x1 % n
# Calculate s = k^-1 (leftmost bits + r * privkey) % n
# sig is pair (r, s)

msg = "Hello world!"
e = hashlib.sha256(msg).hexdigest()
print e
z = e[0]
print z

curve = Curve(65, -65, 3077783)
# curve = Curve(65, -65, 103)
generatorPoint = Point(1, 1, curve)
n = generatorPoint.get_order()
print 'order n', n

# Select random k = random int from [1, n-1]
# n = integer order of G, means that n x G = 0. TODO: What is n?
# Must be different for each msg, otherwise key can be extracted.
# Must be sufficiently random
# To ensure that k is unique, once may generate deterministic signatures by deriving k from the message and private key
# What is n?
k = random.choice(range(1, curve.prime - 1))
print 'k', k

privkey = random.choice(range(1, curve.prime - 1))
print 'privkey', privkey
Пример #5
0
    def setSecretMultiplicand(self, multiplicand : int):
        self.secret = multiplicand

    def receiveECDHMessage(self, ecdhmessage : Point):
        self.ecdhmessage = ecdhmessage

    def getECDHMessage(self) -> Point:
        return self.secret*self.generatorPoint

    def getCommonKey(self) -> Point:
        return self.secret*self.ecdhmessage

# Usage Example
curve = Curve(65, -65, 3077783)
G = Point(1, 1, curve)

alice = ECDH(curve, G)
bob = ECDH(curve, G)

alice.setSecretMultiplicand(13)
bob.setSecretMultiplicand(79)

# Alice -> Bob
bob.receiveECDHMessage(
    alice.getECDHMessage()
)
print("Alice -> Bob", alice.getECDHMessage())

# Bob -> Alice
alice.receiveECDHMessage(
Пример #6
0
    def __init__(self, secret_key):
        self.curve = Curve(65, -65, 3077783)
        self.generatorPoint = Point(1, 1, self.curve)

        self.secret_key = secret_key
        self.received = None
Пример #7
0
print("\n\n1. Testing Fq1 and Fq2 inverse", end='')
Fq1.set_q(631)
Fq2.set_q(2**255 - 19)
for i in range(10):
    a1 = Fq1(random.randrange(1, Fq1.Q))
    b1 = Fq1.one() // a1
    assert a1 * b1 == Fq1.one()
    a2 = Fq2(random.randrange(1, Fq2.Q), random.randrange(1, Fq2.Q))
    b2 = Fq2.one() // a2
    res = a2 * b2
    assert a2 * b2 == Fq2.one()
print("...passed\n\n")

Curve.set_a_b(30, 34)
P = Point(x=Fq1(36), y=Fq1(60))
Q = Point(x=Fq1(121), y=Fq1(387))
S = Point(x=Fq1(0), y=Fq1(36))

print("group1 = [", end='')
for i in range(1, 5):
    g = Curve.multiply(P, i)
    print("(Affine {:3} {:3}), ".format(g.x.q, g.y.q), end='')
print("")

print("group2 = [", end='')
for i in range(1, 5):
    g = Curve.multiply(Q, i)
    print("(Affine {:3} {:3}), ".format(g.x.q, g.y.q), end='')
print("]")
Пример #8
0
from curve import Curve, Point

#ECDSA Implementation...


def hash(m):
    return m

m = 1512

p = 3077783
n = 3078653

curve1 = Curve(65, -65, p)
G = Point(1, 1, curve1)

# Private Key
d = 654321
Q = d * G
print("Q=", Q)

# Public Key
public = (p, n, G, Q, curve1)

# Generating a Signature
print("Signature Generation")

# Pseudorandom k with 1 <= k <= n-1
k = 1234567

#Compute k*G = (x, y)
Пример #9
0
    def compute_curve(self):
        from curve import Line, Point, Curves        
        N, M, T = self.N, self.M, self.T
        K, K0, K1 = self.K, self.K0, self.K1
        S = self.S
        S0 = S/K * K0

        toffset = self.t_offset
        ioffset = Point(0, self.i_offset)
        joffset = Point(0, self.j_offset)

        icurve = Curves()
        jcurve = Curves()

        icurve.add_line(Line(Point(-(4*K1*T)//5,self.io), Point(0, self.io), beta_exp=1.2))
        jcurve.add_line(Line(Point(-(4*K1*T)//5,self.jo), Point(0, self.jo)))

        for j in range(M):
            if j:
                start_t = (K0 + (j-1) * (N+K))*T + T*N + (T*K)//2
            else:
                start_t = (T*K0)//2
            end_t = start_t + 1
            level_i = self.i_offset
            #icurve.add_line(Line (Point(start_t, level_i), Point(end_t, level_i)))

            start_i = 0
            end_i = N
            start_j = j
            end_j = j
            start_t = toffset + (K0 + j * (N+K))*T
            end_t = start_t + T*N
            iline = Line(Point(start_t, start_i), Point (end_t, end_i)\
                             ).stretch_end_x(-toffset + T*S/10).stretch_start_x(T*S) + ioffset
            if j<M-1:
                iline.set_beta_exp(1.3)
            icurve.add_line(iline)
            if j:
                jcurve.add_line(Line(Point(start_t, start_j), Point (end_t, end_j)).stretch_start_x(T*S) + joffset)
            else:
                jcurve.add_line(Line(Point(start_t, start_j), Point (end_t, end_j)).stretch_start_x(T*S0) + joffset)

        start_i = self.io
        end_i = self.io
        start_j = self.jo
        end_j = self.jo
        start_t = (K0 + (M-1)*(N+K) + N + K1)*T
        end_t = start_t + (K1)*T
        icurve.add_line(Line(Point(start_t, start_i), Point (end_t, end_i)))
        jcurve.add_line(Line(Point(start_t, start_j), Point (end_t, end_j)))

        self.icurve = icurve
        self.jcurve = jcurve

        return K, K0, K1
Пример #10
0
from curve import Curve, Point

p = 3077783
curve1 = Curve(65, -65, p)
G = Point(1, 1, curve1)

# Message (!Must be a point on the curve...!)
"""
You need a public known function f : m ↦ Pm which maps messages m to points Pm on E.
It should be invertible, and one way is to use m in the curve's equation as x and calculate the according y.
"""
M = Point(1154, 3038580, curve1)
print("M", M)

# Generating Public/Private Key
a = 34
Y = a * G
print("Y", Y)

public = (Y, G, p, curve1)
private = (a)

# Encrypting
Y = public[0]
G = public[1]
p = public[2]

k = 12
C = k * G
print("C", C)
Пример #11
0
    assert res == Fq6.const(1)

    a12 = Fq12(Fq6(Fq2(Fq1(random.randrange(1, Fq1.PRIME)), Fq1(random.randrange(1, Fq1.PRIME))),
                   Fq2(Fq1(random.randrange(1, Fq1.PRIME)), Fq1(random.randrange(1, Fq1.PRIME))),
                   Fq2(Fq1(random.randrange(1, Fq1.PRIME)), Fq1(random.randrange(1, Fq1.PRIME)))),
               Fq6(Fq2(Fq1(random.randrange(1, Fq1.PRIME)), Fq1(random.randrange(1, Fq1.PRIME))),
                   Fq2(Fq1(random.randrange(1, Fq1.PRIME)), Fq1(random.randrange(1, Fq1.PRIME))),
                   Fq2(Fq1(random.randrange(1, Fq1.PRIME)), Fq1(random.randrange(1, Fq1.PRIME)))))
    b12 = Fq12.const(1) // a12
    res = a12 * b12
    assert res == Fq12.const(1)
print("passed")


print("\n3. Testing g1 mul of Fq1 ...", end='')
G1 = Point(x=Fq1(0x17f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb),
           y=Fq1(0x08b3f481e3aaa0f1a09e30ed741d8ae4fcf5e095d5d00af600db18cb2c04b3edd03cc744a2888ae40caa232946c5e7e1))
res = Curve.multiply(G1, 0x49abcbaa08d87d1cba8fd9c0ea04df30b94df934827a7383098ac39e1aafc218)
chk = Point(x=Fq1(0x19906b4953328ec688ffc9e41ea7d79d295c7de6249eb0397680306c5fe3aa3bbb45324bdbc379e8e4116166f2a0d40),
            y=Fq1(0x165f11693959e7193af31b99b724f95d7b49baa2394b758c2455ef725f32abd56361e1e151f2bbd7a7efc6f3bb5652d4))
assert res == chk
print("passed")


print("\n4. Testing g2 mul of Fq2...", end='')
G2 = Point(x=Fq2(Fq1(0x13e02b6052719f607dacd3a088274f65596bd0d09920b61ab5da61bbdc7f5049334cf11213945d57e5ac7d055d042b7e),
                 Fq1(0x24aa2b2f08f0a91260805272dc51051c6e47ad4fa403b02b4510b647ae3d1770bac0326a805bbefd48056c8c121bdb8)),
           y=Fq2(Fq1(0x606c4a02ea734cc32acd2b02bc28b99cb3e287e85a763af267492ab572e99ab3f370d275cec1da1aaa9075ff05f79be),
                 Fq1(0xce5d527727d6e118cc9cdc6da2e351aadfd9baa8cbdd3a76d429a695160d12c923ac9cc3baca289e193548608b82801)))
res = Curve.multiply(G2, 0x49abcbaa08d87d1cba8fd9c0ea04df30b94df934827a7383098ac39e1aafc218)
chk = Point(x=Fq2(Fq1(0x2433912fa403e0d19d39c7687eb1041f474a82fdd646b1a35afb4d088f11469467468f1ba16c3e5838503919bdbfa24),
                  Fq1(0x14906db96db027e17449a1323198cfccde4d15456ce09f3fef4c7baed5495463b7cc750300e0e2918d5680d97a567122)),