Skip to content

Commit

Permalink
xmr: serializer test fix (+34 squashed commits)
Browse files Browse the repository at this point in the history
Squashed commits:
[823ee19] xmr: crypto comment cleanup
[6debfb6] xmr: ring_ct cleanup
[759f52b] xmr: tsx signer code style, hintins
[0b175bc] xmr: tsx builder external state removed
[fee4a5a] xmr: builder state fix
[92736fa] xmr: sign_tx unimport
[a570ecb] xmr: misc code cleanup
[4a496bb] xmr: hash wrapper not needed in writer
[fefdb83] xmr: signer serialization improved
[8fa6eec] xmr: signer mem clean
[66c53fe] xmr: isort
[6996bd9] xmr: black
[59915a8] xmr: tsx input serialization refactored
[326af13] xmr: msg dump with prefix
[6e39801] xmr: manual serialization of tx prefix
[9e5e047] xmr: manual serialization improvements
[d07cee6] xmr: manual serialization of txout elements
[8d56c80] xmr: TxOut custom serialization optimized II
[c19ba12] xmr: TxOut custom serialization optimized
[ce0d9b0] xmr: TxOut manual serialization
[44e3834] xmr: sing_tx unimport
[61ac61b] xmr: lite log trace rename
[176b427] xmr: de-async overhaul
[89ae3ba] xmr: diag style
[5ccb2fb] xmr: wrappers cleanup
[aa86fb1] xmr: py3 only inheritance
[8031b1b] xmr: builder, log_trace -> mem_trace for clarity
[25bf70d] xmr: debugging logging only in debug mode
[c7c8d3c] xmr: iface cleanup
[b037339] xmr: lite debug only
[b1f6ce0] xmr: diag only in debug
[de7d718] xmr: tsx counter removed
[76729be] xmr: tsx_sign removed
[c6e6ffa] Merge commit 'ba500bf4ec1ef9cd953bdf5a47888c5226db8d0b' into xmr
  • Loading branch information
ph4r05 committed Sep 14, 2018
1 parent ee97ef9 commit 8b4f4d9
Show file tree
Hide file tree
Showing 31 changed files with 806 additions and 1,472 deletions.
6 changes: 3 additions & 3 deletions src/apps/monero/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


# persistent state objects
class Holder(object):
class Holder:
def __init__(self):
self.ctx_sign = None
self.ctx_ki = None
Expand All @@ -16,8 +16,8 @@ def __init__(self):
def boot():
wire.add(MessageType.MoneroGetAddress, __name__, "get_address")
wire.add(MessageType.MoneroGetWatchKey, __name__, "get_watch_only")
wire.add(MessageType.MoneroTransactionSignRequest, __name__, "sign_tx", STATE)
wire.add(MessageType.MoneroKeyImageSyncRequest, __name__, "key_image_sync", STATE)
wire.add(MessageType.MoneroTransactionSignRequest, __name__, "sign_tx")
wire.add(MessageType.MoneroKeyImageSyncRequest, __name__, "key_image_sync")

if hasattr(MessageType, "MoneroLiteInitRequest"):
wire.add(MessageType.MoneroLiteInitRequest, "lite_protocol", STATE, 1)
Expand Down
20 changes: 5 additions & 15 deletions src/apps/monero/controller/iface.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class TrezorInterface(object):
class TrezorInterface:
def __init__(self, ctx=None):
self.ctx = ctx

Expand Down Expand Up @@ -81,29 +81,19 @@ async def confirm_transaction(self, tsx_data, creds=None, ctx=None):
from trezor.ui.text import Text
from trezor import ui
from trezor import loop
from trezor import log
from trezor import workflow
from trezor.ui import BACKLIGHT_DIM, BACKLIGHT_NORMAL

await ui.backlight_slide(BACKLIGHT_DIM)
slide = ui.backlight_slide(BACKLIGHT_NORMAL)
# await ui.backlight_slide(BACKLIGHT_NORMAL)

text = Text("Signing transaction", ui.ICON_SEND, icon_color=ui.BLUE)
text.normal("Signing...")

