Skip to content
Snippets Groups Projects
Commit 2b332db2 authored by Maximilian Stauss's avatar Maximilian Stauss
Browse files

init

parents
No related branches found
No related tags found
No related merge requests found
.DS_Store
from __future__ import absolute_import, division, print_function
from sage.all import *
class AffineTransformation():
def __init__(self, M, y):
if not M.is_invertible():
exit(1)
self.M = M # invertable Matrix
self.y = y # vector
def __call__(self, vector):
"""Transform a given vector with initialized affine transformation"""
if not isinstance(vector, sage.structure.element.Vector):
vector = vector(vector)
return (self.M * vector) + self.y
def inverse(self, vector):
"""Transform a given vector with inverted affine transformation"""
return self.M.inverse() * (vector - self.y)
from __future__ import absolute_import, division, print_function
from sage.all import vector
import cPickle as pickle
from .PublicKey import PublicKey
class PrivateKey(object):
"""Private Key
AffineTransformation::S
PolynomialSystem::P
AffineTransformation::T
"""
def __init__(self, S, P, T, path=None):
# type: (AffineTransformation, PolynomialSystem, AffineTransformation,
# String) -> None
if path is not None:
(self.S, self.P, self.T) = self.load(path)
else:
self.S = S
self.P = P
self.T = T
def generate_public_key(self):
# type: (None) -> PublicKey
x = vector(self.P[0].parent().gens())
sx = list(self.S(x))
public_poly = []
for i in xrange(0, len(self.P)):
public_poly.append(self.P[i](sx))
spt = self.T(vector(public_poly))
return PublicKey(spt)
def encrypt(self, message):
# type: (Vector) -> Vector
x = list(self.S(vector(message)))
y = []
for i in xrange(0, len(self.P)):
y.append(self.P[i](x))
return self.T(vector(y))
def save(self, path="./keys/uov.priv"):
# type: (String) -> None
with open(path, 'wb') as output:
pickle.dump((self.S, self.P, self.T),
output, pickle.HIGHEST_PROTOCOL)
def load(self, path):
# type: (String) -> (AffineTransformation, PolynomialSystem,
# AffineTransformation)
with open(path, 'rb') as output:
return pickle.load(output)
from __future__ import absolute_import, print_function
import cPickle as pickle
class PublicKey(object):
def __init__(self, public_polynomial=None, path=None):
if public_polynomial is None and path is not None:
self.P = self.load(path)
else:
self.P = public_polynomial
def __call__(self, vector):
return self.encrypt(vector)
def __repr__(self):
return "PublicKey: {}".format(list(self.P))
def encrypt(self, vector):
return self.P(list(vector))
def verify(self, vector, signature):
return signature == self.encrypt(vector)
def save(self, path="./keys/uov.pub"):
with open(path, 'wb') as output:
pickle.dump(self.P, output, pickle.HIGHEST_PROTOCOL)
def load(self, path):
with open(path, 'rb') as output:
return pickle.load(output, pickle.HIGHEST_PROTOCOL)
from sage.all import *
class SignatureScheme(object):
"""All Functions for an MQPKC Signature Scheme"""
"""Init will set public and private keys"""
def sign(self, msg):
"""
Idea: generate signature that __encrypts__ to hash(message)
private_key of form (S, P', T)
invert S, P' and T -> we write _S, _P' and _T
"""
if not isinstance(msg, sage.structure.element.Vector):
msg = vector(msg)
_t = self.private_key.T.inverse(msg)
_p = self.invert_MQ(_t)
return self.private_key.S.inverse(_p)
def invert_MQ(self, msg):
"""
Every Scheme has to know how to invert the private polynomial
"""
raise NotImplementedError
def verify(self, signature, msg):
"""
signature: vector of length n
msg: vector of length m
for each entry of msg check:
msg[i] == P(signature)
"""
return self.public_key.verify(signature, msg)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment