nr_free_pages 82492
nr_zone_inactive_anon 2386082
nr_zone_active_anon 388253
nr_zone_inactive_file 520028
nr_zone_active_file 368211
nr_zone_unevictable 6003
nr_zone_write_pending 1220
nr_mlock 6003
nr_bounce 0
nr_zspages 0
nr_free_cma 0
numa_hit 1715555471
numa_miss 0
numa_foreign 0
numa_interleave 867
numa_local 1715555471
numa_other 0
nr_inactive_anon 2386082
nr_active_anon 388253
nr_inactive_file 520028
nr_active_file 368211
nr_unevictable 6003
nr_slab_reclaimable 190884
nr_slab_unreclaimable 44455
nr_isolated_anon 0
nr_isolated_file 0
workingset_nodes 52560
workingset_refault_anon 22290
workingset_refault_file 4534994
workingset_activate_anon 8844
workingset_activate_file 3737855
workingset_restore_anon 5
workingset_restore_file 3054386
workingset_nodereclaim 77189
nr_anon_pages 2659367
nr_mapped 116941
nr_file_pages 994952
nr_dirty 1220
nr_writeback 0
nr_writeback_temp 0
nr_shmem 82233
nr_shmem_hugepages 0
nr_shmem_pmdmapped 0
nr_file_hugepages 0
nr_file_pmdmapped 0
nr_anon_transparent_hugepages 2134
nr_vmscan_write 153022
nr_vmscan_immediate_reclaim 243
nr_dirtied 33680272
nr_written 30002998
nr_throttled_written 0
nr_kernel_misc_reclaimable 0
nr_foll_pin_acquired 0
nr_foll_pin_released 0
nr_kernel_stack 26928
nr_page_table_pages 18725
nr_sec_page_table_pages 0
nr_swapcached 24080
pgpromote_success 0
pgpromote_candidate 0
  B   ˆl–Ðz¾”Šj"Tâ‚À½q¶”Å£�þBÈ³�”�ÆèdzW�üÿÉƒiç\È_ŠI|¥�+Wé3ž¯ÇÆ     #
#  SelfTest/Math/test_modmult.py: Self-test for custom modular multiplication
#
# ===================================================================
#
# Copyright (c) 2023, Helder Eijs <helderijs@gmail.com>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
# ===================================================================

"""Self-test for the custom modular multiplication"""

import unittest

from Cryptodome.SelfTest.st_common import list_test_cases

from Cryptodome.Util.number import long_to_bytes, bytes_to_long

from Cryptodome.Util._raw_api import (create_string_buffer,
                                  get_raw_buffer,
                                  c_size_t)

from Cryptodome.Math._IntegerCustom import _raw_montgomery


class ExceptionModulus(ValueError):
    pass


def monty_mult(term1, term2, modulus):

    if term1 >= modulus:
        term1 %= modulus
    if term2 >= modulus:
        term2 %= modulus

    modulus_b = long_to_bytes(modulus)
    numbers_len = len(modulus_b)
    term1_b = long_to_bytes(term1, numbers_len)
    term2_b = long_to_bytes(term2, numbers_len)

    out = create_string_buffer(numbers_len)
    error = _raw_montgomery.monty_multiply(
                out,
                term1_b,
                term2_b,
                modulus_b,
                c_size_t(numbers_len)
                )

    if error == 17:
        raise ExceptionModulus()