Example #1
0
    UPVISIBLE
    sparseresult<R> sparseset_prepare(sparseset<R, N, H, E>& set, Key const& key, slist_node** bucket_ptr) {
        sparseresult<R> result;
        if (!set.num_buckets()) {
            result.record = nullptr;
            result.success = false;
            return result;
        }

        H const& hasher = set.hasher();
        E const& equals = set.equals();
        size_t const mask = set.num_buckets() - 1;
        size_t const hashcode = hasher(key);
        slist_node* const buckets = set.buckets();
        slist_node* bucket, * node;
        R* record;

        bucket = &buckets[hashcode & mask];
        *bucket_ptr = bucket;

        for (node = bucket->next; node; node = node->next) {
            record = ::up::slist_cast<R*>(node, N);
            if ((hasher(*record) == hashcode) && equals(*record, key)) {
                result.record = record;
                result.success = false;
                return result;
            }
        }

        result.record = nullptr;
        result.success = true;
        return result;
    }
Example #2
0
void installLayerStackTracer() {
  logging::installFailureWriter([](const char* data, int sz) {
    std::lock_guard<std::mutex> guard(gLayerStackTraceMtx);
    if (!gLayerStackTrace.empty()) {
      size_t curTid = -1UL;
      std::hash<std::thread::id> hasher;
      gLayerStackTrace.dump([&curTid, &hasher](std::thread::id tid,
                            bool* isForwarding,
                            const std::string& layerName) {
        if (curTid != hasher(tid)) {
          if (curTid != -1UL) {
            std::cerr << std::endl;
          }
          curTid = hasher(tid);
          std::cerr << "Thread [" << tid << "] ";
          if (isForwarding) {
            std::cerr << (*isForwarding ? "Forwarding ": "Backwarding ");
          }
        }
        std::cerr << layerName << ", ";
      }, FLAGS_layer_stack_error_only_current_thread);
      std::cerr << std::endl;
    }
    std::cerr.write(data, sz);
  });
}
Example #3
0
 size_t operator()(CrossForward const& fwd)const
 {
     hash<string> hasher;
     size_t hash1 = hasher(fwd.first);
     size_t hash2 = hasher(fwd.second);
     return hash1 ^ (hash2 << 1);
 }
Example #4
0
void
HashCreatorTester::Test()
{
    // Run basic test.
    HashCreator hasher(HashCreator::SHA256);
    AnsiString result = hasher.GenerateHash("The quick brown fox jumps over the lazy dog", "");

    if (!hasher.ValidateHash("The quick brown fox jumps over the lazy dog", result, true))
        throw 0;

    // Check that same password hashed twice yealds separate hashes.
    AnsiString test1 = hasher.GenerateHash("The quick brown fox jumps over the lazy dog", "");
    AnsiString test2 = hasher.GenerateHash("The quick brown fox jumps over the lazy dog", "");
    if (test1 == test2)
        throw 0;

    for (int i = 0; i < 250; i++)
    {
        HashCreator memoryTester(HashCreator::SHA256);

        String temp;
        temp.Format(_T("%d"), i);
        AnsiString hashableString = temp;

        hasher.GenerateHash(hashableString, "test");
    }
}
	// Given the various round parameters, this calculates the hash for a particular index value.
	// Multiple hashes of different indices will be combined to produce the overall result.
	bigint_t PoolHash(const Packet_ServerBeginRound *pParams, uint32_t index)
	{
		assert(NLIMBS==4*2);
		
		// Incorporate the existing block chain data - in a real system this is the
		// list of transactions we are signing. This is the FNV hash:
		// http://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
		hash::fnv<64> hasher;
		uint64_t chainHash=hasher((const char*)&pParams->chainData[0], pParams->chainData.size());
		
		// The value x is 8 words long (8*32 bits in total)
		// We build (MSB to LSB) as  [ chainHash ; roundSalt ; roundId ; index ]
		bigint_t x;
		wide_zero(8, x.limbs);
		wide_add(8, x.limbs, x.limbs, index);	//chosen index goes in at two low limbs
		wide_add(6, x.limbs+2, x.limbs+2, pParams->roundId);	// Round goes in at limbs 3 and 2
		wide_add(4, x.limbs+4, x.limbs+4, pParams->roundSalt);	// Salt goes in at limbs 5 and 4
		wide_add(2, x.limbs+6, x.limbs+6, chainHash);	// chainHash at limbs 7 and 6
		
		// Now step forward by the number specified by the server
		for(unsigned j=0;j<pParams->hashSteps;j++){
			PoolHashStep(x, pParams);
		}
		return x;
	}	
