Skip to content

Commit

Permalink
xmr: HashWrapper removed
Browse files Browse the repository at this point in the history
  • Loading branch information
ph4r05 committed Oct 2, 2018
1 parent d0d1f05 commit a59dbb8
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 40 deletions.
3 changes: 1 addition & 2 deletions src/apps/monero/protocol/key_image_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
class KeyImageSync:
def __init__(self, ctx, creds=None):
from apps.monero.xmr import crypto
from apps.monero.xmr.sub.keccak_hasher import HashWrapper

self.ctx = ctx
self.creds = creds # type: monero.AccountCreds
Expand All @@ -17,7 +16,7 @@ def __init__(self, ctx, creds=None):
self.blocked = None
self.enc_key = None
self.subaddresses = {}
self.hasher = HashWrapper(crypto.get_keccak())
self.hasher = crypto.get_keccak()

async def derive_creds(self, msg):
self.creds = await misc.monero_get_creds(
Expand Down
35 changes: 17 additions & 18 deletions src/apps/monero/xmr/mlsag2.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,14 @@ def hasher_message(message):
"""
Returns incremental hasher for MLSAG
"""
from apps.monero.xmr.sub.keccak_hasher import HashWrapper

ctx = HashWrapper(crypto.get_keccak())
ctx = crypto.get_keccak()
ctx.update(message)
ctx.zbuff = bytearray(32)
return ctx


def hash_point(hasher, point):
crypto.encodepoint_into(hasher.zbuff, point)
hasher.update(hasher.zbuff)
def hash_point(hasher, point, tmp_buff):
crypto.encodepoint_into(tmp_buff, point)
hasher.update(tmp_buff)


def gen_mlsag_assert(pk, xx, kLRki, mscout, index, dsRows):
Expand Down Expand Up @@ -86,15 +83,16 @@ def gen_mlsag_rows(message, rv, pk, xx, kLRki, index, dsRows, rows, cols):
alpha = key_vector(rows)
rv.ss = key_matrix(rows, cols)

tmp_buff = bytearray(32)
hasher = hasher_message(message)

for i in range(dsRows):
hasher.update(crypto.encodepoint(pk[index][i]))
if kLRki:
alpha[i] = kLRki.k
rv.II[i] = kLRki.ki
hash_point(hasher, kLRki.L)
hash_point(hasher, kLRki.R)
hash_point(hasher, kLRki.L, tmp_buff)
hash_point(hasher, kLRki.R, tmp_buff)

else:
Hi = crypto.hash_to_point(
Expand All @@ -104,16 +102,16 @@ def gen_mlsag_rows(message, rv, pk, xx, kLRki, index, dsRows, rows, cols):
aGi = crypto.scalarmult_base(alpha[i])
aHPi = crypto.scalarmult(Hi, alpha[i])
rv.II[i] = crypto.scalarmult(Hi, xx[i])
hash_point(hasher, aGi)
hash_point(hasher, aHPi)
hash_point(hasher, aGi, tmp_buff)
hash_point(hasher, aHPi, tmp_buff)

Ip[i] = rv.II[i]

for i in range(dsRows, rows):
alpha[i] = crypto.random_scalar()
aGi = crypto.scalarmult_base(alpha[i])
hash_point(hasher, pk[index][i])
hash_point(hasher, aGi)
hash_point(hasher, pk[index][i], tmp_buff)
hash_point(hasher, aGi, tmp_buff)

c_old = hasher.digest()
c_old = crypto.decodeint(c_old)
Expand All @@ -139,6 +137,7 @@ def gen_mlsag_ext(message, pk, xx, kLRki, mscout, index, dsRows):
if i == 0:
rv.cc = c_old

tmp_buff = bytearray(32)
while i != index:
rv.ss[i] = scalar_gen_vector(rows)
hasher = hasher_message(message)
Expand All @@ -149,14 +148,14 @@ def gen_mlsag_ext(message, pk, xx, kLRki, mscout, index, dsRows):
crypto.encodepoint(pk[i][j])
) # originally hashToPoint()
R = crypto.add_keys3(rv.ss[i][j], Hi, c_old, Ip[j])
hash_point(hasher, pk[i][j])
hash_point(hasher, L)
hash_point(hasher, R)
hash_point(hasher, pk[i][j], tmp_buff)
hash_point(hasher, L, tmp_buff)
hash_point(hasher, R, tmp_buff)

for j in range(dsRows, rows):
L = crypto.add_keys2(rv.ss[i][j], c_old, pk[i][j])
hash_point(hasher, pk[i][j])
hash_point(hasher, L)
hash_point(hasher, pk[i][j], tmp_buff)
hash_point(hasher, L, tmp_buff)

c = crypto.decodeint(hasher.digest())
c_old = c
Expand Down
16 changes: 0 additions & 16 deletions src/apps/monero/xmr/sub/keccak_hasher.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,6 @@ def container_size(
return ar.container_size(container_len, container_type, params)


class HashWrapper:
def __init__(self, ctx):
self.ctx = ctx

def update(self, buf):
if len(buf) == 0:
return
self.ctx.update(buf)

def digest(self):
return self.ctx.digest()

def hexdigest(self):
return self.ctx.hexdigest()


class AHashWriter:
def __init__(self, hasher):
self.hasher = hasher
Expand Down
8 changes: 4 additions & 4 deletions src/apps/monero/xmr/sub/mlsag_hasher.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ class PreMlsagHasher:
"""

def __init__(self, state=None):
from apps.monero.xmr.sub.keccak_hasher import KeccakXmrArchive, HashWrapper
from apps.monero.xmr.sub.keccak_hasher import KeccakXmrArchive

self.is_simple = state[0] if state else None
self.state = state[1] if state else 0
self.kc_master = HashWrapper(state[2] if state else crypto.get_keccak())
self.kc_master = state[2] if state else crypto.get_keccak()
self.rsig_hasher = state[3] if state else crypto.get_keccak()
self.rtcsig_hasher = None
if state:
Expand All @@ -29,11 +29,11 @@ def state_save(self):
)

def state_load(self, x):
from apps.monero.xmr.sub.keccak_hasher import KeccakXmrArchive, HashWrapper
from apps.monero.xmr.sub.keccak_hasher import KeccakXmrArchive

self.is_simple = x[0]
self.state = x[1]
self.kc_master = HashWrapper(x[2])
self.kc_master = x[2]
self.rsig_hasher = x[3]
if x[4]:
self.rtcsig_hasher = KeccakXmrArchive(x[4])
Expand Down

0 comments on commit a59dbb8

Please sign in to comment.