charm-crypto-framework 0.61.1__cp313-cp313-macosx_10_13_universal2.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (323) hide show
  1. charm/__init__.py +5 -0
  2. charm/adapters/__init__.py +0 -0
  3. charm/adapters/abenc_adapt_hybrid.py +90 -0
  4. charm/adapters/dabenc_adapt_hybrid.py +145 -0
  5. charm/adapters/ibenc_adapt_hybrid.py +72 -0
  6. charm/adapters/ibenc_adapt_identityhash.py +80 -0
  7. charm/adapters/kpabenc_adapt_hybrid.py +91 -0
  8. charm/adapters/pkenc_adapt_bchk05.py +121 -0
  9. charm/adapters/pkenc_adapt_chk04.py +91 -0
  10. charm/adapters/pkenc_adapt_hybrid.py +98 -0
  11. charm/adapters/pksig_adapt_naor01.py +89 -0
  12. charm/config.py +7 -0
  13. charm/core/__init__.py +0 -0
  14. charm/core/benchmark/benchmark_util.c +353 -0
  15. charm/core/benchmark/benchmark_util.h +61 -0
  16. charm/core/benchmark/benchmarkmodule.c +476 -0
  17. charm/core/benchmark/benchmarkmodule.h +162 -0
  18. charm/core/benchmark.cpython-313-darwin.so +0 -0
  19. charm/core/crypto/AES/AES.c +1464 -0
  20. charm/core/crypto/AES.cpython-313-darwin.so +0 -0
  21. charm/core/crypto/DES/DES.c +113 -0
  22. charm/core/crypto/DES.cpython-313-darwin.so +0 -0
  23. charm/core/crypto/DES3/DES3.c +26 -0
  24. charm/core/crypto/DES3.cpython-313-darwin.so +0 -0
  25. charm/core/crypto/__init__.py +0 -0
  26. charm/core/crypto/cryptobase/XOR.c +80 -0
  27. charm/core/crypto/cryptobase/_counter.c +496 -0
  28. charm/core/crypto/cryptobase/_counter.h +54 -0
  29. charm/core/crypto/cryptobase/block_template.c +900 -0
  30. charm/core/crypto/cryptobase/block_template.h +69 -0
  31. charm/core/crypto/cryptobase/cryptobasemodule.c +220 -0
  32. charm/core/crypto/cryptobase/libtom/tomcrypt.h +90 -0
  33. charm/core/crypto/cryptobase/libtom/tomcrypt_argchk.h +44 -0
  34. charm/core/crypto/cryptobase/libtom/tomcrypt_cfg.h +186 -0
  35. charm/core/crypto/cryptobase/libtom/tomcrypt_cipher.h +941 -0
  36. charm/core/crypto/cryptobase/libtom/tomcrypt_custom.h +556 -0
  37. charm/core/crypto/cryptobase/libtom/tomcrypt_des.c +1912 -0
  38. charm/core/crypto/cryptobase/libtom/tomcrypt_hash.h +407 -0
  39. charm/core/crypto/cryptobase/libtom/tomcrypt_mac.h +496 -0
  40. charm/core/crypto/cryptobase/libtom/tomcrypt_macros.h +435 -0
  41. charm/core/crypto/cryptobase/libtom/tomcrypt_math.h +534 -0
  42. charm/core/crypto/cryptobase/libtom/tomcrypt_misc.h +103 -0
  43. charm/core/crypto/cryptobase/libtom/tomcrypt_pk.h +653 -0
  44. charm/core/crypto/cryptobase/libtom/tomcrypt_pkcs.h +90 -0
  45. charm/core/crypto/cryptobase/libtom/tomcrypt_prng.h +199 -0
  46. charm/core/crypto/cryptobase/stream_template.c +271 -0
  47. charm/core/crypto/cryptobase/strxor.c +229 -0
  48. charm/core/crypto/cryptobase.cpython-313-darwin.so +0 -0
  49. charm/core/engine/__init__.py +5 -0
  50. charm/core/engine/protocol.py +293 -0
  51. charm/core/engine/util.py +174 -0
  52. charm/core/math/__init__.py +0 -0
  53. charm/core/math/elliptic_curve/ecmodule.c +1986 -0
  54. charm/core/math/elliptic_curve/ecmodule.h +230 -0
  55. charm/core/math/elliptic_curve.cpython-313-darwin.so +0 -0
  56. charm/core/math/elliptic_curve.pyi +63 -0
  57. charm/core/math/integer/integermodule.c +2539 -0
  58. charm/core/math/integer/integermodule.h +145 -0
  59. charm/core/math/integer.cpython-313-darwin.so +0 -0
  60. charm/core/math/integer.pyi +76 -0
  61. charm/core/math/pairing/miracl/miracl_config.h +37 -0
  62. charm/core/math/pairing/miracl/miracl_interface.h +118 -0
  63. charm/core/math/pairing/miracl/miracl_interface2.h +126 -0
  64. charm/core/math/pairing/miracl/pairingmodule2.c +2094 -0
  65. charm/core/math/pairing/miracl/pairingmodule2.h +307 -0
  66. charm/core/math/pairing/pairingmodule.c +2230 -0
  67. charm/core/math/pairing/pairingmodule.h +241 -0
  68. charm/core/math/pairing/relic/pairingmodule3.c +1853 -0
  69. charm/core/math/pairing/relic/pairingmodule3.h +233 -0
  70. charm/core/math/pairing/relic/relic_interface.c +1337 -0
  71. charm/core/math/pairing/relic/relic_interface.h +217 -0
  72. charm/core/math/pairing/relic/test_relic.c +171 -0
  73. charm/core/math/pairing.cpython-313-darwin.so +0 -0
  74. charm/core/math/pairing.pyi +69 -0
  75. charm/core/utilities/base64.c +248 -0
  76. charm/core/utilities/base64.h +15 -0
  77. charm/schemes/__init__.py +0 -0
  78. charm/schemes/abenc/__init__.py +0 -0
  79. charm/schemes/abenc/abenc_accountability_jyjxgd20.py +647 -0
  80. charm/schemes/abenc/abenc_bsw07.py +146 -0
  81. charm/schemes/abenc/abenc_ca_cpabe_ar17.py +684 -0
  82. charm/schemes/abenc/abenc_dacmacs_yj14.py +298 -0
  83. charm/schemes/abenc/abenc_lsw08.py +159 -0
  84. charm/schemes/abenc/abenc_maabe_rw15.py +236 -0
  85. charm/schemes/abenc/abenc_maabe_yj14.py +297 -0
  86. charm/schemes/abenc/abenc_tbpre_lww14.py +309 -0
  87. charm/schemes/abenc/abenc_unmcpabe_yahk14.py +223 -0
  88. charm/schemes/abenc/abenc_waters09.py +144 -0
  89. charm/schemes/abenc/abenc_yct14.py +208 -0
  90. charm/schemes/abenc/abenc_yllc15.py +178 -0
  91. charm/schemes/abenc/ac17.py +248 -0
  92. charm/schemes/abenc/bsw07.py +141 -0
  93. charm/schemes/abenc/cgw15.py +277 -0
  94. charm/schemes/abenc/dabe_aw11.py +204 -0
  95. charm/schemes/abenc/dfa_fe12.py +144 -0
  96. charm/schemes/abenc/pk_hve08.py +179 -0
  97. charm/schemes/abenc/waters11.py +143 -0
  98. charm/schemes/aggrsign_MuSig.py +150 -0
  99. charm/schemes/aggrsign_bls.py +267 -0
  100. charm/schemes/blindsig_ps16.py +654 -0
  101. charm/schemes/chamhash_adm05.py +113 -0
  102. charm/schemes/chamhash_rsa_hw09.py +100 -0
  103. charm/schemes/commit/__init__.py +0 -0
  104. charm/schemes/commit/commit_gs08.py +77 -0
  105. charm/schemes/commit/commit_pedersen92.py +53 -0
  106. charm/schemes/encap_bchk05.py +62 -0
  107. charm/schemes/grpsig/__init__.py +0 -0
  108. charm/schemes/grpsig/groupsig_bgls04.py +114 -0
  109. charm/schemes/grpsig/groupsig_bgls04_var.py +115 -0
  110. charm/schemes/hibenc/__init__.py +0 -0
  111. charm/schemes/hibenc/hibenc_bb04.py +105 -0
  112. charm/schemes/hibenc/hibenc_lew11.py +193 -0
  113. charm/schemes/ibenc/__init__.py +0 -0
  114. charm/schemes/ibenc/clpkc_rp03.py +119 -0
  115. charm/schemes/ibenc/ibenc_CW13_z.py +168 -0
  116. charm/schemes/ibenc/ibenc_bb03.py +94 -0
  117. charm/schemes/ibenc/ibenc_bf01.py +121 -0
  118. charm/schemes/ibenc/ibenc_ckrs09.py +120 -0
  119. charm/schemes/ibenc/ibenc_cllww12_z.py +172 -0
  120. charm/schemes/ibenc/ibenc_lsw08.py +120 -0
  121. charm/schemes/ibenc/ibenc_sw05.py +238 -0
  122. charm/schemes/ibenc/ibenc_waters05.py +144 -0
  123. charm/schemes/ibenc/ibenc_waters05_z.py +164 -0
  124. charm/schemes/ibenc/ibenc_waters09.py +107 -0
  125. charm/schemes/ibenc/ibenc_waters09_z.py +147 -0
  126. charm/schemes/joye_scheme.py +106 -0
  127. charm/schemes/lem_scheme.py +207 -0
  128. charm/schemes/pk_fre_ccv11.py +107 -0
  129. charm/schemes/pk_vrf.py +127 -0
  130. charm/schemes/pkenc/__init__.py +0 -0
  131. charm/schemes/pkenc/pkenc_cs98.py +108 -0
  132. charm/schemes/pkenc/pkenc_elgamal85.py +122 -0
  133. charm/schemes/pkenc/pkenc_gm82.py +98 -0
  134. charm/schemes/pkenc/pkenc_paillier99.py +118 -0
  135. charm/schemes/pkenc/pkenc_rabin.py +254 -0
  136. charm/schemes/pkenc/pkenc_rsa.py +186 -0
  137. charm/schemes/pksig/__init__.py +0 -0
  138. charm/schemes/pksig/pksig_CW13_z.py +135 -0
  139. charm/schemes/pksig/pksig_bls04.py +87 -0
  140. charm/schemes/pksig/pksig_boyen.py +156 -0
  141. charm/schemes/pksig/pksig_chch.py +97 -0
  142. charm/schemes/pksig/pksig_chp.py +70 -0
  143. charm/schemes/pksig/pksig_cl03.py +150 -0
  144. charm/schemes/pksig/pksig_cl04.py +87 -0
  145. charm/schemes/pksig/pksig_cllww12_z.py +142 -0
  146. charm/schemes/pksig/pksig_cyh.py +132 -0
  147. charm/schemes/pksig/pksig_dsa.py +76 -0
  148. charm/schemes/pksig/pksig_ecdsa.py +71 -0
  149. charm/schemes/pksig/pksig_hess.py +104 -0
  150. charm/schemes/pksig/pksig_hw.py +110 -0
  151. charm/schemes/pksig/pksig_lamport.py +63 -0
  152. charm/schemes/pksig/pksig_ps01.py +135 -0
  153. charm/schemes/pksig/pksig_ps02.py +124 -0
  154. charm/schemes/pksig/pksig_ps03.py +119 -0
  155. charm/schemes/pksig/pksig_rsa_hw09.py +206 -0
  156. charm/schemes/pksig/pksig_schnorr91.py +77 -0
  157. charm/schemes/pksig/pksig_waters.py +115 -0
  158. charm/schemes/pksig/pksig_waters05.py +121 -0
  159. charm/schemes/pksig/pksig_waters09.py +121 -0
  160. charm/schemes/pre_mg07.py +150 -0
  161. charm/schemes/prenc/pre_afgh06.py +126 -0
  162. charm/schemes/prenc/pre_bbs98.py +123 -0
  163. charm/schemes/prenc/pre_nal16.py +216 -0
  164. charm/schemes/protocol_a01.py +272 -0
  165. charm/schemes/protocol_ao00.py +215 -0
  166. charm/schemes/protocol_cns07.py +274 -0
  167. charm/schemes/protocol_schnorr91.py +125 -0
  168. charm/schemes/sigma1.py +64 -0
  169. charm/schemes/sigma2.py +129 -0
  170. charm/schemes/sigma3.py +126 -0
  171. charm/schemes/threshold/__init__.py +59 -0
  172. charm/schemes/threshold/dkls23_dkg.py +556 -0
  173. charm/schemes/threshold/dkls23_presign.py +1089 -0
  174. charm/schemes/threshold/dkls23_sign.py +761 -0
  175. charm/schemes/threshold/xrpl_wallet.py +967 -0
  176. charm/test/__init__.py +0 -0
  177. charm/test/adapters/__init__.py +0 -0
  178. charm/test/adapters/abenc_adapt_hybrid_test.py +29 -0
  179. charm/test/adapters/dabenc_adapt_hybrid_test.py +56 -0
  180. charm/test/adapters/ibenc_adapt_hybrid_test.py +36 -0
  181. charm/test/adapters/ibenc_adapt_identityhash_test.py +32 -0
  182. charm/test/adapters/kpabenc_adapt_hybrid_test.py +30 -0
  183. charm/test/benchmark/abenc_yllc15_bench.py +92 -0
  184. charm/test/benchmark/benchmark_test.py +148 -0
  185. charm/test/benchmark_threshold.py +260 -0
  186. charm/test/conftest.py +38 -0
  187. charm/test/fuzz/__init__.py +1 -0
  188. charm/test/fuzz/conftest.py +5 -0
  189. charm/test/fuzz/fuzz_policy_parser.py +76 -0
  190. charm/test/fuzz/fuzz_serialization.py +83 -0
  191. charm/test/schemes/__init__.py +0 -0
  192. charm/test/schemes/abenc/__init__.py +0 -0
  193. charm/test/schemes/abenc/abenc_bsw07_test.py +39 -0
  194. charm/test/schemes/abenc/abenc_dacmacs_yj14_test.py +16 -0
  195. charm/test/schemes/abenc/abenc_lsw08_test.py +33 -0
  196. charm/test/schemes/abenc/abenc_maabe_yj14_test.py +16 -0
  197. charm/test/schemes/abenc/abenc_tbpre_lww14_test.py +16 -0
  198. charm/test/schemes/abenc/abenc_waters09_test.py +38 -0
  199. charm/test/schemes/abenc/abenc_yllc15_test.py +74 -0
  200. charm/test/schemes/chamhash_adm05_test.py +31 -0
  201. charm/test/schemes/chamhash_rsa_hw09_test.py +29 -0
  202. charm/test/schemes/commit/__init__.py +0 -0
  203. charm/test/schemes/commit/commit_gs08_test.py +24 -0
  204. charm/test/schemes/commit/commit_pedersen92_test.py +26 -0
  205. charm/test/schemes/dabe_aw11_test.py +45 -0
  206. charm/test/schemes/encap_bchk05_test.py +21 -0
  207. charm/test/schemes/grpsig/__init__.py +0 -0
  208. charm/test/schemes/grpsig/groupsig_bgls04_test.py +35 -0
  209. charm/test/schemes/grpsig/groupsig_bgls04_var_test.py +39 -0
  210. charm/test/schemes/hibenc/__init__.py +0 -0
  211. charm/test/schemes/hibenc/hibenc_bb04_test.py +28 -0
  212. charm/test/schemes/ibenc/__init__.py +0 -0
  213. charm/test/schemes/ibenc/ibenc_bb03_test.py +26 -0
  214. charm/test/schemes/ibenc/ibenc_bf01_test.py +24 -0
  215. charm/test/schemes/ibenc/ibenc_ckrs09_test.py +25 -0
  216. charm/test/schemes/ibenc/ibenc_lsw08_test.py +31 -0
  217. charm/test/schemes/ibenc/ibenc_sw05_test.py +32 -0
  218. charm/test/schemes/ibenc/ibenc_waters05_test.py +31 -0
  219. charm/test/schemes/ibenc/ibenc_waters09_test.py +27 -0
  220. charm/test/schemes/pk_vrf_test.py +29 -0
  221. charm/test/schemes/pkenc/__init__.py +0 -0
  222. charm/test/schemes/pkenc_test.py +255 -0
  223. charm/test/schemes/pksig/__init__.py +0 -0
  224. charm/test/schemes/pksig_test.py +376 -0
  225. charm/test/schemes/rsa_alg_test.py +340 -0
  226. charm/test/schemes/threshold_test.py +1792 -0
  227. charm/test/serialize/__init__.py +0 -0
  228. charm/test/serialize/serialize_test.py +40 -0
  229. charm/test/toolbox/__init__.py +0 -0
  230. charm/test/toolbox/conversion_test.py +30 -0
  231. charm/test/toolbox/ecgroup_test.py +53 -0
  232. charm/test/toolbox/integer_arithmetic_test.py +441 -0
  233. charm/test/toolbox/paddingschemes_test.py +238 -0
  234. charm/test/toolbox/policy_parser_stress_test.py +969 -0
  235. charm/test/toolbox/secretshare_test.py +28 -0
  236. charm/test/toolbox/symcrypto_test.py +108 -0
  237. charm/test/toolbox/test_policy_expression.py +16 -0
  238. charm/test/vectors/__init__.py +1 -0
  239. charm/test/vectors/test_bls_vectors.py +289 -0
  240. charm/test/vectors/test_pedersen_vectors.py +315 -0
  241. charm/test/vectors/test_schnorr_vectors.py +368 -0
  242. charm/test/zkp_compiler/__init__.py +9 -0
  243. charm/test/zkp_compiler/benchmark_zkp.py +258 -0
  244. charm/test/zkp_compiler/test_and_proof.py +240 -0
  245. charm/test/zkp_compiler/test_batch_verify.py +248 -0
  246. charm/test/zkp_compiler/test_dleq_proof.py +264 -0
  247. charm/test/zkp_compiler/test_or_proof.py +231 -0
  248. charm/test/zkp_compiler/test_proof_serialization.py +121 -0
  249. charm/test/zkp_compiler/test_range_proof.py +241 -0
  250. charm/test/zkp_compiler/test_representation_proof.py +325 -0
  251. charm/test/zkp_compiler/test_schnorr_proof.py +221 -0
  252. charm/test/zkp_compiler/test_thread_safety.py +169 -0
  253. charm/test/zkp_compiler/test_zkp_parser.py +139 -0
  254. charm/toolbox/ABEnc.py +26 -0
  255. charm/toolbox/ABEncMultiAuth.py +66 -0
  256. charm/toolbox/ABEnumeric.py +800 -0
  257. charm/toolbox/Commit.py +24 -0
  258. charm/toolbox/DFA.py +89 -0
  259. charm/toolbox/FSA.py +1254 -0
  260. charm/toolbox/Hash.py +39 -0
  261. charm/toolbox/IBEnc.py +62 -0
  262. charm/toolbox/IBSig.py +64 -0
  263. charm/toolbox/PKEnc.py +66 -0
  264. charm/toolbox/PKSig.py +56 -0
  265. charm/toolbox/PREnc.py +32 -0
  266. charm/toolbox/ZKProof.py +289 -0
  267. charm/toolbox/__init__.py +0 -0
  268. charm/toolbox/bitstring.py +49 -0
  269. charm/toolbox/broadcast.py +220 -0
  270. charm/toolbox/conversion.py +100 -0
  271. charm/toolbox/eccurve.py +149 -0
  272. charm/toolbox/ecgroup.py +143 -0
  273. charm/toolbox/enum.py +60 -0
  274. charm/toolbox/hash_module.py +91 -0
  275. charm/toolbox/integergroup.py +323 -0
  276. charm/toolbox/iterate.py +22 -0
  277. charm/toolbox/matrixops.py +76 -0
  278. charm/toolbox/mpc_utils.py +296 -0
  279. charm/toolbox/msp.py +175 -0
  280. charm/toolbox/mta.py +985 -0
  281. charm/toolbox/node.py +120 -0
  282. charm/toolbox/ot/__init__.py +22 -0
  283. charm/toolbox/ot/base_ot.py +374 -0
  284. charm/toolbox/ot/dpf.py +642 -0
  285. charm/toolbox/ot/mpfss.py +228 -0
  286. charm/toolbox/ot/ot_extension.py +589 -0
  287. charm/toolbox/ot/silent_ot.py +378 -0
  288. charm/toolbox/paddingschemes.py +423 -0
  289. charm/toolbox/paddingschemes_test.py +238 -0
  290. charm/toolbox/pairingcurves.py +85 -0
  291. charm/toolbox/pairinggroup.py +186 -0
  292. charm/toolbox/policy_expression_spec.py +70 -0
  293. charm/toolbox/policytree.py +189 -0
  294. charm/toolbox/reCompiler.py +346 -0
  295. charm/toolbox/redundancyschemes.py +65 -0
  296. charm/toolbox/schemebase.py +188 -0
  297. charm/toolbox/secretshare.py +104 -0
  298. charm/toolbox/secretutil.py +174 -0
  299. charm/toolbox/securerandom.py +73 -0
  300. charm/toolbox/sigmaprotocol.py +46 -0
  301. charm/toolbox/specialprimes.py +45 -0
  302. charm/toolbox/symcrypto.py +279 -0
  303. charm/toolbox/threshold_sharing.py +553 -0
  304. charm/toolbox/xmlserialize.py +94 -0
  305. charm/toolbox/zknode.py +105 -0
  306. charm/zkp_compiler/__init__.py +89 -0
  307. charm/zkp_compiler/and_proof.py +460 -0
  308. charm/zkp_compiler/batch_verify.py +324 -0
  309. charm/zkp_compiler/dleq_proof.py +423 -0
  310. charm/zkp_compiler/or_proof.py +305 -0
  311. charm/zkp_compiler/range_proof.py +417 -0
  312. charm/zkp_compiler/representation_proof.py +466 -0
  313. charm/zkp_compiler/schnorr_proof.py +273 -0
  314. charm/zkp_compiler/thread_safe.py +150 -0
  315. charm/zkp_compiler/zk_demo.py +489 -0
  316. charm/zkp_compiler/zkp_factory.py +330 -0
  317. charm/zkp_compiler/zkp_generator.py +370 -0
  318. charm/zkp_compiler/zkparser.py +269 -0
  319. charm_crypto_framework-0.61.1.dist-info/METADATA +337 -0
  320. charm_crypto_framework-0.61.1.dist-info/RECORD +323 -0
  321. charm_crypto_framework-0.61.1.dist-info/WHEEL +5 -0
  322. charm_crypto_framework-0.61.1.dist-info/licenses/LICENSE.txt +165 -0
  323. charm_crypto_framework-0.61.1.dist-info/top_level.txt +1 -0