Example #6
0
    UPVISIBLE
    R* sparseset_erase(sparseset<R, N, H, E>& set, R* record) {
        if (!set.size || !record) {
            return nullptr;
        }

        H const& hasher = set.hasher();
        size_t const mask = set.num_buckets() - 1;
        size_t const hashcode = hasher(*record);
        slist_node* const buckets = set.buckets();
        slist_node* const record_node = ::up::slist_cast<slist_node*>(record, N);
        slist_node* prev, * curr;

        for (prev = &buckets[hashcode & mask], curr = prev->next; curr; curr = curr->next) {
            if (curr == record_node) {
                prev->next = curr->next;
                curr->next = nullptr;
                --set.size;
                return record;
            }
            else {
                prev = curr;
            }
        }

        return nullptr;
    }
Example #7
0
    UPVISIBLE
    bool sparseset_validate(sparseset<R, N, H, E> const& set) noexcept {
        H const& hasher = set.hasher();
        slist_node const* const buckets = set.buckets();
        size_t const num_buckets = set.num_buckets();
        size_t const size = set.size;
        size_t const mask = num_buckets - 1;
        size_t index, count;
        slist_node const* node;
        R const* record;

        if ( (num_buckets && !buckets)
            || (!num_buckets && buckets)
            || (!num_buckets && (size > 0))
            || ((num_buckets & (num_buckets - 1)) != 0)
        ) {
            return false;
        }

        for (index = 0, count = 0; index < num_buckets; ++index) {
            for (node = buckets[index].next; node; ++count, node = node->next) {
                record = ::up::slist_cast<R const*>(node, N);
                if ((hasher(*record) & mask) != index) {
                    return false;
                }
            }
        }

        if (count != size) {
            return false;
        }

        return true;
    }
