diff --git a/Cargo.toml b/Cargo.toml index 9daa25d25..bae133b8d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [workspace] members = ["crates/specs", "crates/zkwasm", "crates/cli", "crates/host"] -exclude = ["third-party/wasmi", "crates/playground"] +exclude = ["third-party/wasmi", "crates/playground","crates/key_tool"] resolver = "2" [workspace.dependencies] diff --git a/crates/key_tool/Cargo.lock b/crates/key_tool/Cargo.lock new file mode 100644 index 000000000..c5e4cec01 --- /dev/null +++ b/crates/key_tool/Cargo.lock @@ -0,0 +1,446 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "anyhow" +version = "1.0.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da" + +[[package]] +name = "autocfg" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" + +[[package]] +name = "bitvec" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bc2832c24239b0141d5674bb9174f9d68a8b5b3f2753311927c172ca46f7e9c" +dependencies = [ + "funty", + "radium", + "tap", + "wyz", +] + +[[package]] +name = "bls12_381" +version = "0.7.0" +source = "git+https://github.com/lanbones/bls12_381.git#31fcd53a340cadbb5558bb75422516b81704a0a1" +dependencies = [ + "ff", + "group", + "pairing", + "rand_core 0.6.4", + "subtle", +] + +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "chksum-core" +version = "0.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6db20071fdeca52ed6a7745519fb2d343fddcb93af81448373b851f072aaec5" +dependencies = [ + "chksum-hash-core", + "thiserror", +] + +[[package]] +name = "chksum-hash-core" +version = "0.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "221456234d441c788a2c51a27b91c4380f499de560670a67d3303e621d37b3bd" + +[[package]] +name = "chksum-hash-md5" +version = "0.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "80c33d01c33c9e193fe33e719a29a7eb900c08583375dd1d3269991aacbe434a" +dependencies = [ + "chksum-hash-core", + "thiserror", +] + +[[package]] +name = "chksum-md5" +version = "0.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95dda0f76fbb6069e042c370a928457086e1b4eabc7e75f5f49fe1b913634351" +dependencies = [ + "chksum-core", + "chksum-hash-md5", +] + +[[package]] +name = "ff" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d013fc25338cc558c5c2cfbad646908fb23591e2404481826742b651c9af7160" +dependencies = [ + "bitvec", + "rand_core 0.6.4", + "subtle", +] + +[[package]] +name = "ff_ce" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38107cbd8bac0d907d7e7513c9f68c95adbda9e6f6f6bdf3f5111c6ecac4fe47" +dependencies = [ + "byteorder", + "ff_derive_ce", + "hex", + "rand 0.4.6", +] + +[[package]] +name = "ff_derive_ce" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dde5a00073374b4d7aa2d3a8359a5709f9c0bfac8393f254655d16b4acdfe823" +dependencies = [ + "num-bigint", + "num-integer", + "num-traits", + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "fuchsia-cprng" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" + +[[package]] +name = "funty" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c" + +[[package]] +name = "getrandom" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "group" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5dfbfb3a6cfbd390d5c9564ab283a0349b9b9fcd46a706c1eb10e0db70bfbac7" +dependencies = [ + "ff", + "rand_core 0.6.4", + "subtle", +] + +[[package]] +name = "hex" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" + +[[package]] +name = "key_tool" +version = "0.1.0" +dependencies = [ + "anyhow", + "byteorder", + "chksum-md5", + "num-bigint", + "num-traits", + "pairing_bn256", + "pairing_ce", +] + +[[package]] +name = "libc" +version = "0.2.155" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" + +[[package]] +name = "num-bigint" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5e44f723f1133c9deac646763579fdb3ac745e418f2a7af9cd0c431da1f20b9" +dependencies = [ + "num-integer", + "num-traits", +] + +[[package]] +name = "num-integer" +version = "0.1.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + +[[package]] +name = "pairing" +version = "0.22.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "135590d8bdba2b31346f9cd1fb2a912329f5135e832a4f422942eb6ead8b6b3b" +dependencies = [ + "group", +] + +[[package]] +name = "pairing_bn256" +version = "0.1.1" +source = "git+https://github.com/lanbones/pairing.git#5ab08062f53cd29c946f96572aefdb688952d725" +dependencies = [ + "bls12_381", + "ff", + "group", + "rand 0.8.5", + "rand_core 0.6.4", + "static_assertions", + "subtle", +] + +[[package]] +name = "pairing_ce" +version = "0.24.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e753515675eaaa98071d814bea0148ae8c9d7995fa0531bf222e7857e3f1759" +dependencies = [ + "byteorder", + "cfg-if", + "ff_ce", + "rand 0.4.6", +] + +[[package]] +name = "ppv-lite86" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" + +[[package]] +name = "proc-macro2" +version = "1.0.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "radium" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09" + +[[package]] +name = "rand" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293" +dependencies = [ + "fuchsia-cprng", + "libc", + "rand_core 0.3.1", + "rdrand", + "winapi", +] + +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core 0.6.4", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core 0.6.4", +] + +[[package]] +name = "rand_core" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b" +dependencies = [ + "rand_core 0.4.2", +] + +[[package]] +name = "rand_core" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc" + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + +[[package]] +name = "rdrand" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2" +dependencies = [ + "rand_core 0.3.1", +] + +[[package]] +name = "static_assertions" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" + +[[package]] +name = "subtle" +version = "2.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" + +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "syn" +version = "2.0.72" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc4b9b9bf2add8093d3f2c0204471e951b2285580335de42f9d2534f3ae7a8af" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "tap" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" + +[[package]] +name = "thiserror" +version = "1.0.63" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0342370b38b6a11b6cc11d6a805569958d54cfa061a29969c3b5ce2ea405724" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.63" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4558b58466b9ad7ca0f102865eccc95938dca1a74a856f2b57b6629050da261" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.72", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "wyz" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05f360fc0b24296329c78fda852a1e9ae82de9cf7b27dae4b7f62f118f77b9ed" +dependencies = [ + "tap", +] diff --git a/crates/key_tool/Cargo.toml b/crates/key_tool/Cargo.toml new file mode 100644 index 000000000..3b893afb5 --- /dev/null +++ b/crates/key_tool/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "key_tool" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +pairing_bn256 = { git = "https://github.com/lanbones/pairing.git" } +anyhow = "1.0.81" +pairing_ce = "0.24.2" +chksum-md5 = "0.0.0" +byteorder = "1.5.0" +num-traits = "0.2.19" +num-bigint = "0.4.6" \ No newline at end of file diff --git a/crates/key_tool/LICENSE-APACHE b/crates/key_tool/LICENSE-APACHE new file mode 100644 index 000000000..16fe87b06 --- /dev/null +++ b/crates/key_tool/LICENSE-APACHE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + +2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + +3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + +4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + +5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + +6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + +8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS + +APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + +Copyright [yyyy] [name of copyright owner] + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. diff --git a/crates/key_tool/LICENSE-MIT b/crates/key_tool/LICENSE-MIT new file mode 100644 index 000000000..31aa79387 --- /dev/null +++ b/crates/key_tool/LICENSE-MIT @@ -0,0 +1,23 @@ +Permission is hereby granted, free of charge, to any +person obtaining a copy of this software and associated +documentation files (the "Software"), to deal in the +Software without restriction, including without +limitation the rights to use, copy, modify, merge, +publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software +is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice +shall be included in all copies or substantial portions +of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF +ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED +TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT +SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR +IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. diff --git a/crates/key_tool/README.md b/crates/key_tool/README.md new file mode 100644 index 000000000..2d89359ec --- /dev/null +++ b/crates/key_tool/README.md @@ -0,0 +1,32 @@ +# transfer zksync key to halo2 key + +The zkSync keys are divided into two files: one for the monomial key and one for the Lagrange key. + +the halo2 key is a single file that contains both the monomial and Lagrange. + +Therefore, a tool is needed to convert the two zkSync key files into a single halo2 key file. +## Features + +- Converts zkSync key to halo2 key + + +## Download zksync key +Download links for K23: + +monomial: https://storage.googleapis.com/universal-setup/setup_2%5E23.key + +lagrange: https://storage.googleapis.com/universal-setup/setup_2%5E23_lagrange.key + +If you want to download files for other K values, simply change the corresponding number in the link. + +## Usage + +To run the tool, use the following command: + +```sh +cargo run -- ./setup_2^22.key ./setup_2^22_lagrange.key ./K22.params + +Three input parameters are required: +1.The path to the monomial key. +2.The path to the lagrange key. +3.The path to the output halo2 key. \ No newline at end of file diff --git a/crates/key_tool/src/file_util.rs b/crates/key_tool/src/file_util.rs new file mode 100644 index 000000000..8d83c3221 --- /dev/null +++ b/crates/key_tool/src/file_util.rs @@ -0,0 +1,68 @@ +// This file contains code copied from https://github.com/matter-labs/bellman +// Licensed under the APACHE License and MIT License. +// See LICENSE file for details. +use byteorder::BigEndian; +use byteorder::ReadBytesExt; +use pairing_bn256::bn256::G1Affine; +use pairing_bn256::group::GroupEncoding; +use pairing_ce::bn256::G1Affine as G1AffineCE; +use pairing_ce::bn256::G2Affine as G2AffineCE; +use pairing_ce::CurveAffine as CurveAffineCE; +use pairing_ce::EncodedPoint; +use std::io; +use std::io::Read; + +// Read the zkSync key file and convert it to pairing_ce::G1Affine and G2Affine. +pub fn read_zk_sync_key( + mut reader: R, +) -> anyhow::Result<(Vec, Vec)> { + let mut g1_repr = ::Uncompressed::empty(); + let mut g2_repr = ::Uncompressed::empty(); + + let num_g1 = reader.read_u64::()?; + + let mut g1_bases = Vec::with_capacity(num_g1 as usize); + + for _ in 0..num_g1 { + reader.read_exact(g1_repr.as_mut())?; + let p = g1_repr + .into_affine() + .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?; + g1_bases.push(p); + } + + let num_g2 = reader.read_u64::()?; + assert!(num_g2 == 2u64); + + let mut g2_bases = Vec::with_capacity(num_g2 as usize); + + for _ in 0..num_g2 { + reader.read_exact(g2_repr.as_mut())?; + let p = g2_repr + .into_affine() + .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?; + g2_bases.push(p); + } + return Ok((g1_bases, g2_bases)); +} + +// Write the data required by Halo2 to a file. +pub fn write_halo2_params( + writer: &mut W, + k: u32, + g: Vec, + g_lagrange: Vec, + additional_data: Vec, +) -> anyhow::Result<()> { + writer.write_all(&k.to_le_bytes())?; + for el in &g { + writer.write_all(el.to_bytes().as_ref())?; + } + for el in &g_lagrange { + writer.write_all(el.to_bytes().as_ref())?; + } + let additional_data_len = additional_data.len() as u32; + writer.write_all(&additional_data_len.to_le_bytes())?; + writer.write_all(&additional_data)?; + return Ok(()); +} diff --git a/crates/key_tool/src/main.rs b/crates/key_tool/src/main.rs new file mode 100644 index 000000000..89403094a --- /dev/null +++ b/crates/key_tool/src/main.rs @@ -0,0 +1,124 @@ +mod file_util; +use crate::file_util::read_zk_sync_key; +use crate::file_util::write_halo2_params; +use num_traits::Num; +use pairing_bn256::arithmetic::CurveAffine; +use pairing_bn256::bn256::Fq; +use pairing_bn256::bn256::Fq2; +use pairing_bn256::bn256::G1Affine; +use pairing_bn256::bn256::G2Affine; +use pairing_bn256::group::ff::PrimeField; +use pairing_bn256::group::GroupEncoding; +use pairing_ce::bn256::Fq as FqCE; +use pairing_ce::bn256::G2Affine as G2AffineCE; +use pairing_ce::CurveAffine as CurveAffineCE; +use std::env; +use std::fs::File; +use std::io::BufReader; +use std::path::Path; +use std::process; +use std::thread; + +// Convert pairing_ce::G2Affine to pairing::G2Affine. +fn trans_g2(g2: G2AffineCE) -> G2Affine { + let (x_ce, y_ce) = g2.as_xy(); + + let mut x = Fq2::default(); + x.c0 = trans_fq(x_ce.c0); + x.c1 = trans_fq(x_ce.c1); + let mut y = Fq2::default(); + y.c0 = trans_fq(y_ce.c0); + y.c1 = trans_fq(y_ce.c1); + return G2Affine::from_xy(x, y).unwrap(); +} + +// Convert pairing_ce::Fq to pairing::Fq. +fn trans_fq(x: FqCE) -> Fq { + let pp = Fq::from_str_vartime(&*extract_decimal_from_string(&x.to_string())).unwrap(); + return pp; +} + +// Convert fq to a positive decimal. +// input="Fq(0x24fc1e1c263a7de7abec5edaeea87625890c96a018bb8c60522333fa206f70c3)" +// output=16728715820616582450594109459208172618408974327542441440317506932429837791427 +fn extract_decimal_from_string(s: &str) -> String { + let hex_str = &s[5..s.len() - 1]; + let tt = num_bigint::BigUint::from_str_radix(hex_str, 16) + .unwrap() + .to_string(); + return tt; +} + +fn check_file_exist(monomial_key_file: String, lagrange_key_file: String) -> bool { + if !Path::new(&monomial_key_file).exists() { + println!("monomial_key_file not exist {:?}", monomial_key_file); + return false; + } + if !Path::new(&lagrange_key_file).exists() { + println!("lagrange_key_file not exist {:?}", lagrange_key_file); + return false; + } + return true; +} + +fn main() { + let args: Vec = env::args().collect(); + if args.len() != 4 { + eprintln!("Usage: cargo run -- "); + process::exit(1); + } + + let monomial_key_file = &args[1]; + let lagrange_key_file = &args[2]; + let output_halo2_key_file = &args[3]; + println!("monomial_key_file={:?}", monomial_key_file); + println!("lagrange_key_file={:?}", lagrange_key_file); + println!("output_halo2_key_file={:?}", output_halo2_key_file); + + if !check_file_exist(monomial_key_file.clone(), lagrange_key_file.clone()) { + process::exit(1); + } + + let mut buf_reader_lagrange = + BufReader::with_capacity(1 << 29, File::open(lagrange_key_file).unwrap()); + let (lagrange_key, g2_base) = read_zk_sync_key(&mut buf_reader_lagrange).unwrap(); + + let mut buf_reader_monomial = + BufReader::with_capacity(1 << 29, File::open(monomial_key_file).unwrap()); + let (monomial_key, _) = read_zk_sync_key(&mut buf_reader_monomial).unwrap(); + + let handle_lagrange = thread::spawn(move || { + let mut g_lagrange = Vec::new(); + for index in 0..lagrange_key.len() { + let (x, y) = lagrange_key[index].as_xy(); + g_lagrange.push(G1Affine::from_xy(trans_fq(*x), trans_fq(*y)).unwrap()); + } + return g_lagrange; + }); + + let handle_normal = thread::spawn(move || { + let mut g = Vec::new(); + for index in 0..monomial_key.len() { + let (x, y) = monomial_key[index].as_xy(); + g.push(G1Affine::from_xy(trans_fq(*x), trans_fq(*y)).unwrap()); + } + return g; + }); + println!( + "finish read zksync keys monomial={:?} lagrange{:?}", + monomial_key_file, lagrange_key_file + ); + + let g_lagrange = handle_lagrange.join().unwrap(); + let g_monomial = handle_normal.join().unwrap(); + let additional_data = trans_g2(g2_base[1]).to_bytes().as_ref().to_vec(); + let k = (g_lagrange.len() as f64).log2() as u32; + + let mut fd = File::create(output_halo2_key_file).unwrap(); + write_halo2_params(&mut fd, k, g_monomial, g_lagrange, additional_data) + .expect("write halo2 to file failed"); + println!( + "finish write halo2 params k={:?} output_halo2_key_file={:?}", + k, output_halo2_key_file + ) +}