Beispiel #1
0
  void findThreshold(const std::vector<Sample*>& data,
      const std::vector<IntIndex>& valSet,
      Split& split, boost::mt19937* rng_) const {
    split.gain = boost::numeric::bounds<double>::lowest();
    split.info = boost::numeric::bounds<double>::lowest();

    int min_Val = valSet.front().first;
    int max_val = valSet.back().first;
    int valueRange = max_val - min_Val;

    if (valueRange > 0) {
//            double info = Sample::entropie( data, weightClasses, split_mode);

      int nThreshlds = split.num_thresholds;
      bool use_margin = false;
      if (use_margin)
        nThreshlds = 20;

      // Find best threshold
      boost::uniform_int<> dist_tr(0, valueRange);
      boost::variate_generator<boost::mt19937&, boost::uniform_int<> > rand_tr(*rng_, dist_tr);

      int m = std::min(abs(min_Val), abs(max_val));
      if (m <= 0)
        m = 1;
      boost::uniform_int<> dist_margin(0, m);
      boost::variate_generator<boost::mt19937&, boost::uniform_int<> > rand_margin(*rng, dist_margin);
      for (int j = 0; j < nThreshlds; ++j) {
        // Generate some random thresholds
        int tr = rand_tr() + min_Val;
        int margin = 0;

        std::vector<std::vector<Sample*> > sets;
        if (use_margin)
          margin = rand_margin();

        splitVec(data, valSet, sets, tr, margin);

        unsigned int min = 2;
        if (sets[0].size() < min or sets[1].size() < min)
          continue;

        double infoNew = Sample::evalSplit(sets[0], sets[1], weightClasses, split_mode, depth);

        if (infoNew > split.info) {
          split.threshold = tr;
          split.info = infoNew;
          split.gain = infoNew;
          split.margin = margin;
        }
      }
    }
  }
Beispiel #2
0
void el_gamal()
{
    std::ifstream msg("example", std::ios::binary);

    if (!msg) {
        std::cerr << "File not found" << std::endl;
        exit(1);
    }


    llong p{0}, d{0};
    srand(time(0));
    bool isprime = false;
    // Generation a prime number
    while (!isprime) {
        isprime = true;
        p = rand() % 30000 + 5000;
        // Check for prime
        for (size_t i = 2; i <= static_cast<size_t>(sqrt(p)); ++i) {
            if (p % i == 0) {
                isprime = false;
                break;
            }
        }
        // if m is prime, check d
        if (isprime) {
            d = (p - 1) / 2;
            for (size_t i = 2; i <= static_cast<size_t>(sqrt(d)); ++i) {
                if (d % i == 0) {
                    isprime = false;
                    break;
                }
            }
        }
    }

    llong g = 2;
    while (pow_module(g, d, p) == 1) {
        ++g;
    }

    std::default_random_engine gen_cl_key;
    std::uniform_int_distribution<llong> random_cl_key(2, p - 2);
    // Closed keys
    llong Cb = random_cl_key(gen_cl_key);
    // Opened keys
    llong Db = pow_module(g, Cb, p);

    std::default_random_engine gen_tr;
    std::uniform_int_distribution<llong> rand_tr(1, p - 2);

    std::ofstream out("el_gamal_encode", std::ios::binary | std::ios::out);

    char buf;
    // Encode
    while (msg.read(&buf, sizeof(char))) {
        llong k = rand_tr(gen_tr);
        // Send message to B from A
        llong r = pow_module(g, k, p);
        llong e = (buf * (pow_module(Db, k, p))) % p;

        out.write((char*)&e, sizeof(llong));
        out.write((char*)&r, sizeof(llong));
    }
    msg.close();
    out.close();

    // Decode
    llong e{0}, r{0};

    std::ofstream decmsg("el_gamal_decode", std::ios::binary | std::ios::out);
    std::ifstream tmp("el_gamal_encode", std::ios::binary | std::ios::in);
    while (tmp.read((char*)&e, sizeof(llong))) {
        tmp.read((char*)&r, sizeof(llong));
        unsigned short mrecd = (e * (pow_module(r, p - 1 - Cb, p))) % p;
        decmsg.write((char*)&mrecd, sizeof(char));
    }
    decmsg.close();
    tmp.close();
}