Example #8
0
// add a new string to the hashtable
void hashtable_add(hashtable_t *hashtable, const char *s) 
{
	int index = hasher(s, hashtable->size);
	pthread_mutex_lock(&hashtable->hashlock);//lock
	list_add(hashtable->table[index],s);
	pthread_mutex_unlock(&hashtable->hashlock);//unlock
}
Example #9
0
const CBigNum CommitmentProofOfKnowledge::calculateChallenge(const CBigNum& a, const CBigNum& b, const CBigNum &commitOne, const CBigNum &commitTwo) const {
	CHashWriter hasher(0,0);

	// Hash together the following elements:
	// * A string identifying the proof
	// * Commitment A
	// * Commitment B
	// * Ephemeral commitment T1
	// * Ephemeral commitment T2
	// * A serialized instance of the commitment A parameters
	// * A serialized instance of the commitment B parameters

	hasher << std::string(ZEROCOIN_COMMITMENT_EQUALITY_PROOF);
	hasher << commitOne;
	hasher << std::string("||");
	hasher << commitTwo;
	hasher << std::string("||");
	hasher << a;
	hasher << std::string("||");
	hasher << b;
	hasher << std::string("||");
	hasher << *(this->ap);
	hasher << std::string("||");
	hasher << *(this->bp);

	// Convert the SHA256 result into a Bignum
	// Note that if we ever change the size of the hash function we will have
	// to update COMMITMENT_EQUALITY_CHALLENGE_SIZE appropriately!
	return CBigNum(hasher.GetHash());
}
Example #10
0
void worker_thread() {
    std::hash<std::thread::id> hasher;
    long long max_lat = std::numeric_limits<long long>::lowest(), min_lat = std::numeric_limits<long long>::max(), avg_lat = 0, sum = 0;
    size_t tid = hasher(std::this_thread::get_id());
    g_log.set_thread_queue_size(65534);
    g_log.set_thread_ringbuf_size(655340);
    auto start = std::chrono::high_resolution_clock::now();
    for (int i = 1; i <= ITERATIONS; ++i) {
        auto start = std::chrono::high_resolution_clock::now();
        g_log.info("hello %s, world %d, there %d, here %d, idx: %d", "dsads", 231, 0, 0, i);
        auto finish = std::chrono::high_resolution_clock::now();
        long long lat = std::chrono::duration_cast<std::chrono::nanoseconds>(finish-start).count();
        //sum += lat;
        max_lat = std::max(max_lat, lat);
        min_lat = std::min(min_lat, lat);
        g_max_lat = std::max(max_lat, g_max_lat);
        g_min_lat = std::min(min_lat, g_min_lat);
        //avg_lat = sum / i;
        if (lat < 50000000)
            freq_map[lat]++;
        //if (i % 10000 == 0)
        //    usleep(1000);
    }
    auto finish = std::chrono::high_resolution_clock::now();
    long long lat = std::chrono::duration_cast<std::chrono::nanoseconds>(finish-start).count();
    printf("thread_id: %lu max_lat: %lldns, min_lat: %lldns, avg_lat: %lldns, latency sum %lldns\n", tid, 0ll, min_lat, (long long)lat / (long long)ITERATIONS, lat);
}
Example #11
0
unsigned
dictionary_hash<K,V>::locate(const K& key, bool evenIfRemoved) const {
  // An internal routine used by everyone.

  // call hasher(key), but make sure it fits in 31 bits:
  if(hasher == NULL) {
    cerr << "hasher == NULL\n";
    assert(false);
  }
  unsigned hashval = hasher(key) & 0x7fffffff;

  const unsigned bin = hashval % bins.size();
   
  unsigned elem_ndx = bins[bin];
  while (elem_ndx != UINT_MAX) {
    const entry &elem = all_elems[elem_ndx];

    // verify that this elem is in the right bin!
    assert(elem.key_hashval % bins.size() == bin);
      
    if (elem.key_hashval == hashval && elem.key == key) {
      // found it...unless it was removed
      if (elem.removed && !evenIfRemoved)
	elem_ndx = UINT_MAX;

      break;
    }
    else
      elem_ndx = elem.next;
  }

  return elem_ndx;
}
Example #12
0
//------------------------------------------------------------------------------
// Compress a block of data
//------------------------------------------------------------------------------
int IDBCompressInterface::compressBlock(const char* in,
	const size_t   inLen,
	unsigned char* out,
	unsigned int&  outLen) const
{
	size_t snaplen = 0;
	utils::Hasher128 hasher;

	// loose input checking.
	if (outLen < snappy::MaxCompressedLength(inLen) + HEADER_SIZE)
	{
		cerr << "got outLen = " << outLen << " for inLen = " << inLen << ", needed " <<
			(snappy::MaxCompressedLength(inLen) + HEADER_SIZE) << endl;
		return ERR_BADOUTSIZE;
	}

	//apparently this never fails?
	snappy::RawCompress(in, inLen, reinterpret_cast<char*>(&out[HEADER_SIZE]), &snaplen);

	uint8_t *signature = (uint8_t *) &out[SIG_OFFSET];
	uint32_t *checksum = (uint32_t *) &out[CHECKSUM_OFFSET];
	uint32_t *len = (uint32_t *) &out[LEN_OFFSET];
	*signature = CHUNK_MAGIC3;
	*checksum = hasher((char *) &out[HEADER_SIZE], snaplen);
	*len = snaplen;

	//cerr << "cb: " << inLen << '/' << outLen << '/' << (snappy::MaxCompressedLength(inLen) + HEADER_SIZE) <<
	//	" : " << (snaplen + HEADER_SIZE) << endl;

	outLen = snaplen + HEADER_SIZE;

	return ERR_OK;
}
Example #13
0
	/**
	 * @note This function better to be run in separate thread due to disk i/o.
	 * Unfortunately, with current startup process with late fork() this
	 * will give us nothing but pain. Maybe later. See in NetDb as example.
	 */
	void Log::Process(std::shared_ptr<LogMsg> msg)
	{
		if (!msg) return;
		std::hash<std::thread::id> hasher;
		unsigned short short_tid;
		short_tid = (short) (hasher(msg->tid) % 1000);
		switch (m_Destination) {
#ifndef _WIN32
			case eLogSyslog:
				syslog(GetSyslogPrio(msg->level), "[%03u] %s", short_tid, msg->text.c_str());
				break;
#endif
			case eLogFile:
			case eLogStream:
				if (m_LogStream)
					*m_LogStream << TimeAsString(msg->timestamp)
						<< "@" << short_tid
						<< "/" << g_LogLevelStr[msg->level]
						<< " - " << msg->text << std::endl;
				break;
			case eLogStdout:
			default:
				std::cout    << TimeAsString(msg->timestamp)
					<< "@" << short_tid
					<< "/" << LogMsgColors[msg->level] << g_LogLevelStr[msg->level] << LogMsgColors[eNumLogLevels]
					<< " - " << msg->text << std::endl;
				break;
		} // switch
	}
  Message doPut(const std::vector<std::string> &params) {
    if (params.size() < 1) {
      return Message("error", {"Incorrect params"});
    }

    std::string expectedHash = params.front();
    std::hash<User> hasher;
    std::size_t hash = 0;
    std::vector<User> users;
    for (auto it = begin(params) + 1; it != end(params);) {
      User user;
      const auto next = deserialize(it, end(params), user);
      if (next == it) {
        return Message("error", {"Invalid user data"});
      }
      hash ^= hasher(user);
      users.push_back(std::move(user));
      it = next;
    }

    if (std::to_string(hash) == expectedHash) {
      for (const auto &user : users) {
        m_store[user.username] = user;
      }
    }
    return Message("ok");
  }
