Analysis of segregated witness implementation for increasing efficiency and security of the Bitcoin cryptocurrency

ABSTRACT The purpose of this paper is to present mechanisms and algorithms implemented for improving Bitcoin cryptocurrency efficiency and security and to examine the block propagation times from a selected period before and after SegWit was introduced. In this paper, Segregated Witness Implementation issues were verified based both on the simulation and real data from the Bitcoin network. Based on the block propagation times calculated in the simulator, as well as bitcoin network real data, the efficiency and safety of Bitcoin have been analysed and validated.


Introduction
The performance of algorithms is very important in blockchain networks. Excessive block extraction together with long distribution times can cause security vulnerabilities and problems with accepting newly created chains. It leads to amount of security issues and can harden the use of blockchain technology (Kedziora et al., 2020(Kedziora et al., , 2019. It is therefore very important that propagation times in the network would avoid the aforementioned situations (Kuzuno & Karam, 2017;Rudlang, 2017). Hence, over the years, many solutions have been developed to improve the efficiency of propagation and block capacity, one of them is, among others Segregated Witness. SegWit is an implementation of the protocol that allows to process a greater number of transactions by increasing the block's capacity and providing protection against bitcoin transaction malleability. The first thing to note is the ability to increase block capacity (Song, 2017). First time Segregated Witness was heard at the beginning of 2015. It was decided to separate transaction data on the blockchain chain from signature. The created SegWit implementation could not be activated due to blocking by miners who exploited the vulnerability in BIP-9. Because decisions are made by the majority, it was decided to circumvent the ominous miners and implement a so-called soft fork before SegWit. BIP-148, in which the mining majority bypasses blocking miners. Thanks to this operation, on 28 August 2017, Bitcoin was able to activate Segregated Witness, i.e. 4 days after approval by the network. Most important Segregated Witness changes introduced to the Bitcoin network are: BIP91implemented on 23 June 2017 with the change concerned temporary software, which forced the obligatory upgrade of the node to SegWit; BIP148implemented on 1 August 2017 for Changing the mandatory SegWit updates on the node from August 1 was required for another 2 weeks and BIP141, 143, 147implemented on 24 August 2017 with the change updated the node to the latest version of SegWit (Fry, 2017). The purpose of this work is to present SegWit mechanisms and to examine the performance of Bitcoin network after its implementation.

Related work
Research on the Bitcoin network on the implemented proprietary simulator was presented in the paper (Gervais et al., 2016). The aim of this paper was to investigate how consensus parameters, network characteristics and protocol modifications affect the scalability, security and performance of Proof of Work blockchain. The study was conducted on the proprietary Bitcoin simulator. It was built on the NS3 network simulator, which is designed for Internet systems and was created for research and educational purposes. NS3 has been encapsulated with libraries in C++, so as to be able to simulate the Bitcoin network and be able to simulate any Bitcoin parameterization. The authors' goal was to create a realistic simulator. It works with collected real network statistics that have been implemented in the software. In addition, the authors collected data from popular Bitcoin websites to be able to determine and estimate block size distribution, generated blocks, average number of nodes and networks. It was also decided to take care of such details as geographical distribution. The study was conducted from March 2015 to October 2016. The study evaluated the performance of different blockchain instances. The tests were carried out on a simulator and not on a direct network due to the insufficient availability of nodes. Using the simulator, the results were checked based on the manipulation of block parameters.
In the research work (Neudecker & Hartenstein, 2019), authors presented empirical analysis of announcing and block propagation that led to the Bitcoin forks. Analysing the differences in block publication, it was shown that the block propagation delay between miners can have a similar order as the delay in processing the Bitcoin network. The study was conducted in the period from January 2016 to March 2018. The research was carried out on actual Bitcoin network data. The study showed that on the basis of propagation time and forks, it was found that not only the first block advertisement has a meaning in which it becomes part of the main chain, but also the time of data propagation. However, it does not have such a big influence to be the deciding factor whether the block will become an orphan or will be dragged into the main blockchain chain.
The research work (Decker & Wattenhofer, 2013) presents the way in which data is spread and propagated on the Bitcoin network to identify key weaknesses and problems. In particular, information synchronization analysis was performed. These are one of the first studies closely related to block propagation in the Bitcoin network. The study was conducted in 2013. The research explains the way block propagation was calculated. The propagation time results showed that the block size matters for propagation and the median propagation is 6.5 seconds and the average is 12.5 seconds. It has been proven that the larger the block, the larger the propagation time, which gives the network attacker an advantage.

SegWit implementation analysis
The research described above allowed to draw conclusions based on the results related to blockchain and data propagation. Thanks to the ability to configure the simulator set input data in the Bitcoin network, it allowed us to create simulations for a large number of nodes.
Nowadays, we already have up to 9000 nodes available on Bitcoin (TOP, 2018), so we can check the simulator's operation and results. Verification will allow us to assess past work results for correctness and see if it can be used to simulate Bitcoin with Segregated Witness. Previously described research on performance, security and forks based on block propagation times in the Bitcoin network, omitted information from where large time fluctuations come from. It focused on how block propagation affected forks and their orphaning, and no conclusions were drawn as to why such fluctuations in times of block propagation on the network. Recent studies (Neudecker & Hartenstein, 2019) on block propagation in this direction took place in 2013 and 2017, hence the desire to look at changes in the propagation response in the network against the background of recent years, and it was decided to examine time slices to prove that the direction of development and investment in Segregated Witness gives real benefits to the network and it is recommended to implement this mechanism on other nodes.

Testing environment
The first step to perform was the installation of the simulator and all necessary components, including NS3. In this work, a built-in machine was used in Windows 10 since Hyper-V virtualization and Ubuntu version 18.04.2. The next step was to set up the simulation. It consisted of proper input configuration. To achieve this, there are three ways: the first is to edit existing simulator files, the second is to set the input data directly on the simulation start command, the third is to create your individual parameter with which the start is performed and which overwrites the default values. In these work experiments, we used first option.
Parameters we chosen have been set to 1000 blocks to simulate, block size of 1,000,000 bytes (1 MB) and 6000 nodes. These values were selected to check the correct operation of the simulator. The simulation took almost 17 hours. A test simulation confirmed that the simulator is working. This allowed us to verify in what form we get the results and to better prepare for the final simulation and testing of algorithms. Example results of block propagation times are presented in Table 1. All times are given in seconds. Described in related works section ways of obtaining block propagation data in the Bitcoin network shows that there are several ways to approach propagation times. Based on publicly available Blockchain data, propagation times from 2013 to 2019 were collected. The rest of this paper will present in-depth results and analyses (TOP, 2018) ( Figure 2).

Network analysis
The implementation of the Segregated Witness was associated with many new changes in the network. Improving the error with the 'malleability' of the transaction was one of the most serious issues, which thanks to SegWit has been corrected. This issue allowed third parties to correct transaction signatures that were identified using a 64-digit hexadecimal hash called the identifier (txid). This operation made it possible to pay again with the same coins. SegWit protects against third-party vulnerabilities and scripts by enabling susceptible parts of transactions to be transferred to the transaction witness (SegWit) and limits the impact of the witness on the txid calculation (Antonopoulos, 2014, 2017). Next benefit was repair of scaling linear signature mixing operationsthe main problem of increasing blocks is that there are transactions that scale squarely rather than linearly. This state of affairs can double the size of the transaction. It is easy to guess that this can be used by intentional, proper implementation of the transaction (Nakamoto, 2017). On average, a single block needs about 12.5 seconds for confirmation on the node, while a maliciously created transaction can take up to 3 minutes. SegWit solves the problem by changing the transaction hash calculation for the signature per each byte, allowing only a maximum of two transaction bytes to be mixed. Swap allows the same operation, but with much greater efficiency, while being resistant to malicious transactions created (Nakamoto, 2014). Next benefit was a new way of signing input values -SegWit allows to securely sign transactions by receiving only the transaction, index and value abbreviation. Earlier, it was required to have and check the hash of a full copy of the transaction to perform this type of operation. This was problematic because some transactions are very large, making the whole operation very expensive even with small transactions. The change has significantly accelerated this process (Drescher, 2017). As you can see in the histogram graph (Figure 3), the propagation times of the Bitcoin network block after the implementation of the changes on the nodes.

Experiments
To confirm that the new way of signing input values in Segregated Witness has benefited, we compared two histograms of block propagation times from two periods. The first of 127 days before the introduction of Segregated Witness and the second 127 days after the introduction of SegWit. Intervals after 0.632 seconds were adopted. 127 samples were used, each sample equals the mean propagation block times per day. Figure 1 shows improvement in times of block propagation relative to each other. Orange indicates the propagation times after the introduction of SegWit. It can be seen that the histogram shows that as many as 76 samples (59.8%) are in the first compartment (propagation times up to 1 second), before introducing this solution in the first compartment there were not even half of the samples (27 samples, 21.3%). Also comparing the averages, there is a significant difference, 1.97 seconds before the introduction of SegWit and 1.18 seconds after the changes.
Analysing Figure 4, we can see that the impact of the change implementation on the nodes had a very large impact on the decrease in block propagation times. The next part of the work will present a detailed analysis of the wider period of introducing changes on the nodes, including the Bitcoin version in the network. Both manufacturers and portfolio users benefited from the introduced changes by faster transaction confirmation. UTXO (Unspent Transaction Output) growth reduction is next benefit of SegWit. A given  database is used when validating whether the new transaction is valid or false. To be efficient, the base should work in the main RAM memory. Maintaining the database in such a small size becomes problematic when Bitcoin is growing. The increasing number of users means that every user must have their entry to ensure transaction security. Segregated Witness solves this problem by using minimizing transactions. Thanks to this solution, signature data that does not affect the UTXO size cost 75% less. The goal is to encourage users to use this solution to minimize fees and encourage programmers to implement features that implement the least impact on UTXO, and to design smart contracts. People benefiting from a given shift are miners, companies and users who have full nodes. They benefit from lower fees than people who do not use this solution. In addition, faster use of UTXO encourages more people to have full nodes, which increases network security. In terms of increased security, SegWit solved the problem for the payment of multisig contracts, which using P2SH, enabled the funds to be stolen by an attacker with very large power resources, who was able to find an address conflict with part of the multisig script. In SegWit, it was decided to implement a change that the used  HASH160 algorithm would be used only for direct payments only for a single public key using SHA256. In this case, this type of attack is useless and impossible to execute. Everyone using multisig payments benefited from this solution, the guarantee guarantees additional protection. In Area of increasing block capacity, downloaded block without a signature applies to historical nodes, while the new ones understand that they contain a signature, therefore they still allocate by default 1 MB of block for transaction data (without signature), but because they are aware that the transaction contains signatures, are able to increase the capacity of a given block to nearly 4 MB. Currently, the Bitcoin network capacity is increasing from about 1.6 MB to about 2.4 MB. The main reason for only such a small increase in block capacity is the growing time of block propagation, it pays to large miners to jump to a different field than to fight to improve protocol performance (Song, 2019). Performance improvement was achieved by segregation of signature data that allows nodes not interested in signatures to cut them on disk or only downloads them in the second place. Previously, the signature data was part of the transaction and had to be used in calculating the hash function. The solution saves disk space and works with lower bandwidth. SegWit introduced script versioning that solved the problem of the lack of script implementation in other than only backward compatible with soft forks. Adding the version number to the scripts has solved this problem, which makes it possible to handle them with the required hard forks by simply increasing the version of the script. Last concept introduced was Lightning Network which is a network with very high bandwidth. Until recently, it was thought that the Bitcoin network had too complicated structure, mechanisms and algorithms which made it impossible to pursue this concept. The introduction of Segregated Witness has simplified many algorithms and solutions due to the possible implementation of the Lightning Network in the near future (Core, 2016).

Bitcoin data analysis
Thanks to previously obtained results of block propagation times for individual days from 2013 to 2019, some of whose results are presented in Figures 5 and 6, it was possible to analyse the impact of Segregated Witness on the network. To this end, samples were checked to confirm the correctness of the data. Matlab version 9.0 was used to perform the analysis. For the purpose of analysis, 100 collected samples from two available sets were selected for comparison. The samples come from the period 15.07.2015 to 27.10.2019. First, the previously prepared block propagation samples from the Bitcoin network were loaded with a total of 200 observations, each observation with 100 samples. We see that the data sets fit in quite well with the cumulative distribution function. The next step was to check whether we would receive similar conclusions using the K-S test analysis. This will answer whether the difference between the two samples is not statistically significant to be able to conclude that the two sets are not different. Therefore, we should accept the null hypothesis, p-value is greater than 0.5, which is the default value of significance level. Thus, according to tests, the differences between the two samples are not statistically significant to be able to conclude that both these distributions are different. The obtained results of block propagation times are presented in Figure 3. The resulting Figure 3 is hardly legible, therefore it was decided to change the scope of research from 10/08/2015 to 17/05/2019. The new data range given will allow to divide block propagation times into two equal samples after 661 days from the date of SegWit introduction (Figure 4). It should be noted that not all nodes will immediately implement it, which will be shown later in this chapter.
Based on the charts before and after the introduction of SegWit, one can observe a very large improvement in block propagation times, which proves faster network performance and mechanisms implemented in it. In addition, to confirm the numbers, the means and medians for these two periods were counted, as well as for the whole range. The results are presented in Table 2.
The calculated medians and averages also confirm that the network has improved since the introduction of SegWit. We can see a clear decrease in block propagation times. Also comparing with the average times given in the paper (Neudecker &  Hartenstein, 2019), which are 12.5 seconds, also confirm the improvement. One can also observe a large decrease in block propagation times in the period of 30 November 2015 to 22 August 2017, which was caused only by the fact that at the beginning of this period Bitcoin broke popularity records for which it was not properly prepared. Hence the average time was increased than the time from the older period despite newer versions of the Bitcoin network on the nodes. To illustrate the distribution of samples, this is best represented by the histogram for the two time intervals. Each histogram has 0.5 second intervals with 661 samples each.
Analysing two histograms, it can be seen that after the introduction of SegWit ( Figure  6) in the first two intervals (time up to 1 second) there are as much as 86.38% (571 samples) of measurements where before the implementation of this solution only 1.97% (13 samples). In addition, in the first interval of 0.5 seconds before the introduction of Segregated Witness there is not a single sample from the analysed period that would meet this condition. It was decided to supplement Figure 7, which contains block propagation times after the implementation of SegWit with available versions of Bitcoin Core, and with introduced changes in the network.
In Figure 7, it can be seen that after the start of SegWit, there was a sudden drop in block propagation times. At that time, there was no official version that allowed SegWit support, but the network still reacted. The reason for this could be the fact that Bitcoin Core is an open-source program, so everyone has access to it, even in a not yet ready version. Most likely, large mines have prepared themselves and sooner implemented SegWit implementations. After entering version 0.15.0 there is a period of very large time deviations. This was due to many bugs in the software and the fact that not all functions were completed. The most serious error was the bug in version  0.15.0.1, which caused the Bitcoin Core environment to be blocked, which can be seen in the chart. After the repair version 0.15.1 and with many performance improvements, there has been an improvement, but not immediately. The main reason for this was that not all nodes immediately uploaded the implementation of the new version. Lack of conviction and the time needed to implement the solution meant that not everyone uses Segregated Witness immediately, this affects propagation times, because nodes with the old implementation (without SegWit) still take a 1 MB block, therefore the network must deal with blocks larger than 1 MB, sent to nodes able to accept only 1 MB. Backward compatibility has been maintained on the network at the expense of propagation times. The final implementation of SegWit has been published in version 0.16.3. All versions above also have previously implemented Segregated Witness. About 19.8% of nodes still do not have even the oldest version of the software that would support Segregated Witness. Noting that the stable version is 0.16.3 and above, currently as many as 21% of nodes do not have a SegWit implementation, which gives 2060 nodes with old software at 9450. Looking at the data when Segregated Witness was introduced, you can see that the more people on the network has a version larger than 0.15.0, the smaller is the block propagation time and network stability increases. It can be seen that the stability of block propagation times and very small times was achieved only after the implementation of the official version 0.16.0, which was used by more users because it had a full set of functions and support. This fact is followed by minimal jumps in time, but they are caused by small errors in the software that were repaired on a regular basis and did not significantly affect the operation of the network. Currently, block propagation times in the Bitcoin network have remained stable for less than a year, below 1 second, which also greatly increases network security.

Conclusion and future work
The purpose of this work was to present mechanisms improving Bitcoin cryptocurrency and to examine the block propagation time from a selected period, verifying both on the simulator and actual data of the Bitcoin network. To analyse the effectiveness of implemented implementations and confirm that Segregated Witness is a good solution for the Bitcoin network, reactions of block propagation times for individual introduced network changes were examined. Based on the results obtained from the analysis of the Bitcoin network, an improvement in performance after the introduction of Segregated Witness was demonstrated, by reducing the average propagation times of the block to 0.68 seconds, and for the entire period (02.2013-05.2019) the average time fell to 3.2 from 12.5 seconds. Significant changes are also illustrated by the median, which for the whole period was 3.2 seconds from 6.5. The low level of block propagation time protects the network against frequent orphans of blocks and shows that the change in block capacity does not significantly affect the measurement times, which have remained below 1 second for a year. Stable network, code facilitation and implementation bug fixes have increased security, reducing the chance of an effective attack. It has been proved in the paper that the use of the Bitcoin Core version with the Segregated Witness implementation in the stable version 0.16.3 or higher significantly improves the performance and security of the Bitcoin network. The above considerations show that the introduction of Segregated Witness to the Bitcoin network was a very good

Funding
This work was partially supported by the European Union's Horizon 2020 Research and Innovation programme under the grant agreement No. 825183.