@@ -89,6 +89,7 @@ extern "C" {
89
89
#include " mongo/crypto/aead_encryption.h"
90
90
#include " mongo/crypto/encryption_fields_gen.h"
91
91
#include " mongo/crypto/encryption_fields_util.h"
92
+ #include " mongo/crypto/encryption_fields_validation.h"
92
93
#include " mongo/crypto/fle_crypto_predicate.h"
93
94
#include " mongo/crypto/fle_data_frames.h"
94
95
#include " mongo/crypto/fle_field_schema_gen.h"
@@ -931,7 +932,7 @@ std::unique_ptr<Edges> getEdges(FLE2RangeInsertSpec spec, int sparsity) {
931
932
auto element = spec.getValue ().getElement ();
932
933
auto minBound = spec.getMinBound ().map ([](IDLAnyType m) { return m.getElement (); });
933
934
auto maxBound = spec.getMaxBound ().map ([](IDLAnyType m) { return m.getElement (); });
934
- auto trimFactor = spec.getTrimFactor () ? spec. getTrimFactor (). value () : 0 ;
935
+ auto trimFactor = spec.getTrimFactor ();
935
936
936
937
switch (element.type ()) {
937
938
case BSONType::NumberInt:
@@ -4322,8 +4323,20 @@ bool EncryptedPredicateEvaluatorV2::evaluate(
4322
4323
4323
4324
// Edges
4324
4325
4325
- Edges::Edges (std::string leaf, int sparsity, int trimFactor)
4326
- : _leaf(std::move(leaf)), _sparsity(sparsity), _trimFactor(trimFactor) {
4326
+ namespace {
4327
+ int resolveTrimFactorDefault (int maxlen, const boost::optional<int >& optTrimFactor) {
4328
+ if (optTrimFactor) {
4329
+ return *optTrimFactor;
4330
+ }
4331
+
4332
+ return std::clamp (kFLERangeTrimFactorDefault , 0 , maxlen - 1 );
4333
+ }
4334
+ } // namespace
4335
+
4336
+ Edges::Edges (std::string leaf, int sparsity, const boost::optional<int >& optTrimFactor)
4337
+ : _leaf(std::move(leaf)),
4338
+ _sparsity(sparsity),
4339
+ _trimFactor(resolveTrimFactorDefault(_leaf.length(), optTrimFactor)) {
4327
4340
uassert (6775101 , " sparsity must be 1 or larger" , _sparsity > 0 );
4328
4341
dassert (std::all_of (_leaf.begin (), _leaf.end (), [](char c) { return c == ' 1' || c == ' 0' ; }));
4329
4342
uassert (8574105 ,
@@ -4370,7 +4383,8 @@ std::size_t Edges::size() const {
4370
4383
}
4371
4384
4372
4385
template <typename T>
4373
- std::unique_ptr<Edges> getEdgesT (T value, T min, T max, int sparsity, int trimFactor) {
4386
+ std::unique_ptr<Edges> getEdgesT (
4387
+ T value, T min, T max, int sparsity, const boost::optional<int >& trimFactor) {
4374
4388
static_assert (!std::numeric_limits<T>::is_signed);
4375
4389
static_assert (std::numeric_limits<T>::is_integer);
4376
4390
@@ -4388,7 +4402,7 @@ std::unique_ptr<Edges> getEdgesInt32(int32_t value,
4388
4402
boost::optional<int32_t > min,
4389
4403
boost::optional<int32_t > max,
4390
4404
int sparsity,
4391
- int trimFactor) {
4405
+ const boost::optional< int >& trimFactor) {
4392
4406
auto aost = getTypeInfo32 (value, min, max);
4393
4407
return getEdgesT (aost.value , aost.min , aost.max , sparsity, trimFactor);
4394
4408
}
@@ -4397,7 +4411,7 @@ std::unique_ptr<Edges> getEdgesInt64(int64_t value,
4397
4411
boost::optional<int64_t > min,
4398
4412
boost::optional<int64_t > max,
4399
4413
int sparsity,
4400
- int trimFactor) {
4414
+ const boost::optional< int >& trimFactor) {
4401
4415
auto aost = getTypeInfo64 (value, min, max);
4402
4416
return getEdgesT (aost.value , aost.min , aost.max , sparsity, trimFactor);
4403
4417
}
@@ -4407,7 +4421,7 @@ std::unique_ptr<Edges> getEdgesDouble(double value,
4407
4421
boost::optional<double > max,
4408
4422
boost::optional<uint32_t > precision,
4409
4423
int sparsity,
4410
- int trimFactor) {
4424
+ const boost::optional< int >& trimFactor) {
4411
4425
auto aost = getTypeInfoDouble (value, min, max, precision);
4412
4426
return getEdgesT (aost.value , aost.min , aost.max , sparsity, trimFactor);
4413
4427
}
@@ -4417,7 +4431,7 @@ std::unique_ptr<Edges> getEdgesDecimal128(Decimal128 value,
4417
4431
boost::optional<Decimal128> max,
4418
4432
boost::optional<uint32_t > precision,
4419
4433
int sparsity,
4420
- int trimFactor) {
4434
+ const boost::optional< int >& trimFactor) {
4421
4435
auto aost = getTypeInfoDecimal128 (value, min, max, precision);
4422
4436
return getEdgesT (aost.value , aost.min , aost.max , sparsity, trimFactor);
4423
4437
}
@@ -4427,7 +4441,7 @@ std::uint64_t getEdgesLength(BSONType fieldType, StringData fieldPath, QueryType
4427
4441
setRangeDefaults (fieldType, fieldPath, &config);
4428
4442
4429
4443
const auto sparsity = *config.getSparsity ();
4430
- const auto trimFactor = config.getTrimFactor (). get_value_or ( 0 ) ;
4444
+ const auto trimFactor = config.getTrimFactor ();
4431
4445
auto precision = config.getPrecision ().map (
4432
4446
[](auto signedInt) -> uint32_t { return static_cast <uint32_t >(signedInt); });
4433
4447
@@ -4473,20 +4487,21 @@ template <typename T>
4473
4487
class MinCoverGenerator {
4474
4488
public:
4475
4489
static std::vector<std::string> minCover (
4476
- T lowerBound, T upperBound, T max, int sparsity, int trimFactor) {
4490
+ T lowerBound, T upperBound, T max, int sparsity, const boost::optional< int >& trimFactor) {
4477
4491
MinCoverGenerator<T> mcg (lowerBound, upperBound, max, sparsity, trimFactor);
4478
4492
std::vector<std::string> c;
4479
4493
mcg.minCoverRec (c, 0 , mcg._maxlen );
4480
4494
return c;
4481
4495
}
4482
4496
4483
4497
private:
4484
- MinCoverGenerator (T lowerBound, T upperBound, T max, int sparsity, int trimFactor)
4498
+ MinCoverGenerator (
4499
+ T lowerBound, T upperBound, T max, int sparsity, const boost::optional<int >& optTrimFactor)
4485
4500
: _lowerBound(lowerBound),
4486
4501
_upperBound (upperBound),
4487
4502
_sparsity(sparsity),
4488
- _trimFactor(trimFactor ),
4489
- _maxlen(getFirstBitSet(max )) {
4503
+ _maxlen(getFirstBitSet(max) ),
4504
+ _trimFactor(resolveTrimFactorDefault(_maxlen, optTrimFactor )) {
4490
4505
static_assert (!std::numeric_limits<T>::is_signed);
4491
4506
static_assert (std::numeric_limits<T>::is_integer);
4492
4507
tassert (6860001 ,
@@ -4496,7 +4511,7 @@ class MinCoverGenerator {
4496
4511
uassert (8574106 ,
4497
4512
" Trim factor must be >= 0 and less than the number of bits used to represent an "
4498
4513
" element of the domain" ,
4499
- trimFactor >= 0 && (trimFactor == 0 || trimFactor < _maxlen));
4514
+ _trimFactor >= 0 && (_trimFactor == 0 || _trimFactor < _maxlen));
4500
4515
}
4501
4516
4502
4517
// Generate and apply a mask to an integer, filling masked bits with 1;
@@ -4560,13 +4575,17 @@ class MinCoverGenerator {
4560
4575
T _lowerBound;
4561
4576
T _upperBound;
4562
4577
int _sparsity;
4563
- int _trimFactor;
4564
4578
int _maxlen;
4579
+ int _trimFactor;
4565
4580
};
4566
4581
4567
4582
template <typename T>
4568
- std::vector<std::string> minCover (
4569
- T lowerBound, T upperBound, T min, T max, int sparsity, int trimFactor) {
4583
+ std::vector<std::string> minCover (T lowerBound,
4584
+ T upperBound,
4585
+ T min,
4586
+ T max,
4587
+ int sparsity,
4588
+ const boost::optional<int >& trimFactor) {
4570
4589
dassert (0 == min);
4571
4590
return MinCoverGenerator<T>::minCover (lowerBound, upperBound, max, sparsity, trimFactor);
4572
4591
}
@@ -4604,7 +4623,7 @@ std::vector<std::string> minCoverInt32(int32_t lowerBound,
4604
4623
boost::optional<int32_t > min,
4605
4624
boost::optional<int32_t > max,
4606
4625
int sparsity,
4607
- int trimFactor) {
4626
+ const boost::optional< int >& trimFactor) {
4608
4627
auto a = getTypeInfo32 (lowerBound, min, max);
4609
4628
auto b = getTypeInfo32 (upperBound, min, max);
4610
4629
dassert (a.min == b.min );
@@ -4623,7 +4642,7 @@ std::vector<std::string> minCoverInt64(int64_t lowerBound,
4623
4642
boost::optional<int64_t > min,
4624
4643
boost::optional<int64_t > max,
4625
4644
int sparsity,
4626
- int trimFactor) {
4645
+ const boost::optional< int >& trimFactor) {
4627
4646
auto a = getTypeInfo64 (lowerBound, min, max);
4628
4647
auto b = getTypeInfo64 (upperBound, min, max);
4629
4648
dassert (a.min == b.min );
@@ -4643,7 +4662,7 @@ std::vector<std::string> minCoverDouble(double lowerBound,
4643
4662
boost::optional<double > max,
4644
4663
boost::optional<uint32_t > precision,
4645
4664
int sparsity,
4646
- int trimFactor) {
4665
+ const boost::optional< int >& trimFactor) {
4647
4666
auto a = getTypeInfoDouble (lowerBound, min, max, precision);
4648
4667
auto b = getTypeInfoDouble (upperBound, min, max, precision);
4649
4668
dassert (a.min == b.min );
@@ -4662,7 +4681,7 @@ std::vector<std::string> minCoverDecimal128(Decimal128 lowerBound,
4662
4681
boost::optional<Decimal128> max,
4663
4682
boost::optional<uint32_t > precision,
4664
4683
int sparsity,
4665
- int trimFactor) {
4684
+ const boost::optional< int >& trimFactor) {
4666
4685
auto a = getTypeInfoDecimal128 (lowerBound, min, max, precision);
4667
4686
auto b = getTypeInfoDecimal128 (upperBound, min, max, precision);
4668
4687
dassert (a.min == b.min );
0 commit comments