https://github.com/mupq/pqm4
Raw File
Tip revision: ae06d3dfc39e62f24a6dbb62d8e15a198b536d29 authored by Matthias J. Kannwischer on 31 January 2019, 10:15:17 UTC
Add kyber512 and kyber1024 (#35)
Tip revision: ae06d3d
utils.py
import os
import serial
import subprocess
import sys
import string

dev = serial.Serial("/dev/ttyUSB0", 115200,timeout=10)

def m4run(binpath):
    print("Flashing {}..".format(binpath))
    rc = subprocess.run(["st-flash", "--reset",  "write", binpath, "0x8000000"],
                   stdout=sys.stdout.buffer, stderr=sys.stdout.buffer)

    if rc.returncode != 0:
        raise Exception("Flashing failed.")

    state = 'waiting'
    marker = b''
    # This parses test vector output starting with a number of leading '=',
    #  and expects a hashtag '#' after the test vector output.
    while True:
        x = dev.read()
        if x == b'' and state == 'waiting':
            print("timed out while waiting for the markers")
            m4run(binpath)
            return

        if state == 'waiting':
            if x == b'=':
                marker += x
                continue
            # If we saw at least 5 equal signs, assume we've probably started
            elif marker.count(b'=') > 5:
                state = 'beginning'
                vector = []
                print("  .. found output marker..")
        if state == 'beginning':
            if x == b'=':
                continue
            else:
                state = 'reading'
        elif state == 'reading':
            if x == b'#':
                break
            else:
                vector.append(x)
    output = b''.join(vector).decode('utf-8', 'ignore')
    # sometimes there's a line full of markers; strip that out to avoid errors
    lines = [x for x in output.split('\n') if not all(c == '=' for c in x)]
    return '\n'.join(lines) + '\n'

def m4ignore(primitive, scheme, implementation):
    ignores = [os.path.join(primitive, scheme, implementation, '.m4ignore'),
               os.path.join(primitive, scheme, '.m4ignore'),
               os.path.join(primitive, '.m4ignore')]
    return any(os.path.exists(path) for path in ignores)
back to top