Example #1
0
size_t
Any::complexity(bir mults) const
{
    const auto bits = bitsize().get();
    if (bitsize() < bits) {
        return static_cast<size_t>(1) << bits;
    } else {
        return pnw::maxof<size_t>();
    }
}
Example #2
0
Values new_public(const uint32_t & L, const uint32_t & N){
//    L = 1024, N = 160
//    L = 2048, N = 224
//    L = 2048, N = 256
//    L = 3072, N = 256
    RNG::BBS(static_cast <MPI> (static_cast <unsigned int> (now()))); // seed just in case not seeded

    // random prime q
    MPI q = bintompi("1" + RNG::BBS().rand(N - 1));
    q = nextprime(q);
    while (bitsize(q) > N){
        q = bintompi("1" + RNG::BBS().rand(N - 1));
        q = nextprime(q);
    }

    // random prime p = kq + 1
    MPI p = bintompi("1" + RNG::BBS().rand(L - 1));                   // pick random starting point
    p = ((p - 1) / q) * q + 1;                                        // set starting point to value such that p = kq + 1 for some k, while maintaining bitsize
    while (!knuth_prime_test(p, 25)){
        p += q;
    }

    // generator g with order q
    MPI g = 1, h = 1;
    MPI exp = (p - 1) / q;
    while (g == 1){
        h++;
        g = powm(h, exp, p);
    }

    return {p, q, g};
}
Example #3
0
Values keygen(Values & pub){
    RNG::BBS(static_cast <MPI> (static_cast <unsigned int> (now()))); // seed just in case not seeded

    MPI x = 0;
    std::string test = "testing testing 123"; // a string to test the key with, just in case the key doesn't work for some reason
    unsigned int bits = bitsize(pub[1]) - 1;
    while (true){
        // 0 < x < q
        while ((x == 0) || (pub[1] <= x)){
            x = bintompi(RNG::BBS().rand(bits));
        }

        // y = g^x mod p
        MPI y;
        y = powm(pub[2], x, pub[0]);

        // public key = p, q, g, y
        // private key = x
        pub.push_back(y);

        // check that this key works
        Values rs = sign(test, {x}, pub);

        // if it works, break
        if (verify(test, rs, pub)){
            break;
        }

        pub.pop_back();
    }

    return {x};
}
Example #4
0
    void increment(uint64_t i, uint64_t delta, bool subtract = false){

        assert(i<size_);

        //value at position i
        auto pvi = at(i);

        if(subtract){

            assert(pvi>=delta);
            set(i,pvi-delta);

        }else{

            uint64_t s = pvi+delta;

            if(bitsize(s)>width_){

                //in this case rebuild the whole vector with a new bitsize
                rebuild_set( i, s );

            }else{

                //just increment

                set(i,s);

            }

        }

    }
Example #5
0
    void insert(uint64_t i, uint64_t x){

        if(bitsize(x)>width_){

            //auto vec = to_vector(i,x);
            //rebuild(vec);
            rebuild_ins( i, x );

            return;

        }

        //not enough space for the new element:
        //alloc extra_ new words
        if(size_+1>(words.size()*(int_per_word_))){

            //resize words
            auto temp = vector<uint64_t>(words.size()+extra_,0);

            uint64_t j = 0;
            for(auto x:words) temp[j++] = x;

            words = vector<uint64_t>(temp);

        }

        //shift right elements starting from number i
        shift_right(i);

        //insert x
        set(i,x);

        ++size_;

    }