Example #15
0
File: main.cpp Project: CCJY/coliru
 /// @brief Return a hash value for the provided endpoint.
 static std::size_t get_hash(const ip::udp::endpoint& endpoint)
 {
     std::ostringstream stream;
     stream << endpoint;
     std::hash<std::string> hasher;
     return hasher(stream.str());
 }
//
// Validate a slot against the contents of an open file. At most 'length' bytes
// will be read from the file.
//
bool CodeDirectory::validateSlot(FileDesc fd, size_t length, Slot slot) const
{
	MakeHash<CodeDirectory> hasher(this);
	Hashing::Byte digest[hasher->digestLength()];
	generateHash(hasher, fd, digest, length);
	return memcmp(digest, (*this)[slot], hasher->digestLength()) == 0;
}
Example #17
0
int main()
{
    List* L;
    List* Hash[101];
    for(int i=0; i<101; i++)
    {
        Hash[i]=(List*) calloc (1, sizeof(List));
        list_ctor(Hash[i]);
    }
    L=(List*) calloc (1, sizeof(List));
    list_ctor(L);
    FILE* F;
    F=fopen("1.txt","r");
    assert(F);
    while(!feof(F))
    {
        readfromfile(F,L);
    }



    hasher(L,Hash,4);



    for (int i=0; i<101; i++)printf("i=%5d  %d\n",i,Hash[i]->amount);


}
bool SerialNumberSignatureOfKnowledge::Verify(const CBigNum& coinSerialNumber, const CBigNum& valueOfCommitmentToCoin,
        const uint256 msghash) const {
	CBigNum a = params->coinCommitmentGroup.g;
	CBigNum b = params->coinCommitmentGroup.h;
	CBigNum g = params->serialNumberSoKCommitmentGroup.g;
	CBigNum h = params->serialNumberSoKCommitmentGroup.h;
	CHashWriter hasher(0,0);
	hasher << *params << valueOfCommitmentToCoin << coinSerialNumber << msghash;

	vector<CBigNum> tprime(params->zkp_iterations);
	unsigned char *hashbytes = (unsigned char*) &this->hash;

	for(uint32_t i = 0; i < params->zkp_iterations; i++) {
		int bit = i % 8;
		int byte = i / 8;
		bool challenge_bit = ((hashbytes[byte] >> bit) & 0x01);
		if(challenge_bit) {
			tprime[i] = challengeCalculation(coinSerialNumber, s_notprime[i], SeedTo1024(sprime[i].getuint256()));
		} else {
			CBigNum exp = b.pow_mod(s_notprime[i], params->serialNumberSoKCommitmentGroup.groupOrder);
			tprime[i] = ((valueOfCommitmentToCoin.pow_mod(exp, params->serialNumberSoKCommitmentGroup.modulus) % params->serialNumberSoKCommitmentGroup.modulus) *
			             (h.pow_mod(sprime[i], params->serialNumberSoKCommitmentGroup.modulus) % params->serialNumberSoKCommitmentGroup.modulus)) %
			            params->serialNumberSoKCommitmentGroup.modulus;
		}
	}
	for(uint32_t i = 0; i < params->zkp_iterations; i++) {
		hasher << tprime[i];
	}
	return hasher.GetHash() == hash;
}
Example #19
0
QByteArray Scanner::hashFile(QFile &f)
{
    uchar *map;

    QCryptographicHash hasher(QCryptographicHash::Sha1);
    hasher.reset();

    /* Try map first */
    map = f.map( 0, f.size() );
    if (NULL==map) {
        /* no mmap, read in chunks */
        uchar buffer[512];
        qint64 r;
        do {
            r = f.read((char*)buffer,sizeof(buffer));
            if (r<0){
                throw 1;
            }
            if (r<=0)
                break;
            hasher.addData( (const char*)buffer, r);
        } while (r>0);

    } else {
        hasher.addData((const char*)map,f.size());
        f.unmap(map);
    }

    return hasher.result();
}
Example #20
0
	bool Set<T,H,Eq>::Delete(T & value)
{
	AssertObj(this);
	if (!m_prgihsndBuckets)
		return false;
	H hasher;
	Eq equal;
	int nHash = hasher(&value, sizeof(T));
	int ie = (unsigned)nHash % m_cBuckets;
	int ihsnd;
	int ihsndPrev = -1;
	for (ihsnd = m_prgihsndBuckets[ie]; ihsnd != -1; ihsnd = m_prghsnd[ihsnd].GetNext())
	{
		if ((nHash == m_prghsnd[ihsnd].GetHash()) &&
			equal(&value, &m_prghsnd[ihsnd].GetValue(), sizeof(T)))
		{
			if (ihsndPrev == -1)
				m_prgihsndBuckets[ie] = m_prghsnd[ihsnd].GetNext();
			else
				m_prghsnd[ihsndPrev].PutNext(m_prghsnd[ihsnd].GetNext());

			m_prghsnd[ihsnd].~HashNode();		// Ensure member destructors are called.
			memset(&m_prghsnd[ihsnd], 0, sizeof(HashNode));
			m_prghsnd[ihsnd].PutNext(m_ihsndFirstFree);
			m_ihsndFirstFree = FreeListIdx(ihsnd);
			AssertObj(this);
			return true;
		}
		ihsndPrev = ihsnd;
	}
	return false;
}
bool HmacBlockStream::writeHashedBlock()
{
    CryptoHash hasher(CryptoHash::Sha256, true);
    hasher.setKey(getCurrentHmacKey());
    hasher.addData(Endian::sizedIntToBytes<quint64>(m_blockIndex, ByteOrder));
    hasher.addData(Endian::sizedIntToBytes<qint32>(m_buffer.size(), ByteOrder));
    hasher.addData(m_buffer);
    QByteArray hash = hasher.result();

    if (m_baseDevice->write(hash) != hash.size()) {
        m_error = true;
        setErrorString(m_baseDevice->errorString());
        return false;
    }

    if (!Endian::writeSizedInt<qint32>(m_buffer.size(), m_baseDevice, ByteOrder)) {
        m_error = true;
        setErrorString(m_baseDevice->errorString());
        return false;
    }

    if (!m_buffer.isEmpty()) {
        if (m_baseDevice->write(m_buffer) != m_buffer.size()) {
            m_error = true;
            setErrorString(m_baseDevice->errorString());
            return false;
        }

        m_buffer.clear();
    }
    ++m_blockIndex;
    return true;
}
/** Verifies that a commitment c is accumulated in accumulator a
 */
