Esempio n. 1
0
def test_v2():
    for i in lrange(300) + [100000]:
        assert bitlen(1 << i) == i + 1
        assert bitlen((1 << i) - 1) == i
    for i in v2_testcases():
        assert v2(i) == v2_old(i), (hex(i), v2(i), v2_old(i))
        pass
Esempio n. 2
0
 def sizes(cls, p):
     """Return dictionary of constants for a specific p."""
     try:
         return cls.table_dict[p]
     except:
         assert p > 1 and p & (p + 1) == 0, str(p)
         d = cls.tables.copy()
         d["P"] = p
         d["P_BITS"] = P_BITS = bitlen(p)
         FIELD_BITS = P_BITS
         while (FIELD_BITS & (FIELD_BITS - 1)):
             FIELD_BITS += 1
         d["FIELD_BITS"] = FIELD_BITS
         d["LOG_FIELD_BITS"] = hibit(FIELD_BITS)
         d["INT_FIELDS"] = INT_FIELDS = cls.INT_BITS // FIELD_BITS
         d["LOG_INT_FIELDS"] = hibit(INT_FIELDS)
         V24_INTS = 32 // INT_FIELDS
         d["V24_INTS"] = V24_INTS
         d["LOG_V24_INTS"] = hibit(V24_INTS)
         d["V24_INTS_USED"] = V24_INTS - (V24_INTS >> 2)
         V64_INTS = 64 // INT_FIELDS
         d["V64_INTS"] = V64_INTS
         d["LOG_V64_INTS"] = hibit(V64_INTS)
         d["MMV_INTS"] = 3 * (2048 + 24) * V24_INTS + 759 * V64_INTS
         partial_smask = partial(smask_default, FIELD_BITS)
         d["smask"] = UserFormat(partial_smask, fmt=c_hex)
         cls.table_dict[p] = d
         return d
Esempio n. 3
0
def test_make_additon_table(verbose=0):
    print("\nTesting module make_addition_table")
    for i, (m, singleton, granularity) in enumerate(table_testcases()):
        if verbose:
            nrows, ncols = len(m), bitlen(reduce(__or__, m, 0))
            print("\nTest case %d, a %d time %d matrix, singleton = %s" %
                  (i, nrows, ncols, bool(singleton)))
            print("m =", [hex(x) for x in m])
        ops = make_addition_operations(m, singleton, granularity)
        if verbose:
            display_addition_operation(ops)
        check_addition_operations(m, ops, singleton)
    """
    def set_fixed_matrix(self, fixed_matrix, singleton=False):
        """Here 'fixed_matrix' is the fixed left bit matrix factor. 

        Here we use some magic (vulgo: poorly documented) method
        to obtain a straight-line program calculating the
        non-trivial lines of the matrix product via xor operations.

        If you just want to understand the result (not the magic),
        it is best to check member function selftest.

        """
        self.data = list(fixed_matrix)
        self.singleton = singleton
        self.nrows = len(self.data)
        self.ncols = bitlen(reduce(__or__, self.data, 0))
        self.ops = make_addition_operations(fixed_matrix, singleton,
                                            self.granularity)
        self.n_registers = max([op[0] for op in self.ops]) + 1
        self.selftest()
def make_addition_tree(data, singleton=False, granularity=8):
    """Create a direct addition tree for a set of integers

    The function returns a pair ``(dist, stage)``. Here 
    dictionary``dict`` has entries of shape ``r:  (x, y)``
    for integers ``r, x, y`` such that ``r = x ^ y``, and  each of 
    the entries ``x, y`` is either of bit weight 1 or it is also
    contained as a key in the dictionry.

    The dictionary contains all entries of the list ``data`` of
    bit weight > 1 as keys.

    This function calls ``small_addition_tree`` (possibly several 
    times) with a list of data, such that the bit weight of 
    the union of these data is at most equal to ``granularity``.

    if parameter ``singleton`` is True then all bit positions 
    set in any entry of ``date`` are entered into the dictionary 
    as key of bit weight 1, with is value being an empty tuple.

    Dictionary ``shape`` has the same set of keys as dictionary
    ``dict``. For each key the value in that dictionary indicates 
    a **stage** at which this entry is computed.
    """
    dict = {}
    stage = {}
    if len(data) == 0 or max(map(bitweight, data)) <= 1:
        return dict, stage
    all_bits = reduce(__or__, data, 0)
    maxbit = bitlen(all_bits)
    st = 0
    for i in range(0, maxbit, granularity):
        mask1 = (1 << (i + granularity)) - (1 << i)
        mask = (1 << (i + granularity)) - 1
        a = []
        for x in data:
            xm1 = x & mask1
            xm = x & mask
            xm0 = xm1 ^ xm
            if bitweight(xm0) and bitweight(xm1):
                dict[xm] = (xm1, xm0)
                stage[xm] = st + 1

            if bitweight(xm1) > 1 and not xm1 in a:
                a.append(xm1)
        dict1 = small_addition_tree(a)
        for d in dict1.keys():
            dict[d] = dict1[d]
            stage[d] = st
        st += 2
    # Check addition tree in ``dict``
    #show_addition_tree(dict)
    for x in (dict):
        for y in dict[x]:
            if bitweight(y) > 1: assert y in dict.keys(), (hex(x), hex(y))
    # Add singeltons to ``dict`` if requested
    if singleton:
        while all_bits:
            entry = all_bits & -all_bits
            dict[entry] = ()
            all_bits &= ~entry

    return dict, stage