Example #1
0
 void Thread::removeOperator(const Operator* op)
 {
     typedef std::set< std::vector<Input>::iterator > IndexSet;
     
     if (op == 0)
         throw WrongArgument("Operator must not be null.");
     
     m_thread->removeOperator(op);
     
     IndexSet toBeErased;
     
     for(std::vector<Input>::iterator iter1 = m_inputSequence.begin();
         iter1 != m_inputSequence.end();
         ++iter1)
     {
         if((*iter1).op() == op)
             toBeErased.insert(iter1);
     }
     
     for(IndexSet::reverse_iterator iter2 = toBeErased.rbegin();
         iter2 != toBeErased.rend();
         ++iter2)
     {
         m_inputSequence.erase(*iter2);
     }
 }
Example #2
0
IndexSet * mailcore::RangeUnion(Range range1, Range range2)
{
    if (!RangeHasIntersection(range1, range2)) {
        IndexSet * result = IndexSet::indexSet();
        
        result->addRange(range1);
        result->addRange(range2);
        
        return result;
    }
    else {
        uint64_t left1;
        uint64_t right1;
        uint64_t left2;
        uint64_t right2;
        uint64_t resultLeft;
        uint64_t resultRight;
        
        left1 = range1.location;
        right1 = RangeRightBound(range1);
        left2 = range2.location;
        right2 = RangeRightBound(range2);
        
        resultLeft = MIN(left1, left2);
        resultRight = MAX(right1, right2);
        if (resultRight == UINT64_MAX) {
            return IndexSet::indexSetWithRange(RangeMake(resultLeft, UINT64_MAX));
        }
        else {
            return IndexSet::indexSetWithRange(RangeMake(resultLeft, resultRight - resultLeft));
        }
    }
}
Example #3
0
    static inline bool are_holes_inside(RingIterator first,
                                        RingIterator beyond,
                                        ExteriorRing const& exterior_ring,
                                        IndexSet const& rings_with_turns)
    {
        int idx = 0;
        for (RingIterator it = first; it != beyond; ++it, ++idx)
        {
            // check only rings whose index is not associated to any turn
            if ( rings_with_turns.find(idx) == rings_with_turns.end()
                 && !geometry::within(range::front(*it), exterior_ring) )
            {
                return false;
            }
        }

        // for those rings that do not have any associated turns,
        // check if they lie inside another ring
        idx = 0;
        for (RingIterator it1 = first; it1 != beyond; ++it1, ++idx)
        {
            if ( rings_with_turns.find(idx) == rings_with_turns.end() )
            {
                for (RingIterator it2 = first; it2 != beyond; ++it2)
                {
                    if ( it1 != it2
                         && geometry::within(range::front(*it1), *it2) )
                    {
                        return false;
                    }
                }
            }
        }
        return true;
    }