File without changes
@@ -0,0 +1,40 @@
1
+ from charm.core.engine.util import objectToBytes,bytesToObject
2
+ from charm.toolbox.integergroup import IntegerGroup, integer
3
+ from charm.toolbox.pairinggroup import PairingGroup
4
+ from charm.toolbox.ecgroup import ECGroup
5
+ from charm.toolbox.eccurve import prime192v1
6
+ import unittest
7
+
8
+ debug = False
9
+
10
+ class SerializeTest(unittest.TestCase):
11
+ def testIntegerGroup(self):
12
+ self.maxDiff=None
13
+ groupObj = IntegerGroup()
14
+ p = integer(148829018183496626261556856344710600327516732500226144177322012998064772051982752493460332138204351040296264880017943408846937646702376203733370973197019636813306480144595809796154634625021213611577190781215296823124523899584781302512549499802030946698512327294159881907114777803654670044046376468983244647367)
15
+ data={'p':p,'String':"foo",'list':[p,{},1,1.7, b'dfa']}
16
+
17
+ x=objectToBytes(data,groupObj)
18
+ data2=bytesToObject(x,groupObj)
19
+ self.assertEqual(data,data2)
20
+
21
+ def testPairingGroup(self):
22
+ groupobj = PairingGroup('SS512')
23
+ p=groupobj.random()
24
+ data={'p':p,'String':"foo",'list':[p,{},1,1.7, b'dfa',]}
25
+
26
+ x=objectToBytes(data,groupobj)
27
+ data2=bytesToObject(x,groupobj)
28
+ self.assertEqual(data,data2)
29
+
30
+ def testECGroup(self):
31
+ groupObj = ECGroup(prime192v1)
32
+ p=groupObj.random()
33
+ data={'p':p,'String':"foo",'list':[p,{},1,1.7, b'dfa',]}
34
+
35
+ x=objectToBytes(data,groupObj)
36
+ data2=bytesToObject(x,groupObj)
37
+ self.assertEqual(data,data2)
38
+
39
+ if __name__ == "__main__":
40
+ unittest.main()
File without changes
@@ -0,0 +1,30 @@
1
+ '''
2
+ :Date: Jul 5, 2011
3
+ :Authors: Gary Belvin
4
+ '''
5
+ from charm.toolbox.conversion import Conversion
6
+ import unittest
7
+
8
+
9
+ class ConversionTest(unittest.TestCase):
10
+
11
+
12
+ def testOS2IP(self):
13
+ #9,202,000 = (0x)8c 69 50.
14
+ i = Conversion.OS2IP(b'\x8c\x69\x50')
15
+ self.assertEqual(i, 9202000)
16
+
17
+ def testIP2OS(self):
18
+ #9,202,000 = (0x)8c 69 50.
19
+ os = Conversion.IP2OS(9202000)
20
+ self.assertEqual(os, b'\x8c\x69\x50')
21
+
22
+ def testIP2OSLen(self):
23
+ i = 9202000
24
+ os = Conversion.IP2OS(i, 200)
25
+ i2 = Conversion.OS2IP(os)
26
+ self.assertEqual(i, i2)
27
+
28
+ if __name__ == "__main__":
29
+ #import sys;sys.argv = ['', 'Test.testOS2IP']
30
+ unittest.main()
@@ -0,0 +1,53 @@
1
+ '''
2
+ :Date: Aug 26, 2016
3
+ :Authors: J. Ayo Akinyele
4
+ '''
5
+ from charm.toolbox.ecgroup import ECGroup,G
6
+ from charm.toolbox.eccurve import prime192v1,prime192v2
7
+ from charm.toolbox.securerandom import OpenSSLRand
8
+ import unittest
9
+
10
+ runs = 10
11
+
12
+ class ECGroupEncodeAndDecode(unittest.TestCase):
13
+ def testRandomGroupDecode(self):
14
+ group = ECGroup(prime192v1)
15
+
16
+ for i in range(runs):
17
+ r = group.random(G)
18
+ m = group.decode(r, True)
19
+ n = group.encode(m, True)
20
+ assert r == n, "Failed to encode/decode properly including counter"
21
+
22
+ def testRandomMessageDecode(self):
23
+ group = ECGroup(prime192v2)
24
+ for i in range(runs):
25
+ msg_len = group.bitsize()
26
+ s = OpenSSLRand().getRandomBytes(msg_len)
27
+ g = group.encode(s)
28
+ t = group.decode(g)
29
+ assert s == t, "Failed to encode/decode %d properly" % i
30
+
31
+ def testBadMessage1Decode(self):
32
+ group = ECGroup(prime192v1)
33
+ s = b'\x00\x9d\xaa2\xfa\xf2;\xd5\xe56,\xe8\x1c\x17[k4\xa4\x8b\xad'
34
+ g = group.encode(s)
35
+ t = group.decode(g)
36
+ assert s == t, "Failed to encode/decode properly"
37
+
38
+ def testBadMessage2Decode(self):
39
+ group = ECGroup(prime192v2)
40
+ s = b'~3\xfcN\x00\x8eF\xfaq\xdc\x8d\x14\x8d\xde\xebC^1`\x99'
41
+ g = group.encode(s)
42
+ t = group.decode(g)
43
+ assert s == t, "Failed to encode/decode properly"
44
+
45
+ def testBadMessage3Decode(self):
46
+ group = ECGroup(prime192v2)
47
+ s = b'\x8a$\x1b@5xm\x00f\xa5\x98{OJ\xd9,\x17`\xb7\xcf\xd2\x1e\xb3\x99'
48
+ g = group.encode(s, True)
49
+ t = group.decode(g, True)
50
+ assert s == t, "Failed to encode/decode properly"
51
+
52
+ if __name__ == "__main__":
53
+ unittest.main()
@@ -0,0 +1,441 @@
1
+ """
2
+ Comprehensive arithmetic tests for the integer module.
3
+
4
+ These tests validate integer module behavior with GCD operations and integer conversions,
5
+ specifically designed to catch Python 3.12+ compatibility issues like the Py_SIZE() vs lv_tag bug.
6
+
7
+ Tests cover:
8
+ 1. Integer conversion correctness (Python int <-> integer)
9
+ 2. GCD operations and isCoPrime() method
10
+ 3. Modular arithmetic (modular inverse, modular operations)
11
+ 4. Regression tests for Python 3.12+ compatibility
12
+ 5. Integration tests that mirror real scheme usage
13
+ """
14
+
15
+ import sys
16
+ import unittest
17
+ import pytest
18
+
19
+ from charm.core.math.integer import (
20
+ integer, gcd, random, randomPrime, isPrime, bitsize, serialize, deserialize
21
+ )
22
+
23
+
24
+ class IntegerConversionTest(unittest.TestCase):
25
+ """Test integer conversion correctness between Python int and integer objects."""
26
+
27
+ def test_common_rsa_exponents(self):
28
+ """Verify that common RSA exponents convert correctly."""
29
+ common_exponents = [65537, 3, 5, 17, 257, 641, 6700417]
30
+ for exp in common_exponents:
31
+ with self.subTest(exponent=exp):
32
+ result = integer(exp)
33
+ self.assertEqual(int(result), exp, f"integer({exp}) should equal {exp}")
34
+ self.assertEqual(str(result), str(exp), f"str(integer({exp})) should equal '{exp}'")
35
+
36
+ def test_small_values(self):
37
+ """Test edge cases with small values."""
38
+ small_values = [0, 1, 2, 10, 100, 255, 256, 1000]
39
+ for val in small_values:
40
+ with self.subTest(value=val):
41
+ result = integer(val)
42
+ self.assertEqual(int(result), val, f"integer({val}) should equal {val}")
43
+
44
+ def test_large_values(self):
45
+ """Test large values that require multiple digits in PyLongObject."""
46
+ # These values require multiple 30-bit digits in Python's internal representation
47
+ large_values = [
48
+ 2**30, # Just over one digit
49
+ 2**60, # Two digits
50
+ 2**90, # Three digits
51
+ 2**128, # Common cryptographic size
52
+ 2**256, # 256-bit value
53
+ 2**512, # 512-bit value
54
+ 2**1024, # 1024-bit value (RSA key size)
55
+ ]
56
+ for val in large_values:
57
+ with self.subTest(bits=val.bit_length()):
58
+ result = integer(val)
59
+ self.assertEqual(int(result), val, f"integer(2^{val.bit_length()-1}) conversion failed")
60
+
61
+ def test_negative_values(self):
62
+ """Test negative integer conversion."""
63
+ negative_values = [-1, -2, -10, -100, -65537, -2**30, -2**60, -2**128]
64
+ for val in negative_values:
65
+ with self.subTest(value=val):
66
+ result = integer(val)
67
+ self.assertEqual(int(result), val, f"integer({val}) should equal {val}")
68
+
69
+ def test_round_trip_conversion(self):
70
+ """Verify round-trip conversion: Python int -> integer -> Python int preserves value."""
71
+ test_values = [
72
+ 0, 1, -1, 65537, -65537,
73
+ 2**30 - 1, 2**30, 2**30 + 1, # Around digit boundary
74
+ 2**60 - 1, 2**60, 2**60 + 1, # Two digit boundary
75
+ 2**256, -2**256,
76
+ 2**512 + 12345, -2**512 - 12345,
77
+ ]
78
+ for val in test_values:
79
+ with self.subTest(value=val if abs(val) < 1000 else f"2^{val.bit_length()-1}"):
80
+ result = int(integer(val))
81
+ self.assertEqual(result, val, "Round-trip conversion failed")
82
+
83
+ def test_integer_from_integer(self):
84
+ """Test creating integer from another integer object."""
85
+ original = integer(65537)
86
+ copy = integer(original)
87
+ self.assertEqual(int(copy), 65537)
88
+ self.assertEqual(int(original), int(copy))
89
+
90
+
91
+ class GCDOperationsTest(unittest.TestCase):
92
+ """Test GCD operations with various integer types."""
93
+
94
+ def test_gcd_python_ints(self):
95
+ """Test gcd() with Python integers."""
96
+ test_cases = [
97
+ (12, 8, 4),
98
+ (17, 13, 1), # Coprime
99
+ (100, 25, 25),
100
+ (65537, 65536, 1), # Common RSA exponent vs power of 2
101
+ (2**128, 2**64, 2**64),
102
+ ]
103
+ for a, b, expected in test_cases:
104
+ with self.subTest(a=a, b=b):
105
+ result = gcd(a, b)
106
+ self.assertEqual(int(result), expected)
107
+
108
+ def test_gcd_integer_objects(self):
109
+ """Test gcd() with integer objects."""
110
+ a = integer(48)
111
+ b = integer(18)
112
+ result = gcd(a, b)
113
+ self.assertEqual(int(result), 6)
114
+
115
+ def test_gcd_mixed_types(self):
116
+ """Test gcd() with mixed Python int and integer objects."""
117
+ a = integer(48)
118
+ result1 = gcd(a, 18)
119
+ result2 = gcd(48, integer(18))
120
+ self.assertEqual(int(result1), 6)
121
+ self.assertEqual(int(result2), 6)
122
+
123
+ def test_gcd_edge_cases(self):
124
+ """Test gcd edge cases."""
125
+ # gcd(0, n) = n
126
+ self.assertEqual(int(gcd(0, 5)), 5)
127
+ self.assertEqual(int(gcd(5, 0)), 5)
128
+ # gcd(1, n) = 1
129
+ self.assertEqual(int(gcd(1, 12345)), 1)
130
+ self.assertEqual(int(gcd(12345, 1)), 1)
131
+ # gcd(n, n) = n
132
+ self.assertEqual(int(gcd(42, 42)), 42)
133
+
134
+
135
+ class IsCoPrimeTest(unittest.TestCase):
136
+ """Test isCoPrime() method for coprimality checking."""
137
+
138
+ def test_coprime_common_exponents(self):
139
+ """Test isCoPrime() with common RSA exponents vs typical phi_N values."""
140
+ # Simulate phi_N = (p-1)(q-1) for small primes
141
+ p, q = 61, 53
142
+ phi_N = integer((p - 1) * (q - 1)) # 3120
143
+
144
+ # 65537 should be coprime to 3120 (gcd = 1)
145
+ self.assertTrue(phi_N.isCoPrime(65537))
146
+ # 3 should be coprime to 3120 (gcd = 3, not coprime!)
147
+ self.assertFalse(phi_N.isCoPrime(3))
148
+ # 17 should be coprime to 3120
149
+ self.assertTrue(phi_N.isCoPrime(17))
150
+
151
+ def test_coprime_with_integer_objects(self):
152
+ """Test isCoPrime() with integer objects as arguments."""
153
+ a = integer(35) # 5 * 7
154
+ self.assertTrue(a.isCoPrime(12)) # gcd(35, 12) = 1
155
+ self.assertFalse(a.isCoPrime(15)) # gcd(35, 15) = 5
156
+ self.assertTrue(a.isCoPrime(integer(12)))
157
+
158
+ def test_coprime_edge_cases(self):
159
+ """Test isCoPrime() edge cases."""
160
+ one = integer(1)
161
+ self.assertTrue(one.isCoPrime(12345)) # 1 is coprime to everything
162
+
163
+ # Any number is coprime to 1
164
+ n = integer(12345)
165
+ self.assertTrue(n.isCoPrime(1))
166
+
167
+
168
+ class ModularArithmeticTest(unittest.TestCase):
169
+ """Test modular arithmetic operations."""
170
+
171
+ def test_modular_inverse_basic(self):
172
+ """Test basic modular inverse computation."""
173
+ # e = 3, modulus = 11, inverse should be 4 (3*4 = 12 ≡ 1 mod 11)
174
+ e = integer(3, 11)
175
+ d = e ** -1
176
+ self.assertEqual(int(d), 4)
177
+ # Verify: e * d ≡ 1 (mod 11)
178
+ product = integer(int(e) * int(d), 11)
179
+ self.assertEqual(int(product), 1)
180
+
181
+ def test_modular_inverse_rsa_exponent(self):
182
+ """Test modular inverse with RSA-like parameters."""
183
+ # Small RSA example: p=61, q=53, phi_N=3120, e=17
184
+ phi_N = 3120
185
+ e = integer(17, phi_N)
186
+ d = e ** -1
187
+ # Verify: e * d ≡ 1 (mod phi_N)
188
+ product = (int(e) * int(d)) % phi_N
189
+ self.assertEqual(product, 1)
190
+
191
+ def test_modular_operations_respect_modulus(self):
192
+ """Test that modular operations respect the modulus."""
193
+ modulus = 17
194
+ a = integer(20, modulus) # 20 mod 17 = 3
195
+ self.assertEqual(int(a), 3)
196
+
197
+ b = integer(100, modulus) # 100 mod 17 = 15
198
+ self.assertEqual(int(b), 15)
199
+
200
+ def test_modular_exponentiation(self):
201
+ """Test modular exponentiation."""
202
+ base = integer(2, 13)
203
+ # 2^10 = 1024, 1024 mod 13 = 10
204
+ result = base ** 10
205
+ self.assertEqual(int(result), 1024 % 13)
206
+
207
+ def test_integer_without_modulus(self):
208
+ """Test integer behavior when modulus is not set."""
209
+ a = integer(65537)
210
+ b = integer(12345)
211
+ # Without modulus, operations should work as regular integers
212
+ product = a * b
213
+ self.assertEqual(int(product), 65537 * 12345)
214
+
215
+
216
+ class Python312CompatibilityTest(unittest.TestCase):
217
+ """Regression tests for Python 3.12+ compatibility.
218
+
219
+ These tests specifically target the Py_SIZE() vs lv_tag bug that was fixed.
220
+ The bug caused incorrect digit count extraction for multi-digit integers.
221
+ """
222
+
223
+ def test_65537_regression(self):
224
+ """Test the specific value that exposed the Python 3.12+ bug.
225
+
226
+ In the buggy version, integer(65537) returned a huge incorrect value
227
+ like 12259964326940877255866161939725058870607969088809533441.
228
+ """
229
+ result = integer(65537)
230
+ self.assertEqual(int(result), 65537)
231
+ # Also verify string representation
232
+ self.assertEqual(str(result), "65537")
233
+
234
+ def test_multi_digit_integers(self):
235
+ """Test integers that require multiple digits in PyLongObject.
236
+
237
+ Python uses 30-bit digits internally. Values >= 2^30 require multiple digits.
238
+ The bug was in extracting the digit count from lv_tag.
239
+ """
240
+ # Single digit (< 2^30)
241
+ single_digit = 2**29
242
+ self.assertEqual(int(integer(single_digit)), single_digit)
243
+
244
+ # Two digits (2^30 to 2^60-1)
245
+ two_digits = 2**45
246
+ self.assertEqual(int(integer(two_digits)), two_digits)
247
+
248
+ # Three digits (2^60 to 2^90-1)
249
+ three_digits = 2**75
250
+ self.assertEqual(int(integer(three_digits)), three_digits)
251
+
252
+ # Many digits
253
+ many_digits = 2**300
254
+ self.assertEqual(int(integer(many_digits)), many_digits)
255
+
256
+ def test_sign_handling(self):
257
+ """Test sign handling for negative integers.
258
+
259
+ In Python 3.12+, sign is stored in lv_tag bits 0-1:
260
+ - 0 = positive
261
+ - 1 = zero
262
+ - 2 = negative
263
+ """
264
+ # Positive
265
+ pos = integer(12345)
266
+ self.assertEqual(int(pos), 12345)
267
+ self.assertGreater(int(pos), 0)
268
+
269
+ # Zero
270
+ zero = integer(0)
271
+ self.assertEqual(int(zero), 0)
272
+
273
+ # Negative
274
+ neg = integer(-12345)
275
+ self.assertEqual(int(neg), -12345)
276
+ self.assertLess(int(neg), 0)
277
+
278
+ # Large negative
279
+ large_neg = integer(-2**100)
280
+ self.assertEqual(int(large_neg), -2**100)
281
+
282
+ def test_digit_boundary_values(self):
283
+ """Test values at digit boundaries (multiples of 2^30)."""
284
+ boundaries = [
285
+ 2**30 - 1, 2**30, 2**30 + 1,
286
+ 2**60 - 1, 2**60, 2**60 + 1,
287
+ 2**90 - 1, 2**90, 2**90 + 1,
288
+ ]
289
+ for val in boundaries:
290
+ with self.subTest(value=f"2^{val.bit_length()-1}"):
291
+ self.assertEqual(int(integer(val)), val)
292
+ self.assertEqual(int(integer(-val)), -val)
293
+
294
+ def test_mpz_to_pylong_roundtrip(self):
295
+ """Test that mpzToLongObj correctly creates Python integers.
296
+
297
+ This tests the reverse direction: GMP mpz_t -> Python int.
298
+ """
299
+ # Create integer, perform operation, convert back
300
+ a = integer(2**100)
301
+ b = integer(2**50)
302
+ product = a * b
303
+ expected = 2**100 * 2**50
304
+ self.assertEqual(int(product), expected)
305
+
306
+
307
+ class IntegrationSchemeTest(unittest.TestCase):
308
+ """Integration tests that mirror real cryptographic scheme usage."""
309
+
310
+ def test_rsa_coprime_search_pattern(self):
311
+ """Test the RSA keygen coprime search pattern.
312
+
313
+ This mirrors the pattern used in pkenc_rsa.py to find e coprime to phi_N.
314
+ """
315
+ # Simulate small RSA parameters
316
+ p, q = 61, 53
317
+ N = p * q # 3233
318
+ phi_N = integer((p - 1) * (q - 1)) # 3120
319
+
320
+ # Common RSA exponents to try
321
+ common_exponents = [65537, 3, 5, 17, 257, 641]
322
+ e_value = None
323
+
324
+ for candidate in common_exponents:
325
+ if phi_N.isCoPrime(candidate):
326
+ e_value = candidate
327
+ break
328
+
329
+ self.assertIsNotNone(e_value, "Should find a coprime exponent")
330
+ # Verify it's actually coprime
331
+ self.assertEqual(int(gcd(e_value, int(phi_N))), 1)
332
+
333
+ # Compute modular inverse
334
+ e = integer(e_value, int(phi_N))
335
+ d = e ** -1
336
+
337
+ # Verify: e * d ≡ 1 (mod phi_N)
338
+ product = (e_value * int(d)) % int(phi_N)
339
+ self.assertEqual(product, 1)
340
+
341
+ def test_rsa_encryption_decryption_pattern(self):
342
+ """Test RSA encryption/decryption with integer operations."""
343
+ # Small RSA parameters for testing
344
+ p, q = 61, 53
345
+ N = p * q # 3233
346
+ phi_N = (p - 1) * (q - 1) # 3120
347
+ e = 17
348
+ d = int(integer(e, phi_N) ** -1) # 2753
349
+
350
+ # Encrypt message m = 123
351
+ m = 123
352
+ c = pow(m, e, N) # c = 123^17 mod 3233 = 855
353
+
354
+ # Decrypt
355
+ m_decrypted = pow(c, d, N)
356
+ self.assertEqual(m_decrypted, m)
357
+
358
+ def test_paillier_pattern(self):
359
+ """Test Paillier-like integer encoding pattern."""
360
+ # Paillier uses n^2 as modulus
361
+ p, q = 17, 19
362
+ n = p * q # 323
363
+ n_squared = n * n # 104329
364
+
365
+ # Encode a message
366
+ m = 42
367
+ r = 7 # Random value coprime to n
368
+
369
+ # g = n + 1 is a common choice
370
+ g = n + 1
371
+
372
+ # Encrypt: c = g^m * r^n mod n^2
373
+ c = (pow(g, m, n_squared) * pow(r, n, n_squared)) % n_squared
374
+
375
+ # Verify the ciphertext is in the correct range
376
+ self.assertGreater(c, 0)
377
+ self.assertLess(c, n_squared)
378
+
379
+ def test_serialization_roundtrip(self):
380
+ """Test serialization and deserialization of integer objects."""
381
+ test_values = [0, 1, 65537, 2**128, 2**256, -12345, -2**100]
382
+ for val in test_values:
383
+ with self.subTest(value=val if abs(val) < 1000 else f"2^{abs(val).bit_length()-1}"):
384
+ original = integer(val)
385
+ serialized = serialize(original)
386
+ deserialized = deserialize(serialized)
387
+ self.assertEqual(int(deserialized), val)
388
+
389
+
390
+ class ArithmeticOperationsTest(unittest.TestCase):
391
+ """Test basic arithmetic operations on integer objects."""
392
+
393
+ def test_addition(self):
394
+ """Test integer addition."""
395
+ a = integer(100)
396
+ b = integer(200)
397
+ self.assertEqual(int(a + b), 300)
398
+ self.assertEqual(int(a + 50), 150)
399
+
400
+ def test_subtraction(self):
401
+ """Test integer subtraction."""
402
+ a = integer(200)
403
+ b = integer(100)
404
+ self.assertEqual(int(a - b), 100)
405
+ self.assertEqual(int(a - 50), 150)
406
+
407
+ def test_multiplication(self):
408
+ """Test integer multiplication."""
409
+ a = integer(12)
410
+ b = integer(34)
411
+ self.assertEqual(int(a * b), 408)
412
+ self.assertEqual(int(a * 10), 120)
413
+
414
+ def test_division(self):
415
+ """Test integer division."""
416
+ a = integer(100)
417
+ b = integer(25)
418
+ self.assertEqual(int(a / b), 4)
419
+
420
+ def test_exponentiation(self):
421
+ """Test integer exponentiation."""
422
+ a = integer(2)
423
+ self.assertEqual(int(a ** 10), 1024)
424
+
425
+ def test_comparison(self):
426
+ """Test integer comparison operations."""
427
+ a = integer(100)
428
+ b = integer(200)
429
+ c = integer(100)
430
+
431
+ self.assertTrue(a < b)
432
+ self.assertTrue(b > a)
433
+ self.assertTrue(a <= c)
434
+ self.assertTrue(a >= c)
435
+ self.assertTrue(a == c)
436
+ self.assertTrue(a != b)
437
+
438
+
439
+ if __name__ == "__main__":
440
+ unittest.main()
441
+