<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Thibaut Le Guilly on Medium]]></title>
        <description><![CDATA[Stories by Thibaut Le Guilly on Medium]]></description>
        <link>https://medium.com/@leguilly.thibaut?source=rss-87a558d4b9cf------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*PT9ENnxG3seyrNOu.</url>
            <title>Stories by Thibaut Le Guilly on Medium</title>
            <link>https://medium.com/@leguilly.thibaut?source=rss-87a558d4b9cf------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 27 Nov 2026 15:29:34 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@leguilly.thibaut/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[DLC on Lightning]]></title>
            <link>https://medium.com/crypto-garage/dlc-on-lightning-cb5d191f6e64?source=rss-87a558d4b9cf------2</link>
            <guid isPermaLink="false">https://medium.com/p/cb5d191f6e64</guid>
            <category><![CDATA[bitcoin]]></category>
            <category><![CDATA[dlc]]></category>
            <category><![CDATA[lightning]]></category>
            <dc:creator><![CDATA[Thibaut Le Guilly]]></dc:creator>
            <pubDate>Mon, 28 Nov 2022 23:23:45 GMT</pubDate>
            <atom:updated>2023-09-29T01:06:18.761Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oHD7BwQQy1nDbVbHndF_7g.jpeg" /></figure><h3>TLDR;</h3><p>We have opened and closed the first mainnet Lightning channel embedding a Discrete Log Contract (DLC).</p><p>At Crypto Garage we have been working on getting DLC working over Lightning channels for some time, and we have finally reached an important milestone in having successfully opened a Lightning channel, set up a DLC inside it, and closed the entire channel. In this article we’ll explain the technical details of how the Lightning channel and DLC were set up and closed.</p><h3>What is a DLC on Lightning (and what it is not)</h3><p>For the purpose of this article, we refer to opening a DLC on Lightning meaning having a <strong>direct </strong>channel established with a node, and setting up a DLC within the channel with (part of) the funds that were used to open it. The DLC can be closed (reverting back to a regular Lightning channel), renewed or updated, and the channel can of course be closed on chain in case of a non cooperative counter party (this is what we did as it shows the full transaction structure of the Lightning channel and DLC contract on-chain).</p><p>We emphasize that it is not currently possible to have <em>routed DLC</em> over Lightning (meaning opening a DLC between nodes that don’t have a direct channel). See <a href="https://mailmanlists.org/pipermail/dlc-dev/2021-November/000091.html">this mailing list post</a> for more information.</p><h3>Preliminaries</h3><p>In order to try and make this article self contained, we’ll go through the different components that are used to set up a DLC on Lightning: <a href="https://docs.google.com/document/d/1SndejwtOQC93BieINox2Gg5HtnUgFcLTVQODL8MP5Z4/edit#heading=h.jbsgei7du8a5">Lightning channels</a>, <a href="https://docs.google.com/document/d/1SndejwtOQC93BieINox2Gg5HtnUgFcLTVQODL8MP5Z4/edit#heading=h.cb0xldu0vqab">adaptor signatures</a>, <a href="https://docs.google.com/document/d/1SndejwtOQC93BieINox2Gg5HtnUgFcLTVQODL8MP5Z4/edit#heading=h.it41s33fig1">DLCs</a>, and <a href="https://docs.google.com/document/d/1SndejwtOQC93BieINox2Gg5HtnUgFcLTVQODL8MP5Z4/edit#heading=h.niw3p88jfsaz">DLC channels</a>. You can skip the sections related to concepts you’re already familiar with.</p><h3>Lightning Recap</h3><p>As it is difficult to understand how to set up a DLC within a Lightning channel without first having a rough understanding of the transaction structure of regular Lightning channels, let’s recall that quickly (to get a deeper understanding <a href="https://bitcoinmagazine.com/technical/understanding-the-lightning-network-part-building-a-bidirectional-payment-channel-1464710791">this Bitcoin Magazine series</a> is pretty great). We’ll only focus on what is relevant for the context and so we will skip anything related to routing.</p><p>When two Lightning nodes open a channel with each other, they cooperatively build and sign two types of transactions, the <em>funding transaction</em> and the <em>commitment transactions</em>.</p><p>The funding transaction takes a number of UTXOs from one of the parties as inputs (or both in the case of <a href="https://bitcoinops.org/en/topics/dual-funding/">dual funding</a>), and locks the funds to be used for the channel in a 2 of 2 output that requires a signature from both parties to be spent, referred to as the <em>funding output</em>.</p><p>A commitment transaction takes as input the funding output and contains two outputs, with values corresponding to the balances of each party. Note that these balances will change during the lifetime of the channel, and so the commitment transaction needs to be updatable to reflect these changes. This is done by making the commitment transaction <em>revocable</em>. In practice, this means that each party holds a different version of the commitment transaction, with the output paying to themselves locked in a script that can be unlocked either by themselves after a certain amount of time, or by their counter party with knowledge of a secret. When the parties agree to update the channel balance (e.g. one party wishes to send some sats to the other), they build and exchange signatures for the new commitment transactions, and revoke the previous ones by revealing to each other the secrets they previously used. If a party tries to cheat by broadcasting an old commitment transaction, the other party has some time to react and use the secret to take all the channel funds.</p><p>The transaction structure is illustrated in the following figure.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/771/0*knlktdIYJqUpLD3_" /><figcaption>Transaction structure of a Lightning Channel</figcaption></figure><h3>Adaptor Signatures</h3><p>An adaptor signature is a signature that has been encrypted (for the case that is of interest with a public key), and for which we can prove that after decryption (with the corresponding secret key) we obtain a valid signature for a given message. It also has the property that knowledge of the adaptor signature, the encryption public key and the decrypted signature makes it possible to retrieve the secret that was used for encryption. Check <a href="https://medium.com/crypto-garage/optimizing-numeric-outcome-dlc-creation-6d6091ac0e47#96e9">here</a> for a quick overview of the formulas involved.</p><p>The following figure illustrates the different operations involved in encrypting, verifying, and decrypting adaptor signatures, as well as recovering an encryption secret.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*nlKMDMRbdgVVq7wG" /></figure><h3>Discreet Log Contract</h3><p>A DLC enables two parties to set up a contract over some event (sport game, price of an asset…), and settle it directly on the Bitcoin blockchain. The parties need to choose an oracle beforehand, an entity that will attest to the event outcome by releasing a signature over the outcome value.</p><p>A DLC looks rather similar to a Lightning channel at first sight, but instead of having commitment transactions spending from the funding output, we have Contract Execution Transactions (CET), one for each of the possible contract payout. The parties each encrypt their signatures (creating adaptor signatures) for these transactions using the pre-image of the signature that the oracle would produce for the corresponding event outcome (check <a href="https://medium.com/crypto-garage/optimizing-numeric-outcome-dlc-creation-6d6091ac0e47#96e9">here</a> for more details), and exchange and verify them. Once the oracle attests to the event outcome, any of the two party will be able to decrypt a single signature from their counter party, and close the contract by broadcasting the corresponding CET. Note that thanks to using adaptor signatures, the parties hold exactly the same set of CETs. The transaction structure of a DLC is illustrated in the figure below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/601/0*TKw31rinQigX8y2T" /><figcaption>Transaction structure of an on-chain DLC</figcaption></figure><h3>DLC Channels</h3><p>What if two parties wish to renew a contract at expiry? They can of course close the first contract and reopen another one. This however incurs fees for each of the broadcasted transactions. Instead they can use a DLC channel (the folks at ItchySats have written in greater details about this <a href="https://comit.network/blog/2022/01/11/cfd-protocol-explained/">here</a>). A DLC channel enables parties to renew a contract, settle it ahead of its maturity date, and of course to close it on-chain once they want to recover their funds. A simple way would be to make CETs revocable. Unfortunately, this causes an issue when renewing a contract. Once one of the parties has revoked the previous CETs, they cannot prevent their counter party from still being able to use them. This means the counter party has the opportunity to close either the previous contract or the new one, while the party that first revoked the CETs has no choice but to wait for the new contract maturity to close (as they need to wait for the oracle to release the attestation to be able to broadcast a CET). This gives an unfair advantage to one of the parties. To solve this problem, we introduce a <em>buffer transaction</em>, which spends the funding output and outputs the same value (minus fee), which is used as input by the CETs. Then when renewing a contract, if one of the parties doesn’t revoke the previous contract in a timely manner, the other party can broadcast the buffer transaction for the new contract and prevent the malicious (or malfunctioning) party from closing the channel with the previous contract. With this construction, it is enough to make the buffer transaction revocable for revoking a contract.</p><p>So how do we revoke the buffer transaction? One way would be to use the same approach used in Lightning, with each party holding a different version of the buffer transaction. Fortunately, a better solution exists (that was originally proposed in <a href="https://eprint.iacr.org/2020/476.pdf">this paper</a>). It once again relies on adaptor signatures, but in a different way than how a regular DLC makes use of them. Each party uses three sets of key pairs. The first one is the funding key, used by each party in the 2 of 2 script of the funding output. The second one is the revocation key, which gets revealed when a contract is revoked. Finally, the third one is the publishing key. These three keys are used in the buffer transaction output scripts, which can be spent in three different ways (we use Alice and Bob to make things easier):</p><ol><li>With signatures from Alice and Bob funding keys (using one of the CET transaction, which as before can only be used with knowledge of the oracle attestation for the event outcome),</li><li>With a signature from Alice funding key, and signatures from Bob publish and revocation keys,</li><li>With a signature from Bob funding key, and signatures from Alice publish and revocation keys.</li></ol><p>When a contract is set up in the channel, Alice and Bob give each other an adaptor signature for the buffer transaction input, encrypted with their counterparty publish (public) key. If one of them wants to broadcast the buffer transaction, they decrypt the adaptor signature that they obtained from their counterparty using their publish (secret) key. But by broadcasting the buffer transaction (which contains the decrypted signature), they will reveal their publish secret key to their counterparty (who already knows the adaptor signature and encryption key). If the buffer transaction has not been revoked, this is not an issue, they will be able to legitimately spend the buffer transaction output using one of the CET. But if the buffer transaction was revoked, their counterparty must also know the revocation secret and will thus be able to get the entire output funds. Finally, note that the CETs have a relative timelock to give time to a cheated party to spend a revoked buffer transaction output.</p><p>The following figure illustrates the transaction structure and their spending conditions.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/804/0*MBPUApbTOkCdVvun" /><figcaption>Transaction structure of a DLC channel</figcaption></figure><h3>DLC on Lightning</h3><p>Now that we have explained all the building blocks, let’s see how we can embed a DLC channel in a Lightning channel. A first obvious way to do it would be to add an output to the commitment transaction of a Lightning channel to fund a DLC channel. Unfortunately, this would mean that every time the commitment transaction is updated, all the adaptor signatures of the DLC would need to be recomputed (and re-verified), which can be costly for contracts with a large number of possible outcomes (this issue would be fixed with something like SIGHASH_NOINPUT). To avoid this, we introduce a <em>split transaction</em>, which spends the funding input and contains two outputs, one for the Lightning channel, and one for the DLC one (it could potentially contain more inputs to fund multiple DLC channels and have multiple open contracts at the same time). In order to be able to restore the channel to a “typical” Lightning one later on, we make the split transaction revocable using <a href="#19e2">the same adaptor signature based mechanism used for DLC channels</a>. Unfortunately, one issue arises with the Lightning channel. In order for one of the parties to react to the broadcast of a revoked split transaction, there needs to be some timelock on the transactions spending from it. But <a href="https://github.com/lightning/bolts/blob/master/03-transactions.md#commitment-transaction">the Lightning network specifications use both the nLockTime and nSequence fields</a> of the commitment transaction to specify the commitment number. To circumvent this, we insert a <em>glue transaction</em> between the split transaction and the Lightning channel commitment transaction to enforce a relative timelock on it (h/t to Matt Corallo for suggesting this simple workaround). Note that this is really an engineering issue and could be solved by adopting a different specification for the commitment transaction (but this would also require much more changes to a Lightning implementation to support it).</p><p>To summarize, if two parties who have a direct Lightning network channel between them wish to set up a DLC within it they would collaboratively create and sign the split transaction, the glue transaction, the updated commitment transactions (reflecting the funds kept inside Lightning for each party), the buffer transaction and the CETs for the DLC. They can then keep routing through the Lightning channel, and update or settle the DLC contract in the DLC channel. If they wish to close the DLC channel, they can revoke the split transaction and collaboratively create and sign updated commitment transactions.</p><p>The following figure illustrates the transaction structure. Note that different publish and revocation keys are used for the split transaction and buffer transaction.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/872/1*4S_9gCeGgY5EkRkHLtd4Fg.png" /><figcaption>Transaction structure of a Lightning channel embedding a DLC channel</figcaption></figure><h3>Implementation</h3><p>The construction detailed above was implemented <a href="https://github.com/p2pderivatives/rust-dlc/tree/feature/split-tx-manager-2">in the rust-dlc library</a>, using <a href="https://github.com/p2pderivatives/rust-lightning/tree/split-tx-experiment">a fork of the Lightning Development Kit (LDK)</a> that adds support for splitting a Lightning channel. A <a href="https://github.com/p2pderivatives/ldk-sample/tree/ln-dlc-test">small CLI tool</a> based on the LDK sample was also implemented that in addition to regular Lightning functionalities also enables opening and management of DLC channels. Note that the code is very unstable and <strong>we would like to emphasize that using it with mainnet coins is very likely to lead to the loss of the funds</strong>.</p><h3>Mainnet Execution</h3><p>We first opened a channel between our two DLC on Lightning enabled nodes on November 21st, with <a href="https://blockstream.info/tx/f307a6330c25ff4a43290803b088754b03ffd9c90c556aebca9a89d0b0ff9988">this transaction</a>. We then made a keysend payment from the node that opened the channel to the other one (once dual funding is implemented in LDK this step would not be necessary). We defined a DLC contract based on the price of bitcoin on the 21st November at 4pm (JST) and used it to open a DLC channel alongside the Lightning channel. At contract maturity, we made one of the node force close the channel. The node first broadcasted the split transaction that can be seen <a href="https://blockstream.info/tx/a110bc985c6b02e5d4a80a6eca4a4561462975dbe65a70a9dd2fb9af47330326">here</a>. 288 blocks after the inclusion of the split transaction in a block (the nSequence value set on the buffer and glue transactions), we made the node broadcast the buffer and glue transactions (visible <a href="https://blockstream.info/tx/2084f0fab71ceb05a83cc8c1c7430e9aa101cba23d886fa75ccfc46616659af2">here</a> and <a href="https://blockstream.info/tx/5db5f26943af8f8b2e1be37b612d0d6f768c1117d7e6e63d08ee93cede6c63c1">here</a>), as well its local commitment transaction (visible <a href="https://blockstream.info/tx/bfde28fba3b4873e87f2044e861dc117f321091dc635dafa1ffa2df46c723591">here</a>). 288 blocks after the inclusion of the buffer transactions, the node fetched the oracle signature for the outcome event, decrypted the other node adaptor signature matching the outcome, and was able to broadcast a CET to close the DLC (visible <a href="https://blockstream.info/tx/c0819ea9d8fe73ce3ad79e7aedbcbe8931258e4961b456317a64420ae402aa7e">here</a>).</p><h3>Conclusion</h3><p>Being able to open DLC within Lightning opens up to some nice applications such as pegging part of the channel value to some other currency, using some of the funds in a channel to trade some derivatives contracts without having to give away the custody of the fund, or simply bet on the outcome of a football game with a friend. By implementing support for this in the rust-dlc library and demonstrating its use through the execution and closing of a DLC within a Lightning channel, we have shown the feasibility of our proposed approach. Quite some work remains to be done both to analyze and improve the proposal, and to strengthen its implementation. So if you are interested in joining the effort, in using this technology or want to know more about it feel free to contact us!</p><h3>Acknowledgement</h3><p>Thanks to the LDK team for reviewing and accepting a couple of pull requests that made it possible to implement things discussed in this article, as well as for answering multiple questions regarding LDK, Lightning and Rust!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cb5d191f6e64" width="1" height="1" alt=""><hr><p><a href="https://medium.com/crypto-garage/dlc-on-lightning-cb5d191f6e64">DLC on Lightning</a> was originally published in <a href="https://medium.com/crypto-garage">Crypto Garage</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Optimizing Numeric Outcome DLC creation]]></title>
            <link>https://medium.com/crypto-garage/optimizing-numeric-outcome-dlc-creation-6d6091ac0e47?source=rss-87a558d4b9cf------2</link>
            <guid isPermaLink="false">https://medium.com/p/6d6091ac0e47</guid>
            <category><![CDATA[dlc]]></category>
            <category><![CDATA[bitcoin]]></category>
            <category><![CDATA[adaptor-signature]]></category>
            <dc:creator><![CDATA[Thibaut Le Guilly]]></dc:creator>
            <pubDate>Thu, 04 Mar 2021 06:57:43 GMT</pubDate>
            <atom:updated>2021-03-05T02:28:17.553Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5FJtLB92Pk7l9ZQsB88pnw.png" /></figure><h3>TLDR;</h3><p>Creation of adaptor signatures in the case of numeric outcomes was made drastically faster.</p><h3>Introduction</h3><p>We’ve recently <a href="https://twitter.com/CryptoGarageInc/status/1358673826842742784">announced</a> that we implemented so called <em>numerical decomposition</em> in our <a href="https://github.com/p2pderivatives/p2pderivatives-client">P2PDerivatives application</a>. This technique makes it possible to reduce the number of adaptor signatures required to cover ranges of outcome with identical payout values (if you didn’t get the meaning of this sentence, the first section is for you, if you did, you can skip it). While this is a great improvement in terms of space (less signatures to exchange between two peers of a DLC), <a href="https://twitter.com/matthewjablack">Matthew Black</a> reported to us that the creation of adaptor signatures was significantly slower. In this article, we’ll first give a refresher on numerical decomposition, before explaining why this slow down happened, and then finally how we managed to significantly reduce it.</p><h3>Numerical Decomposition DLC</h3><p>Before using numerical decomposition, our Oracle (reporting BTC/USD rate) was simply signing a string representing the price at a certain time. Now imagine that we have Alice and Bob wanting to enter into a DLC based on BTC/USD price with the following conditions:</p><ul><li>Price above $50000 Alice gets everything,</li><li>Price between $40000 and $50000 there is a linear payout function distributing the total collateral to both parties,</li><li>Price below $40000 Bob gets everything</li></ul><p>If they want to make sure that they can close their contract properly for a price of Bitcoin between $0 and $1000000 they would have had to create 100000 different adaptor signatures. This is quite impractical, both in terms of generating these signatures, exchanging them and storing them.</p><p>With numerical decomposition, our Oracle now signs each digit separately. For example, if the price is $45000 (and that the Oracle is set to report on 8 digits), it would release signatures over the numbers ‘0’ ‘0’ ‘0’ ‘4’ ‘5’ ‘0’ ‘0’ ‘0’ separately.</p><p>How does that help Alice and Bob? As a quick example, in order to cover the range of outcome below $10000, they could just create adaptor points using the first four digits set to zero (‘0’ ‘0’ ‘0’ ‘0’) and ignore the remaining four digits. So they need a single adaptor signature instead of 10000 to cover the $0-$9999 range.</p><p>Note that in practice, our oracle signs binary representation of outcome values (instead of base 10 as in the example) as it was found to be more efficient. You can also find more details in the <a href="https://github.com/discreetlogcontracts/dlcspecs/blob/master/NumericOutcome.md">DLC specification repository</a>.</p><h3>Slow down</h3><p>So what caused the slow down of adaptor signature creation after switching to numerical decomposition? To understand, we need to dive a little bit into how adaptor signatures are created (nothing more than addition and multiplications so no worry!).</p><h4>Adaptor Signature</h4><p>Here we’ll only recall very quickly the concept of adaptor signatures, for a more in-depth description have a look at <a href="https://medium.com/crypto-garage/adaptor-signature-in-discreet-log-contracts-on-ecdsa-c8c04197e11f">our previous article on that</a>. An adaptor signature is a signature that has been encrypted with a secret, and for which we can prove that after decryption (or with knowledge of the secret) we obtain a valid signature for a given message. Under ECDSA, an adaptor signature <em>s’</em> for a signature <em>s</em> over a message <em>m</em> is created as follow:</p><p>Given <em>x</em> a private key and <em>t</em> a secret</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/135/1*MzElx-Mua3ixp_Nsdy-9EA.png" /></figure><p>the public image of <em>t</em> (also called adaptor point)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/143/1*ppm2yTTqSEqk9Dby8ziUjw.png" /></figure><p>the public key of <em>x</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/409/1*34WHQVJPfXnUbBaeybbsfA.png" /></figure><p>We’ll skip over the verification part for brevity (and again check <a href="https://medium.com/crypto-garage/adaptor-signature-in-discreet-log-contracts-on-ecdsa-c8c04197e11f">our adaptor series</a> if you’re interested), but with knowledge of the secret <em>t</em> we can obtain a valid signature <em>s</em> as follow:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/152/0*A1RWIPAAMYSOwXsC" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/516/0*Wj9srP2taJfST9b6" /></figure><p>Which is a valid ECDSA signature for <em>m</em>.</p><p>For DLC, we use the signature of an oracle as a secret to create adaptor signatures. The interesting thing is that the actual secret is not known by any of the parties involved at first. And the secret to most of the adaptor signatures that are created for a DLC will (hopefully) never be revealed, as the oracle is supposed to release a single signature for each event. So in practice, both parties compute the images of all the possible signatures that could be released by the oracle (also called signature points) and use them to encrypt their signatures to each of the payout transactions. Note that the oracle uses BIP340 Schnorr signatures whose equation looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/366/0*UMylrf_N3OHCbpny" /></figure><p>So for each possible <em>m</em> (which represents the value of a possible outcome of the DLC), the parties will create an adaptor point <em>S</em> as follow:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/503/0*IM0QbLHa8D5CVJ-w" /></figure><p>Note that <em>S</em> can be created using only publicly known values (recall that DLC oracles need to release the <em>R</em> values in advance for each event).</p><h4>Adaptor Signature with decomposition</h4><p>Now to understand why creating adaptor signatures is much slower with numerical decomposition, let’s see what changes. With numerical decomposition, we use as encryption secret the sum of all the signature images. So if we have an oracle that will release signatures <em>s0,s1,s2,…,s7</em>, we will use the adaptor point <em>S</em> constructed as follow:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/310/0*ayAhsgigUkiXzmHy" /></figure><p>Where each <em>Sx</em> is constructed as shown previously.</p><p>This works thanks to the linearity of Schnorr signatures, and enables us to later decrypt an adaptor signature using:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/289/0*tEXjU_EL68XFpCuC" /></figure><p>So we can see that the main difference with “regular” DLC is that we need to compute an adaptor point that is a sum of points.</p><h4>Adaptor Signature with decomposition and multi-Oracles</h4><p>Using a similar approach than with decomposition, it is possible to create adaptor signatures encrypted using signature points from multiple oracles. This is done by simply summing up the signature point for each oracle. Assuming oracles <em>A, B </em>and <em>C</em> and numerical decomposition, we can compute a signature point for each oracle as follow:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/375/0*Zz5IMSBdtTKtgpp8" /></figure><p>Then we can compute an aggregate <em>S</em> by summing up the signature points:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/262/0*qqOYyOd5CGLKLzx2" /></figure><h3>Understanding</h3><p>To understand why adaptor signature creation is slow when using numerical decomposition and multi oracles, we need to note that <a href="https://en.wikipedia.org/wiki/Elliptic_curve_point_multiplication">Elliptic Curve (EC) multiplications</a> are much more computationally intensive than addition. This is because EC multiplication is performed by successively adding a point to itself. So to compute:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/71/0*KzEyll02HSnZdqjc" /></figure><p>Where <em>A</em> is an EC point, we actually do:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/216/0*NwQyx8_auRe-qDXb" /></figure><p>While there exists algorithmic optimizations to compute them (like <a href="https://en.wikipedia.org/wiki/Elliptic_curve_point_multiplication#Double-and-add">double and add</a>), it remains generally true that multiplication of EC points is much more costly than addition.</p><p>Now let’s look again at our equation for computing signature points in the case of numerical decomposition:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*0YPK4fSKf3rbkUlF" /></figure><p>We can see that we have seven multiplications (or as many as we have nonces or digits). If we in addition use multi-oracle, this number gets multiplied by the number of oracles. We can thus hypothesize that this increase in the number of multiplication in adaptor point computation is the cause for the slowness of adaptor signature computation. To confirm this, we benchmarked the creation of a single adaptor signature on a single decomposed outcome with 5 oracles and 20 nonces for each oracle. Here are the results:</p><p>Full adaptor signature computation</p><p>6,444,640 ns/iter (+/- 628,459)</p><p>Adaptor signature computation from signature point</p><p>280,801 ns/iter (+/- 62,948)</p><p>Signature point computation</p><p>6,222,608 ns/iter (+/- 667,325)</p><p>We can see that indeed most of the time is spent on the computation of the signature point.</p><h3>Optimizing</h3><p>Now that we have an understanding of what makes the computation costly, let’s see how we can improve the situation.</p><h4>Factoring out</h4><p>Recall the equation for signature point computation in the case of numerical decomposition:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*0YPK4fSKf3rbkUlF" /></figure><p>Again we have seven multiplications, but notice that they all involve the public key <em>P</em>. So we can use something that you probably learned a long time ago, factorization! And thankfully it works as well with elliptic curves. So we can rewrite our equation as:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*BlBAqGhpyYT0fwbO" /></figure><p>And we now have a single EC multiplication operation!</p><p>To check we benchmark this factorized version against the “regular” one. We expect our factorization optimization to perform increasingly better than the original one with an increasing number of nonces. We verify this by benchmarking both for different numbers of nonces, with 5 oracles. The following graph shows the results:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*0glg4sRZZFG0pHf2" /></figure><p>We can see that while the original version is slightly better when using a single nonce, our optimized version is already faster when using two nonces.</p><p>While this seems already quite a good improvement, we still need to perform as many EC multiplication as the number of adaptor signatures that we compute.</p><h3>Pre-computing</h3><p>To further reduce the number of EC multiplications when creating a large number of signature points, we need to change strategy. Recall that the signature points for each “final” outcome is the sum of a set of signature points computed using the expected value of each digit. By pre-computing the set of signature points for each possible value of each digit, the maximum number of EC multiplication will thus become <em>nb_nonces * nb_outcome_per_nonce</em>, and after that we only need to perform additions. So if we have two nonces and two outcomes per nonce, we need to pre-compute:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/456/0*HU0WzduZ17Xe4f-8" /></figure><p>Then we can compute all possible combined signature points as follow:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/211/0*FMZ-GIwOg0xSyGM9" /></figure><p>In that example case we didn’t reduce the number of EC multiplication since we pre-computed 4 signature points and that we have 4 possible outcomes in total. But for example, with 10 nonces, instead of performing 2¹⁰=1024 EC multiplications, we only need to do 20.</p><p>To see the impact of this optimization, we benchmarked the creation of signature points for all possible outcomes when using 10 nonces, 3 oracles and 2 outcomes per nonce, for the “original” version, the factorized version and the pre-computation version.</p><p>Original</p><p>1,935,444,337 ns/iter (+/- 187,182,386)</p><p>Factorized</p><p>518,583,393 ns/iter (+/- 19,961,490)</p><p>Pre-computation</p><p>142,026,577 ns/iter (+/- 6,604,498)</p><p>We can see that as expected, the pre-computation version performs much better.</p><h3>Memoization</h3><p>To further optimize, we should note that in the above case, we re-compute some additions. For example, when computing the signature points for the outcomes with values “0 0 0 0 0 0 0 0 0 0” and “0 0 0 0 0 0 0 0 0 1”, the intermediary signature point for the value “0 0 0 0 0 0 0 0 0” is computed two times. By caching intermediate values, we can greatly reduce the number of additions performed. Roughly, the memoization algorithm that we apply works as follow:</p><ol><li>Compute signature point for value “0” with nonce R0</li><li>Compute signature point for value “0 0” adding the signature point using nonce R1 and value “0” to the previous obtained value</li><li>Compute signature point for value “0 0 0” adding the signature point using nonce R2 and value “0 0” to the previous obtained value</li><li>…</li></ol><p>Once we have computed the signature point for the outcome value “0 0 0 0 0 0 0 0 0 0”, we backtrack and reuse the signature point computed for value “0 0 0 0 0 0 0 0 0” to compute the signature point for value “0 0 0 0 0 0 0 0 0 1”. And so on and so forth until we have computed a signature point for all possible outcome values.</p><p>Using the same benchmark as above, we obtain the following result:</p><p>Memoization</p><p>42,049,313 ns/iter (+/- 2,844,778)</p><p>Which gives us an algorithm more than 3 times faster! We could expect that as the number of nonces increase, the performance gains will as well.</p><h3>Parallelizing</h3><p>Another common trick to improve algorithms performance is to parallelize operations, making it possible to run them on separate cores. While it is not always possible or worth doing, in our case we can easily parallelize the signature point computation for each oracle, as well as their aggregation. So instead of sequentially computing:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/342/0*EQ4BYoOKZmspoja0" /></figure><p>We can instead compute (where || means in parallel):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/262/0*nAawCej4V_-W_qFI" /></figure><p>then</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/425/0*uFYH8xaVfs00Smmv" /></figure><p>and finally</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/169/0*hMma6LB4Yk5F6R6m" /></figure><p>(note that in practice the order in which operations are performed is non deterministic)</p><p>Using the same benchmark as before, we obtain the following results:</p><p>Memoization + Parallelization</p><p>24,089,747 ns/iter (+/- 5,290,419)</p><p>Which is roughly a 2 times improvement. If we increase the number of oracles, the improvement will increase as well.</p><h3>Conclusion and future work</h3><p>Thanks to reducing the number of EC multiplications and applying parallelization, we were able to drastically improve the computation of signature points. As DLC is a peer to peer protocol, it is expected that adaptor signatures will be computed by end user devices, making it important to reduce the computational resources required. And while it might not always be possible to apply parallelization, the pre-computation and memoization approaches already showed to provide great benefits.</p><p>We will be implementing these optimizations in our <a href="https://github.com/p2pderivatives/p2pderivatives-client">P2PDerivatives application</a> in the near future to hopefully further improve user experience.</p><p>Finally, <a href="https://twitter.com/nadav_kohen">Nadav Kohen</a> mentioned that we could further improve by adapting the batch verification algorithm mentioned in <a href="https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki#batch-verification">BIP340</a>. <a href="https://mailmanlists.org/pipermail/dlc-dev/2020-December/000002.html">A proposal</a> from <a href="https://twitter.com/llfourn">Lloyd Fournier</a> to replace the message hashes by indexes could also yield further gains. We’ll hopefully cover that in a follow up article, so don’t forget to <a href="https://twitter.com/intent/user?screen_name=cryptogarageinc">follow us on Twitter</a> to be notified!</p><h3>Acknowledgements</h3><p>Thanks to <a href="https://twitter.com/llfourn">Lloyd Fournier</a>, <a href="https://twitter.com/1_LOW_0219">Ichiro Kuwahara</a>, <a href="https://twitter.com/nadav_kohen">Nadav Kohen</a> and <a href="https://twitter.com/matthewjablack">Matthew Black</a> for useful suggestions and discussions that led to the various optimizations described in this article.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6d6091ac0e47" width="1" height="1" alt=""><hr><p><a href="https://medium.com/crypto-garage/optimizing-numeric-outcome-dlc-creation-6d6091ac0e47">Optimizing Numeric Outcome DLC creation</a> was originally published in <a href="https://medium.com/crypto-garage">Crypto Garage</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>