Skip to content

Commit

Permalink
xmr: crypto code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ph4r05 committed Sep 17, 2018
1 parent 20b4113 commit 85ecc15
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 61 deletions.
57 changes: 1 addition & 56 deletions src/apps/monero/xmr/crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
# https://tools.ietf.org/html/draft-josefsson-eddsa-ed25519-00#section-4
# https://github.com/monero-project/research-lab

import ubinascii as binascii

from trezor.crypto import hmac, monero as tcry, pbkdf2 as tpbkdf2, random
from trezor.crypto import hmac, monero as tcry, random
from trezor.crypto.hashlib import sha3_256

NULL_KEY_ENC = b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
Expand Down Expand Up @@ -42,24 +40,11 @@ def keccak_2hash(inp):
return keccak_hash(keccak_hash(inp))


def get_hmac(key, msg=None):
return hmac.new(key, msg=msg, digestmod=keccak_factory)


def compute_hmac(key, msg=None):
h = hmac.new(key, msg=msg, digestmod=keccak_factory)
return h.digest()


def pbkdf2(inp, salt, length=32, count=1000, prf=None):
"""
PBKDF2 with default PRF as HMAC-KECCAK-256
"""
pb = tpbkdf2("hmac-sha256", inp, salt)
pb.update(count)
return pb.key()


#
# EC
#
Expand Down Expand Up @@ -179,32 +164,20 @@ def sc_inv_eight():


def sc_0():
"""
Sets 0 to the scalar value Zmod(m)
"""
return tcry.init256_modm(0)


def sc_0_into(r):
"""
Sets 0 to the scalar value Zmod(m)
"""
return tcry.init256_modm(r, 0)


def sc_init(x):
"""
Sets x to the scalar value Zmod(m)
"""
if x >= (1 << 64):
raise ValueError("Initialization works up to 64-bit only")
return tcry.init256_modm(x)


def sc_init_into(r, x):
"""
Sets x to the scalar value Zmod(m)
"""
if x >= (1 << 64):
raise ValueError("Initialization works up to 64-bit only")
return tcry.init256_modm(r, x)
Expand Down Expand Up @@ -387,20 +360,6 @@ def ge_frombytes_vartime(point):
return point


def precomp(point):
"""
Precomputation placeholder
"""
return point


def ge_dsm_precomp(point):
"""
void ge_dsm_precomp(ge_dsmp r, const ge_p3 *s)
"""
return point


#
# Monero specific
#
Expand Down Expand Up @@ -543,25 +502,11 @@ def get_subaddress_secret_key(secret_key, major=0, minor=0):
return tcry.xmr_get_subaddress_secret_key(major, minor, secret_key)


def b16_to_scalar(bts):
"""
Converts hexcoded bytearray to the scalar
"""
return decodeint(binascii.unhexlify(bts))


#
# Repr invariant
#


def hmac_point(key, point):
"""
HMAC single point
"""
return compute_hmac(key, encodepoint(point))


def generate_signature(data, priv):
"""
Generate EC signature
Expand Down
2 changes: 1 addition & 1 deletion src/apps/monero/xmr/mlsag2.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def gen_mlsag_rows(message, rv, pk, xx, kLRki, index, dsRows, rows, cols):
hash_point(hasher, aGi)
hash_point(hasher, aHPi)

Ip[i] = crypto.precomp(rv.II[i])
Ip[i] = rv.II[i]

for i in range(dsRows, rows):
alpha[i] = crypto.random_scalar()
Expand Down
6 changes: 2 additions & 4 deletions src/apps/monero/xmr/ring_ct.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,6 @@ def generate_ring_signature(prefix_hash, image, pubs, sec, sec_idx, test=False):
crypto.ge_frombytes_vartime_check(k)

image_unp = crypto.ge_frombytes_vartime(image)
image_pre = crypto.ge_dsm_precomp(image_unp)

buff_off = len(prefix_hash)
buff = bytearray(buff_off + 2 * 32 * len(pubs))
Expand Down Expand Up @@ -233,7 +232,7 @@ def generate_ring_signature(prefix_hash, image, pubs, sec, sec_idx, test=False):

tmp3 = crypto.hash_to_ec(crypto.encodepoint(tmp3))
tmp2 = crypto.ge_double_scalarmult_precomp_vartime(
sig[i][1], tmp3, sig[i][0], image_pre
sig[i][1], tmp3, sig[i][0], image_unp
)
crypto.encodepoint_into(mvbuff[buff_off : buff_off + 32], tmp2)
buff_off += 32
Expand All @@ -250,7 +249,6 @@ def check_ring_singature(prefix_hash, image, pubs, sig):
from trezor.utils import memcpy

image_unp = crypto.ge_frombytes_vartime(image)
image_pre = crypto.ge_dsm_precomp(image_unp)

buff_off = len(prefix_hash)
buff = bytearray(buff_off + 2 * 32 * len(pubs))
Expand All @@ -269,7 +267,7 @@ def check_ring_singature(prefix_hash, image, pubs, sig):

tmp3 = crypto.hash_to_ec(crypto.encodepoint(pubs[i]))
tmp2 = crypto.ge_double_scalarmult_precomp_vartime(
sig[i][1], tmp3, sig[i][0], image_pre
sig[i][1], tmp3, sig[i][0], image_unp
)
crypto.encodepoint_into(mvbuff[buff_off : buff_off + 32], tmp2)
buff_off += 32
Expand Down

0 comments on commit 85ecc15

Please sign in to comment.