Example #4
0
void IndexSet::remove(const IndexSet& s) {
  if (this == &s) { clear(); return; }
  if (s.card() == 0) return;
  if (card() == 0) return;

  for (long i = s.first(); i <= s.last(); i = s.next(i)) remove(i);
  // NOTE: traversal order should not matter here
}
Example #5
0
inline
void
VectorDBase<T>::assignT(T v, const IndexSet& is)
{
    for (typename IndexSet::Iter i = is.begin(); i != is.end(); ++i) {
        start[i] = v;
    } 
}
bool isDup(const IndexSet& data, const BSONObj& key, RecordId loc) {
    const IndexSet::const_iterator it = data.find(IndexKeyEntry(key, RecordId()));
    if (it == data.end())
        return false;

    // Not a dup if the entry is for the same loc.
    return it->loc != loc;
}
Example #7
0
// Sanity-check: Check that prime-set is "valid", i.e. that it 
// contains either all the special primes or none of them
bool Ctxt::verifyPrimeSet() const
{
  IndexSet s = primeSet & context.specialPrimes; // special primes in primeSet
  if (!empty(s) && s!=context.specialPrimes) return false;

  s = primeSet / s;                              // ctxt primes in primeSet
  return (s.isInterval() && s.first()<=1 && !empty(s));
}
Example #8
0
void readContextBinary(istream& str, FHEcontext& context)
{
  assert(readEyeCatcher(str, BINIO_EYE_CONTEXT_BEGIN)==0);

  // Get the standard deviation
  context.stdev = read_raw_xdouble(str);

  long sizeOfS = read_raw_int(str);

  IndexSet s;
  for(long tmp, i=0; i<sizeOfS; i++){
    tmp = read_raw_int(str);
    s.insert(tmp);
  }

  context.moduli.clear();
  context.specialPrimes.clear();
  context.ctxtPrimes.clear();

  long nPrimes = read_raw_int(str);

  for (long p,i=0; i<nPrimes; i++) {
    p = read_raw_int(str);

    context.moduli.push_back(Cmodulus(context.zMStar,p,0));

    if (s.contains(i))
      context.specialPrimes.insert(i); // special prime
    else
      context.ctxtPrimes.insert(i);    // ciphertext prime
  }
  
  long nDigits = read_raw_int(str);

  context.digits.resize(nDigits);
  for(long i=0; i<(long)context.digits.size(); i++){
    sizeOfS = read_raw_int(str);

    for(long tmp, n=0; n<sizeOfS; n++){
      tmp = read_raw_int(str);
      context.digits[i].insert(tmp);
    }
  }

  // Read in the partition of m into co-prime factors (if bootstrappable)
  Vec<long> mv;
  read_ntl_vec_long(str, mv);

  long t = read_raw_int(str);
  bool consFlag = read_raw_int(str);  

  if (mv.length()>0) {
    context.makeBootstrappable(mv, t, consFlag);
  }

  assert(readEyeCatcher(str, BINIO_EYE_CONTEXT_END)==0);
}
Example #9
0
void IndexSet::retain(const IndexSet& s) {
  if (this == &s) return;
  if (s.card() == 0) { clear(); return; }
  if (card() == 0) return;

  for (long i = first(); i <= last(); i = next(i)) {
    if (!s.contains(i)) remove(i);
  }
}
bool isDup(const IndexSet& data, const BSONObj& key) {
    IndexSet::const_iterator it = data.find(IndexKeyEntry(key, RecordId()));
    if (it == data.end())
        return false;

    ++it;
    if (it == data.end())
        return false;

    return it->key.woCompare(key, BSONObj(), false) == 0;
}
Example #11
0
inline
void
VectorDBase<T>::assign(const VectorDBase<T>& v1, const IndexSet& is)
{
    assert(v1.get_size() == is.get_size());
    assert(get_size() == is.count());
    T* i = start;
    for (typename IndexSet::Iter i1 = is.begin(); i1 != is.end(); ++i1) {
        *i = v1[i1]; ++i;
    } 
}
Example #12
0
//! @brief How many levels in the "base-set" for that ciphertext
long Ctxt::findBaseLevel() const 
{
  IndexSet s;
  findBaseSet(s);
  if (context.containsSmallPrime()) {
    if (s.contains(context.ctxtPrimes.first()))
      return 2*card(s) -1; // 1st prime is half size
    else
      return 2*card(s);
  }
  else return card(s);     // one prime per level
}
Example #13
0
void
LocalViewSelection::replaceViews(IndexSet const & toBeReplaced)
{
    IndexSet::const_iterator tbr = toBeReplaced.begin();
    while (tbr != toBeReplaced.end()) {
        available[*tbr] = false;
        selected.erase(*tbr);
        ++tbr;
    }
    success = false;
    performVS();
}
Example #14
0
// Find the IndexSet such that modDown to that set of primes makes the
// additive term due to rounding into the dominant noise term 
void Ctxt::findBaseSet(IndexSet& s) const
{
  if (getNoiseVar()<=0.0) { // an empty ciphertext
    s = context.ctxtPrimes;
    return;
  }

  assert(verifyPrimeSet());
  bool halfSize = context.containsSmallPrime();
  double curNoise = log(getNoiseVar())/2;
  double firstNoise = context.logOfPrime(0);
  double noiseThreshold = log(modSwitchAddedNoiseVar())*0.55;
  // FIXME: The above should have been 0.5. Making it a bit more means
  // that we will mod-switch a little less frequently, whether this is
  // a good thing needs to be tested.

  // remove special primes, if they are included in this->primeSet
  s = getPrimeSet();
  if (!s.disjointFrom(context.specialPrimes)) { 
    // scale down noise
    curNoise -= context.logOfProduct(context.specialPrimes);
    s.remove(context.specialPrimes);
  }

  /* We compare below to noiseThreshold+1 rather than to noiseThreshold
   * to make sure that if you mod-switch down to c.findBaseSet() and
   * then immediately call c.findBaseSet() again, it will not tell you
   * to mod-switch further down. Note that mod-switching adds close to
   * noiseThreshold to the scaled noise, so if the scaled noise was
   * equal to noiseThreshold then after mod-switchign you would have
   * roughly twice as much noise. Since we're mesuring the log, it means
   * that you may have as much as noiseThreshold+log(2), which we round
   * up to noiseThreshold+1 in the test below.
   */
  if (curNoise<=noiseThreshold+1) return; // no need to mod down

  // if the first prime in half size, begin by removing it
  if (halfSize && s.contains(0)) {
    curNoise -= firstNoise;
    s.remove(0);
  }

  // while noise is larger than threshold, scale down by the next prime
  while (curNoise>noiseThreshold && !empty(s)) {
    curNoise -= context.logOfPrime(s.last());
    s.remove(s.last());
  }

  // Add 1st prime if s is empty or if this does not increase noise too much
  if (empty(s) || (!s.contains(0) && curNoise+firstNoise<=noiseThreshold)) {
    s.insert(0);
    curNoise += firstNoise;
  }

  if (curNoise>noiseThreshold && log_of_ratio()>-0.5)
    cerr << "Ctxt::findBaseSet warning: already at lowest level\n";
}
Example #15
0
//------------------------------add_liveout------------------------------------
// Add a live-out value to a given blocks live-out set.  If it is new, then
// also add it to the delta set and stick the block on the worklist.
void PhaseLive::add_liveout( Block *p, uint r, VectorSet &first_pass ) {
  IndexSet *live = &_live[p->_pre_order-1];
  if( live->insert(r) ) {       // If actually inserted...
    // We extended the live-out set.  See if the value is generated locally.
    // If it is not, then we must extend the live-in set.
    if( !_defs[p->_pre_order-1].member( r ) ) {
      if( !_deltas[p->_pre_order-1] && // Not on worklist?
          first_pass.test(p->_pre_order) )
        _worklist->push(p);     // Actually go on worklist if already 1st pass
      getset(p)->insert(r);  
    }
  }
}
Example #16
0
//------------------------------Union------------------------------------------
// Union edges of B into A
void PhaseIFG::Union( uint a, uint b ) {
  assert( _is_square, "only on square" );
  IndexSet *A = &_adjs[a];
  IndexSetIterator b_elements(&_adjs[b]);
  uint datum;
  while ((datum = b_elements.next()) != 0) {
    if(A->insert(datum)) {
      _adjs[datum].insert(a);
      lrgs(a).invalid_degree();
      lrgs(datum).invalid_degree();
    }
  }
}
Example #17
0
void IndexSet::insert(const IndexSet& s) {
  if (this == &s) return;
  if (s.card() == 0) return;
  if (card() == 0) {
    *this = s;
    return;
  }

  for (long i = s.last(); i >= s.first(); i = s.prev(i)) insert(i);
  // NOTE: traversal done from high to low so as to trigger at 
  // at most one resize

}
Example #18
0
DoubleCRT::DoubleCRT(const ZZX& poly, const FHEcontext &_context, const IndexSet& s)
: context(_context), map(new DoubleCRTHelper(_context))
{
  assert(s.last() < context.numPrimes());

  map.insert(s);
  if (dryRun) return;

  // convert the integer polynomial to FFT representation modulo the primes
  for (long i = s.first(); i <= s.last(); i = s.next(i)) {
    const Cmodulus &pi = context.ithModulus(i);
    pi.FFT(map[i], poly); // reduce mod pi and store FFT image
  }
}
Example #19
0
DoubleCRT::DoubleCRT(const FHEcontext &_context, const IndexSet& s)
: context(_context), map(new DoubleCRTHelper(_context))
{
  assert(s.last() < context.numPrimes());

  map.insert(s);
  if (dryRun) return;

  long phim = context.zMStar.getPhiM();

  for (long i = s.first(); i <= s.last(); i = s.next(i)) {
    vec_long& row = map[i];
    for (long j = 0; j < phim; j++) row[j] = 0;
  }
}
Example #20
0
DoubleCRT::DoubleCRT(const ZZX& poly)
: context(*activeContext), map(new DoubleCRTHelper(*activeContext))
{
  IndexSet s = IndexSet(0, context.numPrimes()-1);
  // FIXME: maybe the default index set should be determined by context?

  map.insert(s);
  if (dryRun) return;

  // convert the integer polynomial to FFT representation modulo the primes
  for (long i = s.first(); i <= s.last(); i = s.next(i)) {
    const Cmodulus &pi = context.ithModulus(i);
    pi.FFT(map[i], poly); // reduce mod pi and store FFT image
  }
}
Example #21
0
//------------------------------getfreeset-------------------------------------
// Pull from free list, or allocate.  Internal allocation on the returned set
// is always from thread local storage.
IndexSet *PhaseLive::getfreeset( ) {
  IndexSet *f = _free_IndexSet;
  if( !f ) {
    f = new IndexSet;
//    f->set_arena(Thread::current()->resource_area());
    f->initialize(_maxlrg, Thread::current()->resource_area());
  } else {
    // Pull from free list
    _free_IndexSet = f->next();
  //f->_cnt = 0;                        // Reset to empty
//    f->set_arena(Thread::current()->resource_area());
    f->initialize(_maxlrg, Thread::current()->resource_area());
  }
  return f;
}
Example #22
0
inline
void
FullTree<IndexSet>::insert(const IndexSet& support, int index)
{
    IndexSet empty(support.get_size(),0);
    root.insert(support, empty, index);
}
Example #23
0
DoubleCRT::DoubleCRT(const FHEcontext &_context)
: context(_context), map(new DoubleCRTHelper(_context))
{
  IndexSet s = IndexSet(0, context.numPrimes()-1);
  // FIXME: maybe the default index set should be determined by context?

  map.insert(s);
  if (dryRun) return;

  long phim = context.zMStar.getPhiM();

  for (long i = s.first(); i <= s.last(); i = s.next(i)) {
    vec_long& row = map[i];
    for (long j = 0; j < phim; j++) row[j] = 0;
  }
}
Example #24
0
inline
void
BinaryTree<IndexSet>::insert(const IndexSet& support, Index index)
{
    supp_size = support.get_size();
    root = root->insert(support, index);
}
Example #25
0
// expand index set by s1.
// it is assumed that s1 is disjoint from the current index set.
void DoubleCRT::addPrimes(const IndexSet& s1)
{
  if (empty(s1)) return; // nothing to do
  assert( disjoint(s1,map.getIndexSet()) ); // s1 is disjoint from *this

  ZZX poly;
  toPoly(poly); // recover in coefficient representation

  map.insert(s1);  // add new rows to the map
  if (dryRun) return;

  // fill in new rows
  for (long i = s1.first(); i <= s1.last(); i = s1.next(i)) {
    context.ithModulus(i).FFT(map[i], poly); // reduce mod p_i and store FFT image
  }
}
void
readIndexSetFromFile(IndexSet<Index2D> &Lambda, string filename)
{
    std::ifstream infile (filename.c_str());
    if (infile.is_open()) {
        cerr << "   Indexset file is open." << endl;
    }
    else {
        cerr << "   Indexset file " << filename.c_str()  << " is not open." << endl;
    }

	int t1,t2;
    int j1,j2;
    long k1,k2;
    T coeff;

    while(!infile.eof()) {

    	infile >> t1 >> j1 >> k1 >> t2 >> j2 >> k2 >> coeff;

        if (t1 == 1 && t2 == 1) {
            Index1D index_x(j1,k1,XWavelet);
            Index1D index_y(j2,k2,XWavelet);
            Lambda.insert(Index2D(index_x,index_y));
        }
        else if (t1 == 1 && t2 == 0) {
            Index1D index_x(j1,k1,XWavelet);
            Index1D index_y(j2,k2,XBSpline);
            Lambda.insert(Index2D(index_x,index_y));
        }
        else if (t1 == 0 && t2 == 1) {
            Index1D index_x(j1,k1,XBSpline);
            Index1D index_y(j2,k2,XWavelet);
            Lambda.insert(Index2D(index_x,index_y));
        }
        else if (t1 == 0 && t2 == 0) {
            Index1D index_x(j1,k1,XBSpline);
            Index1D index_y(j2,k2,XBSpline);
            Lambda.insert(Index2D(index_x,index_y));
        }
        else {
            std::cerr << "Could not read file." << std::endl;
            exit(1); return;
        }
    }
}
Example #27
0
SingleCRT::SingleCRT(const ZZX&poly, const FHEcontext& _context, const IndexSet& s)
: context(_context)
{
  assert(s.last() < context.numPrimes());

  map.insert(s);
  *this = poly;           // convert polynomial to singleCRT representation
}
Example #28
0
// Without specifying a ZZX, we get the zero polynomial
SingleCRT::SingleCRT(const FHEcontext &_context, const IndexSet& s)
: context(_context)
{
  assert(s.last() < context.numPrimes());

  map.insert(s);
  // default constructor for ZZX creates the zero polynomial
}
Example #29
0
File: Ctxt.cpp Project: 2080/HElib
NTL_CLIENT

#include "FHEContext.h"
#include "Ctxt.h"
#include "FHE.h"
#include "timing.h"


// Sanity-check: Check that prime-set is "valid", i.e. that it 
// contains either all the special primes or none of them
bool Ctxt::verifyPrimeSet() const
{
  IndexSet s = primeSet & context.specialPrimes; // special primes in primeSet
  if (!empty(s) && s!=context.specialPrimes) return false;

  s = primeSet / s;                              // ctxt primes in primeSet
  return (s.isInterval() && s.first()<=1 && !empty(s));
}
Example #30
0
void Procedure::deleteOrphans()
{
    IndexSet<Value> valuesInBlocks;
    for (BasicBlock* block : *this)
        valuesInBlocks.addAll(*block);

    // Since this method is not on any hot path, we do it conservatively: first a pass to
    // identify the values to be removed, and then a second pass to remove them. This avoids any
    // risk of the value iteration being broken by removals.
    Vector<Value*, 16> toRemove;
    for (Value* value : values()) {
        if (!valuesInBlocks.contains(value))
            toRemove.append(value);
    }

    for (Value* value : toRemove)
        deleteValue(value);
}