Example #1
0
    def test_unset_ots_key(self, mock_ots_bitfield_size, mock_ots_tracking_per_page):
        """
        Randomly using OTS key
        :return:
        """
        with set_qrl_dir('no_data'):
            state = State()
            mock_ots_bitfield_size.return_value = ceil(config.dev.ots_tracking_per_page / 8)

            alice_xmss = get_alice_xmss(12)
            address = alice_xmss.address
            address_state = OptimizedAddressState.get_default(address)
            addresses_state = {address: address_state}
            paginated_bitfield = PaginatedBitfield(True, state._db)
            paginated_bitfield.update_used_page_in_address_state(address, addresses_state, 1)
            self.assertEqual(address_state.ots_bitfield_used_page, 0)

            ots_indexes = list(range(3072, 2 ** alice_xmss.height))
            random.shuffle(ots_indexes)
            for i in ots_indexes:
                paginated_bitfield.set_ots_key(addresses_state, address, i)
                self.assertEqual(address_state.ots_bitfield_used_page, 0)

            ots_indexes = list(range(2048, 3072))
            random.shuffle(ots_indexes)
            for i in ots_indexes:
                paginated_bitfield.set_ots_key(addresses_state, address, i)
                self.assertEqual(address_state.ots_bitfield_used_page, 0)

            ots_indexes = list(range(1024, 2048))
            random.shuffle(ots_indexes)
            for i in ots_indexes:
                paginated_bitfield.set_ots_key(addresses_state, address, i)
                self.assertEqual(address_state.ots_bitfield_used_page, 0)

            ots_indexes = list(range(0, 1024))
            random.shuffle(ots_indexes)
            for i in ots_indexes:
                paginated_bitfield.set_ots_key(addresses_state, address, i)
                if i == ots_indexes[-1]:
                    self.assertEqual(address_state.ots_bitfield_used_page, 4)
                else:
                    self.assertEqual(address_state.ots_bitfield_used_page, 0)

            self.assertEqual(address_state.ots_bitfield_used_page, 4)

            paginated_bitfield.unset_ots_key(addresses_state, address, 2049)
            self.assertEqual(address_state.ots_bitfield_used_page, 2)

            paginated_bitfield.unset_ots_key(addresses_state, address, 1023)
            self.assertEqual(address_state.ots_bitfield_used_page, 0)

            paginated_bitfield.set_ots_key(addresses_state, address, 1023)
            self.assertEqual(address_state.ots_bitfield_used_page, 2)
Example #2
0
    def test_unset_ots_key3(self, mock_ots_bitfield_size, mock_ots_tracking_per_page):
        """
        Features Tested
        - Sequentially marking OTS indexes as used
        - Sequentially marking OTS indexes as unused
        - ots_bitfield_used_page value with each OTS index being used

        Expectation
        - The ots_bitfield_used_page must increase by 1 for every sequential 1024 (ots_tracking_per_page) ots indexes
          marked as used

        :param mock_ots_bitfield_size:
        :param mock_ots_tracking_per_page:
        :return:
        """
        with set_qrl_dir('no_data'):
            state = State()
            mock_ots_bitfield_size.return_value = ceil(config.dev.ots_tracking_per_page / 8)

            alice_xmss = get_alice_xmss(12)
            address = alice_xmss.address
            address_state = OptimizedAddressState.get_default(address)
            addresses_state = {address: address_state}
            paginated_bitfield = PaginatedBitfield(True, state._db)
            paginated_bitfield.update_used_page_in_address_state(address, addresses_state, 1)
            self.assertEqual(address_state.ots_bitfield_used_page, 0)

            total_ots = 2 ** alice_xmss.height
            for i in range(0, total_ots + 1):
                paginated_bitfield.set_ots_key(addresses_state, address, i)
                self.assertEqual(address_state.ots_bitfield_used_page, (i + 1) // config.dev.ots_tracking_per_page)

            self.assertEqual(address_state.ots_bitfield_used_page,
                             total_ots // config.dev.ots_tracking_per_page)
            self.assertEqual(total_ots // config.dev.ots_tracking_per_page, 4)

            paginated_bitfield.unset_ots_key(addresses_state, address, total_ots - 1)
            self.assertEqual(address_state.ots_bitfield_used_page, 3)

            for i in range(total_ots - 2, -1, -1):
                paginated_bitfield.unset_ots_key(addresses_state, address, i)
                self.assertEqual(address_state.ots_bitfield_used_page, i // config.dev.ots_tracking_per_page)