libmongocrypt-helper 1.7.4.0.1002 → 1.11.0.0.1001

Sign up to get free protection for your applications and to get access to all the features.
Files changed (600) hide show
  1. checksums.yaml +4 -4
  2. data/ext/libmongocrypt/libmongocrypt/CHANGELOG.md +40 -5
  3. data/ext/libmongocrypt/libmongocrypt/CMakeLists.txt +29 -24
  4. data/ext/libmongocrypt/libmongocrypt/CODEOWNERS +1 -4
  5. data/ext/libmongocrypt/libmongocrypt/Earthfile +522 -0
  6. data/ext/libmongocrypt/libmongocrypt/README.md +36 -40
  7. data/ext/libmongocrypt/libmongocrypt/bindings/cs/CMakeLists.txt +1 -0
  8. data/ext/libmongocrypt/libmongocrypt/bindings/cs/MongoDB.Driver.snk +0 -0
  9. data/ext/libmongocrypt/libmongocrypt/bindings/cs/MongoDB.Libmongocrypt/AssemblyInfo.cs +2 -2
  10. data/ext/libmongocrypt/libmongocrypt/bindings/cs/MongoDB.Libmongocrypt/Binary.cs +16 -2
  11. data/ext/libmongocrypt/libmongocrypt/bindings/cs/MongoDB.Libmongocrypt/CryptClient.cs +25 -6
  12. data/ext/libmongocrypt/libmongocrypt/bindings/cs/MongoDB.Libmongocrypt/CryptClientFactory.cs +8 -4
  13. data/ext/libmongocrypt/libmongocrypt/bindings/cs/MongoDB.Libmongocrypt/Library.cs +35 -3
  14. data/ext/libmongocrypt/libmongocrypt/bindings/cs/MongoDB.Libmongocrypt/LibraryLoader.cs +81 -44
  15. data/ext/libmongocrypt/libmongocrypt/bindings/cs/MongoDB.Libmongocrypt/MongoDB.Libmongocrypt.csproj +2 -1
  16. data/ext/libmongocrypt/libmongocrypt/bindings/cs/MongoDB.Libmongocrypt.Example/MongoDB.Libmongocrypt.Example.csproj +1 -1
  17. data/ext/libmongocrypt/libmongocrypt/bindings/cs/MongoDB.Libmongocrypt.Test/BasicTests.cs +1 -1
  18. data/ext/libmongocrypt/libmongocrypt/bindings/cs/MongoDB.Libmongocrypt.Test/MongoDB.Libmongocrypt.Test.csproj +2 -0
  19. data/ext/libmongocrypt/libmongocrypt/bindings/cs/MongoDB.Libmongocrypt.Test32/MongoDB.Libmongocrypt.Test32.csproj +2 -0
  20. data/ext/libmongocrypt/libmongocrypt/bindings/cs/README.md +3 -0
  21. data/ext/libmongocrypt/libmongocrypt/bindings/cs/Scripts/build.cake +22 -26
  22. data/ext/libmongocrypt/libmongocrypt/bindings/cs/Scripts/build.config +3 -0
  23. data/ext/libmongocrypt/libmongocrypt/bindings/cs/Scripts/build.sh +0 -0
  24. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/benchmarks/build.gradle.kts +28 -0
  25. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/benchmarks/src/main/java/com/mongodb/crypt/benchmark/BenchmarkRunner.java +217 -0
  26. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/benchmarks/src/main/resources/keyDocument.json +24 -0
  27. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/build.gradle.kts +22 -7
  28. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/gradle/wrapper/gradle-wrapper.jar +0 -0
  29. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/gradle/wrapper/gradle-wrapper.properties +1 -1
  30. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/gradlew +154 -108
  31. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/gradlew.bat +7 -18
  32. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/settings.gradle.kts +1 -0
  33. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/src/main/java/com/mongodb/crypt/capi/CAPI.java +41 -6
  34. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/src/main/java/com/mongodb/crypt/capi/CAPIHelper.java +5 -5
  35. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/src/main/java/com/mongodb/crypt/capi/CipherCallback.java +27 -1
  36. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/src/main/java/com/mongodb/crypt/capi/MongoCryptImpl.java +34 -19
  37. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/src/main/java/com/mongodb/crypt/capi/MongoExplicitEncryptOptions.java +6 -4
  38. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/src/main/resources/META-INF/native-image/jni-config.json +180 -0
  39. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/src/main/resources/META-INF/native-image/reflect-config.json +134 -0
  40. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/src/test/java/com/mongodb/crypt/capi/MongoCryptTest.java +50 -6
  41. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/src/test/resources/fle2-find-range-explicit-v2/int32/encrypted-payload.json +26 -0
  42. data/ext/libmongocrypt/libmongocrypt/bindings/{python/test/data/fle2-find-range-explicit → java/mongocrypt/src/test/resources/fle2-find-range-explicit-v2}/int32/rangeopts.json +3 -0
  43. data/ext/libmongocrypt/libmongocrypt/bindings/node/README.md +4 -771
  44. data/ext/libmongocrypt/libmongocrypt/bindings/python/CHANGELOG.rst +86 -0
  45. data/ext/libmongocrypt/libmongocrypt/bindings/python/README.rst +41 -20
  46. data/ext/libmongocrypt/libmongocrypt/bindings/python/RELEASE.rst +6 -24
  47. data/ext/libmongocrypt/libmongocrypt/bindings/python/build-manylinux-wheel.sh +4 -13
  48. data/ext/libmongocrypt/libmongocrypt/bindings/python/hatch_build.py +36 -0
  49. data/ext/libmongocrypt/libmongocrypt/bindings/python/libmongocrypt-version.txt +1 -0
  50. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/__init__.py +2 -2
  51. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/asynchronous/auto_encrypter.py +61 -0
  52. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/asynchronous/credentials.py +156 -0
  53. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/asynchronous/explicit_encrypter.py +156 -0
  54. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/asynchronous/state_machine.py +149 -0
  55. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/auto_encrypter.py +2 -46
  56. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/binary.py +14 -17
  57. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/binding.py +232 -296
  58. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/compat.py +6 -31
  59. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/credentials.py +2 -121
  60. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/crypto.py +31 -20
  61. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/errors.py +2 -2
  62. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/explicit_encrypter.py +2 -233
  63. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/mongocrypt.py +168 -254
  64. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/options.py +265 -0
  65. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/state_machine.py +2 -141
  66. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/synchronous/auto_encrypter.py +61 -0
  67. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/synchronous/credentials.py +156 -0
  68. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/synchronous/explicit_encrypter.py +156 -0
  69. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/synchronous/state_machine.py +149 -0
  70. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/version.py +2 -2
  71. data/ext/libmongocrypt/libmongocrypt/bindings/python/pyproject.toml +118 -0
  72. data/ext/libmongocrypt/libmongocrypt/bindings/python/release.sh +97 -61
  73. data/ext/libmongocrypt/libmongocrypt/bindings/python/requirements-test.txt +7 -0
  74. data/ext/libmongocrypt/libmongocrypt/bindings/python/requirements.txt +4 -0
  75. data/ext/libmongocrypt/libmongocrypt/bindings/python/sbom.json +76 -0
  76. data/ext/libmongocrypt/libmongocrypt/bindings/python/strip_header.py +6 -7
  77. data/ext/libmongocrypt/libmongocrypt/bindings/python/synchro.py +64 -0
  78. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/__init__.py +2 -10
  79. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/collection-info.json +1 -1
  80. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/command.json +1 -1
  81. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/compact/success/encrypted-payload.json +21 -21
  82. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/encrypted-command-reply.json +1 -1
  83. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/encrypted-field-config-map.json +1 -1
  84. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/fle2-find-range-explicit-v2/int32/encrypted-payload.json +26 -0
  85. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/fle2-find-range-explicit-v2/int32/rangeopts.json +14 -0
  86. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/key-document-azure.json +1 -1
  87. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/key-document-gcp.json +1 -1
  88. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/key-document.json +1 -1
  89. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/key-filter.json +1 -1
  90. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/list-collections-filter.json +1 -1
  91. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/mongocryptd-command.json +1 -1
  92. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/mongocryptd-reply.json +1 -1
  93. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/schema-map.json +1 -1
  94. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/performance/keyDocument.json +24 -0
  95. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/performance/perf_test.py +165 -0
  96. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/test_binding.py +8 -12
  97. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/test_crypto.py +9 -11
  98. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/test_mongocrypt.py +991 -343
  99. data/ext/libmongocrypt/libmongocrypt/bindings/python/update-sbom.sh +14 -0
  100. data/ext/libmongocrypt/libmongocrypt/cmake/FetchMongoC.cmake +19 -1
  101. data/ext/libmongocrypt/libmongocrypt/cmake/ImportBSON.cmake +31 -6
  102. data/ext/libmongocrypt/libmongocrypt/cmake/IntelDFP.cmake +20 -227
  103. data/ext/libmongocrypt/libmongocrypt/cmake/Patch.cmake +54 -0
  104. data/ext/libmongocrypt/libmongocrypt/cmake/Platform.cmake +50 -0
  105. data/ext/libmongocrypt/libmongocrypt/cmake/mongocrypt-config.cmake +21 -0
  106. data/ext/libmongocrypt/libmongocrypt/doc/img/cli-icon.png +0 -0
  107. data/ext/libmongocrypt/libmongocrypt/doc/img/reference-targets.png +0 -0
  108. data/ext/libmongocrypt/libmongocrypt/doc/releasing.md +153 -0
  109. data/ext/libmongocrypt/libmongocrypt/etc/c6-vault.repo +39 -0
  110. data/ext/libmongocrypt/libmongocrypt/etc/calc_release_version.py +61 -28
  111. data/ext/libmongocrypt/libmongocrypt/etc/calc_release_version_selftest.sh +73 -0
  112. data/ext/libmongocrypt/libmongocrypt/etc/cyclonedx.sbom.json +108 -0
  113. data/ext/libmongocrypt/libmongocrypt/etc/fle2_aead_generate_tests.py +15 -24
  114. data/ext/libmongocrypt/libmongocrypt/etc/fle2_crypto.py +66 -54
  115. data/ext/libmongocrypt/libmongocrypt/etc/fle2_generate_tests.py +14 -23
  116. data/ext/libmongocrypt/libmongocrypt/etc/fle2v2_aead_generate_tests.py +32 -0
  117. data/ext/libmongocrypt/libmongocrypt/etc/format-all.sh +12 -0
  118. data/ext/libmongocrypt/libmongocrypt/etc/format.sh +16 -0
  119. data/ext/libmongocrypt/libmongocrypt/etc/install-package.sh +48 -0
  120. data/ext/libmongocrypt/libmongocrypt/etc/libbson-remove-GCC-diagnostic-pragma.patch +27 -0
  121. data/ext/libmongocrypt/libmongocrypt/etc/mongo-inteldfp-alpine-arm-fix.patch +17 -0
  122. data/ext/libmongocrypt/libmongocrypt/etc/mongo-inteldfp-libmongocrypt-pr-625.patch +13 -0
  123. data/ext/libmongocrypt/libmongocrypt/etc/packager.py +120 -91
  124. data/ext/libmongocrypt/libmongocrypt/etc/purls.txt +14 -0
  125. data/ext/libmongocrypt/libmongocrypt/etc/repo_config.yaml +56 -0
  126. data/ext/libmongocrypt/libmongocrypt/etc/silk-create-asset-group.sh +70 -0
  127. data/ext/libmongocrypt/libmongocrypt/etc/ssdlc_compliance_report.md +37 -0
  128. data/ext/libmongocrypt/libmongocrypt/etc/third_party_vulnerabilities.md +42 -0
  129. data/ext/libmongocrypt/libmongocrypt/integrating.md +18 -1
  130. data/ext/libmongocrypt/libmongocrypt/kms-message/CMakeLists.txt +11 -3
  131. data/ext/libmongocrypt/libmongocrypt/kms-message/src/kms_gcp_request.c +1 -1
  132. data/ext/libmongocrypt/libmongocrypt/kms-message/src/kms_kmip_reader_writer.c +17 -0
  133. data/ext/libmongocrypt/libmongocrypt/kms-message/src/kms_kmip_reader_writer_private.h +6 -0
  134. data/ext/libmongocrypt/libmongocrypt/kms-message/src/kms_kmip_request.c +211 -1
  135. data/ext/libmongocrypt/libmongocrypt/kms-message/src/kms_kmip_response.c +163 -0
  136. data/ext/libmongocrypt/libmongocrypt/kms-message/src/kms_kmip_tag_type_private.h +2 -1
  137. data/ext/libmongocrypt/libmongocrypt/kms-message/src/kms_message/kms_kmip_request.h +17 -0
  138. data/ext/libmongocrypt/libmongocrypt/kms-message/src/kms_message/kms_kmip_response.h +6 -0
  139. data/ext/libmongocrypt/libmongocrypt/kms-message/src/kms_port.c +3 -2
  140. data/ext/libmongocrypt/libmongocrypt/kms-message/src/kms_request.c +4 -2
  141. data/ext/libmongocrypt/libmongocrypt/kms-message/src/kms_request_str.c +2 -2
  142. data/ext/libmongocrypt/libmongocrypt/kms-message/test/test_kmip_reader_writer.c +23 -2
  143. data/ext/libmongocrypt/libmongocrypt/src/crypto/cng.c +381 -436
  144. data/ext/libmongocrypt/libmongocrypt/src/crypto/commoncrypto.c +162 -227
  145. data/ext/libmongocrypt/libmongocrypt/src/crypto/libcrypto.c +183 -248
  146. data/ext/libmongocrypt/libmongocrypt/src/crypto/none.c +33 -55
  147. data/ext/libmongocrypt/libmongocrypt/src/csfle-markup.cpp +175 -205
  148. data/ext/libmongocrypt/libmongocrypt/src/mc-array-private.h +10 -21
  149. data/ext/libmongocrypt/libmongocrypt/src/mc-array.c +44 -56
  150. data/ext/libmongocrypt/libmongocrypt/src/mc-check-conversions-private.h +4 -8
  151. data/ext/libmongocrypt/libmongocrypt/src/mc-dec128.h +351 -463
  152. data/ext/libmongocrypt/libmongocrypt/src/mc-dec128.test.cpp +59 -66
  153. data/ext/libmongocrypt/libmongocrypt/src/mc-efc-private.h +21 -11
  154. data/ext/libmongocrypt/libmongocrypt/src/mc-efc.c +175 -99
  155. data/ext/libmongocrypt/libmongocrypt/src/mc-fle-blob-subtype-private.h +18 -10
  156. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-encryption-placeholder-private.h +69 -70
  157. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-encryption-placeholder.c +445 -439
  158. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-find-equality-payload-private-v2.h +41 -0
  159. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-find-equality-payload-private.h +11 -18
  160. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-find-equality-payload-v2.c +135 -0
  161. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-find-equality-payload.c +109 -126
  162. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-find-range-payload-private-v2.h +103 -0
  163. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-find-range-payload-private.h +28 -31
  164. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-find-range-payload-v2.c +173 -0
  165. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-find-range-payload.c +106 -125
  166. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-insert-update-payload-private-v2.h +130 -0
  167. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-insert-update-payload-private.h +36 -44
  168. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-insert-update-payload-v2.c +382 -0
  169. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-insert-update-payload.c +237 -278
  170. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-payload-iev-private-v2.h +133 -0
  171. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-payload-iev-private.h +67 -56
  172. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-payload-iev-v2.c +437 -0
  173. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-payload-iev.c +454 -325
  174. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-payload-uev-common-private.h +69 -0
  175. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-payload-uev-common.c +182 -0
  176. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-payload-uev-private.h +23 -31
  177. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-payload-uev-v2-private.h +85 -0
  178. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-payload-uev-v2.c +142 -0
  179. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-payload-uev.c +104 -231
  180. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-range-operator-private.h +8 -7
  181. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-rfds-private.h +56 -56
  182. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-rfds.c +520 -580
  183. data/ext/libmongocrypt/libmongocrypt/src/mc-optional-private.h +47 -66
  184. data/ext/libmongocrypt/libmongocrypt/src/mc-range-edge-generation-private.h +67 -76
  185. data/ext/libmongocrypt/libmongocrypt/src/mc-range-edge-generation.c +201 -190
  186. data/ext/libmongocrypt/libmongocrypt/src/mc-range-encoding-private.h +55 -42
  187. data/ext/libmongocrypt/libmongocrypt/src/mc-range-encoding.c +809 -558
  188. data/ext/libmongocrypt/libmongocrypt/src/mc-range-mincover-generator.template.h +177 -201
  189. data/ext/libmongocrypt/libmongocrypt/src/mc-range-mincover-private.h +54 -53
  190. data/ext/libmongocrypt/libmongocrypt/src/mc-range-mincover.c +188 -240
  191. data/ext/libmongocrypt/libmongocrypt/src/mc-rangeopts-private.h +43 -35
  192. data/ext/libmongocrypt/libmongocrypt/src/mc-rangeopts.c +484 -337
  193. data/ext/libmongocrypt/libmongocrypt/src/mc-reader-private.h +36 -71
  194. data/ext/libmongocrypt/libmongocrypt/src/mc-reader.c +111 -164
  195. data/ext/libmongocrypt/libmongocrypt/src/mc-tokens-private.h +87 -65
  196. data/ext/libmongocrypt/libmongocrypt/src/mc-tokens.c +153 -125
  197. data/ext/libmongocrypt/libmongocrypt/src/mc-writer-private.h +66 -0
  198. data/ext/libmongocrypt/libmongocrypt/src/mc-writer.c +141 -0
  199. data/ext/libmongocrypt/libmongocrypt/src/mlib/check.hpp +37 -55
  200. data/ext/libmongocrypt/libmongocrypt/src/mlib/endian.h +11 -11
  201. data/ext/libmongocrypt/libmongocrypt/src/mlib/error.h +27 -32
  202. data/ext/libmongocrypt/libmongocrypt/src/mlib/int128.h +461 -499
  203. data/ext/libmongocrypt/libmongocrypt/src/mlib/int128.test.cpp +256 -334
  204. data/ext/libmongocrypt/libmongocrypt/src/mlib/macros.h +4 -5
  205. data/ext/libmongocrypt/libmongocrypt/src/mlib/path.h +196 -231
  206. data/ext/libmongocrypt/libmongocrypt/src/mlib/path.test.c +56 -79
  207. data/ext/libmongocrypt/libmongocrypt/src/mlib/str.h +411 -530
  208. data/ext/libmongocrypt/libmongocrypt/src/mlib/str.test.c +116 -131
  209. data/ext/libmongocrypt/libmongocrypt/src/mlib/thread.h +17 -26
  210. data/ext/libmongocrypt/libmongocrypt/src/mlib/user-check.h +2 -2
  211. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-binary-private.h +1 -9
  212. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-binary.c +31 -48
  213. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-buffer-private.h +55 -131
  214. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-buffer.c +442 -565
  215. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-cache-collinfo-private.h +1 -2
  216. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-cache-collinfo.c +28 -45
  217. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-cache-key-private.h +11 -17
  218. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-cache-key.c +104 -132
  219. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-cache-oauth-private.h +16 -24
  220. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-cache-oauth.c +110 -93
  221. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-cache-private.h +26 -45
  222. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-cache.c +220 -273
  223. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-ciphertext-private.h +13 -25
  224. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-ciphertext.c +147 -166
  225. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-compat.h +2 -2
  226. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-crypto-private.h +110 -204
  227. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-crypto.c +1145 -1567
  228. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-ctx-datakey.c +510 -476
  229. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-ctx-decrypt.c +817 -694
  230. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-ctx-encrypt.c +2775 -2697
  231. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-ctx-private.h +189 -184
  232. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-ctx-rewrap-many-datakey.c +308 -351
  233. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-ctx.c +990 -1139
  234. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-dll-private.h +23 -24
  235. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-endian-private.h +44 -58
  236. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-endpoint-private.h +21 -25
  237. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-endpoint.c +167 -181
  238. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-kek-private.h +42 -44
  239. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-kek.c +292 -272
  240. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-key-broker-private.h +70 -110
  241. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-key-broker.c +1010 -1033
  242. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-key-private.h +27 -44
  243. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-key.c +349 -402
  244. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-kms-ctx-private.h +127 -120
  245. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-kms-ctx.c +1692 -1611
  246. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-log-private.h +35 -67
  247. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-log.c +49 -83
  248. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-marking-private.h +31 -36
  249. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-marking.c +1923 -1479
  250. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-mutex-private.h +7 -12
  251. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-opts-private.h +124 -107
  252. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-opts.c +925 -419
  253. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-private.h +99 -122
  254. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-status-private.h +3 -8
  255. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-status.c +92 -119
  256. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-traverse-util-private.h +19 -29
  257. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-traverse-util.c +136 -176
  258. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-util-private.h +11 -21
  259. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-util.c +98 -136
  260. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt.c +924 -1339
  261. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt.h +199 -313
  262. data/ext/libmongocrypt/libmongocrypt/src/os_posix/os_dll.c +83 -79
  263. data/ext/libmongocrypt/libmongocrypt/src/os_posix/os_mutex.c +20 -28
  264. data/ext/libmongocrypt/libmongocrypt/src/os_win/os_dll.c +64 -73
  265. data/ext/libmongocrypt/libmongocrypt/src/os_win/os_mutex.c +8 -16
  266. data/ext/libmongocrypt/libmongocrypt/test/crypt_shared-stub.cpp +57 -87
  267. data/ext/libmongocrypt/libmongocrypt/test/data/bulkWrite/bypassQueryAnalysis/payload.json +53 -0
  268. data/ext/libmongocrypt/libmongocrypt/test/data/bulkWrite/jsonSchema/cmd-to-mongocryptd.json +23 -0
  269. data/ext/libmongocrypt/libmongocrypt/test/data/bulkWrite/jsonSchema/cmd.json +20 -0
  270. data/ext/libmongocrypt/libmongocrypt/test/data/bulkWrite/simple/cmd-to-mongocryptd.json +50 -0
  271. data/ext/libmongocrypt/libmongocrypt/test/data/bulkWrite/simple/cmd.json +20 -0
  272. data/ext/libmongocrypt/libmongocrypt/test/data/bulkWrite/simple/collinfo.json +44 -0
  273. data/ext/libmongocrypt/libmongocrypt/test/data/bulkWrite/simple/encrypted-field-map.json +24 -0
  274. data/ext/libmongocrypt/libmongocrypt/test/data/bulkWrite/simple/encrypted-payload-pattern.json +53 -0
  275. data/ext/libmongocrypt/libmongocrypt/test/data/bulkWrite/simple/mongocryptd-reply.json +62 -0
  276. data/ext/libmongocrypt/libmongocrypt/test/data/bulkWrite/unencrypted/cmd-to-mongocryptd.json +30 -0
  277. data/ext/libmongocrypt/libmongocrypt/test/data/bulkWrite/unencrypted/cmd.json +17 -0
  278. data/ext/libmongocrypt/libmongocrypt/test/data/bulkWrite/unencrypted/mongocryptd-reply.json +39 -0
  279. data/ext/libmongocrypt/libmongocrypt/test/data/bulkWrite/unencrypted/payload.json +21 -0
  280. data/ext/libmongocrypt/libmongocrypt/test/data/cleanup/missing-key-id/collinfo.json +20 -0
  281. data/ext/libmongocrypt/libmongocrypt/test/data/cleanup/no-fields/collinfo.json +9 -0
  282. data/ext/libmongocrypt/libmongocrypt/test/data/cleanup/no-fields/encrypted-payload.json +4 -0
  283. data/ext/libmongocrypt/libmongocrypt/test/data/cleanup/success/cmd.json +1 -0
  284. data/ext/libmongocrypt/libmongocrypt/test/data/cleanup/success/collinfo.json +63 -0
  285. data/ext/libmongocrypt/libmongocrypt/test/data/cleanup/success/encrypted-field-config-map.json +61 -0
  286. data/ext/libmongocrypt/libmongocrypt/test/data/cleanup/success/encrypted-payload-range-v2.json +37 -0
  287. data/ext/libmongocrypt/libmongocrypt/test/data/cleanup/success/encrypted-payload.json +29 -0
  288. data/ext/libmongocrypt/libmongocrypt/test/data/compact/anchor-pad/cmd.json +1 -0
  289. data/ext/libmongocrypt/libmongocrypt/test/data/compact/anchor-pad/collinfo.json +64 -0
  290. data/ext/libmongocrypt/libmongocrypt/test/data/compact/anchor-pad/encrypted-payload-range-v2.json +105 -0
  291. data/ext/libmongocrypt/libmongocrypt/test/data/compact/anchor-pad/encrypted-payload.json +30 -0
  292. data/ext/libmongocrypt/libmongocrypt/test/data/compact/no-range/cmd.json +1 -0
  293. data/ext/libmongocrypt/libmongocrypt/test/data/compact/no-range/collinfo.json +49 -0
  294. data/ext/libmongocrypt/libmongocrypt/test/data/compact/no-range/encrypted-field-config-map.json +47 -0
  295. data/ext/libmongocrypt/libmongocrypt/test/data/compact/no-range/encrypted-payload.json +23 -0
  296. data/ext/libmongocrypt/libmongocrypt/test/data/compact/success/collinfo.json +15 -0
  297. data/ext/libmongocrypt/libmongocrypt/test/data/compact/success/encrypted-field-config-map.json +10 -0
  298. data/ext/libmongocrypt/libmongocrypt/test/data/compact/success/encrypted-payload-range-v2.json +104 -0
  299. data/ext/libmongocrypt/libmongocrypt/test/data/compact/success/encrypted-payload.json +6 -0
  300. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-delete/empty/encrypted-payload-v2.json +60 -0
  301. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-delete/success/encrypted-payload-v2.json +67 -0
  302. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-explain/with-csfle/collinfo.json +4 -4
  303. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-explain/with-csfle/encrypted-payload.json +5 -5
  304. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-explicit/find-indexed-contentionFactor1-v2.json +8 -0
  305. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-explicit/find-indexed-v2.json +8 -0
  306. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-explicit/insert-indexed-contentionFactor1-v2.json +8 -0
  307. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-explicit/insert-indexed-same-user-and-index-key-v2.json +8 -0
  308. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-explicit/insert-indexed-v2.json +8 -0
  309. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-equality-v2/cmd.json +6 -0
  310. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-equality-v2/encrypted-field-map.json +22 -0
  311. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-equality-v2/encrypted-payload.json +40 -0
  312. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-equality-v2/mongocryptd-reply.json +19 -0
  313. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/date-v2/cmd.json +10 -0
  314. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/date-v2/encrypted-field-map.json +27 -0
  315. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/date-v2/encrypted-payload.json +41 -0
  316. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/date-v2/mongocryptd-reply.json +49 -0
  317. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/decimal128-precision-v2/cmd.json +6 -0
  318. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/decimal128-precision-v2/encrypted-field-map.json +30 -0
  319. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/decimal128-precision-v2/encrypted-payload.json +50 -0
  320. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/decimal128-precision-v2/mongocryptd-reply.json +58 -0
  321. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/decimal128-v2/cmd.json +6 -0
  322. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/decimal128-v2/encrypted-field-map.json +27 -0
  323. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/decimal128-v2/encrypted-payload.json +41 -0
  324. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/decimal128-v2/mongocryptd-reply.json +49 -0
  325. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/double-precision-v2/cmd.json +8 -0
  326. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/double-precision-v2/encrypted-field-map.json +30 -0
  327. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/double-precision-v2/encrypted-payload.json +44 -0
  328. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/double-precision-v2/mongocryptd-reply.json +52 -0
  329. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/double-v2/cmd.json +8 -0
  330. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/double-v2/encrypted-field-map.json +27 -0
  331. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/double-v2/encrypted-payload.json +41 -0
  332. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/double-v2/mongocryptd-reply.json +49 -0
  333. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/int32-v2/cmd.json +8 -0
  334. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/int32-v2/encrypted-field-map.json +27 -0
  335. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/int32-v2/encrypted-payload.json +41 -0
  336. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/int32-v2/mongocryptd-reply.json +49 -0
  337. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/int64-v2/cmd.json +8 -0
  338. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/int64-v2/encrypted-field-map.json +27 -0
  339. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/int64-v2/encrypted-payload.json +41 -0
  340. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/int64-v2/mongocryptd-reply.json +49 -0
  341. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range-explicit/double/encrypted-payload-v2.json +26 -0
  342. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range-explicit/double-precision/encrypted-payload-v2.json +26 -0
  343. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range-explicit/int32/encrypted-payload-v2.json +26 -0
  344. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range-explicit/int32-nominmax/encrypted-payload-v2.json +26 -0
  345. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range-explicit/int32-openinterval/encrypted-payload-v2.json +16 -0
  346. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert/cmd.json +1 -1
  347. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/date/RNG_DATA.h +65 -65
  348. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/date-v2/cmd.json +13 -0
  349. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/date-v2/encrypted-field-map.json +27 -0
  350. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/date-v2/encrypted-payload.json +44 -0
  351. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/date-v2/mongocryptd-reply.json +52 -0
  352. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/decimal128/RNG_DATA.h +132 -132
  353. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/decimal128-precision/RNG_DATA.h +71 -71
  354. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/decimal128-precision-v2/cmd.json +9 -0
  355. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/decimal128-precision-v2/encrypted-field-map.json +30 -0
  356. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/decimal128-precision-v2/encrypted-payload.json +53 -0
  357. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/decimal128-precision-v2/mongocryptd-reply.json +61 -0
  358. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/decimal128-v2/cmd.json +9 -0
  359. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/decimal128-v2/encrypted-field-map.json +27 -0
  360. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/decimal128-v2/encrypted-payload.json +44 -0
  361. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/decimal128-v2/mongocryptd-reply.json +52 -0
  362. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/double/RNG_DATA.h +68 -68
  363. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/double-precision/RNG_DATA.h +19 -19
  364. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/double-precision-v2/cmd.json +11 -0
  365. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/double-precision-v2/encrypted-field-map.json +30 -0
  366. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/double-precision-v2/encrypted-payload.json +47 -0
  367. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/double-precision-v2/mongocryptd-reply.json +55 -0
  368. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/double-v2/cmd.json +11 -0
  369. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/double-v2/encrypted-field-map.json +27 -0
  370. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/double-v2/encrypted-payload.json +44 -0
  371. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/double-v2/mongocryptd-reply.json +52 -0
  372. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/int32/RNG_DATA.h +25 -25
  373. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/int32-v2/cmd.json +11 -0
  374. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/int32-v2/encrypted-field-map.json +27 -0
  375. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/int32-v2/encrypted-payload.json +44 -0
  376. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/int32-v2/mongocryptd-reply.json +52 -0
  377. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/int64/RNG_DATA.h +65 -65
  378. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/int64-v2/cmd.json +11 -0
  379. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/int64-v2/encrypted-field-map.json +27 -0
  380. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/int64-v2/encrypted-payload.json +44 -0
  381. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/int64-v2/mongocryptd-reply.json +52 -0
  382. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/double/RNG_DATA.h +68 -68
  383. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/double/encrypted-payload-v2.json +8 -0
  384. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/double-precision/RNG_DATA.h +19 -19
  385. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/double-precision/encrypted-payload-v2.json +8 -0
  386. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/int32/RNG_DATA.h +25 -25
  387. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/int32/encrypted-payload-v2.json +8 -0
  388. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/int32-nominmax/encrypted-payload-v2.json +8 -0
  389. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/sparsity-2/RNG_DATA.h +15 -15
  390. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/sparsity-2/encrypted-payload-v2.json +8 -0
  391. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-unindexed-v2/cmd.json +9 -0
  392. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-unindexed-v2/encrypted-field-map.json +18 -0
  393. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-unindexed-v2/encrypted-payload.json +14 -0
  394. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-unindexed-v2/mongocryptd-reply.json +41 -0
  395. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-v2/cmd.json +9 -0
  396. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-v2/encrypted-field-map.json +22 -0
  397. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-v2/encrypted-payload.json +39 -0
  398. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-v2/mongocryptd-reply.json +49 -0
  399. data/ext/libmongocrypt/libmongocrypt/test/data/iev-v2/FLECrudTest-insertOneRangeV2.json +10 -0
  400. data/ext/libmongocrypt/libmongocrypt/test/data/iev-v2/FLECrudTest-insertOneV2.json +10 -0
  401. data/ext/libmongocrypt/libmongocrypt/test/data/kms-azure/decrypt-response.txt +16 -0
  402. data/ext/libmongocrypt/libmongocrypt/test/data/kms-azure/encrypt-response.txt +16 -0
  403. data/ext/libmongocrypt/libmongocrypt/test/data/kms-azure/oauth-response.txt +19 -0
  404. data/ext/libmongocrypt/libmongocrypt/test/data/no-trimFactor/find/cmd.json +9 -0
  405. data/ext/libmongocrypt/libmongocrypt/test/data/no-trimFactor/find/encrypted-field-map.json +19 -0
  406. data/ext/libmongocrypt/libmongocrypt/test/data/no-trimFactor/find/encrypted-payload.json +62 -0
  407. data/ext/libmongocrypt/libmongocrypt/test/data/no-trimFactor/find/mongocryptd-reply.json +69 -0
  408. data/ext/libmongocrypt/libmongocrypt/test/data/no-trimFactor/insert/cmd.json +11 -0
  409. data/ext/libmongocrypt/libmongocrypt/test/data/no-trimFactor/insert/encrypted-field-map.json +19 -0
  410. data/ext/libmongocrypt/libmongocrypt/test/data/no-trimFactor/insert/encrypted-payload.json +40 -0
  411. data/ext/libmongocrypt/libmongocrypt/test/data/no-trimFactor/insert/mongocryptd-reply.json +47 -0
  412. data/ext/libmongocrypt/libmongocrypt/test/data/range-edge-generation/edges_decimal128.cstruct +1 -1
  413. data/ext/libmongocrypt/libmongocrypt/test/data/range-edge-generation/edges_double.cstruct +8637 -7958
  414. data/ext/libmongocrypt/libmongocrypt/test/data/range-edge-generation/edges_int32.cstruct +5522 -1382
  415. data/ext/libmongocrypt/libmongocrypt/test/data/range-edge-generation/edges_int64.cstruct +5042 -1262
  416. data/ext/libmongocrypt/libmongocrypt/test/data/range-min-cover/mincover_decimal128.cstruct +1 -1
  417. data/ext/libmongocrypt/libmongocrypt/test/data/range-min-cover/mincover_decimal128_precision.cstruct +1 -1
  418. data/ext/libmongocrypt/libmongocrypt/test/data/range-min-cover/mincover_double.cstruct +1 -1
  419. data/ext/libmongocrypt/libmongocrypt/test/data/range-min-cover/mincover_double_precision.cstruct +2 -2
  420. data/ext/libmongocrypt/libmongocrypt/test/data/range-min-cover/mincover_int32.cstruct +1 -1
  421. data/ext/libmongocrypt/libmongocrypt/test/data/range-min-cover/mincover_int64.cstruct +1 -1
  422. data/ext/libmongocrypt/libmongocrypt/test/data/range-sends-cryptoParams/auto-find-int32/cmd.json +8 -0
  423. data/ext/libmongocrypt/libmongocrypt/test/data/range-sends-cryptoParams/auto-find-int32/encrypted-field-map.json +19 -0
  424. data/ext/libmongocrypt/libmongocrypt/test/data/range-sends-cryptoParams/auto-find-int32/encrypted-payload.json +53 -0
  425. data/ext/libmongocrypt/libmongocrypt/test/data/range-sends-cryptoParams/auto-find-int32/mongocryptd-reply.json +58 -0
  426. data/ext/libmongocrypt/libmongocrypt/test/data/range-sends-cryptoParams/auto-insert-int32/cmd.json +11 -0
  427. data/ext/libmongocrypt/libmongocrypt/test/data/range-sends-cryptoParams/auto-insert-int32/encrypted-field-map.json +19 -0
  428. data/ext/libmongocrypt/libmongocrypt/test/data/range-sends-cryptoParams/auto-insert-int32/encrypted-payload.json +40 -0
  429. data/ext/libmongocrypt/libmongocrypt/test/data/range-sends-cryptoParams/auto-insert-int32/mongocryptd-reply.json +45 -0
  430. data/ext/libmongocrypt/libmongocrypt/test/data/range-sends-cryptoParams/explicit-find-int32/expected.json +26 -0
  431. data/ext/libmongocrypt/libmongocrypt/test/data/range-sends-cryptoParams/explicit-find-int32/to-encrypt.json +20 -0
  432. data/ext/libmongocrypt/libmongocrypt/test/data/range-sends-cryptoParams/explicit-find-int32-defaults/expected.json +26 -0
  433. data/ext/libmongocrypt/libmongocrypt/test/data/range-sends-cryptoParams/explicit-find-int32-defaults/to-encrypt.json +20 -0
  434. data/ext/libmongocrypt/libmongocrypt/test/data/range-sends-cryptoParams/explicit-insert-double/expected.json +8 -0
  435. data/ext/libmongocrypt/libmongocrypt/test/data/range-sends-cryptoParams/explicit-insert-int32/expected.json +8 -0
  436. data/ext/libmongocrypt/libmongocrypt/test/data/range-sends-cryptoParams/explicit-insert-int32-defaults/expected.json +8 -0
  437. data/ext/libmongocrypt/libmongocrypt/test/data/roundtrip/README.md +17 -0
  438. data/ext/libmongocrypt/libmongocrypt/test/data/roundtrip/aes-ctr.json +29 -0
  439. data/ext/libmongocrypt/libmongocrypt/test/data/roundtrip/fle2-fixed.json +10 -0
  440. data/ext/libmongocrypt/libmongocrypt/test/data/roundtrip/fle2-generated.json +38 -0
  441. data/ext/libmongocrypt/libmongocrypt/test/data/roundtrip/fle2aead-decrypt.json +35 -0
  442. data/ext/libmongocrypt/libmongocrypt/test/data/roundtrip/fle2aead-fixed.json +29 -0
  443. data/ext/libmongocrypt/libmongocrypt/test/data/roundtrip/fle2aead-generated.json +122 -0
  444. data/ext/libmongocrypt/libmongocrypt/test/data/roundtrip/fle2v2-aead-fixed.json +29 -0
  445. data/ext/libmongocrypt/libmongocrypt/test/data/roundtrip/fle2v2-aead-generated.json +122 -0
  446. data/ext/libmongocrypt/libmongocrypt/test/data/roundtrip/mcgrew.json +12 -0
  447. data/ext/libmongocrypt/libmongocrypt/test/data/roundtrip/nist.json +20 -0
  448. data/ext/libmongocrypt/libmongocrypt/test/data/tokens/README.md +30 -0
  449. data/ext/libmongocrypt/libmongocrypt/test/data/tokens/mc.json +25 -0
  450. data/ext/libmongocrypt/libmongocrypt/test/data/tokens/server.json +25 -0
  451. data/ext/libmongocrypt/libmongocrypt/test/example-no-bson.c +4 -4
  452. data/ext/libmongocrypt/libmongocrypt/test/example-state-machine.c +279 -323
  453. data/ext/libmongocrypt/libmongocrypt/test/fuzz_kms.c +8 -7
  454. data/ext/libmongocrypt/libmongocrypt/test/test-dll.cpp +6 -7
  455. data/ext/libmongocrypt/libmongocrypt/test/test-gcp-auth.c +221 -283
  456. data/ext/libmongocrypt/libmongocrypt/test/test-mc-efc.c +60 -73
  457. data/ext/libmongocrypt/libmongocrypt/test/test-mc-fle2-find-equality-payload-v2.c +78 -0
  458. data/ext/libmongocrypt/libmongocrypt/test/test-mc-fle2-find-range-payload-v2.c +129 -0
  459. data/ext/libmongocrypt/libmongocrypt/test/test-mc-fle2-payload-iev-v2.c +248 -0
  460. data/ext/libmongocrypt/libmongocrypt/test/test-mc-fle2-payload-iev.c +467 -414
  461. data/ext/libmongocrypt/libmongocrypt/test/test-mc-fle2-payload-iup-v2.c +248 -0
  462. data/ext/libmongocrypt/libmongocrypt/test/test-mc-fle2-payload-iup.c +141 -159
  463. data/ext/libmongocrypt/libmongocrypt/test/test-mc-fle2-payload-uev-v2.c +338 -0
  464. data/ext/libmongocrypt/libmongocrypt/test/test-mc-fle2-payload-uev.c +238 -176
  465. data/ext/libmongocrypt/libmongocrypt/test/test-mc-fle2-rfds.c +373 -474
  466. data/ext/libmongocrypt/libmongocrypt/test/test-mc-range-edge-generation.c +421 -388
  467. data/ext/libmongocrypt/libmongocrypt/test/test-mc-range-encoding.c +974 -825
  468. data/ext/libmongocrypt/libmongocrypt/test/test-mc-range-mincover.c +499 -481
  469. data/ext/libmongocrypt/libmongocrypt/test/test-mc-rangeopts.c +312 -148
  470. data/ext/libmongocrypt/libmongocrypt/test/test-mc-reader.c +124 -207
  471. data/ext/libmongocrypt/libmongocrypt/test/test-mc-tokens.c +249 -213
  472. data/ext/libmongocrypt/libmongocrypt/test/test-mc-writer.c +176 -0
  473. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-assert-match-bson.c +632 -808
  474. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-assert-match-bson.h +22 -2
  475. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-assert.h +168 -189
  476. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-buffer.c +187 -211
  477. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-cache-oauth.c +118 -37
  478. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-cache.c +210 -233
  479. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-ciphertext.c +185 -220
  480. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-cleanup.c +374 -0
  481. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-compact.c +410 -445
  482. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-crypto-hooks.c +798 -812
  483. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-crypto-std-hooks.c +142 -135
  484. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-crypto-std-hooks.h +70 -71
  485. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-crypto.c +394 -846
  486. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-csfle-lib.c +159 -183
  487. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-ctx-decrypt.c +870 -1045
  488. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-ctx-encrypt.c +5183 -4717
  489. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-ctx-rewrap-many-datakey.c +750 -964
  490. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-ctx-setopt.c +1093 -1178
  491. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-datakey.c +348 -420
  492. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-dll.c +23 -30
  493. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-endpoint.c +98 -111
  494. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-kek.c +49 -52
  495. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-key-broker.c +770 -920
  496. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-key-cache.c +354 -407
  497. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-key.c +197 -245
  498. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-kms-ctx.c +705 -368
  499. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-kms-responses.c +147 -166
  500. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-local-kms.c +50 -61
  501. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-log.c +85 -100
  502. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-marking.c +1073 -690
  503. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-opts.c +42 -0
  504. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-status.c +46 -58
  505. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-traverse-util.c +381 -451
  506. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-util.c +55 -67
  507. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-util.h +7 -10
  508. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt.c +879 -918
  509. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt.h +119 -169
  510. data/ext/libmongocrypt/libmongocrypt/test/test-named-kms-providers.c +2381 -0
  511. data/ext/libmongocrypt/libmongocrypt/test/util/HELP.autogen +3 -1
  512. data/ext/libmongocrypt/libmongocrypt/test/util/README.md +1 -0
  513. data/ext/libmongocrypt/libmongocrypt/test/util/csfle.c +512 -559
  514. data/ext/libmongocrypt/libmongocrypt/test/util/make_includes.py +1 -1
  515. data/ext/libmongocrypt/libmongocrypt/test/util/util.c +771 -882
  516. data/ext/libmongocrypt/libmongocrypt/test/util/util.h +33 -55
  517. data/lib/libmongocrypt_helper/version.rb +2 -2
  518. metadata +245 -111
  519. checksums.yaml.gz.sig +0 -0
  520. data/ext/libmongocrypt/libmongocrypt/VERSION_CURRENT +0 -1
  521. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/src/test/resources/fle2-find-range-explicit/int32/encrypted-payload.json +0 -26
  522. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/src/test/resources/fle2-find-range-explicit/int32/key-document.json +0 -0
  523. data/ext/libmongocrypt/libmongocrypt/bindings/node/CHANGELOG.md +0 -81
  524. data/ext/libmongocrypt/libmongocrypt/bindings/node/LICENSE +0 -201
  525. data/ext/libmongocrypt/libmongocrypt/bindings/node/binding.gyp +0 -79
  526. data/ext/libmongocrypt/libmongocrypt/bindings/node/etc/README.hbs +0 -44
  527. data/ext/libmongocrypt/libmongocrypt/bindings/node/etc/build-static.sh +0 -39
  528. data/ext/libmongocrypt/libmongocrypt/bindings/node/index.d.ts +0 -515
  529. data/ext/libmongocrypt/libmongocrypt/bindings/node/lib/autoEncrypter.js +0 -420
  530. data/ext/libmongocrypt/libmongocrypt/bindings/node/lib/buffer_pool.js +0 -123
  531. data/ext/libmongocrypt/libmongocrypt/bindings/node/lib/clientEncryption.js +0 -699
  532. data/ext/libmongocrypt/libmongocrypt/bindings/node/lib/common.js +0 -100
  533. data/ext/libmongocrypt/libmongocrypt/bindings/node/lib/credentialsProvider.js +0 -33
  534. data/ext/libmongocrypt/libmongocrypt/bindings/node/lib/cryptoCallbacks.js +0 -87
  535. data/ext/libmongocrypt/libmongocrypt/bindings/node/lib/index.js +0 -42
  536. data/ext/libmongocrypt/libmongocrypt/bindings/node/lib/mongocryptdManager.js +0 -66
  537. data/ext/libmongocrypt/libmongocrypt/bindings/node/lib/stateMachine.js +0 -487
  538. data/ext/libmongocrypt/libmongocrypt/bindings/node/package-lock.json +0 -16051
  539. data/ext/libmongocrypt/libmongocrypt/bindings/node/package.json +0 -81
  540. data/ext/libmongocrypt/libmongocrypt/bindings/node/src/mongocrypt.cc +0 -929
  541. data/ext/libmongocrypt/libmongocrypt/bindings/node/src/mongocrypt.h +0 -114
  542. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/autoEncrypter.test.js +0 -953
  543. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/buffer_pool.test.js +0 -91
  544. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/clientEncryption.test.js +0 -701
  545. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/credentialsProvider.test.js +0 -163
  546. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/cryptoCallbacks.test.js +0 -284
  547. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/data/README.md +0 -5
  548. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/data/cmd.json +0 -6
  549. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/data/collection-info.json +0 -37
  550. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/data/encrypted-document-nested.json +0 -8
  551. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/data/encrypted-document.json +0 -11
  552. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/data/encryptedFields.json +0 -30
  553. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/data/key-document.json +0 -32
  554. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/data/key1-document.json +0 -30
  555. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/data/mongocryptd-reply.json +0 -18
  556. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/mongocryptdManager.test.js +0 -48
  557. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/release.test.js +0 -59
  558. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/requirements.helper.js +0 -29
  559. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/stateMachine.test.js +0 -331
  560. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/tools/mongodb_reporter.js +0 -325
  561. data/ext/libmongocrypt/libmongocrypt/bindings/python/setup.py +0 -84
  562. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/fle2-find-range-explicit/int32/encrypted-payload.json +0 -26
  563. data/ext/libmongocrypt/libmongocrypt/bindings/python/test-requirements.txt +0 -12
  564. data/ext/libmongocrypt/libmongocrypt/debian/build_snapshot.sh +0 -79
  565. data/ext/libmongocrypt/libmongocrypt/debian/changelog +0 -105
  566. data/ext/libmongocrypt/libmongocrypt/debian/compat +0 -1
  567. data/ext/libmongocrypt/libmongocrypt/debian/control +0 -41
  568. data/ext/libmongocrypt/libmongocrypt/debian/copyright +0 -129
  569. data/ext/libmongocrypt/libmongocrypt/debian/gbp.conf +0 -23
  570. data/ext/libmongocrypt/libmongocrypt/debian/libmongocrypt-dev.dirs +0 -2
  571. data/ext/libmongocrypt/libmongocrypt/debian/libmongocrypt-dev.install +0 -5
  572. data/ext/libmongocrypt/libmongocrypt/debian/libmongocrypt0.dirs +0 -1
  573. data/ext/libmongocrypt/libmongocrypt/debian/libmongocrypt0.install +0 -1
  574. data/ext/libmongocrypt/libmongocrypt/debian/rules +0 -46
  575. data/ext/libmongocrypt/libmongocrypt/debian/source/format +0 -1
  576. data/ext/libmongocrypt/libmongocrypt/debian/source/lintian-overrides +0 -3
  577. data/ext/libmongocrypt/libmongocrypt/debian/source/options +0 -1
  578. data/ext/libmongocrypt/libmongocrypt/debian/watch +0 -3
  579. data/ext/libmongocrypt/libmongocrypt/test/data/aes-ctr.cstructs +0 -359
  580. data/ext/libmongocrypt/libmongocrypt/test/data/compact/success/mongocryptd-reply.json +0 -72
  581. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-aead.cstructs +0 -109
  582. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range-explicit/int32-nominmax/encrypted-payload.json +0 -26
  583. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range-explicit/int32-nominmax/rangeopts.json +0 -5
  584. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/int32-nominmax/RNG_DATA.h +0 -70
  585. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/int32-nominmax/encrypted-payload.json +0 -8
  586. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/int32-nominmax/rangeopts.json +0 -5
  587. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/int32-nominmax/value-to-encrypt.json +0 -5
  588. data/ext/libmongocrypt/libmongocrypt/test/data/fle2.cstructs +0 -33
  589. data.tar.gz.sig +0 -0
  590. metadata.gz.sig +0 -0
  591. /data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/src/test/resources/{fle2-find-range-explicit → fle2-find-range-explicit-v2}/int32/key-filter.json +0 -0
  592. /data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/src/test/resources/{fle2-find-range-explicit → fle2-find-range-explicit-v2}/int32/value-to-encrypt.json +0 -0
  593. /data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/{fle2-find-range-explicit → fle2-find-range-explicit-v2}/int32/value-to-encrypt.json +0 -0
  594. /data/ext/libmongocrypt/libmongocrypt/bindings/{java/mongocrypt/src/test/resources/fle2-find-range-explicit → python/test/data/fle2-find-rangePreview-explicit}/int32/rangeopts.json +0 -0
  595. /data/ext/libmongocrypt/libmongocrypt/{test/data/fle2-find-range-explicit/int32-nominmax → bindings/python/test/data/fle2-find-rangePreview-explicit/int32}/value-to-encrypt.json +0 -0
  596. /data/ext/libmongocrypt/libmongocrypt/{bindings/node/test/data/kms-decrypt-reply.txt → test/data/kms-aws/decrypt-response.txt} +0 -0
  597. /data/ext/libmongocrypt/libmongocrypt/{bindings/node/test/data/kms-encrypt-reply.txt → test/data/kms-aws/encrypt-response.txt} +0 -0
  598. /data/ext/libmongocrypt/libmongocrypt/test/data/{gcp-auth → kms-gcp}/decrypt-response.txt +0 -0
  599. /data/ext/libmongocrypt/libmongocrypt/test/data/{gcp-auth → kms-gcp}/encrypt-response.txt +0 -0
  600. /data/ext/libmongocrypt/libmongocrypt/test/data/{gcp-auth → kms-gcp}/oauth-response.txt +0 -0