Example #6
0
Values sign(const MPI & data, const Values & pri, const Values & pub, MPI k){
    RNG::BBS(static_cast <MPI> (static_cast <unsigned int> (now()))); // seed just in case not seeded

    bool set_k = (k == 0);

    MPI r = 0, s = 0;
    while ((r == 0) || (s == 0)){
        // 0 < k < q
        if ( set_k ) {
            k = bintompi(RNG::BBS().rand(bitsize(pub[1])));
            k %= pub[1];
        }

        // r = (g^k mod p) mod q
        r = powm(pub[2], k, pub[0]);
        r %= pub[1];

        // if r == 0, don't bother calculating s
        if (r == 0){
            continue;
        }

        // s = k^-1 (m + x * r) mod q
        s = invert(k, pub[1]);
        s *= data + pri[0] * r;
        s %= pub[1];
    }

    return {r, s};
}
Example #7
0
Values encrypt(const MPI & data, const Values & pub){
    RNG::BBS(static_cast <MPI> (static_cast <unsigned int> (now()))); // seed just in case not seeded
    MPI k = bintompi(RNG::BBS().rand(bitsize(pub[0])));
    k %= pub[0];
    MPI r, s;
    r = powm(pub[1], k, pub[0]);
    s = powm(pub[2], k, pub[0]);
    return {r, (data * s) % pub[0]};
}
 Offsets(uint_t _key_len, uint_t _val_len, uint_t _reprobe_limit) :
   key_len(_key_len),
   val_len(_val_len),
   reprobe_limit(_reprobe_limit),
   reprobe_len(bitsize(reprobe_limit)),
   lval_len(key_len + val_len - reprobe_len),
   block(compute_offsets()),
   bld(block.len)
 { }
Example #9
0
    void init(uint_t _key_len, uint_t _val_len, uint_t _reprobe_limit) {
      key_len       = _key_len;
      val_len       = _val_len;
      reprobe_limit = _reprobe_limit;
      reprobe_len   = bitsize(_reprobe_limit);
      lval_len      = key_len + val_len - reprobe_len;

      compute_offsets();
      bld           = divisor64(block_len);
    }
Example #10
0
      void remove(uint64_t i) {
	 auto x = this->at(i);

	 if (width_>1) { // otherwise, cannot rebuild
	    if (bitsize(x) == width_) {

	       uint8_t max_b = 0;

	       for(ulint j = 0; j < size_ ;++j){
		  if (j != i) {
		     auto x = this->at(j);

		     uint8_t bs = bitsize(x);

		     if(bs>max_b) max_b=bs;
		  }
	       
	       }

	       //rebuild entire vector
	       rebuild_rem( i, max_b );

	       return;

	    }
	 }

	 //shift ints left, from position i + 1 onwords
	 shift_left( i );

	 
	 while ( (words.size() - extra_ - 1)*(int_per_word_) >= size_ - 1 ) {
	    //more than extra_ extra words, delete 
	    words.pop_back();
	    if (words.size() == 0)
	       break;
	 }

	 --size_;
	 psum_ -= x;

      }
Example #11
0
END_TEST

