示例#1
0
def run_giraffe(verifier_info):
    # pylint doesn't seed to understand how classmethods are inherited from metclasses
    from_pws = cver.CircuitVerifier.from_pws  # pylint: disable=no-member

    (input_layer, ver) = from_pws(pypws.parse_pws(verifier_info.pwsFile),
                                  verifier_info.nCopies)
    ver.build_prover()

    inputs = get_inputs(verifier_info, input_layer)
    ver.run(inputs)

    nInBits = util.clog2(len(input_layer))
    nCopies = VerifierInfo.nCopies
    nLayers = len(ver.in0vv)
    print "nInBits: %d, nCopies: %d, nLayers: %d" % (nInBits, nCopies, nLayers)
    (tMul, tAdd, tSub) = (0, 0, 0)
    for fArith in [ver.in_a, ver.out_a, ver.sc_a, ver.tV_a, ver.nlay_a]:
        (mul, add, sub) = fArith.get_counts()
        tMul += mul
        tAdd += add
        tSub += sub
        print "    %s: %d mul, %d add, %d sub" % (fArith.cat, mul, add, sub)

    print "  TOTAL: %d mul, %d add, %d sub" % (tMul, tAdd, tSub)

    lcosts = ver.local_costs()
    print "  LOCAL: %d mul, %d add, %d sub" % (lcosts.get(
        'mul', 0), lcosts.get('add', 0), lcosts.get('sub', 0))
示例#2
0
def main():
    if len(sys.argv) < 2:
        print "usage:", sys.argv[0], "<filename.pws>"
        sys.exit(1)
    elif not os.path.exists(sys.argv[1]):
        print "ERROR: cannot open", sys.argv[1]
        sys.exit(1)

    verbose = False
    if len(sys.argv) > 2:
        verbose = True

    pws_parsed = pypws.parse_pws(sys.argv[1])

    if verbose:
        print "# Info for", sys.argv[1]
        print "inputs:", len(pws_parsed[0])
    else:
        print len(pws_parsed[0])
        print len(pws_parsed[1:])

    totals = {'MUL':0, 'ADD':0, 'SUB':0, 'MUX':0}
    maxw = 0
    totw = 0
    for (num, lay) in enumerate(pws_parsed[1:]):
        oString = "layer %3d:" % (len(pws_parsed) - num - 2)
        counts = {'MUL': 0, 'ADD': 0, 'SUB': 0, 'MUX': 0}
        for (t, _, __, ___) in lay:
            counts[t] += 1
            totals[t] += 1

        if verbose:
            for t in sorted(counts.keys()):
                oString += "\t" + t + ": %5d" % counts[t]

            oString += "\t" + "Total: %5d" % len(lay)
            print oString

            if len(lay) > maxw:
                maxw = len(lay)
            totw += len(lay)
        else:
            print len(lay)

    if verbose:
        oString = "totals:    "
        for t in sorted(counts.keys()):
            oString += "\t" + t + ": %5d" % totals[t]

        oString += "\t" + "Total: %5d" % totw
        oString += "\t" + "Max: %5d" % maxw
        print oString
    else:
        print totals['MUL'], totals['ADD'], totals['SUB']
示例#3
0
def handle_pws():
    pws_parsed = pypws.parse_pws(ServerInfo.pws_file)
    inputs = tutil.pws2sv(pws_parsed, not ServerInfo.quiet,
                          ServerInfo.nCopyBits, ServerInfo.nParBitsP,
                          ServerInfo.nParBitsV, ServerInfo.nParBitsVLay,
                          ServerInfo.plStages, ServerInfo.nParBitsPH)
    nLayers = inputs[0][1]

    if ServerInfo.rundir is None:
        outdir = os.getcwd()
    else:
        outdir = os.path.join(ServerInfo.rundir, "rtl")

    for f in os.listdir(outdir):
        if f[:8] == "frompws_":
            os.remove(os.path.join(outdir, f))

    if not ServerInfo.sweval:
        for i in range(0, nLayers):
            outfile = os.path.join(outdir,
                                   'frompws_computation_layer_%d.sv' % i)
            open(outfile, 'w').write(computation.computation_layer(inputs, i))

    open(os.path.join(outdir, 'frompws_prover_shim.sv'),
         'w').write(prover.prover_shim(inputs))
    for i in range(0, nLayers):
        outfile = os.path.join(outdir, 'frompws_prover_layer_%d.sv' % i)
        open(outfile, 'w').write(prover.prover_layer(inputs, i))

    open(os.path.join(outdir, 'frompws_verifier_input.sv'),
         'w').write(verifier.verifier_input(inputs))
    open(os.path.join(outdir, 'frompws_verifier_output.sv'),
         'w').write(verifier.verifier_output(inputs))
    for i in range(0, nLayers):
        outfile = os.path.join(outdir, 'frompws_verifier_layer_%d.sv' % i)
        open(outfile, 'w').write(verifier.verifier_layer(inputs, i))

    GiraffeCoordinator.initialize(inputs[0], pws_parsed, ServerInfo)