@@ -31,287 +31,393 @@
31
31
  * Encode a signed 32-bit integer as an unsigned 32-bit integer by adding 2^31.
32
32
  * Some documentation references this as making the value "unbiased".
33
33
  */
34
- static uint32_t
35
- encodeInt32 (int32_t v)
36
- {
37
- // Shift the int32_t range [-2^31, 2^31 - 1] to the uint32_t range [0, 2^32].
38
- // new_zero is the mapped 0 value.
39
- uint32_t new_zero = (UINT32_C (1) << 31);
40
-
41
- if (v < 0) {
42
- // Signed integers have a value that there is no positive equivalent and
43
- // must be handled specially
44
- if (v == INT32_MIN) {
45
- return 0;
46
- }
47
-
48
- int32_t v_pos = v * -1;
49
- uint32_t v_u32 = (uint32_t) v_pos;
50
- return new_zero - v_u32;
51
- }
52
-
53
- uint32_t v_u32 = (uint32_t) v;
54
- return new_zero + v_u32;
34
+ static uint32_t encodeInt32(int32_t v) {
35
+ // Shift the int32_t range [-2^31, 2^31 - 1] to the uint32_t range [0, 2^32].
36
+ // new_zero is the mapped 0 value.
37
+ uint32_t new_zero = (UINT32_C(1) << 31);
38
+
39
+ if (v < 0) {
40
+ // Signed integers have a value that there is no positive equivalent and
41
+ // must be handled specially
42
+ if (v == INT32_MIN) {
43
+ return 0;
44
+ }
45
+
46
+ int32_t v_pos = v * -1;
47
+ uint32_t v_u32 = (uint32_t)v_pos;
48
+ return new_zero - v_u32;
49
+ }
50
+
51
+ uint32_t v_u32 = (uint32_t)v;
52
+ return new_zero + v_u32;
55
53
  }
56
54
 
57
- bool
58
- mc_getTypeInfo32 (mc_getTypeInfo32_args_t args,
59
- mc_OSTType_Int32 *out,
60
- mongocrypt_status_t *status)
61
- {
62
- if (args.min.set != args.max.set) {
63
- CLIENT_ERR ("Must specify both a lower and upper bound or no bounds.");
64
- return false;
65
- }
66
-
67
- if (!args.min.set) {
68
- uint32_t v_u32 = encodeInt32 (args.value);
69
- *out = (mc_OSTType_Int32){v_u32, 0, UINT32_MAX};
70
- return true;
71
- }
72
-
73
- if (args.min.value >= args.max.value) {
74
- CLIENT_ERR ("The minimum value must be less than the maximum value, got "
75
- "min: %" PRId32 ", max: %" PRId32,
76
- args.min.value,
77
- args.max.value);
78
- return false;
79
- }
80
-
81
- if (args.value > args.max.value || args.value < args.min.value) {
82
- CLIENT_ERR (
83
- "Value must be greater than or equal to the minimum value "
84
- "and less than or equal to the maximum value, got min: %" PRId32
85
- ", max: %" PRId32 ", value: %" PRId32,
86
- args.min.value,
87
- args.max.value,
88
- args.value);
89
- return false;
90
- }
91
-
92
- // Convert to unbiased uint32. Then subtract the min value.
93
- uint32_t v_u32 = encodeInt32 (args.value);
94
- uint32_t min_u32 = encodeInt32 (args.min.value);
95
- uint32_t max_u32 = encodeInt32 (args.max.value);
96
-
97
- v_u32 -= min_u32;
98
- max_u32 -= min_u32;
99
-
100
- *out = (mc_OSTType_Int32){v_u32, 0, max_u32};
101
- return true;
55
+ bool mc_getTypeInfo32(mc_getTypeInfo32_args_t args, mc_OSTType_Int32 *out, mongocrypt_status_t *status) {
56
+ if (args.min.set != args.max.set) {
57
+ CLIENT_ERR("Must specify both a lower and upper bound or no bounds.");
58
+ return false;
59
+ }
60
+
61
+ if (!args.min.set) {
62
+ uint32_t v_u32 = encodeInt32(args.value);
63
+ *out = (mc_OSTType_Int32){v_u32, 0, UINT32_MAX};
64
+ return true;
65
+ }
66
+
67
+ if (args.min.value >= args.max.value) {
68
+ CLIENT_ERR("The minimum value must be less than the maximum value, got "
69
+ "min: %" PRId32 ", max: %" PRId32,
70
+ args.min.value,
71
+ args.max.value);
72
+ return false;
73
+ }
74
+
75
+ if (args.value > args.max.value || args.value < args.min.value) {
76
+ CLIENT_ERR("Value must be greater than or equal to the minimum value "
77
+ "and less than or equal to the maximum value, got min: %" PRId32 ", max: %" PRId32
78
+ ", value: %" PRId32,
79
+ args.min.value,
80
+ args.max.value,
81
+ args.value);
82
+ return false;
83
+ }
84
+
85
+ // Convert to unbiased uint32. Then subtract the min value.
86
+ uint32_t v_u32 = encodeInt32(args.value);
87
+ uint32_t min_u32 = encodeInt32(args.min.value);
88
+ uint32_t max_u32 = encodeInt32(args.max.value);
89
+
90
+ v_u32 -= min_u32;
91
+ max_u32 -= min_u32;
92
+
93
+ *out = (mc_OSTType_Int32){v_u32, 0, max_u32};
94
+ return true;
102
95
  }
103
96
 
104
97
  /**
105
98
  * Encode a signed 64-bit integer as an unsigned 64-bit integer by adding 2^63.
106
99
  * Some documentation references this as making the value "unbiased".
107
100
  */
108
- static uint64_t
109
- encodeInt64 (int64_t v)
110
- {
111
- // Shift the int64_t range [-2^63, 2^63 - 1] to the uint64_t range [0, 2^64].
112
- // new_zero is the mapped 0 value.
113
- uint64_t new_zero = (UINT64_C (1) << 63);
114
-
115
- if (v < 0) {
116
- // Signed integers have a value that there is no positive equivalent and
117
- // must be handled specially
118
- if (v == INT64_MIN) {
119
- return 0;
120
- }
121
-
122
- int64_t v_pos = v * -1;
123
- uint64_t v_u64 = (uint64_t) v_pos;
124
- return new_zero - v_u64;
125
- }
126
-
127
- uint64_t v_u64 = (uint64_t) v;
128
- return new_zero + v_u64;
101
+ static uint64_t encodeInt64(int64_t v) {
102
+ // Shift the int64_t range [-2^63, 2^63 - 1] to the uint64_t range [0, 2^64].
103
+ // new_zero is the mapped 0 value.
104
+ uint64_t new_zero = (UINT64_C(1) << 63);
105
+
106
+ if (v < 0) {
107
+ // Signed integers have a value that there is no positive equivalent and
108
+ // must be handled specially
109
+ if (v == INT64_MIN) {
110
+ return 0;
111
+ }
112
+
113
+ int64_t v_pos = v * -1;
114
+ uint64_t v_u64 = (uint64_t)v_pos;
115
+ return new_zero - v_u64;
116
+ }
117
+
118
+ uint64_t v_u64 = (uint64_t)v;
119
+ return new_zero + v_u64;
129
120
  }
130
121
 
131
- bool
132
- mc_getTypeInfo64 (mc_getTypeInfo64_args_t args,
133
- mc_OSTType_Int64 *out,
134
- mongocrypt_status_t *status)
135
- {
136
- if (args.min.set != args.max.set) {
137
- CLIENT_ERR ("Must specify both a lower and upper bound or no bounds.");
138
- return false;
139
- }
140
-
141
- if (!args.min.set) {
142
- uint64_t v_u64 = encodeInt64 (args.value);
143
- *out = (mc_OSTType_Int64){v_u64, 0, UINT64_MAX};
144
- return true;
145
- }
146
-
147
- if (args.min.value >= args.max.value) {
148
- CLIENT_ERR ("The minimum value must be less than the maximum value, got "
149
- "min: %" PRId64 ", max: %" PRId64,
150
- args.min.value,
151
- args.max.value);
152
- return false;
153
- }
154
-
155
- if (args.value > args.max.value || args.value < args.min.value) {
156
- CLIENT_ERR ("Value must be greater than or equal to the minimum value "
157
- "and less than or equal to the maximum value, got "
158
- "min: %" PRId64 ", max: %" PRId64 ", value: %" PRId64,
159
- args.min.value,
160
- args.max.value,
161
- args.value);
162
- return false;
163
- }
164
-
165
- // Convert to unbiased uint64. Then subtract the min value.
166
- uint64_t v_u64 = encodeInt64 (args.value);
167
- uint64_t min_u64 = encodeInt64 (args.min.value);
168
- uint64_t max_u64 = encodeInt64 (args.max.value);
169
-
170
- v_u64 -= min_u64;
171
- max_u64 -= min_u64;
172
-
173
- *out = (mc_OSTType_Int64){v_u64, 0, max_u64};
174
- return true;
122
+ bool mc_getTypeInfo64(mc_getTypeInfo64_args_t args, mc_OSTType_Int64 *out, mongocrypt_status_t *status) {
123
+ if (args.min.set != args.max.set) {
124
+ CLIENT_ERR("Must specify both a lower and upper bound or no bounds.");
125
+ return false;
126
+ }
127
+
128
+ if (!args.min.set) {
129
+ uint64_t v_u64 = encodeInt64(args.value);
130
+ *out = (mc_OSTType_Int64){v_u64, 0, UINT64_MAX};
131
+ return true;
132
+ }
133
+
134
+ if (args.min.value >= args.max.value) {
135
+ CLIENT_ERR("The minimum value must be less than the maximum value, got "
136
+ "min: %" PRId64 ", max: %" PRId64,
137
+ args.min.value,
138
+ args.max.value);
139
+ return false;
140
+ }
141
+
142
+ if (args.value > args.max.value || args.value < args.min.value) {
143
+ CLIENT_ERR("Value must be greater than or equal to the minimum value "
144
+ "and less than or equal to the maximum value, got "
145
+ "min: %" PRId64 ", max: %" PRId64 ", value: %" PRId64,
146
+ args.min.value,
147
+ args.max.value,
148
+ args.value);
149
+ return false;
150
+ }
151
+
152
+ // Convert to unbiased uint64. Then subtract the min value.
153
+ uint64_t v_u64 = encodeInt64(args.value);
154
+ uint64_t min_u64 = encodeInt64(args.min.value);
155
+ uint64_t max_u64 = encodeInt64(args.max.value);
156
+
157
+ v_u64 -= min_u64;
158
+ max_u64 -= min_u64;
159
+
160
+ *out = (mc_OSTType_Int64){v_u64, 0, max_u64};
161
+ return true;
175
162
  }
176
163
 
177
- #define exp10Double(x) pow (10, x)
178
-
179
- bool
180
- mc_getTypeInfoDouble (mc_getTypeInfoDouble_args_t args,
181
- mc_OSTType_Double *out,
182
- mongocrypt_status_t *status)
183
- {
184
- if (args.min.set != args.max.set || args.min.set != args.precision.set) {
185
- CLIENT_ERR (
186
- "min, max, and precision must all be set or must all be unset");
187
- return false;
188
- }
189
-
190
- if (mc_isinf (args.value) || mc_isnan (args.value)) {
191
- CLIENT_ERR ("Infinity and NaN double values are not supported.");
192
- return false;
193
- }
194
-
195
- if (args.min.set) {
196
- if (args.min.value >= args.max.value) {
197
- CLIENT_ERR (
198
- "The minimum value must be less than the maximum value, got "
199
- "min: %g, max: %g",
200
- args.min.value,
201
- args.max.value);
202
- return false;
203
- }
204
-
205
- if (args.value > args.max.value || args.value < args.min.value) {
206
- CLIENT_ERR ("Value must be greater than or equal to the minimum value "
207
- "and less than or equal to the maximum value, got "
208
- "min: %g, max: %g, value: %g",
209
- args.min.value,
210
- args.max.value,
211
- args.value);
212
- return false;
213
- }
214
- }
215
-
216
- const bool is_neg = args.value < 0.0;
217
-
218
- // Map negative 0 to zero so sign bit is 0.
219
- if (args.value == 0.0) {
220
- args.value = 0.0;
221
- }
222
-
223
- // When we use precision mode, we try to represent as a double value that
224
- // fits in [-2^63, 2^63] (i.e. is a valid int64)
225
- //
226
- // This check determines if we can represent the precision truncated value as
227
- // a 64-bit integer I.e. Is ((ub - lb) * 10^precision) < 64 bits.
228
- //
229
- bool use_precision_mode = false;
230
- uint32_t bits_range;
231
- if (args.precision.set) {
232
- // Subnormal representations can support up to 5x10^-324 as a number
233
- if (args.precision.value > 324) {
234
- CLIENT_ERR (
235
- "Precision must be between 0 and 324 inclusive, got: %" PRIu32,
236
- args.precision.value);
237
- return false;
238
- }
239
-
240
- double range = args.max.value - args.min.value;
241
-
242
- // We can overflow if max = max double and min = min double so make sure
243
- // we have finite number after we do subtraction
244
- // Ignore conversion warnings to fix error with glibc.
245
- if (mc_isfinite (range)) {
246
- // This creates a range which is wider then we permit by our min/max
247
- // bounds check with the +1 but it is as the algorithm is written in
248
- // WRITING-11907.
249
- double rangeAndPrecision =
250
- (range + 1) * exp10Double (args.precision.value);
251
-
252
- if (mc_isfinite (rangeAndPrecision)) {
253
- double bits_range_double = log2 (rangeAndPrecision);
254
- bits_range = (uint32_t) ceil (bits_range_double);
255
-
256
- if (bits_range < 64) {
257
- use_precision_mode = true;
164
+ #define exp10Double(x) pow(10, x)
165
+ #define SCALED_DOUBLE_BOUNDS 9007199254740992.0 // 2^53
166
+
167
+ uint64_t subtract_int64_t(int64_t max, int64_t min) {
168
+ BSON_ASSERT(max > min);
169
+ // If the values have the same sign, then simple subtraction
170
+ // will work because we know max > min.
171
+ if ((max > 0 && min > 0) || (max < 0 && min < 0)) {
172
+ return (uint64_t)(max - min);
173
+ }
174
+
175
+ // If they are opposite signs, then we can just invert
176
+ // min to be positive and return the sum.
177
+ uint64_t u_return = (uint64_t)max;
178
+ u_return += (uint64_t)(~min + 1);
179
+ return u_return;
180
+ }
181
+
182
+ bool ceil_log2_double(uint64_t i, uint32_t *maxBitsOut, mongocrypt_status_t *status) {
183
+ if (i == 0) {
184
+ CLIENT_ERR("Invalid input to ceil_log2_double function. Input cannot be 0.");
185
+ return false;
186
+ }
187
+
188
+ uint32_t clz = (uint32_t)_mlibCountLeadingZeros_u64(i);
189
+ uint32_t bits;
190
+ if ((i & (i - 1)) == 0) {
191
+ bits = 64 - clz - 1;
192
+ } else {
193
+ bits = 64 - clz;
194
+ }
195
+ *maxBitsOut = bits;
196
+ return true;
197
+ }
198
+
199
+ bool mc_canUsePrecisionModeDouble(double min,
200
+ double max,
201
+ int32_t precision,
202
+ uint32_t *maxBitsOut,
203
+ mongocrypt_status_t *status) {
204
+ BSON_ASSERT_PARAM(maxBitsOut);
205
+ BSON_ASSERT(precision >= 0);
206
+
207
+ if (min >= max) {
208
+ CLIENT_ERR("Invalid bounds for double range precision, min must be less than max. min: %g, max: %g", min, max);
209
+ return false;
210
+ }
211
+
212
+ const double scaled_prc = exp10Double(precision);
213
+
214
+ const double scaled_max = max * scaled_prc;
215
+ const double scaled_min = min * scaled_prc;
216
+
217
+ if (scaled_max != trunc(scaled_max)) {
218
+ CLIENT_ERR("Invalid upper bound for double precision. Fractional digits must be less than the specified "
219
+ "precision value. max: %g",
220
+ max);
221
+ return false;
222
+ }
223
+
224
+ if (scaled_min != trunc(scaled_min)) {
225
+ CLIENT_ERR("Invalid lower bound for double precision. Fractional digits must be less than the specified "
226
+ "precision value. min: %g",
227
+ min);
228
+ return false;
229
+ }
230
+
231
+ if (fabs(scaled_max) >= SCALED_DOUBLE_BOUNDS) {
232
+ CLIENT_ERR(
233
+ "Invalid upper bound for double precision. Absolute scaled value of max must be less than %g. max: %g",
234
+ SCALED_DOUBLE_BOUNDS,
235
+ max);
236
+ return false;
237
+ }
238
+
239
+ if (fabs(scaled_min) >= SCALED_DOUBLE_BOUNDS) {
240
+ CLIENT_ERR(
241
+ "Invalid lower bound for double precision. Absolute scaled value of min must be less than %g. min: %g",
242
+ SCALED_DOUBLE_BOUNDS,
243
+ min);
244
+ return false;
245
+ }
246
+
247
+ const double t_1 = scaled_max - scaled_min;
248
+ const double t_4 = (double)UINT64_MAX - t_1;
249
+ const double t_5 = floor(log10(t_4)) - 1;
250
+
251
+ if ((double)precision > t_5) {
252
+ CLIENT_ERR("Invalid value for precision. precision: %" PRId32, precision);
253
+ return false;
254
+ }
255
+
256
+ const int64_t i_1 = (int64_t)(scaled_max);
257
+ const int64_t i_2 = (int64_t)(scaled_min);
258
+
259
+ const uint64_t range = subtract_int64_t(i_1, i_2);
260
+
261
+ if (((uint64_t)scaled_prc) > UINT64_MAX - range) {
262
+ CLIENT_ERR("Invalid value for min, max, and precision. The calculated domain size is too large. min: %g, max: "
263
+ "%g, precision: %" PRId32,
264
+ min,
265
+ max,
266
+ precision);
267
+ return false;
268
+ }
269
+
270
+ const uint64_t i_3 = range + (uint64_t)(scaled_prc);
271
+
272
+ if (!ceil_log2_double(i_3, maxBitsOut, status)) {
273
+ return false;
274
+ }
275
+
276
+ // Integers between -2^53 and 2^53 can be exactly represented. Outside this range, doubles lose precision by a
277
+ // multiple of 2^(n-52) where n = #bits. We disallow users from using precision mode when the bounds exceed 2^53 to
278
+ // prevent the users from being surprised by how floating point math works.
279
+ if (*maxBitsOut >= 53) {
280
+ return false;
281
+ }
282
+
283
+ return true;
284
+ }
285
+
286
+ bool mc_getTypeInfoDouble(mc_getTypeInfoDouble_args_t args,
287
+ mc_OSTType_Double *out,
288
+ mongocrypt_status_t *status,
289
+ bool use_range_v2) {
290
+ if (args.min.set != args.max.set || args.min.set != args.precision.set) {
291
+ CLIENT_ERR("min, max, and precision must all be set or must all be unset");
292
+ return false;
293
+ }
294
+
295
+ if (mc_isinf(args.value) || mc_isnan(args.value)) {
296
+ CLIENT_ERR("Infinity and NaN double values are not supported.");
297
+ return false;
298
+ }
299
+
300
+ if (args.min.set) {
301
+ if (args.min.value >= args.max.value) {
302
+ CLIENT_ERR("The minimum value must be less than the maximum value, got "
303
+ "min: %g, max: %g",
304
+ args.min.value,
305
+ args.max.value);
306
+ return false;
307
+ }
308
+
309
+ if (args.value > args.max.value || args.value < args.min.value) {
310
+ CLIENT_ERR("Value must be greater than or equal to the minimum value "
311
+ "and less than or equal to the maximum value, got "
312
+ "min: %g, max: %g, value: %g",
313
+ args.min.value,
314
+ args.max.value,
315
+ args.value);
316
+ return false;
317
+ }
318
+ }
319
+
320
+ if (args.precision.set) {
321
+ if (args.precision.value < 0) {
322
+ CLIENT_ERR("Precision must be non-negative, but got %" PRId32, args.precision.value);
323
+ return false;
324
+ }
325
+
326
+ double scaled = exp10Double(args.precision.value);
327
+ if (!mc_isfinite(scaled)) {
328
+ CLIENT_ERR("Precision is too large and cannot be used to calculate the scaled range bounds");
329
+ return false;
330
+ }
331
+ }
332
+
333
+ const bool is_neg = args.value < 0.0;
334
+
335
+ // Map negative 0 to zero so sign bit is 0.
336
+ if (args.value == 0.0) {
337
+ args.value = 0.0;
338
+ }
339
+
340
+ // When we use precision mode, we try to represent as a double value that
341
+ // fits in [-2^63, 2^63] (i.e. is a valid int64)
342
+ //
343
+ // This check determines if we can represent the precision truncated value as
344
+ // a 64-bit integer I.e. Is ((ub - lb) * 10^precision) < 64 bits.
345
+ //
346
+ bool use_precision_mode = false;
347
+ uint32_t bits_range;
348
+ if (args.precision.set) {
349
+ use_precision_mode =
350
+ mc_canUsePrecisionModeDouble(args.min.value, args.max.value, args.precision.value, &bits_range, status);
351
+
352
+ if (!use_precision_mode && use_range_v2) {
353
+ if (!mongocrypt_status_ok(status)) {
354
+ return false;
258
355
  }
259
- }
260
- }
261
- }
262
-
263
- if (use_precision_mode) {
264
- // Take a number of xxxx.ppppp and truncate it xxxx.ppp if precision = 3.
265
- // We do not change the digits before the decimal place.
266
- double v_prime = trunc (args.value * exp10Double (args.precision.value)) /
267
- exp10Double (args.precision.value);
268
- int64_t v_prime2 = (int64_t) ((v_prime - args.min.value) *
269
- exp10Double (args.precision.value));
270
-
271
- BSON_ASSERT (v_prime2 < INT64_MAX && v_prime2 >= 0);
272
-
273
- uint64_t ret = (uint64_t) v_prime2;
274
-
275
- // Adjust maximum value to be the max bit range. This will be used by
276
- // getEdges/minCover to trim bits.
277
- uint64_t max_value = (UINT64_C (1) << bits_range) - 1;
278
- BSON_ASSERT (ret <= max_value);
279
-
280
- *out = (mc_OSTType_Double){ret, 0, max_value};
281
- return true;
282
- }
283
-
284
- // Translate double to uint64 by modifying the bit representation and copying
285
- // into a uint64. Double is assumed to be a IEEE 754 Binary 64.
286
- // It is bit-encoded as sign, exponent, and fraction:
287
- // s eeeeeeee ffffffffffffffffffffffffffffffffffffffffffffffffffff
288
-
289
- // When we translate the double into "bits", the sign bit means that the
290
- // negative numbers get mapped into the higher 63 bits of a 64-bit integer.
291
- // We want them to map into the lower 64-bits so we invert the sign bit.
292
- args.value *= -1.0;
293
-
294
- // On Endianness, we support two sets of architectures
295
- // 1. Little Endian (ppc64le, x64, aarch64) - in these architectures, int64
296
- // and double are both 64-bits and both arranged in little endian byte order.
297
- // 2. Big Endian (s390x) - in these architectures, int64 and double are both
298
- // 64-bits and both arranged in big endian byte order.
299
- //
300
- // Therefore, since the order of bytes on each platform is consistent with
301
- // itself, the conversion below converts a double into correct 64-bit integer
302
- // that produces the same behavior across plaforms.
303
- uint64_t uv;
304
- memcpy (&uv, &args.value, sizeof (uint64_t));
305
-
306
- if (is_neg) {
307
- uint64_t new_zero = UINT64_C (1) << 63;
308
- BSON_ASSERT (uv <= new_zero);
309
- uv = new_zero - uv;
310
- }
311
-
312
- *out = (mc_OSTType_Double){.min = 0, .max = UINT64_MAX, .value = uv};
313
-
314
- return true;
356
+
357
+ CLIENT_ERR("The domain of double values specified by the min, max, and precision cannot be represented in "
358
+ "fewer than 53 bits. min: %g, max: %g, precision: %" PRId32,
359
+ args.min.value,
360
+ args.max.value,
361
+ args.precision.value);
362
+ return false;
363
+ }
364
+
365
+ // If we are not in range_v2, then we don't care about the error returned
366
+ // from canUsePrecisionMode so we can reset the status.
367
+ _mongocrypt_status_reset(status);
368
+ }
369
+
370
+ if (use_precision_mode) {
371
+ // Take a number of xxxx.ppppp and truncate it xxxx.ppp if precision = 3.
372
+ // We do not change the digits before the decimal place.
373
+ int64_t v_prime = (int64_t)(trunc(args.value * exp10Double(args.precision.value)));
374
+ int64_t scaled_min = (int64_t)(args.min.value * exp10Double(args.precision.value));
375
+ int64_t v_prime2 = v_prime - scaled_min;
376
+
377
+ BSON_ASSERT(v_prime2 < INT64_MAX && v_prime2 >= 0);
378
+
379
+ uint64_t ret = (uint64_t)v_prime2;
380
+
381
+ // Adjust maximum value to be the max bit range. This will be used by
382
+ // getEdges/minCover to trim bits.
383
+ uint64_t max_value = (UINT64_C(1) << bits_range) - 1;
384
+ BSON_ASSERT(ret <= max_value);
385
+
386
+ *out = (mc_OSTType_Double){ret, 0, max_value};
387
+ return true;
388
+ }
389
+
390
+ // Translate double to uint64 by modifying the bit representation and copying
391
+ // into a uint64. Double is assumed to be a IEEE 754 Binary 64.
392
+ // It is bit-encoded as sign, exponent, and fraction:
393
+ // s eeeeeeee ffffffffffffffffffffffffffffffffffffffffffffffffffff
394
+
395
+ // When we translate the double into "bits", the sign bit means that the
396
+ // negative numbers get mapped into the higher 63 bits of a 64-bit integer.
397
+ // We want them to map into the lower 64-bits so we invert the sign bit.
398
+ args.value *= -1.0;
399
+
400
+ // On Endianness, we support two sets of architectures
401
+ // 1. Little Endian (ppc64le, x64, aarch64) - in these architectures, int64
402
+ // and double are both 64-bits and both arranged in little endian byte order.
403
+ // 2. Big Endian (s390x) - in these architectures, int64 and double are both
404
+ // 64-bits and both arranged in big endian byte order.
405
+ //
406
+ // Therefore, since the order of bytes on each platform is consistent with
407
+ // itself, the conversion below converts a double into correct 64-bit integer
408
+ // that produces the same behavior across plaforms.
409
+ uint64_t uv;
410
+ memcpy(&uv, &args.value, sizeof(uint64_t));
411
+
412
+ if (is_neg) {
413
+ uint64_t new_zero = UINT64_C(1) << 63;
414
+ BSON_ASSERT(uv <= new_zero);
415
+ uv = new_zero - uv;
416
+ }
417
+
418
+ *out = (mc_OSTType_Double){.min = 0, .max = UINT64_MAX, .value = uv};
419
+
420
+ return true;
315
421
  }
316
422
 
317
423
  #if MONGOCRYPT_HAVE_DECIMAL128_SUPPORT
@@ -322,299 +428,444 @@ mc_getTypeInfoDouble (mc_getTypeInfoDouble_args_t args,
322
428
  * @param dec
323
429
  * @return mlib_int128
324
430
  */
325
- static mlib_int128
326
- dec128_to_int128 (mc_dec128 dec)
327
- {
328
- // Only normal numbers
329
- BSON_ASSERT (mc_dec128_is_finite (dec));
330
- BSON_ASSERT (!mc_dec128_is_nan (dec));
331
- // We don't support negative numbers
332
- BSON_ASSERT (!mc_dec128_is_negative (dec));
333
- // There is no fractional part:
334
- BSON_ASSERT (mc_dec128_is_zero (mc_dec128_modf (dec).frac));
335
-
336
- mlib_int128 ret = mc_dec128_coeff (dec);
337
-
338
- // Scale the resulting number by a power of ten matching the exponent of the
339
- // Decimal128:
340
- int32_t exp =
341
- ((int32_t) mc_dec128_get_biased_exp (dec)) - MC_DEC128_EXPONENT_BIAS;
342
- // We will scale up/down based on whether it is negative:
343
- mlib_int128 e1 = mlib_int128_pow10 ((uint8_t) abs (exp));
344
- if (exp < 0) {
345
- ret = mlib_int128_div (ret, e1);
346
- } else {
347
- ret = mlib_int128_mul (ret, e1);
348
- }
349
-
350
- return ret;
431
+ static mlib_int128 dec128_to_uint128(mc_dec128 dec) {
432
+ // Only normal numbers
433
+ BSON_ASSERT(mc_dec128_is_finite(dec));
434
+ BSON_ASSERT(!mc_dec128_is_nan(dec));
435
+ // We don't support negative numbers
436
+ BSON_ASSERT(!mc_dec128_is_negative(dec));
437
+ // There is no fractional part:
438
+ BSON_ASSERT(mc_dec128_is_zero(mc_dec128_modf(dec).frac));
439
+
440
+ mlib_int128 ret = mc_dec128_coeff(dec);
441
+
442
+ // Scale the resulting number by a power of ten matching the exponent of the
443
+ // Decimal128:
444
+ int32_t exp = ((int32_t)mc_dec128_get_biased_exp(dec)) - MC_DEC128_EXPONENT_BIAS;
445
+ // We will scale up/down based on whether it is negative:
446
+ BSON_ASSERT(abs(exp) <= UINT8_MAX);
447
+ mlib_int128 e1 = mlib_int128_pow10((uint8_t)abs(exp));
448
+ if (exp < 0) {
449
+ ret = mlib_int128_div(ret, e1);
450
+ } else {
451
+ ret = mlib_int128_mul(ret, e1);
452
+ }
453
+
454
+ return ret;
455
+ }
456
+
457
+ // (2^127 - 1) = the maximum signed 128-bit integer value, as a decimal128
458
+ #define INT_128_MAX_AS_DECIMAL mc_dec128_from_string("170141183460469231731687303715884105727")
459
+ // (2^128 - 1) = the max unsigned 128-bit integer value, as a decimal128
460
+ #define UINT_128_MAX_AS_DECIMAL mc_dec128_from_string("340282366920938463463374607431768211455")
461
+
462
+ static mlib_int128 dec128_to_int128(mc_dec128 dec) {
463
+ BSON_ASSERT(mc_dec128_less(dec, INT_128_MAX_AS_DECIMAL));
464
+
465
+ bool negative = false;
466
+
467
+ if (mc_dec128_is_negative(dec)) {
468
+ negative = true;
469
+ dec = mc_dec128_mul(MC_DEC128(-1), dec);
470
+ }
471
+
472
+ mlib_int128 ret_val = dec128_to_uint128(dec);
473
+
474
+ if (negative) {
475
+ ret_val = mlib_int128_mul(MLIB_INT128(-1), ret_val);
476
+ }
477
+
478
+ return ret_val;
479
+ }
480
+
481
+ bool ceil_log2_int128(mlib_int128 i, uint32_t *maxBitsOut, mongocrypt_status_t *status) {
482
+ if (mlib_int128_eq(i, MLIB_INT128(0))) {
483
+ CLIENT_ERR("Invalid input to ceil_log2_int128 function. Input cannot be 0.");
484
+ return false;
485
+ }
486
+
487
+ uint32_t clz = (uint32_t)_mlibCountLeadingZeros_u128(i);
488
+ uint32_t bits;
489
+
490
+ // if i & (i - 1) == 0
491
+ if (mlib_int128_eq((mlib_int128_bitand(i, (mlib_int128_sub(i, MLIB_INT128(1))))), MLIB_INT128(0))) {
492
+ bits = 128 - clz - 1;
493
+ } else {
494
+ bits = 128 - clz;
495
+ }
496
+ *maxBitsOut = bits;
497
+ return true;
351
498
  }
352
499
 
353
- bool
354
- mc_getTypeInfoDecimal128 (mc_getTypeInfoDecimal128_args_t args,
355
- mc_OSTType_Decimal128 *out,
356
- mongocrypt_status_t *status)
357
- {
358
- /// Basic param checks
359
- if (args.min.set != args.max.set || args.min.set != args.precision.set) {
360
- CLIENT_ERR (
361
- "min, max, and precision must all be set or must all be unset");
362
- return false;
363
- }
364
-
365
- // We only accept normal numbers
366
- if (mc_dec128_is_inf (args.value) || mc_dec128_is_nan (args.value)) {
367
- CLIENT_ERR ("Infinity and Nan Decimal128 values are not supported.");
368
- return false;
369
- }
370
-
371
- // Check boundary if a range is set
372
- if (args.min.set) {
373
- // [min,max] must be valid
374
- if (mc_dec128_greater_equal (args.min.value, args.max.value)) {
375
- CLIENT_ERR (
376
- "The minimum value must be less than the maximum value, got "
377
- "min: %s, max: %s",
378
- mc_dec128_to_string (args.min.value).str,
379
- mc_dec128_to_string (args.max.value).str);
380
- return false;
381
- }
382
-
383
- // Value must be within [min,max)
384
- if (mc_dec128_greater (args.value, args.max.value) ||
385
- mc_dec128_less (args.value, args.min.value)) {
386
- CLIENT_ERR ("Value must be greater than or equal to the minimum value "
387
- "and less than or equal to the maximum value, got "
388
- "min: %s, max: %s, value: %s",
389
- mc_dec128_to_string (args.min.value).str,
390
- mc_dec128_to_string (args.max.value).str,
391
- mc_dec128_to_string (args.value).str);
392
- return false;
393
- }
394
- }
395
-
396
- // Should we use precision mode?
397
- //
398
- // When we use precision mode, we try to represent as a decimal128 value that
399
- // fits in [-2^127, 2^127] (i.e. is a valid int128)
400
- //
401
- // This check determines if we can represent any precision-truncated value as
402
- // a 128-bit integer I.e. Is ((ub - lb) * 10^precision) < 128 bits.
403
- //
404
- // It is important that we determine whether a range and its precision would
405
- // fit, regardless of the value to be encoded, because the encoding for
406
- // precision-truncated-decimal128 is incompatible with the encoding of the
407
- // full range.
408
- bool use_precision_mode = false;
409
- // The number of bits required to hold the result (used for precision mode)
410
- uint8_t bits_range = 0;
411
- if (args.precision.set) {
412
- // Subnormal representations can support up to 5x10^-6182 as a number
413
- if (args.precision.value > 6182) {
414
- CLIENT_ERR (
415
- "Precision must be between 0 and 6182 inclusive, got: %" PRIu32,
416
- args.precision.value);
417
- return false;
418
- }
419
-
420
- // max - min
421
- mc_dec128 bounds_n1 = mc_dec128_sub (args.max.value, args.min.value);
422
- // The size of [min, max]: (max - min) + 1
423
- mc_dec128 bounds = mc_dec128_add (bounds_n1, MC_DEC128_ONE);
424
-
425
- // We can overflow if max = max_dec128 and min = min_dec128 so make sure
426
- // we have finite number after we do subtraction
427
- if (mc_dec128_is_finite (bounds)) {
428
- // This creates a range which is wider then we permit by our min/max
429
- // bounds check with the +1 but it is as the algorithm is written in
430
- // WRITING-11907.
431
- mc_dec128 precision_scaled_bounds =
432
- mc_dec128_scale (bounds, args.precision.value);
433
- /// The number of bits required to hold the result for the given
434
- /// precision (as decimal)
435
- mc_dec128 bits_range_dec = mc_dec128_log2 (precision_scaled_bounds);
436
-
437
- if (mc_dec128_is_finite (bits_range_dec) &&
438
- mc_dec128_less (bits_range_dec, MC_DEC128 (128))) {
439
- // We need fewer than 128 bits to hold the result. But round up,
440
- // just to be sure:
441
- int64_t r = mc_dec128_to_int64 (mc_dec128_round_integral_ex (
442
- bits_range_dec, MC_DEC128_ROUND_UPWARD, NULL));
443
- BSON_ASSERT (r >= 0);
444
- BSON_ASSERT (r <= UINT8_MAX);
445
- // We've computed the proper 'bits_range'
446
- bits_range = (uint8_t) r;
447
-
448
- if (bits_range < 128) {
449
- use_precision_mode = true;
500
+ bool mc_canUsePrecisionModeDecimal(mc_dec128 min,
501
+ mc_dec128 max,
502
+ int32_t precision,
503
+ uint32_t *maxBitsOut,
504
+ mongocrypt_status_t *status) {
505
+ BSON_ASSERT_PARAM(maxBitsOut);
506
+ BSON_ASSERT(precision >= 0);
507
+
508
+ if (!mc_dec128_is_finite(max)) {
509
+ CLIENT_ERR("Invalid upper bound for Decimal128 precision. Max is infinite.");
510
+ return false;
511
+ }
512
+
513
+ if (!mc_dec128_is_finite(min)) {
514
+ CLIENT_ERR("Invalid lower bound for Decimal128 precision. Min is infinite.");
515
+ return false;
516
+ }
517
+
518
+ if (mc_dec128_greater_equal(min, max)) {
519
+ CLIENT_ERR("Invalid upper and lower bounds for Decimal128 precision. Min must be strictly less than max. min: "
520
+ "%s, max: %s",
521
+ mc_dec128_to_string(min).str,
522
+ mc_dec128_to_string(max).str);
523
+ return false;
524
+ }
525
+
526
+ mc_dec128 scaled_max = mc_dec128_scale(max, precision);
527
+ mc_dec128 scaled_min = mc_dec128_scale(min, precision);
528
+
529
+ mc_dec128 scaled_max_trunc = mc_dec128_round_integral_ex(scaled_max, MC_DEC128_ROUND_TOWARD_ZERO, NULL);
530
+ mc_dec128 scaled_min_trunc = mc_dec128_round_integral_ex(scaled_min, MC_DEC128_ROUND_TOWARD_ZERO, NULL);
531
+
532
+ if (mc_dec128_not_equal(scaled_max, scaled_max_trunc)) {
533
+ CLIENT_ERR("Invalid upper bound for Decimal128 precision. Fractional digits must be less than "
534
+ "the specified precision value. max: %s, precision: %" PRId32,
535
+ mc_dec128_to_string(max).str,
536
+ precision);
537
+ return false;
538
+ }
539
+
540
+ if (mc_dec128_not_equal(scaled_min, scaled_min_trunc)) {
541
+ CLIENT_ERR("Invalid lower bound for Decimal128 precision. Fractional digits must be less than "
542
+ "the specified precision value. min: %s, precision: %" PRId32,
543
+ mc_dec128_to_string(min).str,
544
+ precision);
545
+ return false;
546
+ }
547
+
548
+ if (mc_dec128_greater(mc_dec128_abs(scaled_max), INT_128_MAX_AS_DECIMAL)) {
549
+ CLIENT_ERR("Invalid upper bound for Decimal128 precision. Absolute scaled value must be less than "
550
+ "or equal to %s. max: %s",
551
+ mc_dec128_to_string(INT_128_MAX_AS_DECIMAL).str,
552
+ mc_dec128_to_string(max).str);
553
+ return false;
554
+ }
555
+
556
+ if (mc_dec128_greater(mc_dec128_abs(scaled_min), INT_128_MAX_AS_DECIMAL)) {
557
+ CLIENT_ERR("Invalid lower bound for Decimal128 precision. Absolute scaled value must be less than "
558
+ "or equal to %s. min: %s",
559
+ mc_dec128_to_string(INT_128_MAX_AS_DECIMAL).str,
560
+ mc_dec128_to_string(min).str);
561
+ return false;
562
+ }
563
+
564
+ mc_dec128 t_1 = mc_dec128_sub(scaled_max, scaled_min);
565
+ mc_dec128 t_4 = mc_dec128_sub(UINT_128_MAX_AS_DECIMAL, t_1);
566
+
567
+ // t_5 = floor(log10(t_4)) - 1;
568
+ mc_dec128 t_5 = mc_dec128_sub(mc_dec128_round_integral_ex(mc_dec128_log10(t_4), MC_DEC128_ROUND_TOWARD_ZERO, NULL),
569
+ MC_DEC128(1));
570
+
571
+ // We convert precision to a double so we can avoid warning C4146 on Windows.
572
+ mc_dec128 prc_dec = mc_dec128_from_double((double)precision);
573
+
574
+ if (mc_dec128_less(t_5, prc_dec)) {
575
+ CLIENT_ERR("Invalid value for precision. precision: %" PRId32, precision);
576
+ return false;
577
+ }
578
+
579
+ mlib_int128 i_1 = dec128_to_int128(scaled_max);
580
+ mlib_int128 i_2 = dec128_to_int128(scaled_min);
581
+
582
+ // Because we have guaranteed earlier that max is greater than min, we can
583
+ // subtract these values and guarantee that taking their unsigned
584
+ // representation will yield the actual range result.
585
+ mlib_int128 range128 = mlib_int128_sub(i_1, i_2);
586
+
587
+ if (precision > UINT8_MAX) {
588
+ CLIENT_ERR("Invalid value for precision. Must be less than 255. precision: %" PRId32, precision);
589
+ return false;
590
+ }
591
+
592
+ mlib_int128 i_3 = mlib_int128_add(range128, mlib_int128_pow10((uint8_t)precision));
593
+ if (!ceil_log2_int128(i_3, maxBitsOut, status)) {
594
+ return false;
595
+ }
596
+
597
+ if (*maxBitsOut >= 128) {
598
+ return false;
599
+ }
600
+
601
+ return true;
602
+ }
603
+
604
+ bool mc_getTypeInfoDecimal128(mc_getTypeInfoDecimal128_args_t args,
605
+ mc_OSTType_Decimal128 *out,
606
+ mongocrypt_status_t *status,
607
+ bool use_range_v2) {
608
+ /// Basic param checks
609
+ if (args.min.set != args.max.set || args.min.set != args.precision.set) {
610
+ CLIENT_ERR("min, max, and precision must all be set or must all be unset");
611
+ return false;
612
+ }
613
+
614
+ if (args.precision.set) {
615
+ if (args.precision.value < 0) {
616
+ CLIENT_ERR("Precision must be non-negative, but got %" PRId32, args.precision.value);
617
+ return false;
618
+ }
619
+
620
+ mc_dec128 scaled = mc_dec128_scale(MC_DEC128(1), args.precision.value);
621
+ if (!mc_dec128_is_finite(scaled)) {
622
+ CLIENT_ERR("Precision is too large and cannot be used to calculate the scaled range bounds");
623
+ return false;
624
+ }
625
+ }
626
+
627
+ // We only accept normal numbers
628
+ if (mc_dec128_is_inf(args.value) || mc_dec128_is_nan(args.value)) {
629
+ CLIENT_ERR("Infinity and Nan Decimal128 values are not supported.");
630
+ return false;
631
+ }
632
+
633
+ // Check boundary if a range is set
634
+ if (args.min.set) {
635
+ // [min,max] must be valid
636
+ if (mc_dec128_greater_equal(args.min.value, args.max.value)) {
637
+ CLIENT_ERR("The minimum value must be less than the maximum value, got "
638
+ "min: %s, max: %s",
639
+ mc_dec128_to_string(args.min.value).str,
640
+ mc_dec128_to_string(args.max.value).str);
641
+ return false;
642
+ }
643
+
644
+ // Value must be within [min,max)
645
+ if (mc_dec128_greater(args.value, args.max.value) || mc_dec128_less(args.value, args.min.value)) {
646
+ CLIENT_ERR("Value must be greater than or equal to the minimum value "
647
+ "and less than or equal to the maximum value, got "
648
+ "min: %s, max: %s, value: %s",
649
+ mc_dec128_to_string(args.min.value).str,
650
+ mc_dec128_to_string(args.max.value).str,
651
+ mc_dec128_to_string(args.value).str);
652
+ return false;
653
+ }
654
+ }
655
+
656
+ // Should we use precision mode?
657
+ //
658
+ // When we use precision mode, we try to represent as a decimal128 value that
659
+ // fits in [-2^127, 2^127] (i.e. is a valid int128)
660
+ //
661
+ // This check determines if we can represent any precision-truncated value as
662
+ // a 128-bit integer I.e. Is ((ub - lb) * 10^precision) < 128 bits.
663
+ //
664
+ // It is important that we determine whether a range and its precision would
665
+ // fit, regardless of the value to be encoded, because the encoding for
666
+ // precision-truncated-decimal128 is incompatible with the encoding of the
667
+ // full range.
668
+ bool use_precision_mode = false;
669
+ // The number of bits required to hold the result (used for precision mode)
670
+ uint32_t bits_range = 0;
671
+ if (args.precision.set) {
672
+ use_precision_mode =
673
+ mc_canUsePrecisionModeDecimal(args.min.value, args.max.value, args.precision.value, &bits_range, status);
674
+
675
+ if (use_range_v2 && !use_precision_mode) {
676
+ if (!mongocrypt_status_ok(status)) {
677
+ return false;
450
678
  }
451
- }
452
- }
453
- }
454
-
455
- // Constant zero
456
- const mlib_int128 i128_zero = MLIB_INT128 (0);
457
- // Constant 1
458
- const mlib_int128 i128_one = MLIB_INT128 (1);
459
- // Constant 10
460
- const mlib_int128 i128_ten = MLIB_INT128 (10);
461
- // Constant: 2^127
462
- const mlib_int128 i128_2pow127 = mlib_int128_lshift (i128_one, 127);
463
- // ↑ Coincidentally has the same bit pattern as INT128_SMIN, but we're
464
- // treating it as an unsigned number here, so don't get confused!
465
-
466
- if (use_precision_mode) {
467
- BSON_ASSERT (args.precision.set);
468
- // Example value: 31.4159
469
- // Example Precision = 2
470
-
471
- // Shift the number up
472
- // Returns: 3141.9
473
- mc_dec128 valScaled = mc_dec128_scale (args.value, args.precision.value);
474
-
475
- // Round the number down
476
- // Returns 3141.0
477
- mc_dec128 valTrunc = mc_dec128_round_integral_ex (
478
- valScaled, MC_DEC128_ROUND_TOWARD_ZERO, NULL);
479
-
480
- // Shift the number down
481
- // Returns: 31.41
482
- mc_dec128 v_prime =
483
- mc_dec128_scale (valTrunc, -(int32_t) args.precision.value);
484
-
485
- // Adjust the number by the lower bound
486
- // Make it an integer by scaling the number
487
- //
488
- // Returns 3141.0
489
- mc_dec128 v_prime2 = mc_dec128_scale (
490
- mc_dec128_sub (v_prime, args.min.value), args.precision.value);
491
- // Round the number down again. min may have a fractional value with more
492
- // decimal places than the precision (e.g. .001). Subtracting min may have
493
- // resulted in v_prime2 with a non-zero fraction. v_prime2 is expected to
494
- // have no fractional value when converting to int128.
495
- v_prime2 = mc_dec128_round_integral_ex (
496
- v_prime2, MC_DEC128_ROUND_TOWARD_ZERO, NULL);
497
-
498
- BSON_ASSERT (mc_dec128_less (mc_dec128_log2 (v_prime2), MC_DEC128 (128)));
499
-
500
- // Resulting OST maximum
501
- mlib_int128 ost_max =
502
- mlib_int128_sub (mlib_int128_pow2 (bits_range), i128_one);
503
-
504
- // Now we need to get the Decimal128 out as a 128-bit integer
505
- // But Decimal128 does not support conversion to Int128.
506
- //
507
- // If we think the Decimal128 fits in the range, based on the maximum
508
- // value, we try to convert to int64 directly.
509
- if (bits_range < 64) {
510
- // Try conversion to int64, it may fail but since it is easy we try
511
- // this first.
512
- mc_dec128_flagset flags = {0};
513
- int64_t as64 = mc_dec128_to_int64_ex (v_prime2, &flags);
514
- if (flags.bits == 0) {
515
- // No error. It fits
516
- *out = (mc_OSTType_Decimal128){
517
- .value = MLIB_INT128_CAST (as64),
518
- .min = i128_zero,
519
- .max = ost_max,
520
- };
521
- return true;
522
- } else {
523
- // Conversion failure to 64-bit. Possible overflow, imprecision,
524
- // etc. Fallback to slower dec128_to_int128
525
- }
526
- }
527
-
528
- mlib_int128 u_ret = dec128_to_int128 (v_prime2);
529
-
530
- *out = (mc_OSTType_Decimal128){
531
- .value = u_ret,
532
- .min = i128_zero,
533
- .max = ost_max,
534
- };
535
-
536
- return true;
537
- }
538
-
539
- // The coefficient of the number, without exponent/sign
540
- const mlib_int128 coeff = mc_dec128_coeff (args.value);
541
-
542
- if (mlib_int128_eq (coeff, i128_zero)) {
543
- // If the coefficient is zero, the result is encoded as the midpoint
544
- // between zero and 2^128-1
545
- *out = (mc_OSTType_Decimal128){
546
- .value = i128_2pow127,
547
- .min = i128_zero,
548
- .max = MLIB_INT128_UMAX,
549
- };
550
- return true;
551
- }
552
-
553
- // Coefficient is an unsigned value. We'll later scale our answer based on
554
- // the sign of the actual Decimal128
555
- const bool isNegative = mc_dec128_is_negative (args.value);
556
-
557
- // cMax = 10^34 - 1 (The largest integer representable in Decimal128)
558
- const mlib_int128 cMax =
559
- mlib_int128_sub (mlib_int128_pow10 (34), MLIB_INT128_CAST (1));
560
- const mlib_int128 cMax_div_ten = mlib_int128_div (cMax, i128_ten);
561
-
562
- // The biased exponent from the decimal number. The paper refers to the
563
- // expression (e - e_min), which is the value of the biased exponent.
564
- const uint32_t exp_biased = mc_dec128_get_biased_exp (args.value);
565
-
566
- // ρ (rho) is the greatest integer such that: coeff×10^ρ <= cMax
567
- unsigned rho = 0;
568
- // Keep track of the subexpression coeff×10^ρ rather than recalculating it
569
- // time.
570
- // Initially: (ρ = 0) -> (10^ρ = 1) -> (coeff×10^ρ = coeff×1 = coeff):
571
- mlib_int128 coeff_scaled = coeff;
572
- // Calculate ρ: This could be done using a log10 with a division, but that
573
- // is far more work than just a few multiplications.
574
- // While: coeff×ten< cMax/10:
575
- while (mlib_int128_ucmp (coeff_scaled, cMax_div_ten) < 0) {
576
- // Increase rho until we pass cMax/10
577
- rho++;
578
- // Scale our computed subexpression rather than fully recomputing it
579
- coeff_scaled = mlib_int128_mul (coeff_scaled, i128_ten);
580
- }
581
-
582
- // No multiplication by 10 should ever send us from N < cMax/10 to N > cMax
583
- BSON_ASSERT (mlib_int128_ucmp (coeff_scaled, cMax) <= 0);
584
-
585
- mlib_int128 result;
586
- if (rho <= exp_biased) {
587
- // ρ is less-than/equal to the exponent with bias.
588
-
589
- // Diff between the biased exponent and ρ.
590
- // Value in paper is spelled "e - e_min - ρ"
591
- const uint32_t exp_diff = exp_biased - (uint32_t) rho;
592
- // cMax * (exp_diff)
593
- const mlib_int128 cmax_scaled =
594
- mlib_int128_mul (cMax, MLIB_INT128_CAST (exp_diff));
595
- // coeff * 10^rho * cMax * (exp_biased - rho)
596
- result = mlib_int128_add (coeff_scaled, cmax_scaled);
597
- } else {
598
- const mlib_int128 biased_scale = mlib_int128_pow10 ((uint8_t) exp_biased);
599
- result = mlib_int128_mul (biased_scale, coeff);
600
- }
601
-
602
- // Always add 2^127:
603
- result = mlib_int128_add (result, i128_2pow127);
604
-
605
- if (isNegative) {
606
- // We calculated the value of the positive coefficient, but the decimal is
607
- // negative. That's okay: Just flip the sign of the encoded result:
608
- result = mlib_int128_negate (result);
609
- }
610
-
611
- *out = (mc_OSTType_Decimal128){
612
- .value = result,
613
- .min = i128_zero,
614
- .max = MLIB_INT128_UMAX,
615
- };
616
-
617
- return true;
679
+
680
+ CLIENT_ERR("The domain of decimal values specified by the min, max, and precision cannot be represented in "
681
+ "fewer than 128 bits. min: %s, max: %s, precision: %" PRIu32,
682
+ mc_dec128_to_string(args.min.value).str,
683
+ mc_dec128_to_string(args.max.value).str,
684
+ args.precision.value);
685
+ return false;
686
+ }
687
+
688
+ // If we are not in range_v2, then we don't care about the error returned
689
+ // from canUsePrecisionMode so we can reset the status.
690
+ _mongocrypt_status_reset(status);
691
+ }
692
+
693
+ // Constant zero
694
+ const mlib_int128 i128_zero = MLIB_INT128(0);
695
+ // Constant 1
696
+ const mlib_int128 i128_one = MLIB_INT128(1);
697
+ // Constant 10
698
+ const mlib_int128 i128_ten = MLIB_INT128(10);
699
+ // Constant: 2^127
700
+ const mlib_int128 i128_2pow127 = mlib_int128_lshift(i128_one, 127);
701
+ // Coincidentally has the same bit pattern as INT128_SMIN, but we're
702
+ // treating it as an unsigned number here, so don't get confused!
703
+
704
+ if (use_precision_mode) {
705
+ BSON_ASSERT(args.precision.set);
706
+ // Example value: 31.4159
707
+ // Example Precision = 2
708
+
709
+ // Shift the number up
710
+ // Returns: 3141.9
711
+ mc_dec128 valScaled = mc_dec128_scale(args.value, args.precision.value);
712
+
713
+ // Round the number down
714
+ // Returns 3141.0
715
+ mc_dec128 valTrunc = mc_dec128_round_integral_ex(valScaled, MC_DEC128_ROUND_TOWARD_ZERO, NULL);
716
+
717
+ // Shift the number down
718
+ // Returns: 31.41
719
+ mc_dec128 v_prime = mc_dec128_scale(valTrunc, -(int32_t)args.precision.value);
720
+
721
+ // Adjust the number by the lower bound
722
+ // Make it an integer by scaling the number
723
+ //
724
+ // Returns 3141.0
725
+ mc_dec128 v_prime2 = mc_dec128_scale(mc_dec128_sub(v_prime, args.min.value), args.precision.value);
726
+ // Round the number down again. min may have a fractional value with more
727
+ // decimal places than the precision (e.g. .001). Subtracting min may have
728
+ // resulted in v_prime2 with a non-zero fraction. v_prime2 is expected to
729
+ // have no fractional value when converting to int128.
730
+ v_prime2 = mc_dec128_round_integral_ex(v_prime2, MC_DEC128_ROUND_TOWARD_ZERO, NULL);
731
+
732
+ BSON_ASSERT(mc_dec128_less(mc_dec128_log2(v_prime2), MC_DEC128(128)));
733
+ BSON_ASSERT(bits_range < 128);
734
+ // Resulting OST maximum
735
+ mlib_int128 ost_max = mlib_int128_sub(mlib_int128_pow2((uint8_t)bits_range), i128_one);
736
+
737
+ // Now we need to get the Decimal128 out as a 128-bit integer
738
+ // But Decimal128 does not support conversion to Int128.
739
+ //
740
+ // If we think the Decimal128 fits in the range, based on the maximum
741
+ // value, we try to convert to int64 directly.
742
+ if (bits_range < 64) {
743
+ // Try conversion to int64, it may fail but since it is easy we try
744
+ // this first.
745
+ mc_dec128_flagset flags = {0};
746
+ int64_t as64 = mc_dec128_to_int64_ex(v_prime2, &flags);
747
+ if (flags.bits == 0) {
748
+ // No error. It fits
749
+ *out = (mc_OSTType_Decimal128){
750
+ .value = MLIB_INT128_CAST(as64),
751
+ .min = i128_zero,
752
+ .max = ost_max,
753
+ };
754
+ return true;
755
+ } else {
756
+ // Conversion failure to 64-bit. Possible overflow, imprecision,
757
+ // etc. Fallback to slower dec128_to_int128
758
+ }
759
+ }
760
+
761
+ mlib_int128 u_ret = dec128_to_int128(v_prime2);
762
+
763
+ *out = (mc_OSTType_Decimal128){
764
+ .value = u_ret,
765
+ .min = i128_zero,
766
+ .max = ost_max,
767
+ };
768
+
769
+ return true;
770
+ }
771
+
772
+ // The coefficient of the number, without exponent/sign
773
+ const mlib_int128 coeff = mc_dec128_coeff(args.value);
774
+
775
+ if (mlib_int128_eq(coeff, i128_zero)) {
776
+ // If the coefficient is zero, the result is encoded as the midpoint
777
+ // between zero and 2^128-1
778
+ *out = (mc_OSTType_Decimal128){
779
+ .value = i128_2pow127,
780
+ .min = i128_zero,
781
+ .max = MLIB_INT128_UMAX,
782
+ };
783
+ return true;
784
+ }
785
+
786
+ // Coefficient is an unsigned value. We'll later scale our answer based on
787
+ // the sign of the actual Decimal128
788
+ const bool isNegative = mc_dec128_is_negative(args.value);
789
+
790
+ // cMax = 10^34 - 1 (The largest integer representable in Decimal128)
791
+ const mlib_int128 cMax = mlib_int128_sub(mlib_int128_pow10(34), MLIB_INT128_CAST(1));
792
+ const mlib_int128 cMax_div_ten = mlib_int128_div(cMax, i128_ten);
793
+
794
+ // The biased exponent from the decimal number. The paper refers to the
795
+ // expression (e - e_min), which is the value of the biased exponent.
796
+ const uint32_t exp_biased = mc_dec128_get_biased_exp(args.value);
797
+
798
+ // ρ (rho) is the greatest integer such that: coeff×10^ρ <= cMax
799
+ unsigned rho = 0;
800
+ // Keep track of the subexpression coeff×10^ρ rather than recalculating it
801
+ // time.
802
+ // Initially: (ρ = 0) -> (10= 1) -> (coeff×10^ρ = coeff×1 = coeff):
803
+ mlib_int128 coeff_scaled = coeff;
804
+ // Calculate ρ: This could be done using a log10 with a division, but that
805
+ // is far more work than just a few multiplications.
806
+ // While: coeff×ten^ρ < cMax/10:
807
+ while (mlib_int128_ucmp(coeff_scaled, cMax_div_ten) < 0) {
808
+ // Increase rho until we pass cMax/10
809
+ rho++;
810
+ // Scale our computed subexpression rather than fully recomputing it
811
+ coeff_scaled = mlib_int128_mul(coeff_scaled, i128_ten);
812
+ }
813
+
814
+ // No multiplication by 10 should ever send us from N < cMax/10 to N > cMax
815
+ BSON_ASSERT(mlib_int128_ucmp(coeff_scaled, cMax) <= 0);
816
+
817
+ mlib_int128 result;
818
+ if (rho <= exp_biased) {
819
+ // ρ is less-than/equal to the exponent with bias.
820
+
821
+ // Diff between the biased exponent and ρ.
822
+ // Value in paper is spelled "e - e_min - ρ"
823
+ const uint32_t exp_diff = exp_biased - (uint32_t)rho;
824
+ // cMax * (exp_diff)
825
+ const mlib_int128 cmax_scaled = mlib_int128_mul(cMax, MLIB_INT128_CAST(exp_diff));
826
+ // coeff * 10^rho * cMax * (exp_biased - rho)
827
+ result = mlib_int128_add(coeff_scaled, cmax_scaled);
828
+ } else {
829
+ const mlib_int128 biased_scale = mlib_int128_pow10((uint8_t)exp_biased);
830
+ result = mlib_int128_mul(biased_scale, coeff);
831
+ }
832
+
833
+ // Always add 2^127:
834
+ result = mlib_int128_add(result, i128_2pow127);
835
+
836
+ if (isNegative) {
837
+ // We calculated the value of the positive coefficient, but the decimal is
838
+ // negative. That's okay: Just flip the sign of the encoded result:
839
+ result = mlib_int128_negate(result);
840
+ }
841
+
842
+ *out = (mc_OSTType_Decimal128){
843
+ .value = result,
844
+ .min = i128_zero,
845
+ .max = MLIB_INT128_UMAX,
846
+ };
847
+
848
+ return true;
618
849
  }
619
850
 
620
851
  #endif // defined MONGOCRYPT_HAVE_DECIMAL128_SUPPORT
852
+
853
+ const int64_t mc_FLERangeSparsityDefault = 2;
854
+ const int32_t mc_FLERangeTrimFactorDefault = 6;
855
+
856
+ int32_t trimFactorDefault(size_t maxlen, mc_optional_int32_t trimFactor, bool use_range_v2) {
857
+ if (trimFactor.set) {
858
+ return trimFactor.value;
859
+ }
860
+
861
+ if (!use_range_v2) {
862
+ // Preserve old default.
863
+ return 0;
864
+ }
865
+
866
+ if (bson_cmp_greater_su(mc_FLERangeTrimFactorDefault, maxlen - 1)) {
867
+ return (int32_t)(maxlen - 1);
868
+ } else {
869
+ return mc_FLERangeTrimFactorDefault;
870
+ }
871
+ }