bool AccumulatorProofOfKnowledge:: Verify(const Accumulator& a, const CBigNum& valueOfCommitmentToCoin) const {
	CBigNum sg = params->accumulatorPoKCommitmentGroup.g;
	CBigNum sh = params->accumulatorPoKCommitmentGroup.h;

	CBigNum g_n = params->accumulatorQRNCommitmentGroup.g;
	CBigNum h_n = params->accumulatorQRNCommitmentGroup.h;

	//According to the proof, this hash should be of length k_prime bits.  It is currently greater than that, which should not be a problem, but we should check this.
	CHashWriter hasher(0,0);
	hasher << *params << sg << sh << g_n << h_n << valueOfCommitmentToCoin << C_e << C_u << C_r << st_1 << st_2 << st_3 << t_1 << t_2 << t_3 << t_4;

	CBigNum c = CBigNum(hasher.GetHash()); //this hash should be of length k_prime bits

	CBigNum st_1_prime = (valueOfCommitmentToCoin.pow_mod(c, params->accumulatorPoKCommitmentGroup.modulus) * sg.pow_mod(s_alpha, params->accumulatorPoKCommitmentGroup.modulus) * sh.pow_mod(s_phi, params->accumulatorPoKCommitmentGroup.modulus)) % params->accumulatorPoKCommitmentGroup.modulus;
	CBigNum st_2_prime = (sg.pow_mod(c, params->accumulatorPoKCommitmentGroup.modulus) * ((valueOfCommitmentToCoin * sg.inverse(params->accumulatorPoKCommitmentGroup.modulus)).pow_mod(s_gamma, params->accumulatorPoKCommitmentGroup.modulus)) * sh.pow_mod(s_psi, params->accumulatorPoKCommitmentGroup.modulus)) % params->accumulatorPoKCommitmentGroup.modulus;
	CBigNum st_3_prime = (sg.pow_mod(c, params->accumulatorPoKCommitmentGroup.modulus) * (sg * valueOfCommitmentToCoin).pow_mod(s_sigma, params->accumulatorPoKCommitmentGroup.modulus) * sh.pow_mod(s_xi, params->accumulatorPoKCommitmentGroup.modulus)) % params->accumulatorPoKCommitmentGroup.modulus;

	CBigNum t_1_prime = (C_r.pow_mod(c, params->accumulatorModulus) * h_n.pow_mod(s_zeta, params->accumulatorModulus) * g_n.pow_mod(s_epsilon, params->accumulatorModulus)) % params->accumulatorModulus;
	CBigNum t_2_prime = (C_e.pow_mod(c, params->accumulatorModulus) * h_n.pow_mod(s_eta, params->accumulatorModulus) * g_n.pow_mod(s_alpha, params->accumulatorModulus)) % params->accumulatorModulus;
	CBigNum t_3_prime = ((a.getValue()).pow_mod(c, params->accumulatorModulus) * C_u.pow_mod(s_alpha, params->accumulatorModulus) * ((h_n.inverse(params->accumulatorModulus)).pow_mod(s_beta, params->accumulatorModulus))) % params->accumulatorModulus;
	CBigNum t_4_prime = (C_r.pow_mod(s_alpha, params->accumulatorModulus) * ((h_n.inverse(params->accumulatorModulus)).pow_mod(s_delta, params->accumulatorModulus)) * ((g_n.inverse(params->accumulatorModulus)).pow_mod(s_beta, params->accumulatorModulus))) % params->accumulatorModulus;

	bool result_st1 = (st_1 == st_1_prime);
	bool result_st2 = (st_2 == st_2_prime);
	bool result_st3 = (st_3 == st_3_prime);

	bool result_t1 = (t_1 == t_1_prime);
	bool result_t2 = (t_2 == t_2_prime);
	bool result_t3 = (t_3 == t_3_prime);
	bool result_t4 = (t_4 == t_4_prime);

	bool result_range = ((s_alpha >= -(params->maxCoinValue * CBigNum(2).pow(params->k_prime + params->k_dprime + 1))) && (s_alpha <= (params->maxCoinValue * CBigNum(2).pow(params->k_prime + params->k_dprime + 1))));

    return result_st1 && result_st2 && result_st3 && result_t1 && result_t2 && result_t3 && result_t4 && result_range;
}
Example #23
0
void TestVector(const Hasher &h, const In &in, const Out &out) {
    Out hash;
    BOOST_CHECK(out.size() == h.OUTPUT_SIZE);
    hash.resize(out.size());
    {
        // Test that writing the whole input string at once works.
        Hasher(h).Write((unsigned char*)&in[0], in.size()).Finalize(&hash[0]);
        BOOST_CHECK(hash == out);
    }
    for (int i=0; i<32; i++) {
        // Test that writing the string broken up in random pieces works.
        Hasher hasher(h);
        size_t pos = 0;
        while (pos < in.size()) {
            size_t len = insecure_rand() % ((in.size() - pos + 1) / 2 + 1);
            hasher.Write((unsigned char*)&in[pos], len);
            pos += len;
            if (pos > 0 && pos + 2 * out.size() > in.size() && pos < in.size()) {
                // Test that writing the rest at once to a copy of a hasher works.
                Hasher(hasher).Write((unsigned char*)&in[pos], in.size() - pos).Finalize(&hash[0]);
                BOOST_CHECK(hash == out);
            }
        }
        hasher.Finalize(&hash[0]);
        BOOST_CHECK(hash == out);
    }
}
Example #24
0
// TODO: This should ignore immediates of many instructions, in order to be less sensitive. If it did,
// this could work okay.
static u32 ComputeHash(u32 start, u32 size)
{
    u32 hash=0;
    for (unsigned int i=start; i<start+size; i+=4)
        hash = hasher(hash, Memory::Read_Instruction(i));
    return hash;
}
Example #25
0
    UPVISIBLE
    R* sparseset_erase(sparseset<R, N, H, E>& set, Key const& key, Hasher const& hasher, Equals const& equals) {
        if (!set.size) {
            return nullptr;
        }

        H const& record_hasher = set.hasher();
        size_t const mask = set.num_buckets() - 1;
        size_t const hashcode = hasher(key);
        slist_node* const buckets = set.buckets();
        slist_node* prev, * curr;
        R* record;

        for (prev = &buckets[hashcode & mask], curr = prev->next; curr; curr = curr->next) {
            record = ::up::slist_cast<R*>(curr, N);
            if ((record_hasher(*record) == hashcode) && equals(*record, key)) {
                ::up::slist_unlink(prev, curr);
                --set.size;
                return record;
            }
            else {
                prev = curr;
            }
        }

        return nullptr;
    }
