bignum_mod.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. """Framework classes for generation of bignum mod test cases."""
  2. # Copyright The Mbed TLS Contributors
  3. # SPDX-License-Identifier: Apache-2.0
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  6. # not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. from typing import Dict, List
  17. from . import test_data_generation
  18. from . import bignum_common
  19. from .bignum_data import ONLY_PRIME_MODULI
  20. class BignumModTarget(test_data_generation.BaseTarget):
  21. #pylint: disable=abstract-method, too-few-public-methods
  22. """Target for bignum mod test case generation."""
  23. target_basename = 'test_suite_bignum_mod.generated'
  24. # BEGIN MERGE SLOT 1
  25. # END MERGE SLOT 1
  26. # BEGIN MERGE SLOT 2
  27. class BignumModMul(bignum_common.ModOperationCommon,
  28. BignumModTarget):
  29. # pylint:disable=duplicate-code
  30. """Test cases for bignum mpi_mod_mul()."""
  31. symbol = "*"
  32. test_function = "mpi_mod_mul"
  33. test_name = "mbedtls_mpi_mod_mul"
  34. input_style = "arch_split"
  35. arity = 2
  36. def arguments(self) -> List[str]:
  37. return [self.format_result(self.to_montgomery(self.int_a)),
  38. self.format_result(self.to_montgomery(self.int_b)),
  39. bignum_common.quote_str(self.arg_n)
  40. ] + self.result()
  41. def result(self) -> List[str]:
  42. result = (self.int_a * self.int_b) % self.int_n
  43. return [self.format_result(self.to_montgomery(result))]
  44. # END MERGE SLOT 2
  45. # BEGIN MERGE SLOT 3
  46. class BignumModSub(bignum_common.ModOperationCommon, BignumModTarget):
  47. """Test cases for bignum mpi_mod_sub()."""
  48. symbol = "-"
  49. test_function = "mpi_mod_sub"
  50. test_name = "mbedtls_mpi_mod_sub"
  51. input_style = "fixed"
  52. arity = 2
  53. def result(self) -> List[str]:
  54. result = (self.int_a - self.int_b) % self.int_n
  55. # To make negative tests easier, append 0 for success to the
  56. # generated cases
  57. return [self.format_result(result), "0"]
  58. class BignumModInvNonMont(bignum_common.ModOperationCommon, BignumModTarget):
  59. """Test cases for bignum mpi_mod_inv() - not in Montgomery form."""
  60. moduli = ONLY_PRIME_MODULI # for now only prime moduli supported
  61. symbol = "^ -1"
  62. test_function = "mpi_mod_inv_non_mont"
  63. test_name = "mbedtls_mpi_mod_inv non-Mont. form"
  64. input_style = "fixed"
  65. arity = 1
  66. suffix = True
  67. disallow_zero_a = True
  68. def result(self) -> List[str]:
  69. result = bignum_common.invmod_positive(self.int_a, self.int_n)
  70. # To make negative tests easier, append 0 for success to the
  71. # generated cases
  72. return [self.format_result(result), "0"]
  73. class BignumModInvMont(bignum_common.ModOperationCommon, BignumModTarget):
  74. """Test cases for bignum mpi_mod_inv() - Montgomery form."""
  75. moduli = ONLY_PRIME_MODULI # for now only prime moduli supported
  76. symbol = "^ -1"
  77. test_function = "mpi_mod_inv_mont"
  78. test_name = "mbedtls_mpi_mod_inv Mont. form"
  79. input_style = "arch_split" # Mont. form requires arch_split
  80. arity = 1
  81. suffix = True
  82. disallow_zero_a = True
  83. montgomery_form_a = True
  84. def result(self) -> List[str]:
  85. result = bignum_common.invmod_positive(self.int_a, self.int_n)
  86. mont_result = self.to_montgomery(result)
  87. # To make negative tests easier, append 0 for success to the
  88. # generated cases
  89. return [self.format_result(mont_result), "0"]
  90. # END MERGE SLOT 3
  91. # BEGIN MERGE SLOT 4
  92. # END MERGE SLOT 4
  93. # BEGIN MERGE SLOT 5
  94. class BignumModAdd(bignum_common.ModOperationCommon, BignumModTarget):
  95. """Test cases for bignum mpi_mod_add()."""
  96. count = 0
  97. symbol = "+"
  98. test_function = "mpi_mod_add"
  99. test_name = "mbedtls_mpi_mod_add"
  100. input_style = "fixed"
  101. def result(self) -> List[str]:
  102. result = (self.int_a + self.int_b) % self.int_n
  103. # To make negative tests easier, append "0" for success to the
  104. # generated cases
  105. return [self.format_result(result), "0"]
  106. # END MERGE SLOT 5
  107. # BEGIN MERGE SLOT 6
  108. # END MERGE SLOT 6
  109. # BEGIN MERGE SLOT 7
  110. # END MERGE SLOT 7
  111. # BEGIN MERGE SLOT 8
  112. # END MERGE SLOT 8
  113. # BEGIN MERGE SLOT 9
  114. # END MERGE SLOT 9
  115. # BEGIN MERGE SLOT 10
  116. # END MERGE SLOT 10