diff --git a/src/apps/monero/controller/iface.py b/src/apps/monero/controller/iface.py index 10fb39c48..334c04363 100644 --- a/src/apps/monero/controller/iface.py +++ b/src/apps/monero/controller/iface.py @@ -81,7 +81,6 @@ 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 diff --git a/src/apps/monero/controller/misc.py b/src/apps/monero/controller/misc.py index 830384655..bbcbe2312 100644 --- a/src/apps/monero/controller/misc.py +++ b/src/apps/monero/controller/misc.py @@ -49,33 +49,33 @@ 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): +def parse_vini(bts): from apps.monero.xmr.serialize_messages.tx_prefix import TxinToKey - return await parse_msg(bts, TxinToKey()) + return parse_msg(bts, TxinToKey()) -async def dump_msg(msg, preallocate=None, msg_type=None): +def dump_msg(msg, preallocate=None, msg_type=None): from apps.monero.xmr.serialize import xmrserialize from apps.monero.xmr.serialize.readwriter import MemoryReaderWriter writer = MemoryReaderWriter(preallocate=preallocate) 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 diff --git a/src/apps/monero/protocol/key_image_sync.py b/src/apps/monero/protocol/key_image_sync.py index 1ce8e82cc..8a7c04b83 100644 --- a/src/apps/monero/protocol/key_image_sync.py +++ b/src/apps/monero/protocol/key_image_sync.py @@ -83,9 +83,7 @@ async def sync(self, ctx, tds): chash = key_image.compute_hash(td) self.hasher.update(chash) - ki, sig = await key_image.export_key_image( - self.creds, self.subaddresses, td - ) + ki, sig = key_image.export_key_image(self.creds, self.subaddresses, td) crypto.encodepoint_into(buff_mv[0:32], ki) crypto.encodeint_into(buff_mv[32:64], sig[0][0]) diff --git a/src/apps/monero/protocol/tsx_sign_builder.py b/src/apps/monero/protocol/tsx_sign_builder.py index 063e29b0c..4e5a06d1c 100644 --- a/src/apps/monero/protocol/tsx_sign_builder.py +++ b/src/apps/monero/protocol/tsx_sign_builder.py @@ -414,7 +414,7 @@ async def gen_hmac_vini(self, src_entr, vini, idx): kwriter = get_keccak_writer() ar = xmrserialize.Archive(kwriter, True) await protobuf.dump_message(kwriter, src_entr) - await ar.message(vini, TxinToKey) + ar.message(vini, TxinToKey) hmac_key_vini = self.hmac_key_txin(idx) hmac_vini = crypto.compute_hmac(hmac_key_vini, kwriter.get_digest()) @@ -436,7 +436,7 @@ async def gen_hmac_vouti(self, dst_entr, tx_out, idx): kwriter = get_keccak_writer() await protobuf.dump_message(kwriter, dst_entr) ar = xmrserialize.Archive(kwriter, True) - await ar.message(tx_out, TxOut) + ar.message(tx_out, TxOut) hmac_key_vouti = self.hmac_key_txout(idx) hmac_vouti = crypto.compute_hmac(hmac_key_vouti, kwriter.get_digest()) @@ -459,14 +459,14 @@ async def gen_hmac_tsxdest(self, dst_entr, idx): hmac_tsxdest = crypto.compute_hmac(hmac_key, kwriter.get_digest()) return hmac_tsxdest - async def _tprefix_update(self): + def _tprefix_update(self): from apps.monero.xmr.serialize_messages.tx_prefix import TransactionPrefix tx_fields = TransactionPrefix.f_specs() self.tx_prefix_hasher.keep() - await self.tx_prefix_hasher.message_field(self.tx, tx_fields[0]) - await self.tx_prefix_hasher.message_field(self.tx, tx_fields[1]) - await self.tx_prefix_hasher.container_size(self.num_inputs(), tx_fields[2][1]) + self.tx_prefix_hasher.message_field(self.tx, tx_fields[0]) + self.tx_prefix_hasher.message_field(self.tx, tx_fields[1]) + self.tx_prefix_hasher.container_size(self.num_inputs(), tx_fields[2][1]) self.tx_prefix_hasher.release() self._mem_trace(10, True) @@ -541,17 +541,17 @@ async def init_transaction(self, tsx_data): # Extra processing, payment id self.tx.version = 2 self.tx.unlock_time = tsx_data.unlock_time - await self.process_payment_id(tsx_data) + self.process_payment_id(tsx_data) await self.compute_sec_keys(tsx_data) gc.collect() # Iterative tx_prefix_hash hash computation - await self._tprefix_update() + self._tprefix_update() gc.collect() # Final message hasher self.full_message_hasher.init(self.use_simple_rct) - await self.full_message_hasher.set_type_fee(self.get_rct_type(), self.get_fee()) + self.full_message_hasher.set_type_fee(self.get_rct_type(), self.get_fee()) # Sub address precomputation if tsx_data.account is not None and tsx_data.minor_indices: @@ -579,7 +579,7 @@ async def init_transaction(self, tsx_data): rsig_data=rsig_data, ) - async def process_payment_id(self, tsx_data): + def process_payment_id(self, tsx_data): """ Payment id -> extra :return: @@ -627,7 +627,7 @@ async def compute_sec_keys(self, tsx_data): writer = get_keccak_writer() await protobuf.dump_message(writer, tsx_data) - await writer.awrite(crypto.encodeint(self.r)) + writer.write(crypto.encodeint(self.r)) self.key_master = crypto.keccak_2hash( writer.get_digest() + crypto.encodeint(crypto.random_scalar()) @@ -746,10 +746,10 @@ async def set_input(self, src_entr): # All inputs done? if self.inp_idx + 1 == self.num_inputs(): - await self.tsx_inputs_done() + self.tsx_inputs_done() return MoneroTransactionSetInputAck( - vini=await misc.dump_msg(vini, preallocate=64), + vini=misc.dump_msg(vini, preallocate=64), vini_hmac=hmac_vini, pseudo_out=pseudo_out, pseudo_out_hmac=pseudo_out_hmac, @@ -757,7 +757,7 @@ async def set_input(self, src_entr): spend_enc=spend_enc, ) - async def tsx_inputs_done(self): + def tsx_inputs_done(self): """ All inputs set :return: @@ -769,9 +769,9 @@ async def tsx_inputs_done(self): raise ValueError("Input count mismatch") if self.in_memory(): - return await self.tsx_inputs_done_inm() + return self.tsx_inputs_done_inm() - async def tsx_inputs_done_inm(self): + def tsx_inputs_done_inm(self): """ In-memory post processing - tx.vin[i] sorting by key image. Used only if number of inputs is small - computable in Trezor without offloading. @@ -781,7 +781,7 @@ async def tsx_inputs_done_inm(self): # Sort tx.in by key image self.source_permutation = list(range(self.num_inputs())) self.source_permutation.sort(key=lambda x: self.tx.vin[x].k_image, reverse=True) - await self._tsx_inputs_permutation(self.source_permutation) + self._tsx_inputs_permutation(self.source_permutation) async def tsx_inputs_permutation(self, permutation): """ @@ -798,10 +798,10 @@ async def tsx_inputs_permutation(self, permutation): if self.in_memory(): return - await self._tsx_inputs_permutation(permutation) + self._tsx_inputs_permutation(permutation) return MoneroTransactionInputsPermutationAck() - async def _tsx_inputs_permutation(self, permutation): + def _tsx_inputs_permutation(self, permutation): """ Set permutation on the inputs - sorted by key image on host. @@ -835,7 +835,7 @@ def swapper(x, y): # Incremental hashing if self.in_memory(): for idx in range(self.num_inputs()): - await self.hash_vini_pseudo_out(self.tx.vin[idx], idx) + self.hash_vini_pseudo_out(self.tx.vin[idx], idx) self._mem_trace("i: %s" % idx if __debug__ else None, True) async def input_vini(self, src_entr, vini, hmac, pseudo_out, pseudo_out_hmac): @@ -874,10 +874,10 @@ async def input_vini(self, src_entr, vini, hmac, pseudo_out, pseudo_out_hmac): if not common.ct_equal(hmac_vini, hmac): raise ValueError("HMAC is not correct") - await self.hash_vini_pseudo_out(vini, self.inp_idx, pseudo_out, pseudo_out_hmac) + self.hash_vini_pseudo_out(vini, self.inp_idx, pseudo_out, pseudo_out_hmac) return MoneroTransactionInputViniAck() - async def hash_vini_pseudo_out( + def hash_vini_pseudo_out( self, vini, inp_idx, pseudo_out=None, pseudo_out_hmac=None ): """ @@ -892,7 +892,7 @@ async def hash_vini_pseudo_out( from apps.monero.xmr.serialize import xmrserialize from apps.monero.xmr.serialize_messages.tx_prefix import TxInV - await self.tx_prefix_hasher.field(vini, TxInV, xser=xmrserialize) + self.tx_prefix_hasher.field(vini, TxInV, xser=xmrserialize) # Pseudo_out incremental hashing - applicable only in simple rct if not self.use_simple_rct or self.use_bulletproof: @@ -908,7 +908,7 @@ async def hash_vini_pseudo_out( else: pseudo_out = self.input_pseudo_outs[inp_idx] - await self.full_message_hasher.set_pseudo_out(pseudo_out) + self.full_message_hasher.set_pseudo_out(pseudo_out) async def all_in_set(self, rsig_data): """ @@ -1029,7 +1029,7 @@ def _return_rsig_data(self, rsig): return MoneroTransactionRsigData(rsig=rsig) - async def _range_proof(self, idx, amount, rsig_data=None): + def _range_proof(self, idx, amount, rsig_data=None): """ Computes rangeproof and related information - out_sk, out_pk, ecdh_info. In order to optimize incremental transaction build, the mask computation is changed compared @@ -1079,19 +1079,19 @@ async def _range_proof(self, idx, amount, rsig_data=None): self._mem_trace("pre-rproof" if __debug__ else None, collect=True) if not self.rsig_offload and self.use_bulletproof: - rsig = await ring_ct.prove_range_bp_batch( - self.output_amounts, self.output_masks - ) + rsig = ring_ct.prove_range_bp_batch(self.output_amounts, self.output_masks) self._mem_trace("post-bp" if __debug__ else None, collect=True) # Incremental hashing - await self.full_message_hasher.rsig_val(rsig, True, raw=False) + self.full_message_hasher.rsig_val(rsig, True, raw=False) self._mem_trace("post-bp-hash" if __debug__ else None, collect=True) - rsig = await misc.dump_msg_gc( + rsig = misc.dump_msg_gc( rsig, preallocate=ring_ct.bp_size(batch_size) + 8, del_msg=True ) - self._mem_trace("post-bp-ser, size: %s" % len(rsig) if __debug__ else None, collect=True) + self._mem_trace( + "post-bp-ser, size: %s" % len(rsig) if __debug__ else None, collect=True + ) elif not self.rsig_offload and not self.use_bulletproof: rsig_buff = bytearray(32 * (64 + 64 + 64 + 1)) @@ -1104,7 +1104,7 @@ async def _range_proof(self, idx, amount, rsig_data=None): del (rsig_buff, rsig_mv, ring_ct) # Incremental hashing - await self.full_message_hasher.rsig_val(rsig, False, raw=True) + self.full_message_hasher.rsig_val(rsig, False, raw=True) self._check_out_commitment(amount, mask, C) elif self.rsig_offload and self.use_bulletproof: @@ -1117,17 +1117,17 @@ async def _range_proof(self, idx, amount, rsig_data=None): for ix in range(batch_size) ] - bp_obj = await misc.parse_msg(rsig_data.rsig, Bulletproof()) + bp_obj = misc.parse_msg(rsig_data.rsig, Bulletproof()) rsig_data.rsig = None - await self.full_message_hasher.rsig_val(bp_obj, True, raw=False) - res = await ring_ct.verify_bp(bp_obj, self.output_amounts, masks) + self.full_message_hasher.rsig_val(bp_obj, True, raw=False) + res = ring_ct.verify_bp(bp_obj, self.output_amounts, masks) self.assrt(res, "BP verification fail") self._mem_trace("BP verified" if __debug__ else None, collect=True) del (bp_obj, ring_ct) elif self.rsig_offload and not self.use_bulletproof: - await self.full_message_hasher.rsig_val(rsig_data.rsig, False, raw=True) + self.full_message_hasher.rsig_val(rsig_data.rsig, False, raw=True) rsig_data.rsig = None else: @@ -1139,7 +1139,7 @@ async def _range_proof(self, idx, amount, rsig_data=None): self.output_masks = [] return rsig, mask - async def _set_out1_ecdh(self, idx, dest_pub_key, amount, mask, amount_key): + def _set_out1_ecdh(self, idx, dest_pub_key, amount, mask, amount_key): from apps.monero.xmr import ring_ct # Mask sum @@ -1161,14 +1161,14 @@ async def _set_out1_ecdh(self, idx, dest_pub_key, amount, mask, amount_key): return out_pk, ecdh_info - async def _set_out1_prefix(self): + def _set_out1_prefix(self): from apps.monero.xmr.serialize_messages.tx_prefix import TransactionPrefix - await self.tx_prefix_hasher.container_size( + self.tx_prefix_hasher.container_size( self.num_dests(), TransactionPrefix.f_specs()[3][1] ) - async def _set_out1_additional_keys(self, dst_entr): + def _set_out1_additional_keys(self, dst_entr): additional_txkey = None additional_txkey_priv = None if self.need_additional_txkeys: @@ -1192,7 +1192,7 @@ async def _set_out1_additional_keys(self, dst_entr): self.additional_tx_private_keys.append(additional_txkey_priv) return additional_txkey_priv - async def _set_out1_derivation(self, dst_entr, additional_txkey_priv): + def _set_out1_derivation(self, dst_entr, additional_txkey_priv): from apps.monero.xmr.sub.addr import addr_eq change_addr = self.change_address() @@ -1223,14 +1223,14 @@ async def _set_out1_tx_out(self, dst_entr, tx_out_key): self._mem_trace(8) # Tx header prefix hashing - await self.tx_prefix_hasher.field(tx_out, TxOut) + self.tx_prefix_hasher.field(tx_out, TxOut) self._mem_trace(9, True) # Hmac dest_entr. hmac_vouti = await self.gen_hmac_vouti(dst_entr, tx_out, self.out_idx) self._mem_trace(10, True) - tx_out_bin = await misc.dump_msg(tx_out, preallocate=34) + tx_out_bin = misc.dump_msg(tx_out, preallocate=34) return tx_out_bin, hmac_vouti async def set_out1(self, dst_entr, dst_entr_hmac, rsig_data=None): @@ -1270,7 +1270,7 @@ async def set_out1(self, dst_entr, dst_entr_hmac, rsig_data=None): # First output - tx prefix hasher - size of the container if self.out_idx == 0: - await self._set_out1_prefix() + self._set_out1_prefix() self._mem_trace(4, True) self.summary_outs_money += dst_entr.amount @@ -1278,13 +1278,13 @@ async def set_out1(self, dst_entr, dst_entr_hmac, rsig_data=None): self._mem_trace(5, True) # Range proof first, memory intensive - rsig, mask = await self._range_proof(self.out_idx, dst_entr.amount, rsig_data) + rsig, mask = self._range_proof(self.out_idx, dst_entr.amount, rsig_data) utils.unimport_end(mods) self._mem_trace(6, True) # Amount key, tx out key - additional_txkey_priv = await self._set_out1_additional_keys(dst_entr) - derivation = await self._set_out1_derivation(dst_entr, additional_txkey_priv) + additional_txkey_priv = self._set_out1_additional_keys(dst_entr) + derivation = self._set_out1_derivation(dst_entr, additional_txkey_priv) amount_key = crypto.derivation_to_scalar(derivation, self.out_idx) tx_out_key = crypto.derive_public_key( derivation, self.out_idx, crypto.decodepoint(dst_entr.addr.spend_public_key) @@ -1297,7 +1297,7 @@ async def set_out1(self, dst_entr, dst_entr_hmac, rsig_data=None): self._mem_trace(11, True) # Out_pk, ecdh_info - out_pk, ecdh_info = await self._set_out1_ecdh( + out_pk, ecdh_info = self._set_out1_ecdh( self.out_idx, dest_pub_key=tx_out_key, amount=dst_entr.amount, @@ -1309,7 +1309,7 @@ async def set_out1(self, dst_entr, dst_entr_hmac, rsig_data=None): # Incremental hashing of the ECDH info. # RctSigBase allows to hash only one of the (ecdh, out_pk) as they are serialized # as whole vectors. Hashing ECDH info saves state space. - await self.full_message_hasher.set_ecdh(ecdh_info) + self.full_message_hasher.set_ecdh(ecdh_info) self._mem_trace(13, True) # Output_pk is stored to the state as it is used during the signature and hashed to the @@ -1326,11 +1326,11 @@ async def set_out1(self, dst_entr, dst_entr_hmac, rsig_data=None): tx_out=tx_out_bin, vouti_hmac=hmac_vouti, rsig_data=self._return_rsig_data(rsig), - out_pk=await misc.dump_msg(out_pk, preallocate=64, msg_type=CtKey), - ecdh_info=await misc.dump_msg(ecdh_info, preallocate=64), + out_pk=misc.dump_msg(out_pk, preallocate=64, msg_type=CtKey), + ecdh_info=misc.dump_msg(ecdh_info, preallocate=64), ) - async def all_out1_set_tx_extra(self): + def all_out1_set_tx_extra(self): from apps.monero.xmr.sub import tsx_helper self.tx.extra = tsx_helper.add_tx_pub_key_to_extra(self.tx.extra, self.r_pub) @@ -1338,20 +1338,20 @@ async def all_out1_set_tx_extra(self): # Not needed to remove - extra is clean # self.tx.extra = await monero.remove_field_from_tx_extra(self.tx.extra, xmrtypes.TxExtraAdditionalPubKeys) if self.need_additional_txkeys: - self.tx.extra = await tsx_helper.add_additional_tx_pub_keys_to_extra( + self.tx.extra = tsx_helper.add_additional_tx_pub_keys_to_extra( self.tx.extra, pub_enc=self.additional_tx_public_keys ) - async def all_out1_set_tx_prefix(self): + def all_out1_set_tx_prefix(self): from apps.monero.xmr.serialize.message_types import BlobType - await self.tx_prefix_hasher.message_field(self.tx, ("extra", BlobType)) # extra + self.tx_prefix_hasher.message_field(self.tx, ("extra", BlobType)) # extra self.tx_prefix_hash = self.tx_prefix_hasher.get_digest() self.tx_prefix_hasher = None # Hash message to the final_message - await self.full_message_hasher.set_message(self.tx_prefix_hash) + self.full_message_hasher.set_message(self.tx_prefix_hash) async def all_out1_set(self): """ @@ -1387,7 +1387,7 @@ async def all_out1_set(self): # Set public key to the extra # Not needed to remove - extra is clean - await self.all_out1_set_tx_extra() + self.all_out1_set_tx_extra() self.additional_tx_public_keys = None gc.collect() @@ -1400,7 +1400,7 @@ async def all_out1_set(self): ) # Hashing transaction prefix - await self.all_out1_set_tx_prefix() + self.all_out1_set_tx_prefix() extra_b = self.tx.extra self.tx = None gc.collect() @@ -1427,7 +1427,7 @@ async def all_out1_set(self): extra=extra_b, tx_prefix_hash=self.tx_prefix_hash, rv=rv_pb ) - async def tsx_mlsag_ecdh_info(self): + def tsx_mlsag_ecdh_info(self): """ Sets ecdh info for the incremental hashing mlsag. @@ -1435,7 +1435,7 @@ async def tsx_mlsag_ecdh_info(self): """ pass - async def tsx_mlsag_out_pk(self): + def tsx_mlsag_out_pk(self): """ Sets out_pk for the incremental hashing mlsag. @@ -1445,7 +1445,7 @@ async def tsx_mlsag_out_pk(self): raise ValueError("Invalid number of ecdh") for out in self.output_pk: - await self.full_message_hasher.set_out_pk(out) + self.full_message_hasher.set_out_pk(out) async def mlsag_done(self): """ @@ -1460,13 +1460,13 @@ async def mlsag_done(self): self.state.set_final_message_done() await self.iface.transaction_step(self.STEP_MLSAG) - await self.tsx_mlsag_ecdh_info() - await self.tsx_mlsag_out_pk() - await self.full_message_hasher.rctsig_base_done() + self.tsx_mlsag_ecdh_info() + self.tsx_mlsag_out_pk() + self.full_message_hasher.rctsig_base_done() self.out_idx = -1 self.inp_idx = -1 - self.full_message = await self.full_message_hasher.get_digest() + self.full_message = self.full_message_hasher.get_digest() self.full_message_hasher = None return MoneroTransactionMlsagDoneAck(full_message_hash=self.full_message) @@ -1651,8 +1651,7 @@ async def sign_input( ) return MoneroTransactionSignInputAck( - signature=await misc.dump_msg_gc(mgs[0], preallocate=488, del_msg=True), - cout=cout, + signature=misc.dump_msg_gc(mgs[0], preallocate=488, del_msg=True), cout=cout ) async def final_msg(self, *args, **kwargs): diff --git a/src/apps/monero/protocol_lite/lite.py b/src/apps/monero/protocol_lite/lite.py index 1d2ee0792..13b81d331 100644 --- a/src/apps/monero/protocol_lite/lite.py +++ b/src/apps/monero/protocol_lite/lite.py @@ -1,5 +1,4 @@ import gc -import micropython import ustruct from trezor import log diff --git a/src/apps/monero/sign_tx.py b/src/apps/monero/sign_tx.py index 975f547b8..4bc52be97 100644 --- a/src/apps/monero/sign_tx.py +++ b/src/apps/monero/sign_tx.py @@ -99,7 +99,7 @@ async def tsx_input_vini(tsx, msg): """ from apps.monero.controller.misc import parse_vini - vini = await parse_vini(msg.vini) + vini = parse_vini(msg.vini) del msg.vini return await tsx.input_vini( @@ -154,7 +154,7 @@ async def tsx_sign_input(tsx, msg): """ from apps.monero.controller.misc import parse_vini - vini = await parse_vini(msg.vini) + vini = parse_vini(msg.vini) del msg.vini return await tsx.sign_input( diff --git a/src/apps/monero/xmr/common.py b/src/apps/monero/xmr/common.py index b86aea83b..52cbec1cf 100644 --- a/src/apps/monero/xmr/common.py +++ b/src/apps/monero/xmr/common.py @@ -1,4 +1,4 @@ -from trezor.crypto import monero, random +from trezor.crypto import monero class XmrException(Exception): @@ -41,4 +41,3 @@ def is_empty(inp): def defval_empty(val, default=None): return val if not is_empty(val) else default - diff --git a/src/apps/monero/xmr/key_image.py b/src/apps/monero/xmr/key_image.py index 2135752e2..dcffceb12 100644 --- a/src/apps/monero/xmr/key_image.py +++ b/src/apps/monero/xmr/key_image.py @@ -19,7 +19,7 @@ def compute_hash(rr): return kck.digest() -async def export_key_image(creds, subaddresses, td): +def export_key_image(creds, subaddresses, td): """ Key image export :param creds: diff --git a/src/apps/monero/xmr/ring_ct.py b/src/apps/monero/xmr/ring_ct.py index d81179354..211a009ee 100644 --- a/src/apps/monero/xmr/ring_ct.py +++ b/src/apps/monero/xmr/ring_ct.py @@ -15,9 +15,9 @@ def bp_size(outputs): return 32 * (21 + outputs + 2 * logM) -async def prove_range_bp(amount, last_mask=None): +def prove_range_bp(amount, last_mask=None): mask = last_mask if last_mask is not None else crypto.random_scalar() - bp_proof = await prove_range_bp_batch([amount], [mask]) + bp_proof = prove_range_bp_batch([amount], [mask]) C = crypto.decodepoint(bp_proof.V[0]) C = crypto.point_mul8(C) @@ -30,7 +30,7 @@ async def prove_range_bp(amount, last_mask=None): return C, mask, bp_proof -async def prove_range_bp_batch(amounts, masks): +def prove_range_bp_batch(amounts, masks): from apps.monero.xmr import bulletproof as bp bpi = bp.BulletProofBuilder() @@ -41,7 +41,7 @@ async def prove_range_bp_batch(amounts, masks): return bp_proof -async def verify_bp(bp_proof, amounts=None, masks=None): +def verify_bp(bp_proof, amounts=None, masks=None): from apps.monero.xmr import bulletproof as bp if amounts: diff --git a/src/apps/monero/xmr/serialize/int_serialize.py b/src/apps/monero/xmr/serialize/int_serialize.py index 939cec1e5..e3dfd1eff 100644 --- a/src/apps/monero/xmr/serialize/int_serialize.py +++ b/src/apps/monero/xmr/serialize/int_serialize.py @@ -1,7 +1,7 @@ _UINT_BUFFER = bytearray(1) -async def load_uint(reader, width): +def load_uint(reader, width): """ Constant-width integer serialization """ @@ -9,20 +9,20 @@ async def load_uint(reader, width): result = 0 shift = 0 for _ in range(width): - await reader.areadinto(buffer) + reader.readinto(buffer) result += buffer[0] << shift shift += 8 return result -async def dump_uint(writer, n, width): +def dump_uint(writer, n, width): """ Constant-width integer serialization """ buffer = _UINT_BUFFER for _ in range(width): buffer[0] = n & 0xff - await writer.awrite(buffer) + writer.write(buffer) n >>= 8 @@ -63,6 +63,8 @@ def dump_uvarint_b_into(n, buffer, offset=0): """ Serializes n as variable size integer to the provided buffer. """ + if n < 0: + raise ValueError("Cannot dump signed value, convert it to unsigned first.") shifted = True while shifted: shifted = n >> 7 @@ -98,3 +100,28 @@ def dump_uint_b_into(n, width, buffer, offset=0): buffer[idx + offset] = n & 0xff n >>= 8 return buffer + + +def load_uvarint(reader): + buffer = _UINT_BUFFER + result = 0 + shift = 0 + byte = 0x80 + while byte & 0x80: + reader.readinto(buffer) + byte = buffer[0] + result += (byte & 0x7F) << shift + shift += 7 + return result + + +def dump_uvarint(writer, n): + if n < 0: + raise ValueError("Cannot dump signed value, convert it to unsigned first.") + buffer = _UINT_BUFFER + shifted = True + while shifted: + shifted = n >> 7 + buffer[0] = (n & 0x7F) | (0x80 if shifted else 0x00) + writer.write(buffer) + n = shifted diff --git a/src/apps/monero/xmr/serialize/message_types.py b/src/apps/monero/xmr/serialize/message_types.py index 8eab70dea..ab6a5b996 100644 --- a/src/apps/monero/xmr/serialize/message_types.py +++ b/src/apps/monero/xmr/serialize/message_types.py @@ -102,8 +102,8 @@ def _field(self, fname=None, idx=None): fld = specs[idx] return fld - async def _msg_field(self, ar, fname=None, idx=None, **kwargs): - return await ar.message_field(self, self._field(fname=fname, idx=idx), **kwargs) + def _msg_field(self, ar, fname=None, idx=None, **kwargs): + return ar.message_field(self, self._field(fname=fname, idx=idx), **kwargs) def container_elem_type(container_type, params): diff --git a/src/apps/monero/xmr/serialize/readwriter.py b/src/apps/monero/xmr/serialize/readwriter.py index 0eba726e1..54cd8ddaa 100644 --- a/src/apps/monero/xmr/serialize/readwriter.py +++ b/src/apps/monero/xmr/serialize/readwriter.py @@ -38,7 +38,7 @@ def preallocate(self, size): self.offset = 0 self.woffset = 0 - async def areadinto(self, buf): + def readinto(self, buf): ln = len(buf) if not self.read_empty and ln > 0 and self.offset == len(self.buffer): raise EOFError @@ -62,7 +62,10 @@ async def areadinto(self, buf): return nread - async def awrite(self, buf): + async def areadinto(self, buf): + return self.readinto(buf) + + def write(self, buf): nwritten = len(buf) nall = len(self.buffer) towrite = nwritten @@ -95,6 +98,9 @@ async def awrite(self, buf): self.ndata += nwritten return nwritten + async def awrite(self, buf): + return self.write(buf) + def get_buffer(self): mv = memoryview(self.buffer) return mv[self.offset : self.woffset] diff --git a/src/apps/monero/xmr/serialize/xmrserialize.py b/src/apps/monero/xmr/serialize/xmrserialize.py index 57b368e43..c565ccfbc 100644 --- a/src/apps/monero/xmr/serialize/xmrserialize.py +++ b/src/apps/monero/xmr/serialize/xmrserialize.py @@ -33,12 +33,16 @@ import sys -from protobuf import dump_uvarint, load_uvarint from trezor import log from apps.monero.xmr.serialize.base_types import IntType, UVarintType, XmrType from apps.monero.xmr.serialize.erefs import eref, get_elem, set_elem -from apps.monero.xmr.serialize.int_serialize import dump_uint, load_uint +from apps.monero.xmr.serialize.int_serialize import ( + dump_uint, + load_uint, + dump_uvarint, + load_uvarint, +) from apps.monero.xmr.serialize.message_types import ( BlobType, ContainerType, @@ -81,7 +85,7 @@ def __init__(self, iobj, writing=True, **kwargs): self.writing = writing self.iobj = iobj - async def prepare_container(self, size, container, elem_type=None): + def prepare_container(self, size, container, elem_type=None): """ Prepares container for serialization """ @@ -96,7 +100,7 @@ async def prepare_container(self, size, container, elem_type=None): set_elem(container, fvalue) return fvalue - async def prepare_message(self, msg, msg_type): + def prepare_message(self, msg, msg_type): """ Prepares message for serialization """ @@ -104,75 +108,67 @@ async def prepare_message(self, msg, msg_type): return return set_elem(msg, msg_type()) - async def uvarint(self, elem): + def uvarint(self, elem): """ Uvarint """ if self.writing: - return await dump_uvarint(self.iobj, elem) + return dump_uvarint(self.iobj, elem) else: - return await load_uvarint(self.iobj) + return load_uvarint(self.iobj) - async def uint(self, elem, elem_type, params=None): + def uint(self, elem, elem_type, params=None): """ Fixed size int """ if self.writing: - return await dump_uint(self.iobj, elem, elem_type.WIDTH) + return dump_uint(self.iobj, elem, elem_type.WIDTH) else: - return await load_uint(self.iobj, elem_type.WIDTH) + return load_uint(self.iobj, elem_type.WIDTH) - async def unicode_type(self, elem): + def unicode_type(self, elem): """ Unicode type """ if self.writing: - return await dump_unicode(self.iobj, elem) + return dump_unicode(self.iobj, elem) else: - return await load_unicode(self.iobj) + return load_unicode(self.iobj) - async def blob(self, elem=None, elem_type=None, params=None): + def blob(self, elem=None, elem_type=None, params=None): """ Loads/dumps blob """ elem_type = elem_type if elem_type else elem.__class__ if hasattr(elem_type, "serialize_archive"): elem = elem_type() if elem is None else elem - return await elem.serialize_archive( + return elem.serialize_archive( self, elem=elem, elem_type=elem_type, params=params ) if self.writing: - return await dump_blob( - self.iobj, elem=elem, elem_type=elem_type, params=params - ) + return dump_blob(self.iobj, elem=elem, elem_type=elem_type, params=params) else: - return await load_blob( - self.iobj, elem_type=elem_type, params=params, elem=elem - ) + return load_blob(self.iobj, elem_type=elem_type, params=params, elem=elem) - async def container(self, container=None, container_type=None, params=None): + def container(self, container=None, container_type=None, params=None): """ Loads/dumps container """ if hasattr(container_type, "serialize_archive"): container = container_type() if container is None else container - return await container.serialize_archive( + return container.serialize_archive( self, elem=container, elem_type=container_type, params=params ) if self.writing: - return await self._dump_container( - self.iobj, container, container_type, params - ) + return self._dump_container(self.iobj, container, container_type, params) else: - return await self._load_container( + return self._load_container( self.iobj, container_type, params=params, container=container ) - async def container_size( - self, container_len=None, container_type=None, params=None - ): + def container_size(self, container_len=None, container_type=None, params=None): """ Container size """ @@ -180,62 +176,58 @@ async def container_size( raise ValueError("not supported") if self.writing: - return await self._dump_container_size( + return self._dump_container_size( self.iobj, container_len, container_type, params ) else: raise ValueError("Not supported") - async def container_val(self, elem, container_type, params=None): + def container_val(self, elem, container_type, params=None): """ Single cont value """ if hasattr(container_type, "serialize_archive"): raise ValueError("not supported") if self.writing: - return await self._dump_container_val( - self.iobj, elem, container_type, params - ) + return self._dump_container_val(self.iobj, elem, container_type, params) else: raise ValueError("Not supported") - async def tuple(self, elem=None, elem_type=None, params=None): + def tuple(self, elem=None, elem_type=None, params=None): """ Loads/dumps tuple """ if hasattr(elem_type, "serialize_archive"): container = elem_type() if elem is None else elem - return await container.serialize_archive( + return container.serialize_archive( self, elem=elem, elem_type=elem_type, params=params ) if self.writing: - return await self._dump_tuple(self.iobj, elem, elem_type, params) + return self._dump_tuple(self.iobj, elem, elem_type, params) else: - return await self._load_tuple( - self.iobj, elem_type, params=params, elem=elem - ) + return self._load_tuple(self.iobj, elem_type, params=params, elem=elem) - async def variant(self, elem=None, elem_type=None, params=None, wrapped=None): + def variant(self, elem=None, elem_type=None, params=None, wrapped=None): """ Loads/dumps variant type """ elem_type = elem_type if elem_type else elem.__class__ if hasattr(elem_type, "serialize_archive"): elem = elem_type() if elem is None else elem - return await elem.serialize_archive( + return elem.serialize_archive( self, elem=elem, elem_type=elem_type, params=params ) if self.writing: - return await self._dump_variant( + return self._dump_variant( self.iobj, elem=elem, elem_type=elem_type if elem_type else elem.__class__, params=params, ) else: - return await self._load_variant( + return self._load_variant( self.iobj, elem_type=elem_type if elem_type else elem.__class__, params=params, @@ -243,35 +235,35 @@ async def variant(self, elem=None, elem_type=None, params=None, wrapped=None): wrapped=wrapped, ) - async def message(self, msg, msg_type=None): + def message(self, msg, msg_type=None): """ Loads/dumps message """ elem_type = msg_type if msg_type is not None else msg.__class__ if hasattr(elem_type, "serialize_archive"): msg = elem_type() if msg is None else msg - return await msg.serialize_archive(self) + return msg.serialize_archive(self) if self.writing: - return await self._dump_message(self.iobj, msg, msg_type=msg_type) + return self._dump_message(self.iobj, msg, msg_type=msg_type) else: - return await self._load_message(self.iobj, msg_type, msg=msg) + return self._load_message(self.iobj, msg_type, msg=msg) - async def message_field(self, msg, field, fvalue=None): + def message_field(self, msg, field, fvalue=None): """ Dumps/Loads message field """ if self.writing: - await self._dump_message_field(self.iobj, msg, field, fvalue=fvalue) + self._dump_message_field(self.iobj, msg, field, fvalue=fvalue) else: - await self._load_message_field(self.iobj, msg, field) + self._load_message_field(self.iobj, msg, field) - async def message_fields(self, msg, fields): + def message_fields(self, msg, fields): """ Load/dump individual message fields """ for field in fields: - await self.message_field(msg, field) + self.message_field(msg, field) return msg def _get_type(self, elem_type): @@ -323,44 +315,38 @@ def _get_type(self, elem_type): def _is_type(self, elem_type, test_type): return issubclass(elem_type, test_type) - async def field(self, elem=None, elem_type=None, params=None): + def field(self, elem=None, elem_type=None, params=None): elem_type = elem_type if elem_type else elem.__class__ fvalue = None etype = self._get_type(elem_type) if self._is_type(etype, UVarintType): - fvalue = await self.uvarint(get_elem(elem)) + fvalue = self.uvarint(get_elem(elem)) elif self._is_type(etype, IntType): - fvalue = await self.uint( - elem=get_elem(elem), elem_type=elem_type, params=params - ) + fvalue = self.uint(elem=get_elem(elem), elem_type=elem_type, params=params) elif self._is_type(etype, BlobType): - fvalue = await self.blob( - elem=get_elem(elem), elem_type=elem_type, params=params - ) + fvalue = self.blob(elem=get_elem(elem), elem_type=elem_type, params=params) elif self._is_type(etype, UnicodeType): - fvalue = await self.unicode_type(get_elem(elem)) + fvalue = self.unicode_type(get_elem(elem)) elif self._is_type(etype, VariantType): - fvalue = await self.variant( + fvalue = self.variant( elem=get_elem(elem), elem_type=elem_type, params=params ) elif self._is_type(etype, ContainerType): # container ~ simple list - fvalue = await self.container( + fvalue = self.container( container=get_elem(elem), container_type=elem_type, params=params ) elif self._is_type(etype, TupleType): # tuple ~ simple list - fvalue = await self.tuple( - elem=get_elem(elem), elem_type=elem_type, params=params - ) + fvalue = self.tuple(elem=get_elem(elem), elem_type=elem_type, params=params) elif self._is_type(etype, MessageType): - fvalue = await self.message(get_elem(elem), msg_type=elem_type) + fvalue = self.message(get_elem(elem), msg_type=elem_type) else: raise TypeError( @@ -369,72 +355,62 @@ async def field(self, elem=None, elem_type=None, params=None): return fvalue if self.writing else set_elem(elem, fvalue) - async def dump_field(self, writer, elem, elem_type, params=None): + def dump_field(self, writer, elem, elem_type, params=None): assert self.iobj == writer - return await self.field(elem=elem, elem_type=elem_type, params=params) + return self.field(elem=elem, elem_type=elem_type, params=params) - async def load_field(self, reader, elem_type, params=None, elem=None): + def load_field(self, reader, elem_type, params=None, elem=None): assert self.iobj == reader - return await self.field(elem=elem, elem_type=elem_type, params=params) + return self.field(elem=elem, elem_type=elem_type, params=params) - async def root(self): + def root(self): """ Root level archive init """ - async def _dump_container_size( - self, writer, container_len, container_type, params=None - ): + def _dump_container_size(self, writer, container_len, container_type, params=None): """ Dumps container size - per element streaming """ if not container_type or not container_type.FIX_SIZE: - await dump_uvarint(writer, container_len) + dump_uvarint(writer, container_len) elif container_len != container_type.SIZE: raise ValueError( "Fixed size container has not defined size: %s" % container_type.SIZE ) - async def _dump_container_val(self, writer, elem, container_type, params=None): + def _dump_container_val(self, writer, elem, container_type, params=None): """ Single elem dump """ elem_type = container_elem_type(container_type, params) - await self.dump_field(writer, elem, elem_type, params[1:] if params else None) + self.dump_field(writer, elem, elem_type, params[1:] if params else None) - async def _dump_container(self, writer, container, container_type, params=None): + def _dump_container(self, writer, container, container_type, params=None): """ Dumps container of elements to the writer. """ - await self._dump_container_size(writer, len(container), container_type) + self._dump_container_size(writer, len(container), container_type) elem_type = container_elem_type(container_type, params) for elem in container: - await self.dump_field( - writer, elem, elem_type, params[1:] if params else None - ) + self.dump_field(writer, elem, elem_type, params[1:] if params else None) - async def _load_container( - self, reader, container_type, params=None, container=None - ): + def _load_container(self, reader, container_type, params=None, container=None): """ Loads container of elements from the reader. Supports the container ref. Returns loaded container. """ - c_len = ( - container_type.SIZE - if container_type.FIX_SIZE - else await load_uvarint(reader) - ) + c_len = container_type.SIZE if container_type.FIX_SIZE else load_uvarint(reader) if container and c_len != len(container): raise ValueError("Size mismatch") elem_type = container_elem_type(container_type, params) res = container if container else [] for i in range(c_len): - fvalue = await self.load_field( + fvalue = self.load_field( reader, elem_type, params[1:] if params else None, @@ -444,7 +420,7 @@ async def _load_container( res.append(fvalue) return res - async def _dump_tuple(self, writer, elem, elem_type, params=None): + def _dump_tuple(self, writer, elem, elem_type, params=None): """ Dumps tuple of elements to the writer. """ @@ -452,23 +428,23 @@ async def _dump_tuple(self, writer, elem, elem_type, params=None): raise ValueError( "Fixed size tuple has not defined size: %s" % len(elem_type.f_specs()) ) - await dump_uvarint(writer, len(elem)) + dump_uvarint(writer, len(elem)) elem_fields = params[0] if params else None if elem_fields is None: elem_fields = elem_type.f_specs() for idx, elem in enumerate(elem): - await self.dump_field( + self.dump_field( writer, elem, elem_fields[idx], params[1:] if params else None ) - async def _load_tuple(self, reader, elem_type, params=None, elem=None): + def _load_tuple(self, reader, elem_type, params=None, elem=None): """ Loads tuple of elements from the reader. Supports the tuple ref. Returns loaded tuple. """ - c_len = await load_uvarint(reader) + c_len = load_uvarint(reader) if elem and c_len != len(elem): raise ValueError("Size mismatch") if c_len != len(elem_type.f_specs()): @@ -480,7 +456,7 @@ async def _load_tuple(self, reader, elem_type, params=None, elem=None): res = elem if elem else [] for i in range(c_len): - fvalue = await self.load_field( + fvalue = self.load_field( reader, elem_fields[i], params[1:] if params else None, @@ -490,23 +466,23 @@ async def _load_tuple(self, reader, elem_type, params=None, elem=None): res.append(fvalue) return res - async def _dump_message_field(self, writer, msg, field, fvalue=None): + def _dump_message_field(self, writer, msg, field, fvalue=None): """ Dumps a message field to the writer. Field is defined by the message field specification. """ fname, ftype, params = field[0], field[1], field[2:] fvalue = getattr(msg, fname, None) if fvalue is None else fvalue - await self.dump_field(writer, fvalue, ftype, params) + self.dump_field(writer, fvalue, ftype, params) - async def _load_message_field(self, reader, msg, field): + def _load_message_field(self, reader, msg, field): """ Loads message field from the reader. Field is defined by the message field specification. Returns loaded value, supports field reference. """ fname, ftype, params = field[0], field[1], field[2:] - await self.load_field(reader, ftype, params, eref(msg, fname)) + self.load_field(reader, ftype, params, eref(msg, fname)) - async def _dump_message(self, writer, msg, msg_type=None): + def _dump_message(self, writer, msg, msg_type=None): """ Dumps message to the writer. """ @@ -516,9 +492,9 @@ async def _dump_message(self, writer, msg, msg_type=None): raise ValueError("Cannot directly load, has to use archive with %s" % mtype) for field in fields: - await self._dump_message_field(writer, msg=msg, field=field) + self._dump_message_field(writer, msg=msg, field=field) - async def _load_message(self, reader, msg_type, msg=None): + def _load_message(self, reader, msg_type, msg=None): """ Loads message if the given type from the reader. Supports reading directly to existing message. @@ -531,29 +507,27 @@ async def _load_message(self, reader, msg_type, msg=None): ) for field in fields: - await self._load_message_field(reader, msg, field) + self._load_message_field(reader, msg, field) return msg - async def _dump_variant(self, writer, elem, elem_type=None, params=None): + def _dump_variant(self, writer, elem, elem_type=None, params=None): """ Dumps variant type to the writer. Supports both wrapped and raw variant. """ if isinstance(elem, VariantType) or elem_type.WRAPS_VALUE: - await dump_uint(writer, elem.variant_elem_type.VARIANT_CODE, 1) - await self.dump_field( + dump_uint(writer, elem.variant_elem_type.VARIANT_CODE, 1) + self.dump_field( writer, getattr(elem, elem.variant_elem), elem.variant_elem_type ) else: fdef = find_variant_fdef(elem_type, elem) - await dump_uint(writer, fdef[1].VARIANT_CODE, 1) - await self.dump_field(writer, elem, fdef[1]) + dump_uint(writer, fdef[1].VARIANT_CODE, 1) + self.dump_field(writer, elem, fdef[1]) - async def _load_variant( - self, reader, elem_type, params=None, elem=None, wrapped=None - ): + def _load_variant(self, reader, elem_type, params=None, elem=None, wrapped=None): """ Loads variant type from the reader. Supports both wrapped and raw variant. @@ -566,11 +540,11 @@ async def _load_variant( if is_wrapped: elem = elem_type() if elem is None else elem - tag = await load_uint(reader, 1) + tag = load_uint(reader, 1) for field in elem_type.f_specs(): ftype = field[1] if ftype.VARIANT_CODE == tag: - fvalue = await self.load_field( + fvalue = self.load_field( reader, ftype, field[2:], elem if not is_wrapped else None ) if is_wrapped: @@ -579,7 +553,7 @@ async def _load_variant( raise ValueError("Unknown tag: %s" % tag) -async def dump_blob(writer, elem, elem_type, params=None): +def dump_blob(writer, elem, elem_type, params=None): """ Dumps blob message to the writer. Supports both blob and raw value. @@ -589,19 +563,19 @@ async def dump_blob(writer, elem, elem_type, params=None): data = bytes(getattr(elem, BlobType.DATA_ATTR) if elem_is_blob else elem) if not elem_params.FIX_SIZE: - await dump_uvarint(writer, len(elem)) + dump_uvarint(writer, len(elem)) elif len(data) != elem_params.SIZE: raise ValueError("Fixed size blob has not defined size: %s" % elem_params.SIZE) - await writer.awrite(data) + writer.write(data) -async def load_blob(reader, elem_type, params=None, elem=None): +def load_blob(reader, elem_type, params=None, elem=None): """ Loads blob from reader to the element. Returns the loaded blob. """ - ivalue = elem_type.SIZE if elem_type.FIX_SIZE else await load_uvarint(reader) + ivalue = elem_type.SIZE if elem_type.FIX_SIZE else load_uvarint(reader) fvalue = bytearray(ivalue) - await reader.areadinto(fvalue) + reader.readinto(fvalue) if elem is None: return fvalue # array by default @@ -616,15 +590,15 @@ async def load_blob(reader, elem_type, params=None, elem=None): return elem -async def dump_unicode(writer, elem): - await dump_uvarint(writer, len(elem)) - await writer.awrite(bytes(elem, "utf8")) +def dump_unicode(writer, elem): + dump_uvarint(writer, len(elem)) + writer.write(bytes(elem, "utf8")) -async def load_unicode(reader): - ivalue = await load_uvarint(reader) +def load_unicode(reader): + ivalue = load_uvarint(reader) fvalue = bytearray(ivalue) - await reader.areadinto(fvalue) + reader.readinto(fvalue) return str(fvalue, "utf8") diff --git a/src/apps/monero/xmr/serialize_messages/tx_full.py b/src/apps/monero/xmr/serialize_messages/tx_full.py index 08f6acf7b..57de1752f 100644 --- a/src/apps/monero/xmr/serialize_messages/tx_full.py +++ b/src/apps/monero/xmr/serialize_messages/tx_full.py @@ -39,14 +39,14 @@ def f_specs(cls): ("outPk", CtkeyV), ) - async def serialize_rctsig_base(self, ar, inputs, outputs): + def serialize_rctsig_base(self, ar, inputs, outputs): """ Custom serialization :param ar: :type ar: x.Archive :return: """ - await self._msg_field(ar, idx=0) + self._msg_field(ar, idx=0) if self.type == RctType.Null: return if ( @@ -57,25 +57,25 @@ async def serialize_rctsig_base(self, ar, inputs, outputs): ): raise ValueError("Unknown type") - await self._msg_field(ar, idx=1) + self._msg_field(ar, idx=1) if self.type == RctType.Simple: - await ar.prepare_container(inputs, eref(self, "pseudoOuts"), KeyV) + ar.prepare_container(inputs, eref(self, "pseudoOuts"), KeyV) if ar.writing and len(self.pseudoOuts) != inputs: raise ValueError("pseudoOuts size mismatch") for i in range(inputs): - await ar.field(eref(self.pseudoOuts, i), KeyV.ELEM_TYPE) + ar.field(eref(self.pseudoOuts, i), KeyV.ELEM_TYPE) - await ar.prepare_container(outputs, eref(self, "ecdhInfo"), EcdhTuple) + ar.prepare_container(outputs, eref(self, "ecdhInfo"), EcdhTuple) if ar.writing and len(self.ecdhInfo) != outputs: raise ValueError("EcdhInfo size mismatch") for i in range(outputs): - await ar.field(eref(self.ecdhInfo, i), EcdhInfo.ELEM_TYPE) + ar.field(eref(self.ecdhInfo, i), EcdhInfo.ELEM_TYPE) - await ar.prepare_container((outputs), eref(self, "outPk"), CtKey) + ar.prepare_container((outputs), eref(self, "outPk"), CtKey) if ar.writing and len(self.outPk) != outputs: raise ValueError("outPk size mismatch") for i in range(outputs): - await ar.field(eref(self.outPk[i], "mask"), ECKey) + ar.field(eref(self.outPk[i], "mask"), ECKey) diff --git a/src/apps/monero/xmr/serialize_messages/tx_sig.py b/src/apps/monero/xmr/serialize_messages/tx_sig.py index 3823f9238..e3370a014 100644 --- a/src/apps/monero/xmr/serialize_messages/tx_sig.py +++ b/src/apps/monero/xmr/serialize_messages/tx_sig.py @@ -16,7 +16,7 @@ class Signature(MessageType): def f_specs(cls): return (("c", ECKey), ("r", ECKey)) - async def serialize_archive(self, ar): + def serialize_archive(self, ar): ar.field(eref(self, "c"), ECKey) ar.field(eref(self, "r"), ECKey) return self diff --git a/src/apps/monero/xmr/sub/keccak_hasher.py b/src/apps/monero/xmr/sub/keccak_hasher.py index a0a2e90bf..4da38f7ce 100644 --- a/src/apps/monero/xmr/sub/keccak_hasher.py +++ b/src/apps/monero/xmr/sub/keccak_hasher.py @@ -36,19 +36,19 @@ def keep(self, keep=True): def release(self): self.ar = None - async def field(self, elem=None, elem_type=None, params=None, xser=None): + def field(self, elem=None, elem_type=None, params=None, xser=None): ar = self._ar(xser) - return await ar.field(elem, elem_type, params) + return ar.field(elem, elem_type, params) - async def message_field(self, msg, field, fvalue=None, xser=None): + def message_field(self, msg, field, fvalue=None, xser=None): ar = self._ar(xser) - return await ar.message_field(msg, field, fvalue) + return ar.message_field(msg, field, fvalue) - async def container_size( + def container_size( self, container_len=None, container_type=None, params=None, xser=None ): ar = self._ar(xser) - return await ar.container_size(container_len, container_type, params) + return ar.container_size(container_len, container_type, params) class HashWrapper: @@ -68,16 +68,16 @@ def hexdigest(self): class AHashWriter: - def __init__(self, hasher, sub_writer=None): + def __init__(self, hasher): self.hasher = hasher - self.sub_writer = sub_writer - async def awrite(self, buf): + def write(self, buf): self.hasher.update(buf) - if self.sub_writer: - await self.sub_writer.awrite(buf) return len(buf) + async def awrite(self, buf): + return self.write(buf) + def get_digest(self, *args) -> bytes: return self.hasher.digest(*args) @@ -85,10 +85,5 @@ def ctx(self): return self.hasher.ctx -def get_keccak_writer(sub_writer=None, ctx=None): - """ - Creates new fresh async Keccak writer - """ - return AHashWriter( - HashWrapper(crypto.get_keccak() if ctx is None else ctx), sub_writer=sub_writer - ) +def get_keccak_writer(ctx=None): + return AHashWriter(HashWrapper(crypto.get_keccak() if ctx is None else ctx)) diff --git a/src/apps/monero/xmr/sub/mlsag_hasher.py b/src/apps/monero/xmr/sub/mlsag_hasher.py index eeb8a8db7..2958ecd4c 100644 --- a/src/apps/monero/xmr/sub/mlsag_hasher.py +++ b/src/apps/monero/xmr/sub/mlsag_hasher.py @@ -47,10 +47,10 @@ def init(self, is_simple): self.state = 1 self.is_simple = is_simple - async def set_message(self, message): + def set_message(self, message): self.kc_master.update(message) - async def set_type_fee(self, rv_type, fee): + def set_type_fee(self, rv_type, fee): if self.state != 1: raise ValueError("State error") self.state = 2 @@ -58,37 +58,37 @@ async def set_type_fee(self, rv_type, fee): from apps.monero.xmr.serialize_messages.tx_full import RctSigBase rfields = RctSigBase.f_specs() - await self.rtcsig_hasher.message_field(None, field=rfields[0], fvalue=rv_type) - await self.rtcsig_hasher.message_field(None, field=rfields[1], fvalue=fee) + self.rtcsig_hasher.message_field(None, field=rfields[0], fvalue=rv_type) + self.rtcsig_hasher.message_field(None, field=rfields[1], fvalue=fee) - async def set_pseudo_out(self, out): + def set_pseudo_out(self, out): if self.state != 2 and self.state != 3: raise ValueError("State error") self.state = 3 from apps.monero.xmr.serialize_messages.ct_keys import KeyV - await self.rtcsig_hasher.field(out, KeyV.ELEM_TYPE) + self.rtcsig_hasher.field(out, KeyV.ELEM_TYPE) - async def set_ecdh(self, ecdh): + def set_ecdh(self, ecdh): if self.state != 2 and self.state != 3 and self.state != 4: raise ValueError("State error") self.state = 4 from apps.monero.xmr.serialize_messages.tx_ecdh import EcdhInfo - await self.rtcsig_hasher.field(ecdh, EcdhInfo.ELEM_TYPE) + self.rtcsig_hasher.field(ecdh, EcdhInfo.ELEM_TYPE) - async def set_out_pk(self, out_pk, mask=None): + def set_out_pk(self, out_pk, mask=None): if self.state != 4 and self.state != 5: raise ValueError("State error") self.state = 5 from apps.monero.xmr.serialize_messages.base import ECKey - await self.rtcsig_hasher.field(mask if mask else out_pk.mask, ECKey) + self.rtcsig_hasher.field(mask if mask else out_pk.mask, ECKey) - async def rctsig_base_done(self): + def rctsig_base_done(self): if self.state != 5: raise ValueError("State error") self.state = 6 @@ -97,7 +97,7 @@ async def rctsig_base_done(self): self.kc_master.update(c_hash) self.rtcsig_hasher = None - async def rsig_val(self, p, bulletproof, raw=False): + def rsig_val(self, p, bulletproof, raw=False): if self.state == 8: raise ValueError("State error") @@ -129,7 +129,7 @@ async def rsig_val(self, p, bulletproof, raw=False): for i in range(64): self.rsig_hasher.update(p.Ci[i]) - async def get_digest(self): + def get_digest(self): if self.state != 6: raise ValueError("State error") self.state = 8 diff --git a/src/apps/monero/xmr/sub/tsx_helper.py b/src/apps/monero/xmr/sub/tsx_helper.py index 0c783de9e..171e8d515 100644 --- a/src/apps/monero/xmr/sub/tsx_helper.py +++ b/src/apps/monero/xmr/sub/tsx_helper.py @@ -9,7 +9,7 @@ ) -async def parse_extra_fields(extra_buff): +def parse_extra_fields(extra_buff): """ Parses extra buffer to the extra fields vector """ @@ -17,7 +17,7 @@ async def parse_extra_fields(extra_buff): rw = MemoryReaderWriter(extra_buff) ar2 = xmrserialize.Archive(rw, False) while len(rw.get_buffer()) > 0: - extras.append(await ar2.variant(elem_type=TxExtraField)) + extras.append(ar2.variant(elem_type=TxExtraField)) return extras @@ -134,7 +134,7 @@ def set_encrypted_payment_id_to_tx_extra_nonce(payment_id): return b"\x01" + payment_id -async def remove_field_from_tx_extra(extra, mtype): +def remove_field_from_tx_extra(extra, mtype): """ Removes extra field of fiven type from the buffer Reserializes with skipping the given mtype. @@ -147,9 +147,9 @@ async def remove_field_from_tx_extra(extra, mtype): ar_read = xmrserialize.Archive(reader, False) ar_write = xmrserialize.Archive(writer, True) while len(reader.get_buffer()) > 0: - c_extras = await ar_read.variant(elem_type=TxExtraField) + c_extras = ar_read.variant(elem_type=TxExtraField) if not isinstance(c_extras, mtype): - await ar_write.variant(c_extras, elem_type=TxExtraField) + ar_write.variant(c_extras, elem_type=TxExtraField) return writer.get_buffer() @@ -174,7 +174,7 @@ def add_tx_pub_key_to_extra(tx_extra, pub_key): return tx_extra + to_add -async def add_additional_tx_pub_keys_to_extra( +def add_additional_tx_pub_keys_to_extra( tx_extra, additional_pub_keys=None, pub_enc=None ): """ @@ -190,6 +190,6 @@ async def add_additional_tx_pub_keys_to_extra( ar = xmrserialize.Archive(rw, True) # format: variant_tag (0x4) | array len varint | 32B | 32B | ... - await ar.variant(pubs_msg, TxExtraField) + ar.variant(pubs_msg, TxExtraField) tx_extra += bytes(rw.get_buffer()) return tx_extra