示例#1
0
文件: ecdsa.py 项目: WalrusCow/co487
class Checker():
    prime = 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f
    curve = EllipticCurve(0, 7, GF(prime))

    G = curve.pt(
        0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798,
        0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8)
    # q is the order of G
    q = 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141
    field = GF(q)

    P = curve.pt(
        0xbac380aaefa5c656280bea4093ce7a06677f37c43bd121d5720b9ed2a6e1e2a5,
        0x5d88d4b08d21963b9a5c47ca9f875b05cb42237444727d5b42eb17a0f8d9dc40)
    hash = field(
        0xc0535e4be2b79ffd93291305436bf889314e4a3faec05ecffcbb7df31ad9e51a)
    r = field(
        0x32d80046e3257c4fe9b948e9b5b4d4aa4fcf05dff9bad6e056528f622f626b8c)
    s = field(
        0x10e669184888d1427e0e3f0fd4d345cc7cec53f7be0688e58a7d858f3471c995)

    @staticmethod
    def check_key(key):
        calc_r = (key * Checker.G).x
        if Checker.r.value != calc_r.value:
            return False
        # Probably right!
        sekrit_key = (Checker.s * key - Checker.hash) / Checker.r
        print('Found sekrit key: {}'.format(sekrit_key))
        key_valid = (sekrit_key.value * Checker.G) == Checker.P
        print('sekrit * G == P? {}'.format(key_valid))
        return sekrit_key if key_valid else None
示例#2
0
    tasks = []
    bgtasks = []
    for i in range(N):
        context = PassiveMpc('sid', N, t, i, sends[i], recvs[i], program)
        tasks.append(loop.create_task(context._run()))

    await asyncio.gather(*tasks)


#######################
# Generating test files
#######################

# Fix the field for now
Field = GF(0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001)
Poly = polynomialsOver(Field)


def write_polys(prefix, modulus, N, t, polys):
    for i in range(N):
        shares = [f(i + 1) for f in polys]
        with open('%s-%d.share' % (prefix, i), 'w') as f:
            write_shares(f, modulus, t, i, shares)


def generate_test_triples(prefix, k, N, t):
    # Generate k triples, store in files of form "prefix-%d.share"
    polys = []
    for j in range(k):
        a = Field(random.randint(0, Field.modulus - 1))
示例#3
0
        if not isinstance(n, int):
            raise ValueError('{} is not an int'.format(str(n)))

        if n == 0:
            return Point(self.curve, 0, 0)
        if n % 2 == 0:
            Q = (n >> 1) * self
            return Q + Q
        else:
            return ((n - 1) * self) + self

class EllipticCurve():
    def __init__(self, a, b, gf):
        ''' Elliptic curve of the form y^2 - x^3 + ax + b. '''
        self.a = a
        self.b = b
        self.gf = gf

    def __eq__(self, o):
        return isinstance(o, EllipticCurve) and o.a == self.a and o.b == self.b

    def pt(self, x, y):
        return Point(self, self.gf(x), self.gf(y))

if __name__ == '__main__':
    f = GF(11)
    e = EllipticCurve(1, 6, f)
    print(e.pt(2, 4) + e.pt(5, 2))
    pt = e.pt(2, 4)
    print(pt + pt)
示例#4
0
from rs import RS
from field import GF
from es import encode_file, decode_file
import sys

# usado en la práctica
gf = GF.primitive()
rs = RS(255, 223, 33, gf)

if len(sys.argv) < 3:
  print("Uso: python3 main.py option input output \n Opciones:\n --encode codifica [file] usando RS(255, 223) al archivo [output]\n --decode decodifica [file] usando RS(255, 223) al archivo [output]")
elif sys.argv[1] == "--encode":
    encode_file(sys.argv[2], sys.argv[3], rs)
elif sys.argv[1] == "--decode":
    decode_file(sys.argv[2], sys.argv[3], rs)