START_TEST(test_alphabet)
{
	int side;
	unsigned char bitdata[QR_MAX_BITDATA];

	memset(bitdata, sizeof(bitdata), 0);
	side = qr_encode(QR_LEVEL_L, 0, "test", 0, bitdata);
	ck_assert_int_eq(side, 21);
	ck_assert_int_eq(memcmp(bitdata, RESULT_ALPHA_1, bitsize(side)), 0);

	memset(bitdata, sizeof(bitdata), 0);
	side = qr_encode(QR_LEVEL_M, 0, "test", 0, bitdata);
	ck_assert_int_eq(side, 21);
	ck_assert_int_eq(memcmp(bitdata, RESULT_ALPHA_2, bitsize(side)), 0);

	memset(bitdata, sizeof(bitdata), 0);
	side = qr_encode(QR_LEVEL_Q, 0, "test", 0, bitdata);
	ck_assert_int_eq(side, 21);
	ck_assert_int_eq(memcmp(bitdata, RESULT_ALPHA_3, bitsize(side)), 0);

	memset(bitdata, sizeof(bitdata), 0);
	side = qr_encode(QR_LEVEL_H, 0, "test", 0, bitdata);
	ck_assert_int_eq(side, 21);
	ck_assert_int_eq(memcmp(bitdata, RESULT_ALPHA_4, bitsize(side)), 0);

#if QR_MAX_VERSION >= QR_VERSION_M
	const char *lipsum = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras at risus sodales, rhoncus arcu eu, accumsan augue. Mauris a mauris et ante porta eleifend. Sed condimentum metus vitae tortor bibendum, et scelerisque quam placerat. Nam at sapien lacus. Proin magna ipsum, dapibus non dignissim a, posuere vitae nulla. Donec pretium odio sit amet lorem interdum, nec ullamcorper diam iaculis. Mauris at est sit amet purus venenatis pretium vitae sed magna.";

	memset(bitdata, sizeof(bitdata), 0);
	side = qr_encode(QR_LEVEL_L, 0, lipsum, 0, bitdata);
	ck_assert_int_eq(side, 73);
	ck_assert_int_eq(memcmp(bitdata, RESULT_ALPHA_5, bitsize(side)), 0);

	memset(bitdata, sizeof(bitdata), 0);
	side = qr_encode(QR_LEVEL_M, 0, lipsum, 0, bitdata);
	ck_assert_int_eq(side, 85);
	ck_assert_int_eq(memcmp(bitdata, RESULT_ALPHA_6, bitsize(side)), 0);

	memset(bitdata, sizeof(bitdata), 0);
	side = qr_encode(QR_LEVEL_Q, 0, lipsum, 0, bitdata);
	ck_assert_int_eq(side, 97);
	ck_assert_int_eq(memcmp(bitdata, RESULT_ALPHA_7, bitsize(side)), 0);

	memset(bitdata, sizeof(bitdata), 0);
	side = qr_encode(QR_LEVEL_H, 0, lipsum, 0, bitdata);
	ck_assert_int_eq(side, 109);
	ck_assert_int_eq(memcmp(bitdata, RESULT_ALPHA_8, bitsize(side)), 0);
#endif
}
 // Assume _size is already a power of 2
 // map must point to a memory area written by "write_blocks". No header
 array(char *map, size_t _size, uint_t _key_len, uint_t _val_len,
       uint_t _reprobe_limit, size_t *_reprobes,
       SquareBinaryMatrix &_hash_matrix, 
       SquareBinaryMatrix &_hash_inverse_matrix) :
   lsize(ceilLog2(_size)), size(_size), size_mask(size-1),
   reprobe_limit(_reprobe_limit, _reprobes, size), key_len(_key_len),
   key_mask(key_len <= lsize ? 0 : (((word)1) << (key_len - lsize)) - 1),
   key_off(key_len <= lsize ? 0 : key_len - lsize),
   offsets(key_off + bitsize(reprobe_limit.val() + 1), _val_len,
           reprobe_limit.val() + 1),
   data((word *)map), reprobes(_reprobes),
   hash_matrix(_hash_matrix), hash_inverse_matrix(_hash_inverse_matrix)
 { }
Example #13
0
    void set(uint64_t i, uint64_t x, vector< uint64_t >& new_words, uint8_t& new_int_per_word_, uint8_t& new_width_, uint64_t& new_MASK ){

        assert(bitsize(x)<=new_width_);

        uint64_t word_nr = i/new_int_per_word_;
        uint8_t pos = i%new_int_per_word_;

        //set to 0 i-th entry
        uint64_t MASK1 = ~(new_MASK<<(new_width_*pos));
        new_words[word_nr] &= MASK1;

        //insert x inside i-th position
        new_words[word_nr] |= (x<<(new_width_*pos));

    }