try:
layout = await layout.simple_text(text, tm=500)
log.debug(__name__, "layout: %s", layout)
workflow.closedefault()
workflow.onlayoutstart(layout)
loop.schedule(slide)
# display.clear()

finally:
pass
# loop.close(slide)
# workflow.onlayoutclose(layout)
layout = await layout.simple_text(text, tm=500)
workflow.closedefault()
workflow.onlayoutstart(layout)
loop.schedule(slide)

await loop.sleep(200 * 1000)
return True
Expand Down
22 changes: 9 additions & 13 deletions src/apps/monero/controller/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class TrezorChangeAddressError(TrezorError):
pass


class StdObj(object):
class StdObj:
def __init__(self, **kwargs):
for kw in kwargs:
setattr(self, kw, kwargs[kw])
Expand Down Expand Up @@ -49,33 +49,29 @@ def compute_tx_key(spend_key_private, tx_prefix_hash, salt=None, rand_mult=None)
return tx_key, salt, rand_mult


async def parse_msg(bts, msg):
def parse_msg(bts, msg):
from apps.monero.xmr.serialize import xmrserialize
from apps.monero.xmr.serialize.readwriter import MemoryReaderWriter

reader = MemoryReaderWriter(memoryview(bts))
ar = xmrserialize.Archive(reader, False)
return await ar.message(msg)
return ar.message(msg)


async def parse_vini(bts):
from apps.monero.xmr.serialize_messages.tx_prefix import TxinToKey

return await parse_msg(bts, TxinToKey())


async def dump_msg(msg, preallocate=None, msg_type=None):
def dump_msg(msg, preallocate=None, msg_type=None, prefix=None):
from apps.monero.xmr.serialize import xmrserialize
from apps.monero.xmr.serialize.readwriter import MemoryReaderWriter

writer = MemoryReaderWriter(preallocate=preallocate)
if prefix:
writer.write(prefix)
ar = xmrserialize.Archive(writer, True)
await ar.message(msg, msg_type=msg_type)
ar.message(msg, msg_type=msg_type)
return writer.get_buffer()


async def dump_msg_gc(msg, preallocate=None, msg_type=None, del_msg=False):
b = await dump_msg(msg, preallocate=preallocate, msg_type=msg_type)
def dump_msg_gc(msg, preallocate=None, msg_type=None, del_msg=False):
b = dump_msg(msg, preallocate=preallocate, msg_type=msg_type)
if del_msg:
del msg

Expand Down
227 changes: 112 additions & 115 deletions src/apps/monero/diag.py
Original file line number Diff line number Diff line change
@@ -1,117 +1,114 @@
import gc
import micropython
import sys
if __debug__:
import gc
import micropython
import sys

from trezor import log

PREV_MEM = gc.mem_free()
CUR_MES = 0

def log_trace(x=None):
log.debug(
__name__,
"Log trace %s, ... F: %s A: %s, S: %s",
x,
gc.mem_free(),
gc.mem_alloc(),
micropython.stack_use(),
)

def check_mem(x=""):
global PREV_MEM, CUR_MES

gc.collect()
free = gc.mem_free()
diff = PREV_MEM - free
log.debug(
__name__,
"======= {} {} Diff: {} Free: {} Allocated: {}".format(
CUR_MES, x, diff, free, gc.mem_alloc()
),
)
micropython.mem_info()
gc.collect()
CUR_MES += 1
PREV_MEM = free

def retit(**kwargs):
from trezor.messages.Failure import Failure

return Failure(**kwargs)

async def diag(ctx, msg, **kwargs):
log.debug(__name__, "----diagnostics")
gc.collect()

if msg.ins == 0:
check_mem(0)
return retit()

elif msg.ins == 1:
check_mem(1)
micropython.mem_info(1)
return retit()

elif msg.ins == 2:
log.debug(__name__, "_____________________________________________")
log.debug(__name__, "_____________________________________________")
log.debug(__name__, "_____________________________________________")
return retit()

elif msg.ins == 3:
pass

elif msg.ins == 4:
total = 0
monero = 0

