Merge pull request #886 from trezor/onvej-sl/monero-optimizations-squashed

xmr/bp: memory optimizations and improvements, fixed style
pull/889/head
Pavol Rusnak 4 years ago committed by GitHub
commit 624b639ba8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -972,13 +972,15 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(
mod_trezorcrypto_monero_xmr_random_scalar_obj, 0, 1,
mod_trezorcrypto_monero_xmr_random_scalar);
/// def xmr_fast_hash(r: Optional[bytes], buff: bytes) -> bytes:
// clang-format off
/// def xmr_fast_hash(r: Optional[bytes], buff: bytes, length: int, offset: int) -> bytes:
// clang-format on
/// """
/// XMR fast hash
/// """
STATIC mp_obj_t mod_trezorcrypto_monero_xmr_fast_hash(size_t n_args,
const mp_obj_t *args) {
const int off = n_args == 2 ? 0 : -1;
const int off = n_args >= 2 ? 0 : -1;
uint8_t buff[32];
uint8_t *buff_use = buff;
if (n_args > 1) {
@ -992,47 +994,76 @@ STATIC mp_obj_t mod_trezorcrypto_monero_xmr_fast_hash(size_t n_args,
mp_buffer_info_t data;
mp_get_buffer_raise(args[1 + off], &data, MP_BUFFER_READ);
xmr_fast_hash(buff_use, data.buf, data.len);
return n_args == 2 ? args[0] : mp_obj_new_bytes(buff, 32);
mp_int_t length = n_args >= 3 ? mp_obj_get_int(args[2]) : data.len;
mp_int_t offset = n_args >= 4 ? mp_obj_get_int(args[3]) : 0;
if (length < 0) length += data.len;
if (offset < 0) offset += data.len;
if (length < 0 || offset < 0 || offset + length > data.len) {
mp_raise_ValueError("Illegal offset/length");
}
xmr_fast_hash(buff_use, (const char *)data.buf + offset, length);
return n_args >= 2 ? args[0] : mp_obj_new_bytes(buff, 32);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(
mod_trezorcrypto_monero_xmr_fast_hash_obj, 1, 2,
mod_trezorcrypto_monero_xmr_fast_hash_obj, 1, 4,
mod_trezorcrypto_monero_xmr_fast_hash);
/// def xmr_hash_to_ec(r: Optional[Ge25519], buff: bytes) -> Ge25519:
// clang-format off
/// def xmr_hash_to_ec(r: Optional[Ge25519], buff: bytes, length: int, offset:
/// int) -> Ge25519:
// clang-format on
/// """
/// XMR hashing to EC point
/// """
STATIC mp_obj_t mod_trezorcrypto_monero_xmr_hash_to_ec(size_t n_args,
const mp_obj_t *args) {
const bool res_arg = n_args == 2;
const bool res_arg = n_args >= 2;
const int off = res_arg ? 0 : -1;
mp_obj_t res = mp_obj_new_ge25519_r(res_arg ? args[0] : mp_const_none);
mp_buffer_info_t data;
mp_get_buffer_raise(args[1 + off], &data, MP_BUFFER_READ);
xmr_hash_to_ec(&MP_OBJ_GE25519(res), data.buf, data.len);
mp_int_t length = n_args >= 3 ? mp_obj_get_int(args[2]) : data.len;
mp_int_t offset = n_args >= 4 ? mp_obj_get_int(args[3]) : 0;
if (length < 0) length += data.len;
if (offset < 0) offset += data.len;
if (length < 0 || offset < 0 || offset + length > data.len) {
mp_raise_ValueError("Illegal offset/length");
}
xmr_hash_to_ec(&MP_OBJ_GE25519(res), (const char *)data.buf + offset, length);
return res;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(
mod_trezorcrypto_monero_xmr_hash_to_ec_obj, 1, 2,
mod_trezorcrypto_monero_xmr_hash_to_ec_obj, 1, 4,
mod_trezorcrypto_monero_xmr_hash_to_ec);
/// def xmr_hash_to_scalar(r: Optional[Sc25519], buff: bytes) -> Sc25519:
// clang-format off
/// def xmr_hash_to_scalar(r: Optional[Sc25519], buff: bytes, length: int,
/// offset: int) -> Sc25519:
// clang-format on
/// """
/// XMR hashing to EC scalar
/// """
STATIC mp_obj_t mod_trezorcrypto_monero_xmr_hash_to_scalar(
size_t n_args, const mp_obj_t *args) {
const bool res_arg = n_args == 2;
const bool res_arg = n_args >= 2;
const int off = res_arg ? 0 : -1;
mp_obj_t res = mp_obj_new_scalar_r(res_arg ? args[0] : mp_const_none);
mp_buffer_info_t data;
mp_get_buffer_raise(args[1 + off], &data, MP_BUFFER_READ);
xmr_hash_to_scalar(MP_OBJ_SCALAR(res), data.buf, data.len);
mp_int_t length = n_args >= 3 ? mp_obj_get_int(args[2]) : data.len;
mp_int_t offset = n_args >= 4 ? mp_obj_get_int(args[3]) : 0;
if (length < 0) length += data.len;
if (offset < 0) offset += data.len;
if (length < 0 || offset < 0 || offset + length > data.len) {
mp_raise_ValueError("Illegal offset/length");
}
xmr_hash_to_scalar(MP_OBJ_SCALAR(res), (const char *)data.buf + offset,
length);
return res;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(
mod_trezorcrypto_monero_xmr_hash_to_scalar_obj, 1, 2,
mod_trezorcrypto_monero_xmr_hash_to_scalar_obj, 1, 4,
mod_trezorcrypto_monero_xmr_hash_to_scalar);
/// def xmr_derivation_to_scalar(

@ -292,21 +292,23 @@ def xmr_random_scalar(r: Optional[Sc25519] = None) -> Sc25519:
# extmod/modtrezorcrypto/modtrezorcrypto-monero.h
def xmr_fast_hash(r: Optional[bytes], buff: bytes) -> bytes:
def xmr_fast_hash(r: Optional[bytes], buff: bytes, length: int, offset: int) -> bytes:
"""
XMR fast hash
"""
# extmod/modtrezorcrypto/modtrezorcrypto-monero.h
def xmr_hash_to_ec(r: Optional[Ge25519], buff: bytes) -> Ge25519:
def xmr_hash_to_ec(r: Optional[Ge25519], buff: bytes, length: int, offset:
int) -> Ge25519:
"""
XMR hashing to EC point
"""
# extmod/modtrezorcrypto/modtrezorcrypto-monero.h
def xmr_hash_to_scalar(r: Optional[Sc25519], buff: bytes) -> Sc25519:
def xmr_hash_to_scalar(r: Optional[Sc25519], buff: bytes, length: int,
offset: int) -> Sc25519:
"""
XMR hashing to EC scalar
"""

File diff suppressed because it is too large Load Diff

@ -285,102 +285,27 @@ class TestMoneroBulletproof(unittest.TestCase):
bpi.use_det_masks = False
self.mask_consistency_check(bpi)
def test_verify_testnet(self):
bpi = bp.BulletProofBuilder()
# fmt: off
bp_proof = Bulletproof(
V=[bytes(
[0x67, 0x54, 0xbf, 0x40, 0xcb, 0x45, 0x63, 0x0d, 0x4b, 0xea, 0x08, 0x9e, 0xd7, 0x86, 0xec, 0x3c, 0xe5,
0xbd, 0x4e, 0xed, 0x8f, 0xf3, 0x25, 0x76, 0xae, 0xca, 0xb8, 0x9e, 0xf2, 0x5e, 0x41, 0x16])],
A=bytes(
[0x96, 0x10, 0x17, 0x66, 0x87, 0x7e, 0xef, 0x97, 0xb3, 0x82, 0xfb, 0x8e, 0x0c, 0x2a, 0x93, 0x68, 0x9e,
0x05, 0x22, 0x07, 0xe3, 0x30, 0x94, 0x20, 0x58, 0x6f, 0x5d, 0x01, 0x6d, 0x4e, 0xd5, 0x88]),
S=bytes(
[0x50, 0x51, 0x38, 0x32, 0x96, 0x20, 0x7c, 0xc9, 0x60, 0x4d, 0xac, 0x7c, 0x7c, 0x21, 0xf9, 0xad, 0x1c,
0xc2, 0x2d, 0xee, 0x88, 0x7b, 0xa2, 0xe2, 0x61, 0x81, 0x46, 0xf5, 0x99, 0xc3, 0x12, 0x57]),
T1=bytes(
[0x1a, 0x7d, 0x06, 0x51, 0x41, 0xe6, 0x12, 0xbe, 0xad, 0xd7, 0x68, 0x60, 0x85, 0xfc, 0xc4, 0x86, 0x0b,
0x39, 0x4b, 0x06, 0xf7, 0xca, 0xb3, 0x29, 0xdf, 0x1d, 0xbf, 0x96, 0x5f, 0xbe, 0x8c, 0x87]),
T2=bytes(
[0x57, 0xae, 0x91, 0x04, 0xfa, 0xac, 0xf3, 0x73, 0x75, 0xf2, 0x83, 0xd6, 0x9a, 0xcb, 0xef, 0xe4, 0xfc,
0xe5, 0x37, 0x55, 0x52, 0x09, 0xb5, 0x60, 0x6d, 0xab, 0x46, 0x85, 0x01, 0x23, 0x9e, 0x47]),
taux=bytes(
[0x44, 0x7a, 0x87, 0xd9, 0x5f, 0x1b, 0x17, 0xed, 0x53, 0x7f, 0xc1, 0x4f, 0x91, 0x9b, 0xca, 0x68, 0xce,
0x20, 0x43, 0xc0, 0x88, 0xf1, 0xdf, 0x12, 0x7b, 0xd7, 0x7f, 0xe0, 0x27, 0xef, 0xef, 0x0d]),
mu=bytes(
[0x32, 0xf9, 0xe4, 0xe1, 0xc2, 0xd8, 0xe4, 0xb0, 0x0d, 0x49, 0xd1, 0x02, 0xbc, 0xcc, 0xf7, 0xa2, 0x5a,
0xc7, 0x28, 0xf3, 0x05, 0xb5, 0x64, 0x2e, 0xde, 0xcf, 0x01, 0x61, 0xb8, 0x62, 0xfb, 0x0d]),
L=[
bytes([0xde, 0x71, 0xca, 0x09, 0xf9, 0xd9, 0x1f, 0xa2, 0xae, 0xdf, 0x39, 0x49, 0x04, 0xaa, 0x6b, 0x58,
0x67, 0x9d, 0x61, 0xa6, 0xfa, 0xec, 0x81, 0xf6, 0x4c, 0x15, 0x09, 0x9d, 0x10, 0x21, 0xff, 0x39]),
bytes([0x90, 0x47, 0xbf, 0xf0, 0x1f, 0x72, 0x47, 0x4e, 0xd5, 0x58, 0xfb, 0xc1, 0x16, 0x43, 0xb7, 0xd8,
0xb1, 0x00, 0xa4, 0xa3, 0x19, 0x9b, 0xda, 0x5b, 0x27, 0xd3, 0x6c, 0x5a, 0x87, 0xf8, 0xf0, 0x28]),
bytes([0x03, 0x45, 0xef, 0x57, 0x19, 0x8b, 0xc7, 0x38, 0xb7, 0xcb, 0x9c, 0xe7, 0xe8, 0x23, 0x27, 0xbb,
0xd3, 0x54, 0xcb, 0x38, 0x3c, 0x24, 0x8a, 0x60, 0x11, 0x20, 0x92, 0x99, 0xec, 0x35, 0x71, 0x9f]),
bytes([0x7a, 0xb6, 0x36, 0x42, 0x36, 0x83, 0xf3, 0xa6, 0xc1, 0x24, 0xc5, 0x63, 0xb0, 0x4c, 0x8b, 0xef,
0x7c, 0x77, 0x25, 0x83, 0xa8, 0xbb, 0x8b, 0x57, 0x75, 0x1c, 0xb6, 0xd7, 0xca, 0xc9, 0x0d, 0x78]),
bytes([0x9d, 0x79, 0x66, 0x21, 0x64, 0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0x7a, 0x40, 0xae, 0xf9, 0x36, 0x0e, 0xe3, 0xcc]),
bytes([0x88, 0x38, 0x64, 0xe9, 0x63, 0xe3, 0x33, 0xd9, 0xf6, 0xca, 0x47, 0xc4, 0xc7, 0x36, 0x70, 0x01,
0xd2, 0xe4, 0x8c, 0x9f, 0x25, 0xc2, 0xce, 0xcf, 0x81, 0x89, 0x4f, 0x24, 0xcb, 0xb8, 0x40, 0x73]),
bytes([0xdc, 0x35, 0x65, 0xed, 0x6b, 0xb0, 0xa7, 0x1a, 0x1b, 0xf3, 0xd6, 0xfb, 0x47, 0x00, 0x48, 0x00,
0x20, 0x6d, 0xd4, 0xeb, 0xff, 0xb9, 0xdc, 0x43, 0x30, 0x8a, 0x90, 0xfe, 0x43, 0x74, 0x75, 0x68]),
],
a=bytes(
[0xb4, 0x8e, 0xc2, 0x31, 0xce, 0x05, 0x9a, 0x7a, 0xbc, 0x82, 0x8c, 0x30, 0xb3, 0xe3, 0x80, 0x86, 0x05,
0xb8, 0x4c, 0x93, 0x9a, 0x8e, 0xce, 0x39, 0x0f, 0xb6, 0xee, 0x28, 0xf6, 0x7e, 0xd5, 0x07]),
b=bytes(
[0x47, 0x10, 0x62, 0xc2, 0xad, 0xc7, 0xe2, 0xc9, 0x14, 0x6f, 0xf4, 0xd1, 0xfe, 0x52, 0xa9, 0x1a, 0xe4,
0xb6, 0xd0, 0x25, 0x4b, 0x19, 0x80, 0x7c, 0xcd, 0x62, 0x62, 0x1d, 0x97, 0x20, 0x71, 0x0b]),
t=bytes(
[0x47, 0x06, 0xea, 0x76, 0x8f, 0xdb, 0xa3, 0x15, 0xe0, 0x2c, 0x6b, 0x25, 0xa1, 0xf7, 0x3c, 0xc8, 0x1d,
0x97, 0xa6, 0x52, 0x48, 0x75, 0x37, 0xf9, 0x1e, 0x14, 0xac, 0xb1, 0x2a, 0x34, 0xc6, 0x06])
)
# fmt: on
self.assertTrue(bpi.verify_testnet(bp_proof))
def test_verify(self):
bpi = bp.BulletProofBuilder()
self.assertTrue(bpi.verify(self.bproof_1()))
self.assertTrue(bpi.verify(self.bproof_2()))
self.assertTrue(bpi.verify(self.bproof_4()))
def test_prove_testnet(self):
def test_prove(self):
bpi = bp.BulletProofBuilder()
val = crypto.sc_init(123)
mask = crypto.sc_init(432)
bp_res = bpi.prove_testnet(val, mask)
bpi.verify_testnet(bp_res)
try:
bp_res.S[0] += 1
bpi.verify(bp_res)
self.fail("Verification should have failed")
except:
pass
bp_res = bpi.prove(val, mask)
bpi.verify(bp_res)
def test_prove_testnet_2(self):
def test_prove_2(self):
bpi = bp.BulletProofBuilder()
val = crypto.sc_init((1 << 30) - 1 + 16)
mask = crypto.random_scalar()
bp_res = bpi.prove_testnet(val, mask)
bpi.verify_testnet(bp_res)
bp_res = bpi.prove(val, mask)
bpi.verify(bp_res)
def test_verify_batch_1(self):
bpi = bp.BulletProofBuilder()
@ -403,15 +328,6 @@ class TestMoneroBulletproof(unittest.TestCase):
bp_res = bpi.prove(val, mask)
bpi.verify(bp_res)
def test_prove_testnet_random_masks(self):
bpi = bp.BulletProofBuilder()
bpi.use_det_masks = False # trully randomly generated mask vectors
val = crypto.sc_init((1 << 30) - 1 + 16)
mask = crypto.random_scalar()
bp_res = bpi.prove_testnet(val, mask)
bpi.verify_testnet(bp_res)
def ctest_multiexp(self):
scalars = [0, 1, 2, 3, 4, 99]
point_base = [0, 2, 4, 7, 12, 18]
@ -438,6 +354,13 @@ class TestMoneroBulletproof(unittest.TestCase):
proof = bpi.prove_batch(sv, gamma)
bpi.verify_batch([proof])
def test_prove_batch16(self):
bpi = bp.BulletProofBuilder()
sv = [crypto.sc_init(137*i) for i in range(16)]
gamma = [crypto.sc_init(991*i) for i in range(16)]
proof = bpi.prove_batch(sv, gamma)
bpi.verify_batch([proof])
if __name__ == "__main__":
unittest.main()

Loading…
Cancel
Save