bignum_mod_raw.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. """Framework classes for generation of bignum mod_raw 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 Iterator, List
  17. from . import test_case
  18. from . import test_data_generation
  19. from . import bignum_common
  20. from .bignum_data import ONLY_PRIME_MODULI
  21. class BignumModRawTarget(test_data_generation.BaseTarget):
  22. #pylint: disable=abstract-method, too-few-public-methods
  23. """Target for bignum mod_raw test case generation."""
  24. target_basename = 'test_suite_bignum_mod_raw.generated'
  25. # BEGIN MERGE SLOT 1
  26. # END MERGE SLOT 1
  27. # BEGIN MERGE SLOT 2
  28. class BignumModRawSub(bignum_common.ModOperationCommon,
  29. BignumModRawTarget):
  30. """Test cases for bignum mpi_mod_raw_sub()."""
  31. symbol = "-"
  32. test_function = "mpi_mod_raw_sub"
  33. test_name = "mbedtls_mpi_mod_raw_sub"
  34. input_style = "fixed"
  35. arity = 2
  36. def arguments(self) -> List[str]:
  37. return [bignum_common.quote_str(n) for n in [self.arg_a,
  38. self.arg_b,
  39. 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(result)]
  44. class BignumModRawFixQuasiReduction(bignum_common.ModOperationCommon,
  45. BignumModRawTarget):
  46. """Test cases for ecp quasi_reduction()."""
  47. symbol = "-"
  48. test_function = "mpi_mod_raw_fix_quasi_reduction"
  49. test_name = "fix_quasi_reduction"
  50. input_style = "fixed"
  51. arity = 1
  52. # Extend the default values with n < x < 2n
  53. input_values = bignum_common.ModOperationCommon.input_values + [
  54. "73",
  55. # First number generated by random.getrandbits(1024) - seed(3,2)
  56. "ea7b5bf55eb561a4216363698b529b4a97b750923ceb3ffd",
  57. # First number generated by random.getrandbits(1024) - seed(1,2)
  58. ("cd447e35b8b6d8fe442e3d437204e52db2221a58008a05a6c4647159c324c985"
  59. "9b810e766ec9d28663ca828dd5f4b3b2e4b06ce60741c7a87ce42c8218072e8c"
  60. "35bf992dc9e9c616612e7696a6cecc1b78e510617311d8a3c2ce6f447ed4d57b"
  61. "1e2feb89414c343c1027c4d1c386bbc4cd613e30d8f16adf91b7584a2265b1f5")
  62. ] # type: List[str]
  63. def result(self) -> List[str]:
  64. result = self.int_a % self.int_n
  65. return [self.format_result(result)]
  66. @property
  67. def is_valid(self) -> bool:
  68. return bool(self.int_a < 2 * self.int_n)
  69. class BignumModRawMul(bignum_common.ModOperationCommon,
  70. BignumModRawTarget):
  71. """Test cases for bignum mpi_mod_raw_mul()."""
  72. symbol = "*"
  73. test_function = "mpi_mod_raw_mul"
  74. test_name = "mbedtls_mpi_mod_raw_mul"
  75. input_style = "arch_split"
  76. arity = 2
  77. def arguments(self) -> List[str]:
  78. return [self.format_result(self.to_montgomery(self.int_a)),
  79. self.format_result(self.to_montgomery(self.int_b)),
  80. bignum_common.quote_str(self.arg_n)
  81. ] + self.result()
  82. def result(self) -> List[str]:
  83. result = (self.int_a * self.int_b) % self.int_n
  84. return [self.format_result(self.to_montgomery(result))]
  85. # END MERGE SLOT 2
  86. # BEGIN MERGE SLOT 3
  87. class BignumModRawInvPrime(bignum_common.ModOperationCommon,
  88. BignumModRawTarget):
  89. """Test cases for bignum mpi_mod_raw_inv_prime()."""
  90. moduli = ONLY_PRIME_MODULI
  91. symbol = "^ -1"
  92. test_function = "mpi_mod_raw_inv_prime"
  93. test_name = "mbedtls_mpi_mod_raw_inv_prime (Montgomery form only)"
  94. input_style = "arch_split"
  95. arity = 1
  96. suffix = True
  97. montgomery_form_a = True
  98. disallow_zero_a = True
  99. def result(self) -> List[str]:
  100. result = bignum_common.invmod_positive(self.int_a, self.int_n)
  101. mont_result = self.to_montgomery(result)
  102. return [self.format_result(mont_result)]
  103. # END MERGE SLOT 3
  104. # BEGIN MERGE SLOT 4
  105. # END MERGE SLOT 4
  106. # BEGIN MERGE SLOT 5
  107. class BignumModRawAdd(bignum_common.ModOperationCommon,
  108. BignumModRawTarget):
  109. """Test cases for bignum mpi_mod_raw_add()."""
  110. symbol = "+"
  111. test_function = "mpi_mod_raw_add"
  112. test_name = "mbedtls_mpi_mod_raw_add"
  113. input_style = "fixed"
  114. arity = 2
  115. def result(self) -> List[str]:
  116. result = (self.int_a + self.int_b) % self.int_n
  117. return [self.format_result(result)]
  118. # END MERGE SLOT 5
  119. # BEGIN MERGE SLOT 6
  120. class BignumModRawConvertRep(bignum_common.ModOperationCommon,
  121. BignumModRawTarget):
  122. # This is an abstract class, it's ok to have unimplemented methods.
  123. #pylint: disable=abstract-method
  124. """Test cases for representation conversion."""
  125. symbol = ""
  126. input_style = "arch_split"
  127. arity = 1
  128. rep = bignum_common.ModulusRepresentation.INVALID
  129. def set_representation(self, r: bignum_common.ModulusRepresentation) -> None:
  130. self.rep = r
  131. def arguments(self) -> List[str]:
  132. return ([bignum_common.quote_str(self.arg_n), self.rep.symbol(),
  133. bignum_common.quote_str(self.arg_a)] +
  134. self.result())
  135. def description(self) -> str:
  136. base = super().description()
  137. mod_with_rep = 'mod({})'.format(self.rep.name)
  138. return base.replace('mod', mod_with_rep, 1)
  139. @classmethod
  140. def test_cases_for_values(cls, rep: bignum_common.ModulusRepresentation,
  141. n: str, a: str) -> Iterator[test_case.TestCase]:
  142. """Emit test cases for the given values (if any).
  143. This may emit no test cases if a isn't valid for the modulus n,
  144. or multiple test cases if rep requires different data depending
  145. on the limb size.
  146. """
  147. for bil in cls.limb_sizes:
  148. test_object = cls(n, a, bits_in_limb=bil)
  149. test_object.set_representation(rep)
  150. # The class is set to having separate test cases for each limb
  151. # size, because the Montgomery representation requires it.
  152. # But other representations don't require it. So for other
  153. # representations, emit a single test case with no dependency
  154. # on the limb size.
  155. if rep is not bignum_common.ModulusRepresentation.MONTGOMERY:
  156. test_object.dependencies = \
  157. [dep for dep in test_object.dependencies
  158. if not dep.startswith('MBEDTLS_HAVE_INT')]
  159. if test_object.is_valid:
  160. yield test_object.create_test_case()
  161. if rep is not bignum_common.ModulusRepresentation.MONTGOMERY:
  162. # A single test case (emitted, or skipped due to invalidity)
  163. # is enough, since this test case doesn't depend on the
  164. # limb size.
  165. break
  166. # The parent class doesn't support non-bignum parameters. So we override
  167. # test generation, in order to have the representation as a parameter.
  168. @classmethod
  169. def generate_function_tests(cls) -> Iterator[test_case.TestCase]:
  170. for rep in bignum_common.ModulusRepresentation.supported_representations():
  171. for n in cls.moduli:
  172. for a in cls.input_values:
  173. yield from cls.test_cases_for_values(rep, n, a)
  174. class BignumModRawCanonicalToModulusRep(BignumModRawConvertRep):
  175. """Test cases for mpi_mod_raw_canonical_to_modulus_rep."""
  176. test_function = "mpi_mod_raw_canonical_to_modulus_rep"
  177. test_name = "Rep canon->mod"
  178. def result(self) -> List[str]:
  179. return [self.format_result(self.convert_from_canonical(self.int_a, self.rep))]
  180. class BignumModRawModulusToCanonicalRep(BignumModRawConvertRep):
  181. """Test cases for mpi_mod_raw_modulus_to_canonical_rep."""
  182. test_function = "mpi_mod_raw_modulus_to_canonical_rep"
  183. test_name = "Rep mod->canon"
  184. @property
  185. def arg_a(self) -> str:
  186. return self.format_arg("{:x}".format(self.convert_from_canonical(self.int_a, self.rep)))
  187. def result(self) -> List[str]:
  188. return [self.format_result(self.int_a)]
  189. # END MERGE SLOT 6
  190. # BEGIN MERGE SLOT 7
  191. class BignumModRawConvertToMont(bignum_common.ModOperationCommon,
  192. BignumModRawTarget):
  193. """ Test cases for mpi_mod_raw_to_mont_rep(). """
  194. test_function = "mpi_mod_raw_to_mont_rep"
  195. test_name = "Convert into Mont: "
  196. symbol = "R *"
  197. input_style = "arch_split"
  198. arity = 1
  199. def result(self) -> List[str]:
  200. result = self.to_montgomery(self.int_a)
  201. return [self.format_result(result)]
  202. class BignumModRawConvertFromMont(bignum_common.ModOperationCommon,
  203. BignumModRawTarget):
  204. """ Test cases for mpi_mod_raw_from_mont_rep(). """
  205. test_function = "mpi_mod_raw_from_mont_rep"
  206. test_name = "Convert from Mont: "
  207. symbol = "1/R *"
  208. input_style = "arch_split"
  209. arity = 1
  210. def result(self) -> List[str]:
  211. result = self.from_montgomery(self.int_a)
  212. return [self.format_result(result)]
  213. class BignumModRawModNegate(bignum_common.ModOperationCommon,
  214. BignumModRawTarget):
  215. """ Test cases for mpi_mod_raw_neg(). """
  216. test_function = "mpi_mod_raw_neg"
  217. test_name = "Modular negation: "
  218. symbol = "-"
  219. input_style = "arch_split"
  220. arity = 1
  221. def result(self) -> List[str]:
  222. result = (self.int_n - self.int_a) % self.int_n
  223. return [self.format_result(result)]
  224. # END MERGE SLOT 7
  225. # BEGIN MERGE SLOT 8
  226. # END MERGE SLOT 8
  227. # BEGIN MERGE SLOT 9
  228. # END MERGE SLOT 9
  229. # BEGIN MERGE SLOT 10
  230. # END MERGE SLOT 10