Example #26
0
 /** Gets the value associated with the key. returns true on success.. */
 std::pair<bool, ValueType> get(const KeyType &key) const {
   // figure out who owns the key
   size_t hashvalue = hasher(key);
   size_t owningmachine = hashvalue % rpc.dc().numprocs();
   
   std::pair<bool, ValueType> ret;
   // if I own the key, get it from the map table
   if (owningmachine == rpc.dc().procid()) {
     datalock.lock();
     typename map_type::const_iterator iter = data.find(key);    
     if (iter == data.end()) {
       ret.first = false;
     }
     else {
       ret.first = true;
       ret.second = iter->second;
     }
     datalock.unlock();
   }
   else {
     ret = rpc.remote_request(owningmachine, 
                              &caching_dht<KeyType,ValueType>::get, 
                              key);
     if (ret.first) update_cache(key, ret.second);
     else invalidate(key);
   }
   return ret;
 }
Example #27
0
//assumes write lock is held and that it is properly sized already
void VBBM::_insert(VBBMEntry& e, VBShmsegHeader *dest, int *destHash,
				  VBBMEntry *destStorage, bool loading)
{
	int hashIndex, cHashlen = sizeof(LBID_t) + sizeof(VER_t), insertIndex;
	char* cHash = (char*)alloca(cHashlen);
	utils::Hasher hasher;

	memcpy(cHash, &e.lbid, sizeof(LBID_t));
	memcpy(&cHash[sizeof(LBID_t)], &e.verID, sizeof(VER_t));
	hashIndex = hasher(cHash, cHashlen) % dest->numHashBuckets;

	insertIndex = dest->vbLWM;
	while (destStorage[insertIndex].lbid != -1) {
		insertIndex++;
#ifdef BRM_DEBUG
		if (insertIndex == dest->vbCapacity) {
			log("VBBM:_insert(): There are no empty entries. Possibly bad resize condition.",
				logging::LOG_TYPE_DEBUG);
			throw logic_error("VBBM:_insert(): There are no empty entries. Possibly bad resize condition.");
		}
#endif
	}

	if (!loading) {
		makeUndoRecord(dest, sizeof(VBShmsegHeader));
		makeUndoRecord(&destStorage[insertIndex], sizeof(VBBMEntry));
		makeUndoRecord(&destHash[hashIndex], sizeof(int));
	}

	dest->vbLWM = insertIndex;

	e.next = destHash[hashIndex];
	destStorage[insertIndex] = e;
	destHash[hashIndex] = insertIndex;
}
Example #28
0
 /** Gets the value associated with the key, reading from cache if available
     Note that the cache may be out of date. */
 std::pair<bool, ValueType> get_cached(const KeyType &key) const {
   // if this is to my current machine, just get it and don't go to cache
   size_t hashvalue = hasher(key);
   size_t owningmachine = hashvalue % rpc.dc().numprocs();
   if (owningmachine == rpc.dc().procid()) return get(key);
   
   
   reqs++;
   cachelock.lock();
   // check if it is in the cache
   typename cache_type::iterator i = cache.find(key);
   if (i == cache.end()) {
     // nope. not in cache. Call the regular get
     cachelock.unlock();
     misses++;
     return get(key);
   }
   else {
     // yup. in cache. return the value
     std::pair<bool, ValueType> ret;
     ret.first = true;
     ret.second = i->second->value;
     // shift the cache entry to the head of the LRU list
     lruage.erase(lru_list_type::s_iterator_to(*(i->second)));
     lruage.push_front(*(i->second));
     cachelock.unlock();
     return ret;
   }
 }
