<commit_before>// Copyright 2020 The Any Sketch Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "sketch_encrypter.h"

// TODO(wangyaopw): use "external/*" path for external dependencies
#include "absl/container/flat_hash_map.h"
#include "crypto/commutative_elgamal.h"
#include "crypto/context.h"
#include "crypto/ec_group.h"
#include "src/main/cc/any_sketch/util/macros.h"
#include "util/status_macros.h"
#include "wfa/any_sketch/crypto/sketch_encryption_methods.pb.h"

namespace wfa::any_sketch::crypto {

namespace {
using ::private_join_and_compute::BigNum;
using ::private_join_and_compute::CommutativeElGamal;
using ::private_join_and_compute::Context;
using ::private_join_and_compute::ECGroup;
using ::private_join_and_compute::ECPoint;
using ::wfa::measurement::api::v1alpha::Sketch;
using ::wfa::measurement::api::v1alpha::SketchConfig;
using DestroyedRegisterStrategy =
    ::wfa::any_sketch::crypto::EncryptSketchRequest::DestroyedRegisterStrategy;
using BlindersCiphertext = std::pair<std::string, std::string>;
// Each Compression of ECPoint has size 33-bytes (32 bytes for x, 1 byte for the
// sign of y). An ElGamal ciphertext contains two ECPoints, i.e., u and e.
constexpr int kBytesPerCipherText = 66;
constexpr absl::string_view KUnitECPointSeed = "unit_ec_point";
constexpr absl::string_view KDestroyedRegisterKey = "destroyed_register_key";

// Check if the sketch is valid or not.
// A Sketch is valid if and only if all its registers contain the same number
// of indexes and same number of values as the SketchConfig specifies.
bool ValidateSketch(const Sketch& sketch) {
  const int values_size = sketch.config().values_size();
  for (int i = 0; i < sketch.registers_size(); i++) {
    const Sketch::Register& reg = sketch.registers(i);
    if (values_size != reg.values_size()) {
      return false;
    }
  }
  return true;
}

// Check if a register is destroyed, i.e., if any UNIQUE value is equal to -1
bool IsRegisterDestroyed(const Sketch::Register& reg,
                         const SketchConfig& sketch_config) {
  for (int i = 0; i < reg.values_size(); i++) {
    if (sketch_config.values(i).aggregator() ==
            SketchConfig::ValueSpec::UNIQUE &&
        reg.values(i) <= 0)
      return true;
  }
  return false;
}<commit_msg>Update a comment in the any-sketch lib.<commit_after>// Copyright 2020 The Any Sketch Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "sketch_encrypter.h"

// TODO(wangyaopw): use "external/*" path for external dependencies
#include "absl/container/flat_hash_map.h"
#include "crypto/commutative_elgamal.h"
#include "crypto/context.h"
#include "crypto/ec_group.h"
#include "src/main/cc/any_sketch/util/macros.h"
#include "util/status_macros.h"
#include "wfa/any_sketch/crypto/sketch_encryption_methods.pb.h"

namespace wfa::any_sketch::crypto {

namespace {
using ::private_join_and_compute::BigNum;
using ::private_join_and_compute::CommutativeElGamal;
using ::private_join_and_compute::Context;
using ::private_join_and_compute::ECGroup;
using ::private_join_and_compute::ECPoint;
using ::wfa::measurement::api::v1alpha::Sketch;
using ::wfa::measurement::api::v1alpha::SketchConfig;
using DestroyedRegisterStrategy =
    ::wfa::any_sketch::crypto::EncryptSketchRequest::DestroyedRegisterStrategy;
using BlindersCiphertext = std::pair<std::string, std::string>;
// Each Compression of ECPoint has size 33-bytes (32 bytes for x, 1 byte for the
// sign of y). An ElGamal ciphertext contains two ECPoints, i.e., u and e.
constexpr int kBytesPerCipherText = 66;
constexpr absl::string_view KUnitECPointSeed = "unit_ec_point";
constexpr absl::string_view KDestroyedRegisterKey = "destroyed_register_key";

// Check if the sketch is valid or not.
// A Sketch is valid if and only if all its registers contain the same number
// of indexes and same number of values as the SketchConfig specifies.
bool ValidateSketch(const Sketch& sketch) {
  const int values_size = sketch.config().values_size();
  for (int i = 0; i < sketch.registers_size(); i++) {
    const Sketch::Register& reg = sketch.registers(i);
    if (values_size != reg.values_size()) {
      return false;
    }
  }
  return true;
}

// Check if a register is destroyed, i.e., if any UNIQUE value is equal to 0.
// Note that we use -1 as the key of destroyed registers. However, we shift the
// value by 1 when encoding the anysketch object to proto message. In other
// words, destroyed registers in the proto message would have key value 0.
// Here We check <= 0 for flexibility.
bool IsRegisterDestroyed(const Sketch::Register& reg,
                         const SketchConfig& sketch_config) {
  for (int i = 0; i < reg.values_size(); i++) {
    if (sketch_config.values(i).aggregator() ==
            SketchConfig::ValueSpec::UNIQUE &&
        reg.values(i) <= 0)
      return true;
  }
  return false;
}
{'fraction_non_alphanumeric': 0.1110922659430122, 'fraction_numerical': 0.007801899592944369, 'mean_word_length': 4.375569735642662, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4885791', 'n_tokens_mistral': 1850, 'n_tokens_neox': 1796, 'n_words': 592}