for k, v in sys.modules.items():
log.info(__name__, "Mod[%s]: %s", k, v)
total += 1
if k.startswith("apps.monero"):
monero += 1
log.info(__name__, "Total modules: %s, Monero modules: %s", total, monero)
return retit()

elif msg.ins in [5, 6, 7]:
check_mem()
from apps.monero.xmr import bulletproof as bp

check_mem("BP Imported")
from apps.monero.xmr import crypto

check_mem("Crypto Imported")

bpi = bp.BulletProofBuilder()
bpi.gc_fnc = gc.collect
bpi.gc_trace = log_trace

vals = [crypto.sc_init((1 << 30) - 1 + 16), crypto.sc_init(22222)]
masks = [crypto.random_scalar(), crypto.random_scalar()]
check_mem("BP pre input")

if msg.ins == 5:
bp_res = bpi.prove_testnet(vals[0], masks[0])
check_mem("BP post prove")
bpi.verify_testnet(bp_res)
check_mem("BP post verify")

elif msg.ins == 6:
bp_res = bpi.prove(vals[0], masks[0])
check_mem("BP post prove")
bpi.verify(bp_res)
check_mem("BP post verify")

elif msg.ins == 7:
bp_res = bpi.prove_batch(vals, masks)
check_mem("BP post prove")
bpi.verify(bp_res)
check_mem("BP post verify")

return retit()

from trezor import log

PREV_MEM = gc.mem_free()
CUR_MES = 0


def log_trace(x=None):
log.debug(
__name__,
"Log trace %s, ... F: %s A: %s, S: %s",
x,
gc.mem_free(),
gc.mem_alloc(),
micropython.stack_use(),
)


def check_mem(x=""):
global PREV_MEM, CUR_MES

gc.collect()
free = gc.mem_free()
diff = PREV_MEM - free
log.debug(
__name__,
"======= {} {} Diff: {} Free: {} Allocated: {}".format(
CUR_MES, x, diff, free, gc.mem_alloc()
),
)
micropython.mem_info()
gc.collect()
CUR_MES += 1
PREV_MEM = free


def retit(**kwargs):
from trezor.messages.Failure import Failure

return Failure(**kwargs)


async def diag(ctx, msg, **kwargs):
log.debug(__name__, "----diagnostics")
gc.collect()

if msg.ins == 0:
check_mem(0)
return retit()

elif msg.ins == 1:
check_mem(1)
micropython.mem_info(1)
return retit()

elif msg.ins == 2:
log.debug(__name__, "_____________________________________________")
log.debug(__name__, "_____________________________________________")
log.debug(__name__, "_____________________________________________")
return retit()

elif msg.ins == 3:
pass

elif msg.ins == 4:
total = 0
monero = 0

for k, v in sys.modules.items():
log.info(__name__, "Mod[%s]: %s", k, v)
total += 1
if k.startswith("apps.monero"):
monero += 1
log.info(__name__, "Total modules: %s, Monero modules: %s", total, monero)
return retit()

elif msg.ins in [5, 6, 7]:
check_mem()
from apps.monero.xmr import bulletproof as bp

check_mem("BP Imported")
from apps.monero.xmr import crypto

check_mem("Crypto Imported")

bpi = bp.BulletProofBuilder()
bpi.gc_fnc = gc.collect
bpi.gc_trace = log_trace

vals = [crypto.sc_init((1 << 30) - 1 + 16), crypto.sc_init(22222)]
masks = [crypto.random_scalar(), crypto.random_scalar()]
check_mem("BP pre input")

if msg.ins == 5:
bp_res = bpi.prove_testnet(vals[0], masks[0])
check_mem("BP post prove")
bpi.verify_testnet(bp_res)
check_mem("BP post verify")

elif msg.ins == 6:
bp_res = bpi.prove(vals[0], masks[0])
check_mem("BP post prove")
bpi.verify(bp_res)
check_mem("BP post verify")

elif msg.ins == 7:
bp_res = bpi.prove_batch(vals, masks)
check_mem("BP post prove")
bpi.verify(bp_res)
check_mem("BP post verify")

return retit()

return retit()
Loading

0 comments on commit 8b4f4d9

Please sign in to comment.