示例#4
0
def main():
    if len(sys.argv) < 2:
        print "usage:", sys.argv[0], "<filename.pws>"
        sys.exit(1)
    elif not os.path.exists(sys.argv[1]):
        print "ERROR: cannot open", sys.argv[1]
        sys.exit(1)

    if len(sys.argv) > 2:
        pws_parsed = pypws.parse_pws_unopt(sys.argv[1], str(Defs.prime))
    else:
        pws_parsed = pypws.parse_pws(sys.argv[1], str(Defs.prime))

    print "# Info for", sys.argv[1]
    print "inputs:", len(pws_parsed[0])

    totals = {
        'MUL': 0,
        'ADD': 0,
        'SUB': 0,
        'MUX': 0,
        'OR': 0,
        'XOR': 0,
        'NOT': 0,
        'NAND': 0,
        'NOR': 0,
        'NXOR': 0,
        'NAAB': 0,
        'PASS': 0
    }
    maxw = 0
    totw = 0
    for (num, lay) in enumerate(pws_parsed[1:]):
        oString = "layer %3d:" % (len(pws_parsed) - num - 2)
        counts = {
            'MUL': 0,
            'ADD': 0,
            'SUB': 0,
            'MUX': 0,
            'OR': 0,
            'XOR': 0,
            'NOT': 0,
            'NAND': 0,
            'NOR': 0,
            'NXOR': 0,
            'NAAB': 0,
            'PASS': 0
        }
        for (t, _, __, ___) in lay:
            counts[t] += 1
            totals[t] += 1

        for t in sorted(counts.keys()):
            oString += "\t" + t + ": %5d" % counts[t]

        oString += "\t" + "Total: %5d" % len(lay)
        print oString

        if len(lay) > maxw:
            maxw = len(lay)
        totw += len(lay)

    oString = "totals:    "
    for t in sorted(counts.keys()):
        oString += "\t" + t + ": %5d" % totals[t]

    oString += "\t" + "Total: %5d" % totw
    oString += "\t" + "Max: %5d" % maxw
    print oString
示例#5
0
def run_fennel(verifier_info):
    # set curve and prime
    Defs.curve = verifier_info.curve
    util.set_prime(libfennel.commit.MiraclEC.get_order(Defs.curve))

    # pylint doesn't seed to understand how classmethods are inherited from metclasses
    p_from_pws = verifier_info.proofType.ProverClass.from_pws  # pylint: disable=no-member
    v_from_pws = verifier_info.proofType.VerifierClass.from_pws  # pylint: disable=no-member
    pFile = pypws.parse_pws(verifier_info.pwsFile, str(Defs.prime))

    # handle RDL
    if verifier_info.rdlFile is not None:
        rFile = pypws.parse_pws_unopt(verifier_info.rdlFile, str(Defs.prime))
        (r_input_layer,
         rdl_map) = libfennel.parse_pws.parse_rdl(rFile, verifier_info.nCopies,
                                                  pFile[0])

    # either generate or read in proof
    if verifier_info.vProofFile is None:
        (input_layer, prv) = p_from_pws(pFile, verifier_info.nCopies)
        prv.build_prover()

        # set up RDL
        if verifier_info.rdlFile is not None:
            inputs = get_inputs(verifier_info, r_input_layer)
            prv.set_rdl(rdl_map, len(r_input_layer))
        else:
            inputs = get_inputs(verifier_info, input_layer)

        # handle nondeterminism options
        if verifier_info.ndBits is not None:
            prv.set_nondet_range(verifier_info.ndBits)
        if verifier_info.ndGen is not None:
            prv.set_nondet_gen(verifier_info.ndGen)
        if verifier_info.rvStart is not None and verifier_info.rvEnd is not None:
            prv.set_rval_range(verifier_info.rvStart, verifier_info.rvEnd)
        if verifier_info.witnessDiv is not None:
            prv.set_wdiv(verifier_info.witnessDiv)

        verifier_info.pStartTime = time.time()
        proof = prv.run(inputs)
        verifier_info.pEndTime = time.time()
    else:
        with open(verifier_info.vProofFile, 'r') as fh:
            proof = bz2.decompress(fh.read())

    verifier_info.Log.log(
        "Proof size: %d elems, %d bytes" % FiatShamir.proof_size(proof), True)

    # either verify or write out proof
    if verifier_info.pProofFile is None:
        (_, ver) = v_from_pws(pFile, verifier_info.nCopies)

        # set up RDL
        if verifier_info.rdlFile is not None:
            ver.set_rdl(rdl_map, len(r_input_layer))

        verifier_info.vStartTime = time.time()
        try:
            ver.run(proof)
        except Exception as e:  # pylint: disable=broad-except
            verifier_info.Log.log("Verification failed: %s" % e, True)
            verifier_info.Log.log(traceback.format_exc(), True)
        else:
            verifier_info.Log.log("Verification succeeded.", True)
        verifier_info.vEndTime = time.time()
    else:
        with open(verifier_info.pProofFile, 'w') as fh:
            fh.write(bz2.compress(proof))

    nInBits = util.clog2(len(input_layer))
    if verifier_info.rdlFile is not None:
        nInBits = util.clog2(len(r_input_layer))
    nCopies = verifier_info.nCopies
    nLayers = len(ver.in0vv) + 1 if verifier_info.rdlFile is not None else 0
    verifier_info.Log.log(
        "nInBits: %d, nCopies: %d, nLayers: %d" % (nInBits, nCopies, nLayers),
        verifier_info.showPerf)
    if Defs.track_fArith:
        verifier_info.Log.log(str(Defs.fArith()), verifier_info.showPerf)