Example #29
0
//read lock
int VBBM::getIndex(LBID_t lbid, VER_t verID, int &prev, int &bucket) const
{
	int cHashlen = sizeof(LBID_t) + sizeof(VER_t), currentIndex;
	char* cHash = (char*)alloca(cHashlen);
	VBBMEntry *listEntry;
	utils::Hasher hasher;

	memcpy(cHash, &lbid, sizeof(LBID_t));
	memcpy(&cHash[sizeof(LBID_t)], &verID, sizeof(VER_t));
	bucket = hasher(cHash, cHashlen) % vbbm->numHashBuckets;
	prev = -1;

	if (hashBuckets[bucket] == -1)
		return -1;

	currentIndex = hashBuckets[bucket];
	while (currentIndex != -1) {
		listEntry = &storage[currentIndex];
		if (listEntry->lbid == lbid && listEntry->verID == verID)
			return currentIndex;
		prev = currentIndex;
		currentIndex = listEntry->next;
	}

	return -1;
}
Example #30
0
void File::computeChecksums(std::vector<size_t> *ret)
{
    ret->clear();

    std::ifstream stream(absolutePath.toStdString());
    if (!stream.good())
        return;

    size_t hash = 0;
    std::hash<std::string> hasher;
    std::string s;
    bool hasHash = false;
    while (std::getline(stream, s))
    {
        if (s == "")
        {
            if (hasHash)
            {
                ret->push_back(hash);
                hash = 0;
                hasHash = false;
            }
        }
        else
        {
            hash += hasher(s);
            hasHash = true;
        }
    }

    stream.close();
}