Example #14
0
    void set(uint64_t i, uint64_t x){

        assert(bitsize(x)<=width_);

        uint64_t word_nr = i/int_per_word_;
        uint8_t pos = i%int_per_word_;

        //set to 0 i-th entry
        uint64_t MASK1 = ~(MASK<<(width_*pos));
        words[word_nr] &= MASK1;

        //insert x inside i-th position
        words[word_nr] |= (x<<(width_*pos));

    }
 array(size_t _size, uint_t _key_len, uint_t _val_len,
       uint_t _reprobe_limit, size_t *_reprobes) :
   lsize(ceilLog2(_size)), size(((size_t)1) << lsize),
   size_mask(size - 1),
   reprobe_limit(_reprobe_limit, _reprobes, size), key_len(_key_len),
   key_mask(key_len <= lsize ? 0 : (((word)1) << (key_len - lsize)) - 1),
   key_off(key_len <= lsize ? 0 : key_len - lsize),
   offsets(key_off + bitsize(reprobe_limit.val() + 1), _val_len,
           reprobe_limit.val() + 1),
   mem_block(div_ceil(size, (size_t)offsets.get_block_len()) * offsets.get_block_word_len() * sizeof(word)),
   data((word *)mem_block.get_ptr()), reprobes(_reprobes),
   hash_matrix(key_len), 
   hash_inverse_matrix(hash_matrix.init_random_inverse())
 {
   if(!data)
     eraise(ErrorAllocation) << "Failed to allocate " 
                            << (div_ceil(size, (size_t)offsets.get_block_len()) * offsets.get_block_word_len() * sizeof(word))
                            << " bytes of memory";
 }
Example #16
0
    hacked_vector to_vector(uint64_t j,uint64_t y){

        auto w2 = std::max(width_,bitsize(y));
        auto vec = hacked_vector(size_+1,w2);

        //vector<uint64_t> vec(size_+1);

        uint64_t i = 0;
        for(uint64_t k=0;k<size_;++k){

            if(k==j) vec[i++] = y;
            vec[i++] = at(k);

        }

        if(j==size_) vec[size_] = y;

        return vec;

    }
Example #17
0
Values keygen(unsigned int bits){
    RNG::BBS(static_cast <MPI> (static_cast <unsigned int> (now()))); // seed just in case not seeded

    bits /= 5;
    // random prime q - only used for key generation
    MPI q = bintompi(RNG::BBS().rand(bits));
    q = nextprime(q);
    while (bitsize(q) > bits){
        q = bintompi(RNG::BBS().rand(bits));
        q = nextprime(q);
    }
    bits *= 5;

    // random prime p = kq + 1
    MPI p = bintompi("1" + RNG::BBS().rand(bits - 1));                // pick random starting point
    p = ((p - 1) / q) * q + 1;                                        // set starting point to value such that p = kq + 1 for some k, while maintaining bitsize
    while (!knuth_prime_test(p, 25)){
        p += q;
    }

    // generator g with order p
    MPI g = 1;
    MPI h = 1;
    MPI exp = (p - 1) / q;
    while (g == 1){
        g = powm(++h, exp, p);
    }

    // 0 < x < p
    MPI x = 0;
    while ((x == 0) || (p <= x)){
        x = bintompi(RNG::BBS().rand(bits));
    }

    // y = g^x mod p
    MPI y;
    y = powm(g, x, p);

    return {p, g, y, x};
}
Example #18
0
    /*
     * efficient push-back, implemented with a push-back on the underlying container
     * the insertion of an element whose bit-size exceeds the current width causes a
     * rebuild of the whole vector!
     */
    void push_back(uint64_t x){

        if(bitsize(x)>width_){

            //auto vec = to_vector(size(),x);
            //rebuild(vec);
            rebuild_ins(size(), x);

            return;

        }

        //not enough space for the new element:
        //push back a new word
        if(size_+1>(words.size()*(int_per_word_))) words.push_back(0);

        //insert x
        set(size(),x);

        size_++;

    }
Example #19
0
//-----------------------------------------------------------------------------
// 將值寫入旗標
bool C_FlagCtrl::SetValue(IN unsigned long ulPos, IN unsigned long ulSize, IN int iValue)
{
	if(m_pData == nullptr)
		return false;

	if(ulPos >= m_ulSize)
		return false;

	if(ulPos + ulSize >= m_ulSize)
		return false;

	if(bitsize(iValue) > ulSize)
		return false;

	for(int i = ulSize - 1; i >= 0; i--, iValue /= 2)
	{
		if(iValue % 2)
			Set(i + ulPos);
		else 
			Reset(i + ulPos);
	}//for

	return true;
}
Example #20
0
pHit
Any::match_in_local(const char* buf, size_t len,
                    bir roi) const
{
    return (bitsize() <= roi.bitsize()) ? pHit(roi.low(), bitsize()) : pHit();
}