|
|
|
@ -79,6 +79,8 @@ random_iters = int(os.environ.get("ITERS", 1))
|
|
|
|
|
|
|
|
|
|
DIR = os.path.abspath(os.path.dirname(__file__))
|
|
|
|
|
lib = c.cdll.LoadLibrary(os.path.join(DIR, "libtrezor-crypto.so"))
|
|
|
|
|
if not lib.zkp_context_is_initialized():
|
|
|
|
|
lib.zkp_context_init()
|
|
|
|
|
|
|
|
|
|
BIGNUM = c.c_uint32 * 9
|
|
|
|
|
|
|
|
|
@ -139,9 +141,7 @@ def r(request):
|
|
|
|
|
return Random(seed + int(os.environ.get("SEED", 0)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(params=list(sorted(curves)))
|
|
|
|
|
def curve(request):
|
|
|
|
|
name = request.param
|
|
|
|
|
def get_curve_obj(name):
|
|
|
|
|
curve_ptr = lib.get_curve_by_name(bytes(name, "ascii")).contents.params
|
|
|
|
|
assert curve_ptr, "curve {} not found".format(name)
|
|
|
|
|
curve_obj = curves[name]
|
|
|
|
@ -150,6 +150,11 @@ def curve(request):
|
|
|
|
|
return curve_obj
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(params=list(sorted(curves)))
|
|
|
|
|
def curve(request):
|
|
|
|
|
return get_curve_obj(request.param)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(params=points)
|
|
|
|
|
def point(request):
|
|
|
|
|
name = request.param.curve
|
|
|
|
@ -304,6 +309,29 @@ def test_sign(curve, r):
|
|
|
|
|
assert vk.verify_digest(sig, digest, sigdecode)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_sign_zkp(r):
|
|
|
|
|
curve = get_curve_obj("secp256k1")
|
|
|
|
|
|
|
|
|
|
priv = r.randbytes(32)
|
|
|
|
|
digest = r.randbytes(32)
|
|
|
|
|
sig = r.randbytes(64)
|
|
|
|
|
|
|
|
|
|
lib.zkp_ecdsa_sign_digest(
|
|
|
|
|
curve.ptr, priv, digest, sig, c.c_void_p(0), c.c_void_p(0)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
exp = bytes2num(priv)
|
|
|
|
|
sk = ecdsa.SigningKey.from_secret_exponent(exp, curve, hashfunc=hashlib.sha256)
|
|
|
|
|
vk = sk.get_verifying_key()
|
|
|
|
|
|
|
|
|
|
sig_ref = sk.sign_digest_deterministic(
|
|
|
|
|
digest, hashfunc=hashlib.sha256, sigencode=ecdsa.util.sigencode_string_canonize
|
|
|
|
|
)
|
|
|
|
|
assert binascii.hexlify(sig) == binascii.hexlify(sig_ref)
|
|
|
|
|
|
|
|
|
|
assert vk.verify_digest(sig, digest, sigdecode)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_validate_pubkey(curve, r):
|
|
|
|
|
p = r.randpoint(curve)
|
|
|
|
|
assert lib.ecdsa_validate_pubkey(curve.ptr, to_POINT(p))
|
|
|
|
|