{"id":5324,"date":"2024-11-25T22:56:26","date_gmt":"2024-11-25T21:56:26","guid":{"rendered":"https:\/\/wsj-crypto.com\/?p=5324"},"modified":"2024-11-25T22:56:26","modified_gmt":"2024-11-25T21:56:26","slug":"formal-verification-achieved-for-safegcds-implementation","status":"publish","type":"post","link":"https:\/\/wsj-crypto.com\/index.php\/2024\/11\/25\/formal-verification-achieved-for-safegcds-implementation\/","title":{"rendered":"Formal Verification Achieved for Safegcd&#8217;s Implementation"},"content":{"rendered":"<br \/>\n<h2>Overview<\/h2>\n<p>The protection of Bitcoin and other blockchains, like Liquid, relies on the implementation of digital signature algorithms, including ECDSA and Schnorr signatures. A C library known as libsecp256k1, named after the elliptic curve it functions on, is utilized by both Bitcoin Core and Liquid to offer these digital signature algorithms. These algorithms incorporate a mathematical process known as a <em>modular inverse<\/em>, which constitutes a relatively resource-intensive aspect of the calculations.<\/p>\n<p>In \u201c<a href=\"https:\/\/gcd.cr.yp.to\/papers.html#safegcd\">Fast constant-time gcd computation and modular inversion<\/a>,\u201d Daniel J. Bernstein and Bo-Yin Yang introduce a new modular inversion method. In 2021, this method, termed \u201csafegcd,\u201d was <a href=\"https:\/\/github.com\/bitcoin-core\/secp256k1\/pull\/831\">applied<\/a> to libsecp256k1 by Peter Dettman. As part of the evaluation process for this innovative algorithm, Blockstream Research was the first to execute a <a href=\"https:\/\/blog.blockstream.com\/a-formal-proof-of-safegcd-bounds\/\">formal validation<\/a> of the algorithm\u2019s structure, utilizing the Coq proof assistant to confirm that the algorithm does indeed complete with the correct modular inverse output for 256-bit inputs.<\/p>\n<h2>The Disparity between Algorithm and Implementation<\/h2>\n<p>The formalization initiative in 2021 simply demonstrated that the algorithm created by Bernstein and Yang functions accurately. However, employing that algorithm in libsecp256k1 necessitates the translation of the mathematical characterization of the safegcd algorithm into the C programming language. For instance, the mathematical representation of the algorithm executes matrix multiplication of vectors that can extend up to 256 bit signed integers, but the C programming language primarily supports integers up to 64 bits (or 128 bits with certain language extensions).<\/p>\n<p>To implement the safegcd algorithm, one needs to program the matrix multiplication and other calculations utilizing C\u2019s 64-bit integers. Furthermore, <a href=\"https:\/\/github.com\/bitcoin-core\/secp256k1\/blob\/master\/doc\/safegcd_implementation.md\">numerous additional enhancements<\/a> have been incorporated to accelerate the implementation. Ultimately, there are four distinct implementations of the safegcd algorithm in libsecp256k1: two constant time algorithms for signature generation, one tailored for 32-bit systems and another for 64-bit systems, alongside two variable time algorithms for signature validation, again one for 32-bit systems and one for 64-bit systems.<\/p>\n<h2>Verifiable C<\/h2>\n<p>To ensure that the C code accurately executes the safegcd algorithm, all implementation specifics must be scrutinized. We use <a href=\"https:\/\/vst.cs.princeton.edu\/\">Verifiable C<\/a>, a part of the Verified Software Toolchain, for reasoning about C code with the aid of the Coq theorem prover.<\/p>\n<p>The verification process commences by outlining preconditions and postconditions using separation logic for each function subjected to verification. <a href=\"https:\/\/en.wikipedia.org\/wiki\/Separation_logic\">Separation logic<\/a> is a logic tailored for reasoning about subroutines, memory allocations, concurrency, and more.<\/p>\n<p>After assigning a specification to each function, verification moves forward by starting from a function\u2019s precondition, establishing a new invariant after each statement within the function body, culminating in the establishment of the postcondition at the conclusion of the function body or at the end of each return statement. Much of the formalization effort is concentrated \u201cbetween\u201d the lines of code, employing the invariants to interpret the basic operations of each C expression into more abstract statements about what the data structures being manipulated signify mathematically. For instance, what the C language perceives as an array of 64-bit integers may, in actuality, represent a 256-bit integer.<\/p>\n<p>The final outcome is <a href=\"https:\/\/htmlpreview.github.io\/?https:\/\/github.com\/BlockstreamResearch\/simplicity\/blob\/master\/alectryon\/verif_modinv64_impl.v.html\">a formal proof<\/a>, validated by the Coq proof assistant, demonstrating that libsecp256k1\u2019s 64-bit variable time implementation of the safegcd modular inverse algorithm is functionally sound.<\/p>\n<h2>Constraints of the Verification<\/h2>\n<p>There are several constraints to the proof of functional correctness. The separation logic employed in Verifiable C enforces what is termed as <a href=\"https:\/\/en.wikipedia.org\/wiki\/Correctness_(computer_science)\">partial correctness<\/a>. This indicates that it only certifies the C code returns with the correct result <em>if<\/em> it returns, but it does not ensure termination itself. We address this limitation by utilizing <a href=\"https:\/\/blog.blockstream.com\/a-formal-proof-of-safegcd-bounds\/\">our prior Coq proof<\/a> regarding the bounds on the safegcd algorithm to confirm that the loop counter in the primary loop never surpasses 11 iterations.<\/p>\n<p>Another concern is that the C language lacks a formal specification. Instead, the Verifiable C project employs the <a href=\"https:\/\/compcert.org\/\">CompCert compiler project<\/a> to provide a formal specification of the C language. This ensures that when a verified C program is compiled using the CompCert compiler, the resulting assembly code will adhere to its specification (subject to the aforementioned limitation). Nevertheless, this does not guarantee that the code produced by GCC, clang, or any other compiler will necessarily function correctly. For instance, C compilers may follow different evaluation orders for arguments within a function call. Even if the C language had a formal specification, any compiler that is not itself formally verified could still miscompile programs. This does <a href=\"https:\/\/github.com\/bitcoin-core\/secp256k1\/issues\/823\">occur<\/a> in practical scenarios.<\/p>\n<p>Finally, Verifiable C does not accommodate passing structures, returning structures, or assigning structures. Although in libsecp256k1, structures are consistently passed by pointer (which is permitted in Verifiable C), there are some instances where structure assignments are utilized. For the modular inverse correctness proof, there were 3 assignments that needed to be substituted with a specialized function call that executes the structure assignment field by field.<\/p>\n<h2>Conclusion<\/h2>\n<p>Blockstream Research has formally validated the accuracy of libsecp256k1\u2019s modular inverse function. This initiative further substantiates that the verification of C code is feasible in real-world applications. Employing a general-purpose proof assistant enables us to validate software constructed upon intricate mathematical foundations.<\/p>\n<p>There is nothing preventing the rest of the functions implemented within libsecp256k1 from being verified as well. Thus, it remains possible for libsecp256k1 to achieve the highest attainable software correctness guarantees.<\/p>\n<p><em>This is a guest article by Russell O&#8217;Connor and Andrew Poelstra. The views expressed are solely their own and do not necessarily represent those of BTC Inc or Bitcoin Magazine.<\/em><\/p>\n<p><br \/>\n<br \/><a href=\"https:\/\/bitcoinmagazine.com\/technical\/safegcds-implementation-formally-verified\">Source link <\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Overview The protection of Bitcoin and other blockchains, like Liquid, relies on the implementation of digital signature algorithms, including ECDSA and Schnorr signatures. A C library known as libsecp256k1, named after the elliptic curve it functions on, is utilized by both Bitcoin Core and Liquid to offer these digital signature algorithms. These algorithms incorporate a<\/p>\n","protected":false},"author":3,"featured_media":5325,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22],"tags":[162],"class_list":{"0":"post-5324","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-bitcoin","8":"tag-return-a-list-of-comma-separated-tags-from-this-title-safegcds-implementation-formally-verified"},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Formal Verification Achieved for Safegcd&#039;s Implementation - WSJ-Crypto<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/wsj-crypto.com\/index.php\/2024\/11\/25\/formal-verification-achieved-for-safegcds-implementation\/\" \/>\n<meta property=\"og:locale\" content=\"it_IT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Formal Verification Achieved for Safegcd&#039;s Implementation - WSJ-Crypto\" \/>\n<meta property=\"og:description\" content=\"Overview The protection of Bitcoin and other blockchains, like Liquid, relies on the implementation of digital signature algorithms, including ECDSA and Schnorr signatures. A C library known as libsecp256k1, named after the elliptic curve it functions on, is utilized by both Bitcoin Core and Liquid to offer these digital signature algorithms. These algorithms incorporate a\" \/>\n<meta property=\"og:url\" content=\"https:\/\/wsj-crypto.com\/index.php\/2024\/11\/25\/formal-verification-achieved-for-safegcds-implementation\/\" \/>\n<meta property=\"og:site_name\" content=\"WSJ-Crypto\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-25T21:56:26+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/wsj-crypto.com\/wp-content\/uploads\/2024\/11\/leonardo_lightning_xl_cryptography_1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1067\" \/>\n\t<meta property=\"og:image:height\" content=\"800\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"wsjcrypto\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Scritto da\" \/>\n\t<meta name=\"twitter:data1\" content=\"wsjcrypto\" \/>\n\t<meta name=\"twitter:label2\" content=\"Tempo di lettura stimato\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minuti\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/wsj-crypto.com\/index.php\/2024\/11\/25\/formal-verification-achieved-for-safegcds-implementation\/\",\"url\":\"https:\/\/wsj-crypto.com\/index.php\/2024\/11\/25\/formal-verification-achieved-for-safegcds-implementation\/\",\"name\":\"Formal Verification Achieved for Safegcd's Implementation - WSJ-Crypto\",\"isPartOf\":{\"@id\":\"https:\/\/wsj-crypto.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/wsj-crypto.com\/index.php\/2024\/11\/25\/formal-verification-achieved-for-safegcds-implementation\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/wsj-crypto.com\/index.php\/2024\/11\/25\/formal-verification-achieved-for-safegcds-implementation\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/wsj-crypto.com\/wp-content\/uploads\/2024\/11\/leonardo_lightning_xl_cryptography_1.jpg\",\"datePublished\":\"2024-11-25T21:56:26+00:00\",\"author\":{\"@id\":\"https:\/\/wsj-crypto.com\/#\/schema\/person\/88a93723b30416db1a352d5a0096c4a7\"},\"breadcrumb\":{\"@id\":\"https:\/\/wsj-crypto.com\/index.php\/2024\/11\/25\/formal-verification-achieved-for-safegcds-implementation\/#breadcrumb\"},\"inLanguage\":\"it-IT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/wsj-crypto.com\/index.php\/2024\/11\/25\/formal-verification-achieved-for-safegcds-implementation\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"it-IT\",\"@id\":\"https:\/\/wsj-crypto.com\/index.php\/2024\/11\/25\/formal-verification-achieved-for-safegcds-implementation\/#primaryimage\",\"url\":\"https:\/\/wsj-crypto.com\/wp-content\/uploads\/2024\/11\/leonardo_lightning_xl_cryptography_1.jpg\",\"contentUrl\":\"https:\/\/wsj-crypto.com\/wp-content\/uploads\/2024\/11\/leonardo_lightning_xl_cryptography_1.jpg\",\"width\":1067,\"height\":800},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/wsj-crypto.com\/index.php\/2024\/11\/25\/formal-verification-achieved-for-safegcds-implementation\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/wsj-crypto.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Formal Verification Achieved for Safegcd&#8217;s Implementation\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/wsj-crypto.com\/#website\",\"url\":\"https:\/\/wsj-crypto.com\/\",\"name\":\"WSJ-Crypto\",\"description\":\"Just Another Crypto News Website\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/wsj-crypto.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"it-IT\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/wsj-crypto.com\/#\/schema\/person\/88a93723b30416db1a352d5a0096c4a7\",\"name\":\"wsjcrypto\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"it-IT\",\"@id\":\"https:\/\/wsj-crypto.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/86fe8af82ea089646d6639ca2f87e0243d8688d957bd8e3ec22ec3c457cc16d4?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/86fe8af82ea089646d6639ca2f87e0243d8688d957bd8e3ec22ec3c457cc16d4?s=96&d=mm&r=g\",\"caption\":\"wsjcrypto\"},\"url\":\"https:\/\/wsj-crypto.com\/index.php\/author\/wsjcrypto\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Formal Verification Achieved for Safegcd's Implementation - WSJ-Crypto","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/wsj-crypto.com\/index.php\/2024\/11\/25\/formal-verification-achieved-for-safegcds-implementation\/","og_locale":"it_IT","og_type":"article","og_title":"Formal Verification Achieved for Safegcd's Implementation - WSJ-Crypto","og_description":"Overview The protection of Bitcoin and other blockchains, like Liquid, relies on the implementation of digital signature algorithms, including ECDSA and Schnorr signatures. A C library known as libsecp256k1, named after the elliptic curve it functions on, is utilized by both Bitcoin Core and Liquid to offer these digital signature algorithms. These algorithms incorporate a","og_url":"https:\/\/wsj-crypto.com\/index.php\/2024\/11\/25\/formal-verification-achieved-for-safegcds-implementation\/","og_site_name":"WSJ-Crypto","article_published_time":"2024-11-25T21:56:26+00:00","og_image":[{"width":1067,"height":800,"url":"https:\/\/wsj-crypto.com\/wp-content\/uploads\/2024\/11\/leonardo_lightning_xl_cryptography_1.jpg","type":"image\/jpeg"}],"author":"wsjcrypto","twitter_card":"summary_large_image","twitter_misc":{"Scritto da":"wsjcrypto","Tempo di lettura stimato":"5 minuti"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/wsj-crypto.com\/index.php\/2024\/11\/25\/formal-verification-achieved-for-safegcds-implementation\/","url":"https:\/\/wsj-crypto.com\/index.php\/2024\/11\/25\/formal-verification-achieved-for-safegcds-implementation\/","name":"Formal Verification Achieved for Safegcd's Implementation - WSJ-Crypto","isPartOf":{"@id":"https:\/\/wsj-crypto.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/wsj-crypto.com\/index.php\/2024\/11\/25\/formal-verification-achieved-for-safegcds-implementation\/#primaryimage"},"image":{"@id":"https:\/\/wsj-crypto.com\/index.php\/2024\/11\/25\/formal-verification-achieved-for-safegcds-implementation\/#primaryimage"},"thumbnailUrl":"https:\/\/wsj-crypto.com\/wp-content\/uploads\/2024\/11\/leonardo_lightning_xl_cryptography_1.jpg","datePublished":"2024-11-25T21:56:26+00:00","author":{"@id":"https:\/\/wsj-crypto.com\/#\/schema\/person\/88a93723b30416db1a352d5a0096c4a7"},"breadcrumb":{"@id":"https:\/\/wsj-crypto.com\/index.php\/2024\/11\/25\/formal-verification-achieved-for-safegcds-implementation\/#breadcrumb"},"inLanguage":"it-IT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/wsj-crypto.com\/index.php\/2024\/11\/25\/formal-verification-achieved-for-safegcds-implementation\/"]}]},{"@type":"ImageObject","inLanguage":"it-IT","@id":"https:\/\/wsj-crypto.com\/index.php\/2024\/11\/25\/formal-verification-achieved-for-safegcds-implementation\/#primaryimage","url":"https:\/\/wsj-crypto.com\/wp-content\/uploads\/2024\/11\/leonardo_lightning_xl_cryptography_1.jpg","contentUrl":"https:\/\/wsj-crypto.com\/wp-content\/uploads\/2024\/11\/leonardo_lightning_xl_cryptography_1.jpg","width":1067,"height":800},{"@type":"BreadcrumbList","@id":"https:\/\/wsj-crypto.com\/index.php\/2024\/11\/25\/formal-verification-achieved-for-safegcds-implementation\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/wsj-crypto.com\/"},{"@type":"ListItem","position":2,"name":"Formal Verification Achieved for Safegcd&#8217;s Implementation"}]},{"@type":"WebSite","@id":"https:\/\/wsj-crypto.com\/#website","url":"https:\/\/wsj-crypto.com\/","name":"WSJ-Crypto","description":"Just Another Crypto News Website","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/wsj-crypto.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"it-IT"},{"@type":"Person","@id":"https:\/\/wsj-crypto.com\/#\/schema\/person\/88a93723b30416db1a352d5a0096c4a7","name":"wsjcrypto","image":{"@type":"ImageObject","inLanguage":"it-IT","@id":"https:\/\/wsj-crypto.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/86fe8af82ea089646d6639ca2f87e0243d8688d957bd8e3ec22ec3c457cc16d4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/86fe8af82ea089646d6639ca2f87e0243d8688d957bd8e3ec22ec3c457cc16d4?s=96&d=mm&r=g","caption":"wsjcrypto"},"url":"https:\/\/wsj-crypto.com\/index.php\/author\/wsjcrypto\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/wsj-crypto.com\/index.php\/wp-json\/wp\/v2\/posts\/5324","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wsj-crypto.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wsj-crypto.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wsj-crypto.com\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/wsj-crypto.com\/index.php\/wp-json\/wp\/v2\/comments?post=5324"}],"version-history":[{"count":2,"href":"https:\/\/wsj-crypto.com\/index.php\/wp-json\/wp\/v2\/posts\/5324\/revisions"}],"predecessor-version":[{"id":5327,"href":"https:\/\/wsj-crypto.com\/index.php\/wp-json\/wp\/v2\/posts\/5324\/revisions\/5327"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wsj-crypto.com\/index.php\/wp-json\/wp\/v2\/media\/5325"}],"wp:attachment":[{"href":"https:\/\/wsj-crypto.com\/index.php\/wp-json\/wp\/v2\/media?parent=5324"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wsj-crypto.com\/index.php\/wp-json\/wp\/v2\/categories?post=5324"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wsj-crypto.com\/index.php\/wp-json\/wp\/v2\/tags?post=5324"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}