def run_modexp_blinded(bn_words, exp): """Runs the primitive for modular exponentiation (modexp)""" global dmem global inst_cnt global cycle_cnt global stats global ctx start_addr = 338 stop_addr = 413 load_full_bn_val(DMEMP_EXP, exp) load_pointer(bn_words, DMEM_LOC_IN_PTRS, DMEMP_IN, DMEMP_RR, DMEMP_IN) load_pointer(bn_words, DMEM_LOC_SQR_PTRS, DMEMP_OUT, DMEMP_OUT, DMEMP_OUT) load_pointer(bn_words, DMEM_LOC_MUL_PTRS, DMEMP_IN, DMEMP_OUT, DMEMP_OUT) load_pointer(bn_words, DMEM_LOC_OUT_PTRS, DMEMP_OUT, DMEMP_EXP, DMEMP_OUT) load_blinding(EXP_PUB, 0, 0, 0) machine = Machine(dmem.copy(), ins_objects, start_addr, stop_addr, ctx=ctx) machine.stats = stats cont = True while cont: cont, trace_str, cycles = machine.step() dump_trace_str(trace_str) inst_cnt += 1 cycle_cnt += cycles res = get_full_bn_val(DMEMP_OUT, machine, bn_words) dmem = machine.dmem.copy() return res
def run_sign(d, k, msg): """Runs the sign primitive to perform an ecdsa sign""" global dmem global inst_cnt global cycle_cnt global ctx global stats load_pointer() machine = Machine(dmem.copy(), ins_objects, P256INIT_START_ADDR, P256INIT_STOP_ADDR, ctx=ctx) cont = True while cont: cont, trace_str, cycles = machine.step() dump_trace_str(trace_str) inst_cnt += 1 cycle_cnt += cycles dmem = machine.dmem.copy() load_msg(msg) load_d(d) load_k(k) machine.dmem = dmem.copy() machine.pc = P256SIGN_START_ADDR machine.stop_addr = P256SIGN_STOP_ADDR machine.stats = stats cont = True while cont: cont, trace_str, cycles = machine.step() dump_trace_str(trace_str) inst_cnt += 1 cycle_cnt += cycles dmem = machine.dmem.copy() return dmem[pR], dmem[pS]
def run_modload(bn_words): """Runs the modload primitive (modload). Other than it's name suggests this primitive computes RR and the montgomery inverse dinv. The modulus is actually directly loaded into dmem beforehand. This primitive has to be executed every time, dmem was cleared. """ global dmem global inst_cnt global cycle_cnt global stats global ctx start_addr = 414 stop_addr = 425 load_pointer(bn_words, DMEM_LOC_IN_PTRS, DMEMP_IN, DMEMP_EXP, DMEMP_OUT) machine = Machine(dmem.copy(), ins_objects, start_addr, stop_addr, ctx=ctx) machine.stats = stats cont = True while cont: cont, trace_str, cycles = machine.step() dump_trace_str(trace_str) inst_cnt += 1 cycle_cnt += cycles dmem = machine.dmem.copy() dinv_res = dmem[DMEMP_DINV] rr_res = get_full_bn_val(DMEMP_RR, machine, bn_words) return dinv_res, rr_res
def run_verify(x, y, r, s, msg): """Runs the sign primitive to perform an ecdsa sign""" global dmem global inst_cnt global cycle_cnt global ctx global stats global breakpoints load_pointer() machine = Machine(dmem.copy(), ins_objects, start_addr_dict['p256init'], stop_addr_dict['p256init'], ctx=ctx, breakpoints=breakpoints) machine.stats = stats cont = True while cont: cont, trace_str, cycles = machine.step() dump_trace_str(trace_str) inst_cnt += 1 cycle_cnt += cycles dmem = machine.dmem.copy() load_x(x) load_y(y) load_r(r) load_s(s) load_msg(msg) machine.dmem = dmem.copy() machine.pc = start_addr_dict['p256verify'] machine.stop_addr = stop_addr_dict['p256verify'] machine.stats = stats cont = True while cont: cont, trace_str, cycles = machine.step() dump_trace_str(trace_str) inst_cnt += 1 cycle_cnt += cycles dmem = machine.dmem.copy() # Verification successful if r == rnd return dmem[pR] == dmem[pRnd]
def run_verify(x, y, r, s, msg): """Runs the sign primitive to perform an ecdsa sign""" global dmem global inst_cnt global cycle_cnt global ctx global stats load_pointer() machine = Machine(dmem.copy(), ins_objects, P256INIT_START_ADDR, P256INIT_STOP_ADDR, ctx=ctx) machine.stats = stats cont = True while cont: cont, trace_str, cycles = machine.step() dump_trace_str(trace_str) inst_cnt += 1 cycle_cnt += cycles dmem = machine.dmem.copy() load_x(x) load_y(y) load_r(r) load_s(s) load_msg(msg) machine.dmem = dmem.copy() machine.pc = P256VERIFY_START_ADDR machine.stop_addr = P256VERIFY_STOP_ADDR machine.stats = stats cont = True while cont: cont, trace_str, cycles = machine.step() dump_trace_str(trace_str) inst_cnt += 1 cycle_cnt += cycles dmem = machine.dmem.copy() # Verification successful if r == rnd return dmem[pR] == dmem[pRnd]
def run_scalarmult(x, y, k): """Runs the scalarmult primitive to multiply a curve point with a scalar""" global dmem global inst_cnt global cycle_cnt global ctx global stats global breakpoints load_pointer() machine = Machine(dmem.copy(), ins_objects, start_addr_dict['p256init'], stop_addr_dict['p256init'], ctx=ctx, breakpoints=breakpoints) machine.stats = stats cont = True while cont: cont, trace_str, cycles = machine.step() dump_trace_str(trace_str) inst_cnt += 1 cycle_cnt += cycles dmem = machine.dmem.copy() load_x(x) load_y(y) load_k(k) machine.dmem = dmem.copy() machine.pc = start_addr_dict['p256scalarmult'] machine.stop_addr = stop_addr_dict['p256scalarmult'] machine.stats = stats cont = True while cont: cont, trace_str, cycles = machine.step() dump_trace_str(trace_str) inst_cnt += 1 cycle_cnt += cycles dmem = machine.dmem.copy() return dmem[pX], dmem[pY]
def run_scalarmult(x, y, k): """Runs the scalarmult primitive to multiply a curve point with a scalar""" global dmem global inst_cnt global cycle_cnt global ctx global stats load_pointer() machine = Machine(dmem.copy(), ins_objects, P256INIT_START_ADDR, P256INIT_STOP_ADDR, ctx=ctx) machine.stats = stats cont = True while cont: cont, trace_str, cycles = machine.step() dump_trace_str(trace_str) inst_cnt += 1 cycle_cnt += cycles dmem = machine.dmem.copy() load_x(x) load_y(y) load_k(k) machine.dmem = dmem.copy() machine.pc = P256SCALARMULT_START_ADDR machine.stop_addr = P256SCALARMULT_STOP_ADDR machine.stats = stats cont = True while cont: cont, trace_str, cycles = machine.step() dump_trace_str(trace_str) inst_cnt += 1 cycle_cnt += cycles dmem = machine.dmem.copy() return dmem[pX], dmem[pY]
def run_montout(bn_words, p_a, p_out): """Runs the primitive for back-transformation from the montgomery domain (mul1)""" global dmem global inst_cnt global cycle_cnt global stats global ctx load_pointer(bn_words, DMEM_LOC_IN_PTRS, p_a, 0, p_out) machine = Machine(dmem.copy(), ins_objects, start_addr_dict['mul1'], stop_addr_dict['mul1'], ctx=ctx, breakpoints=breakpoints) machine.stats = stats cont = True while cont: cont, trace_str, cycles = machine.step() dump_trace_str(trace_str) inst_cnt += 1 cycle_cnt += cycles res = get_full_bn_val(DMEMP_OUT, machine, bn_words) dmem = machine.dmem.copy() return res
def run_montmul(bn_words, p_a, p_b, p_out): """Runs the primitive for montgomery multiplication (mulx)""" global dmem global inst_cnt global cycle_cnt global stats global ctx global breakpoints load_pointer(bn_words, DMEM_LOC_IN_PTRS, p_a, p_b, p_out) machine = Machine(dmem.copy(), ins_objects, start_addr_dict['mulx'], stop_addr_dict['mulx'], ctx=ctx, breakpoints=breakpoints) machine.stats = stats cont = True i = 0 while cont: cont, trace_str, cycles = machine.step() i += 1 dump_trace_str(trace_str) inst_cnt += 1 cycle_cnt += cycles res = get_full_bn_val(DMEMP_OUT, machine, bn_words) dmem = machine.dmem.copy() return res
def run_sign(d, k, msg): """Runs the sign primitive to perform an ecdsa sign""" global dmem global inst_cnt global cycle_cnt global ctx global stats global breakpoints load_pointer() machine = Machine(dmem.copy(), ins_objects, start_addr_dict['p256init'], stop_addr_dict['p256init'], ctx=ctx, breakpoints=breakpoints) cont = True while cont: cont, trace_str, cycles = machine.step() dump_trace_str(trace_str) inst_cnt += 1 cycle_cnt += cycles dmem = machine.dmem.copy() load_msg(msg) load_d(d) load_k(k) machine.dmem = dmem.copy() machine.pc = start_addr_dict['p256sign'] machine.stop_addr = stop_addr_dict['p256sign'] machine.stats = stats cont = True while cont: cont, trace_str, cycles = machine.step() dump_trace_str(trace_str) inst_cnt += 1 cycle_cnt += cycles dmem = machine.dmem.copy() return dmem[pR], dmem[pS]
def run_isoncurve(x, y): """Runs the isoncurve primitive to check if a point is a valid curve point""" global dmem global inst_cnt global cycle_cnt global ctx global stats load_pointer() machine = Machine(dmem.copy(), ins_objects, start_addr_dict['p256init'], stop_addr_dict['p256init'], ctx=ctx, breakpoints=breakpoints) cont = True while cont: cont, trace_str, cycles = machine.step() dump_trace_str(trace_str) inst_cnt += 1 cycle_cnt += cycles dmem = machine.dmem.copy() load_x(x) load_y(y) machine.dmem = dmem.copy() machine.pc = start_addr_dict['p256isoncurve'] machine.stop_addr = stop_addr_dict['p256isoncurve'] cont = True machine.stats = stats while cont: cont, trace_str, cycles = machine.step() dump_trace_str(trace_str) inst_cnt += 1 cycle_cnt += cycles dmem = machine.dmem.copy() # point is on curve if r and s are equal on_curve = (dmem[pS] == dmem[pR]) return on_curve
def run_isoncurve(x, y): """Runs the isoncurve primitive to check if a point is a valid curve point""" global dmem global inst_cnt global cycle_cnt global ctx global stats load_pointer() machine = Machine(dmem.copy(), ins_objects, P256INIT_START_ADDR, P256INIT_STOP_ADDR, ctx=ctx) cont = True while cont: cont, trace_str, cycles = machine.step() dump_trace_str(trace_str) inst_cnt += 1 cycle_cnt += cycles dmem = machine.dmem.copy() load_x(x) load_y(y) machine.dmem = dmem.copy() machine.pc = P256ISONCURVE_START_ADDR machine.stop_addr = P256ISONCURVE_STOP_ADDR cont = True machine.stats = stats while cont: cont, trace_str, cycles = machine.step() dump_trace_str(trace_str) inst_cnt += 1 cycle_cnt += cycles dmem = machine.dmem.copy() # point is on curve if r and s are equal on_curve = (dmem[pS] == dmem[pR]) return on_curve