├── .gitignore ├── LICENSE ├── README.md ├── conway ├── .cargo │ └── config.toml ├── Cargo.lock ├── Cargo.toml ├── LICENSE ├── LICENSE.TXT ├── README.md ├── rust-toolchain.toml └── src │ ├── lib.rs │ └── main.rs ├── ed25519 ├── .cargo │ └── config.toml ├── Cargo.lock ├── Cargo.toml ├── README.md └── src │ └── main.rs ├── factorial ├── .cargo │ └── config.toml ├── Cargo.lock ├── Cargo.toml ├── README.md ├── rust-toolchain.toml └── src │ └── main.rs ├── fibonacci ├── .cargo │ └── config.toml ├── Cargo.lock ├── Cargo.toml ├── LICENSE.TXT ├── README.md ├── rust-toolchain.toml └── src │ └── main.rs ├── fizzbuzz ├── .cargo │ └── config.toml ├── Cargo.lock ├── Cargo.toml ├── README.md ├── rust-toolchain.toml └── src │ └── main.rs ├── grep ├── .cargo │ └── config.toml ├── Cargo.lock ├── Cargo.toml ├── README.md ├── rust-toolchain.toml └── src │ └── main.rs ├── guessing_game ├── .cargo │ └── config.toml ├── .gitignore ├── Cargo.lock ├── Cargo.toml ├── LICENSE ├── README.md ├── rust-toolchain.toml └── src │ └── main.rs ├── hello_world ├── .cargo │ └── config.toml ├── Cargo.lock ├── Cargo.toml ├── README.md ├── rust-toolchain.toml └── src │ └── main.rs ├── json_contains ├── .cargo │ └── config.toml ├── .gitignore ├── Cargo.lock ├── Cargo.toml ├── README.md ├── rust-toolchain.toml └── src │ └── main.rs ├── palindrome ├── .cargo │ └── config.toml ├── Cargo.lock ├── Cargo.toml ├── README.md ├── rust-toolchain.toml └── src │ └── main.rs ├── prime_factorization ├── .cargo │ └── config.toml ├── Cargo.lock ├── Cargo.toml ├── README.md ├── rust-toolchain.toml └── src │ ├── lib.rs │ └── main.rs ├── rust_test_script.bash ├── secp256k1 ├── .cargo │ └── config.toml ├── Cargo.lock ├── Cargo.toml ├── README.md └── src │ └── main.rs ├── sha256 ├── .cargo │ └── config.toml ├── Cargo.lock ├── Cargo.toml ├── README.md └── src │ └── main.rs ├── simple_calculator ├── .cargo │ └── config.toml ├── Cargo.lock ├── Cargo.toml ├── README.md ├── rust-toolchain.toml └── src │ └── main.rs ├── sudoku ├── .cargo │ └── config.toml ├── Cargo.toml ├── README.md └── src │ └── main.rs ├── test_data ├── conway │ └── input ├── ed25519 │ └── input ├── factorial │ └── input ├── fibonacci │ └── input ├── fizzbuzz │ └── input ├── grep │ └── input ├── guessing_game │ ├── expected_output │ └── input ├── hello_world │ └── input ├── json_contains │ └── input ├── palindrome │ └── input ├── prime_factorization │ └── input ├── secp256k1 │ └── input ├── sha256 │ └── input ├── simple_calculator │ └── input ├── sudoku │ └── input └── unit_tests │ └── input └── unit_tests ├── .cargo └── config.toml ├── Cargo.lock ├── Cargo.toml ├── README.md ├── rust-toolchain.toml └── src ├── atomics.rs └── main.rs /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | */target/ 3 | log 4 | **/actual_output 5 | **/expected_output 6 | !test_data/guessing_game/expected_output 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Rust-Examples 2 | 3 | **ATTENTION: This repository is archived. For the latest versions of these examples, please see [the latest release bundle for the Valida toolchain](https://github.com/lita-xyz/valida-releases/releases).** 4 | 5 | ## General Description 6 | 7 | This project is a collection of various applications built using the Valida framework, which allows for the execution of Rust code with proofs of correctness. Each application serves as a template for demonstrating different functionalities, from simple calculations to more complex algorithms like Conway's Game of Life. The goal of this project is to provide a set of examples that can be used as a foundation for developing more advanced applications while ensuring correctness through formal verification. 8 | 9 | This documentation provides an overview of the various projects in this directory, along with links to their respective README files for detailed instructions and usage. 10 | 11 | ## Projects 12 | 13 | 1. **Fibonacci** 14 | - Description: A template project for calculating the n-th Fibonacci number and proving the execution of the computation in Valida. 15 | - [Read the README](fibonacci/README.md) 16 | 17 | 2. **Hello World** 18 | - Description: A simple program that prints "Hello, World!" in Valida. 19 | - [Read the README](hello_world/README.md) 20 | 21 | 3. **Conway's Game of Life** 22 | - Description: A program that plays Conway's Game of Life and proves the execution of the computation in Valida. 23 | - [Read the README](conway/README.md) 24 | 25 | 4. **Simple Calculator** 26 | - Description: A simple calculator program on Valida. 27 | - [Read the README](simple_calculator/README.md) 28 | 29 | 5. **Random Number Guessing Game** 30 | - Description: A random number guessing game on Valida. 31 | - [Read the README](guessing_game/README.md) 32 | 33 | 6. **JSON Path Contains** 34 | - Description: A project to prove a JsonPath contains a value. 35 | - [Read the README](json_contains/README.md) 36 | 37 | ## Conclusion 38 | 39 | For more information on each project, please refer to the linked README files. Each project contains specific instructions on setup, usage, and system requirements. 40 | -------------------------------------------------------------------------------- /conway/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [build] 2 | target = "valida-unknown-baremetal-gnu" 3 | 4 | [target.valida-unknown-baremetal-gnu] 5 | linker = "/valida-toolchain/bin/ld.lld" 6 | rustflags = [ 7 | "-C", "link-arg=/valida-toolchain/ValidaEntryPoint.o", 8 | "-C", "link-arg=--script=/valida-toolchain/valida.ld", 9 | "-C", "link-arg=/valida-toolchain/lib/valida-unknown-baremetal-gnu/libc.a", 10 | "-C", "link-arg=/valida-toolchain/lib/valida-unknown-baremetal-gnu/libm.a", 11 | "-C", "link-arg=--noinhibit-exec", 12 | ] 13 | 14 | [env] 15 | CC_valida_unknown_baremetal_gnu = "/valida-toolchain/bin/clang" 16 | CFLAGS_valida_unknown_baremetal_gnu = "--sysroot=/valida-toolchain/ -isystem /valida-toolchain/include" 17 | -------------------------------------------------------------------------------- /conway/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 4 4 | 5 | [[package]] 6 | name = "conway" 7 | version = "0.1.0" 8 | dependencies = [ 9 | "valida-rs", 10 | ] 11 | 12 | [[package]] 13 | name = "valida-rs" 14 | version = "0.1.0" 15 | source = "git+https://github.com/lita-xyz/valida-rs.git?branch=no-deps#b2216526d65b777df76d4d8267b98c0cc53b42e0" 16 | -------------------------------------------------------------------------------- /conway/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "conway" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | [dependencies] 7 | valida-rs = { git = "https://github.com/lita-xyz/valida-rs.git", branch = "no-deps" } 8 | -------------------------------------------------------------------------------- /conway/LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /conway/LICENSE.TXT: -------------------------------------------------------------------------------- 1 | Copyright © [2024] Lita Inc. All Rights Reserved. 2 | All rights reserved. 3 | This software and associated documentation files (the “Software”) are owned by Lita Inc. and are protected by copyright law and international treaties. 4 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to use the Software for personal, non-commercial purposes only, subject to the following conditions: 5 | 1. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 2. The Software may not be used for commercial purposes without the express written permission of Lita Inc. 7 | For inquiries regarding commercial use, please contact us at: ops@lita.foundation 8 | 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. -------------------------------------------------------------------------------- /conway/README.md: -------------------------------------------------------------------------------- 1 | # Conway's Game of Life for Valida 2 | 3 | This is a simple program that plays Conway's Game of Life and proves the execution of the computation in Valida. 4 | 5 | ## System requirements 6 | 7 | This template supports x86-64 Linux. [`rustup`](https://www.rust-lang.org/tools/install) is required. Arch Linux and Ubuntu are specifically supported, with other platforms possibly requiring some tinkering to make work. 8 | 9 | ## Toolchain installation 10 | 11 | To run this template project in the Valida VM, you need the Valida toolchain installed. Go to [LLVM Valida releases](https://github.com/lita-xyz/llvm-valida-releases/releases) to find the latest release. Download the release tarball, then follow these steps, substituting `$path_to_release_tarball` with a fully qualified path to the release tarball: 12 | 13 | ```bash 14 | sudo mkdir -p /valida-toolchain 15 | sudo chown $(whoami):users /valida-toolchain 16 | cd / 17 | tar xf $path_to_release_tarball 18 | sudo install /valida-toolchain/valida-shell /usr/local/bin 19 | ``` 20 | 21 | In the final `install` command, you can replace `/usr/local/bin` with any folder on your `PATH` environment variable (e.g., `~/.local/bin`). You can omit the `sudo` if your user has write permissions on the target folder. 22 | 23 | ## Entering the Valida shell 24 | 25 | To put the Valida toolchain on your PATH, you can enter the Valida shell by running `valida-shell` in your shell. The above installation process should have resulted in `valida-shell` being on your `PATH`. 26 | 27 | ## Usage 28 | 29 | Build the project for Valida, from the root directory of this repo: 30 | 31 | ``` 32 | cargo +valida build --release 33 | ``` 34 | 35 | To run the program in Valida, in the Valida shell, from the root directory of this repo: 36 | 37 | ``` 38 | valida> valida run ./target/valida-unknown-baremetal-gnu/release/conway log 39 | ``` 40 | 41 | The `run` command will load the binary, and execute the program. The program will then run, and print the output to the console and the file `log` in the current directory. 42 | -------------------------------------------------------------------------------- /conway/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly" 3 | components = ["rustc", "rust-src", "rustfmt", "cargo", "clippy", "rust-docs", "rust-analyzer"] 4 | -------------------------------------------------------------------------------- /conway/src/lib.rs: -------------------------------------------------------------------------------- 1 | // This code is adapted from https://github.com/rustwasm/wasm_game_of_life 2 | // by six different contributors, licensed under the MIT and Apache 2.0 licenses. 3 | 4 | #![no_std] 5 | 6 | #[repr(u8)] 7 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] 8 | pub enum Cell { 9 | Dead = 0, 10 | Alive = 1, 11 | } 12 | 13 | impl Cell { 14 | fn toggle(&mut self) { 15 | *self = match *self { 16 | Cell::Dead => Cell::Alive, 17 | Cell::Alive => Cell::Dead, 18 | }; 19 | } 20 | } 21 | 22 | const UNIVERSE_WIDTH: u32 = 10; 23 | const UNIVERSE_HEIGHT: u32 = 10; 24 | 25 | pub struct Universe { 26 | cells: [Cell; 100], 27 | } 28 | 29 | impl Universe { 30 | pub fn get_index(&self, row: u32, column: u32) -> usize { 31 | (row * UNIVERSE_WIDTH + column) as usize 32 | } 33 | 34 | /// Get the dead and alive values of the entire universe. 35 | pub fn get_cells(&self) -> &[Cell] { 36 | &self.cells 37 | } 38 | 39 | /// Set cells to be alive in a universe by passing the row and column 40 | /// of each cell as an array. 41 | pub fn set_cells(&mut self, cells: &[(u32, u32)]) { 42 | for (row, col) in cells.iter().cloned() { 43 | let idx = self.get_index(row, col); 44 | self.cells[idx] = Cell::Alive; 45 | } 46 | } 47 | 48 | fn live_neighbor_count(&self, row: u32, column: u32) -> u8 { 49 | let mut count = 0; 50 | 51 | let north = if row == 0 { 52 | UNIVERSE_HEIGHT - 1 53 | } else { 54 | row - 1 55 | }; 56 | 57 | let south = if row == UNIVERSE_HEIGHT - 1 { 58 | 0 59 | } else { 60 | row + 1 61 | }; 62 | 63 | let west = if column == 0 { 64 | UNIVERSE_WIDTH - 1 65 | } else { 66 | column - 1 67 | }; 68 | 69 | let east = if column == UNIVERSE_WIDTH - 1 { 70 | 0 71 | } else { 72 | column + 1 73 | }; 74 | 75 | let nw = self.get_index(north, west); 76 | count += self.cells[nw] as u8; 77 | 78 | let n = self.get_index(north, column); 79 | count += self.cells[n] as u8; 80 | 81 | let ne = self.get_index(north, east); 82 | count += self.cells[ne] as u8; 83 | 84 | let w = self.get_index(row, west); 85 | count += self.cells[w] as u8; 86 | 87 | let e = self.get_index(row, east); 88 | count += self.cells[e] as u8; 89 | 90 | let sw = self.get_index(south, west); 91 | count += self.cells[sw] as u8; 92 | 93 | let s = self.get_index(south, column); 94 | count += self.cells[s] as u8; 95 | 96 | let se = self.get_index(south, east); 97 | count += self.cells[se] as u8; 98 | 99 | count 100 | } 101 | } 102 | 103 | /// Public methods, exported to JavaScript. 104 | impl Universe { 105 | pub fn tick(&mut self) { 106 | // let _timer = Timer::new("Universe::tick"); 107 | 108 | let mut next = self.cells.clone(); 109 | 110 | for row in 0..UNIVERSE_HEIGHT { 111 | for col in 0..UNIVERSE_WIDTH { 112 | let idx = self.get_index(row, col); 113 | let cell = self.cells[idx]; 114 | let live_neighbors = self.live_neighbor_count(row, col); 115 | 116 | let next_cell = match (cell, live_neighbors) { 117 | // Rule 1: Any live cell with fewer than two live neighbours 118 | // dies, as if caused by underpopulation. 119 | (Cell::Alive, x) if x < 2 => Cell::Dead, 120 | // Rule 2: Any live cell with two or three live neighbours 121 | // lives on to the next generation. 122 | (Cell::Alive, 2) | (Cell::Alive, 3) => Cell::Alive, 123 | // Rule 3: Any live cell with more than three live 124 | // neighbours dies, as if by overpopulation. 125 | (Cell::Alive, x) if x > 3 => Cell::Dead, 126 | // Rule 4: Any dead cell with exactly three live neighbours 127 | // becomes a live cell, as if by reproduction. 128 | (Cell::Dead, 3) => Cell::Alive, 129 | // All other cells remain in the same state. 130 | (otherwise, _) => otherwise, 131 | }; 132 | 133 | next[idx] = next_cell; 134 | } 135 | } 136 | 137 | self.cells = next; 138 | } 139 | 140 | pub fn new() -> Universe { 141 | let width = UNIVERSE_WIDTH; 142 | let height = UNIVERSE_HEIGHT; 143 | 144 | let mut cells = [Cell::Dead; (UNIVERSE_WIDTH * UNIVERSE_HEIGHT) as usize]; 145 | 146 | for i in 0 .. (width * height) { 147 | if i % 2 == 0 || i % 7 == 0 { 148 | cells[i as usize] = Cell::Alive; 149 | } 150 | } 151 | 152 | Universe { 153 | cells, 154 | } 155 | } 156 | 157 | pub fn width(&self) -> u32 { 158 | UNIVERSE_WIDTH 159 | } 160 | 161 | pub fn height(&self) -> u32 { 162 | UNIVERSE_HEIGHT 163 | } 164 | 165 | pub fn cells(&self) -> *const Cell { 166 | self.cells.as_ptr() 167 | } 168 | 169 | pub fn toggle_cell(&mut self, row: u32, column: u32) { 170 | let idx = self.get_index(row, column); 171 | self.cells[idx].toggle(); 172 | } 173 | } 174 | -------------------------------------------------------------------------------- /conway/src/main.rs: -------------------------------------------------------------------------------- 1 | #![no_main] 2 | 3 | use conway::Universe; 4 | 5 | #[no_mangle] 6 | 7 | pub fn main() { 8 | let mut universe = Universe::new(); 9 | for _i in 0..10 { 10 | universe.tick(); 11 | } 12 | for i in 0..10 { 13 | for j in 0..10 { 14 | let idx = universe.get_index(i, j); 15 | let cell = universe.get_cells()[idx] as u8; 16 | println!("{}", cell.to_string()); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /ed25519/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [build] 2 | target = "valida-unknown-baremetal-gnu" 3 | 4 | [target.valida-unknown-baremetal-gnu] 5 | linker = "/valida-toolchain/bin/ld.lld" 6 | rustflags = [ 7 | "-C", "link-arg=/valida-toolchain/ValidaEntryPoint.o", 8 | "-C", "link-arg=--script=/valida-toolchain/valida.ld", 9 | "-C", "link-arg=/valida-toolchain/lib/valida-unknown-baremetal-gnu/libc.a", 10 | "-C", "link-arg=/valida-toolchain/lib/valida-unknown-baremetal-gnu/libm.a", 11 | "-C", "link-arg=--noinhibit-exec", 12 | ] 13 | 14 | [env] 15 | CC_valida_unknown_baremetal_gnu = "/valida-toolchain/bin/clang" 16 | CFLAGS_valida_unknown_baremetal_gnu = "--sysroot=/valida-toolchain/ -isystem /valida-toolchain/include" 17 | -------------------------------------------------------------------------------- /ed25519/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "base64ct" 7 | version = "1.6.0" 8 | source = "registry+https://github.com/rust-lang/crates.io-index" 9 | checksum = "8c3c1a368f70d6cf7302d78f8f7093da241fb8e8807c05cc9e51a125895a6d5b" 10 | 11 | [[package]] 12 | name = "bincode" 13 | version = "1.3.3" 14 | source = "registry+https://github.com/rust-lang/crates.io-index" 15 | checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad" 16 | dependencies = [ 17 | "serde", 18 | ] 19 | 20 | [[package]] 21 | name = "block-buffer" 22 | version = "0.10.4" 23 | source = "registry+https://github.com/rust-lang/crates.io-index" 24 | checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" 25 | dependencies = [ 26 | "generic-array", 27 | ] 28 | 29 | [[package]] 30 | name = "byteorder" 31 | version = "1.5.0" 32 | source = "registry+https://github.com/rust-lang/crates.io-index" 33 | checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" 34 | 35 | [[package]] 36 | name = "cfg-if" 37 | version = "1.0.0" 38 | source = "registry+https://github.com/rust-lang/crates.io-index" 39 | checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" 40 | 41 | [[package]] 42 | name = "const-oid" 43 | version = "0.9.6" 44 | source = "registry+https://github.com/rust-lang/crates.io-index" 45 | checksum = "c2459377285ad874054d797f3ccebf984978aa39129f6eafde5cdc8315b612f8" 46 | 47 | [[package]] 48 | name = "cpufeatures" 49 | version = "0.2.15" 50 | source = "registry+https://github.com/rust-lang/crates.io-index" 51 | checksum = "0ca741a962e1b0bff6d724a1a0958b686406e853bb14061f218562e1896f95e6" 52 | dependencies = [ 53 | "libc", 54 | ] 55 | 56 | [[package]] 57 | name = "crypto-common" 58 | version = "0.1.6" 59 | source = "registry+https://github.com/rust-lang/crates.io-index" 60 | checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" 61 | dependencies = [ 62 | "generic-array", 63 | "typenum", 64 | ] 65 | 66 | [[package]] 67 | name = "curve25519-dalek" 68 | version = "4.1.3" 69 | source = "git+https://github.com/lita-xyz/curve25519-dalek.git?branch=main#43a16f03d4c635a8836c23ac07244c116ea3aab8" 70 | dependencies = [ 71 | "cfg-if", 72 | "cpufeatures", 73 | "curve25519-dalek-derive", 74 | "digest", 75 | "fiat-crypto", 76 | "rustc_version", 77 | "subtle", 78 | "zeroize", 79 | ] 80 | 81 | [[package]] 82 | name = "curve25519-dalek-derive" 83 | version = "0.1.1" 84 | source = "git+https://github.com/lita-xyz/curve25519-dalek.git?branch=main#43a16f03d4c635a8836c23ac07244c116ea3aab8" 85 | dependencies = [ 86 | "proc-macro2", 87 | "quote", 88 | "syn", 89 | ] 90 | 91 | [[package]] 92 | name = "der" 93 | version = "0.7.9" 94 | source = "registry+https://github.com/rust-lang/crates.io-index" 95 | checksum = "f55bf8e7b65898637379c1b74eb1551107c8294ed26d855ceb9fd1a09cfc9bc0" 96 | dependencies = [ 97 | "const-oid", 98 | "zeroize", 99 | ] 100 | 101 | [[package]] 102 | name = "digest" 103 | version = "0.10.7" 104 | source = "registry+https://github.com/rust-lang/crates.io-index" 105 | checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" 106 | dependencies = [ 107 | "block-buffer", 108 | "crypto-common", 109 | ] 110 | 111 | [[package]] 112 | name = "ed25519" 113 | version = "0.1.0" 114 | dependencies = [ 115 | "ed25519-dalek", 116 | "hex", 117 | "rand", 118 | "valida-rs", 119 | ] 120 | 121 | [[package]] 122 | name = "ed25519" 123 | version = "2.2.3" 124 | source = "registry+https://github.com/rust-lang/crates.io-index" 125 | checksum = "115531babc129696a58c64a4fef0a8bf9e9698629fb97e9e40767d235cfbcd53" 126 | dependencies = [ 127 | "pkcs8", 128 | "signature", 129 | ] 130 | 131 | [[package]] 132 | name = "ed25519-dalek" 133 | version = "2.1.1" 134 | source = "git+https://github.com/lita-xyz/curve25519-dalek.git?branch=main#43a16f03d4c635a8836c23ac07244c116ea3aab8" 135 | dependencies = [ 136 | "curve25519-dalek", 137 | "ed25519 2.2.3", 138 | "rand_core", 139 | "serde", 140 | "sha2", 141 | "subtle", 142 | "zeroize", 143 | ] 144 | 145 | [[package]] 146 | name = "fiat-crypto" 147 | version = "0.2.9" 148 | source = "registry+https://github.com/rust-lang/crates.io-index" 149 | checksum = "28dea519a9695b9977216879a3ebfddf92f1c08c05d984f8996aecd6ecdc811d" 150 | 151 | [[package]] 152 | name = "generic-array" 153 | version = "0.14.7" 154 | source = "registry+https://github.com/rust-lang/crates.io-index" 155 | checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" 156 | dependencies = [ 157 | "typenum", 158 | "version_check", 159 | ] 160 | 161 | [[package]] 162 | name = "getrandom" 163 | version = "0.2.15" 164 | source = "registry+https://github.com/rust-lang/crates.io-index" 165 | checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" 166 | dependencies = [ 167 | "cfg-if", 168 | "libc", 169 | "wasi", 170 | ] 171 | 172 | [[package]] 173 | name = "hex" 174 | version = "0.4.3" 175 | source = "registry+https://github.com/rust-lang/crates.io-index" 176 | checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" 177 | 178 | [[package]] 179 | name = "libc" 180 | version = "0.2.162" 181 | source = "registry+https://github.com/rust-lang/crates.io-index" 182 | checksum = "18d287de67fe55fd7e1581fe933d965a5a9477b38e949cfa9f8574ef01506398" 183 | 184 | [[package]] 185 | name = "once_cell" 186 | version = "1.20.2" 187 | source = "registry+https://github.com/rust-lang/crates.io-index" 188 | checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" 189 | 190 | [[package]] 191 | name = "pkcs8" 192 | version = "0.10.2" 193 | source = "registry+https://github.com/rust-lang/crates.io-index" 194 | checksum = "f950b2377845cebe5cf8b5165cb3cc1a5e0fa5cfa3e1f7f55707d8fd82e0a7b7" 195 | dependencies = [ 196 | "der", 197 | "spki", 198 | ] 199 | 200 | [[package]] 201 | name = "ppv-lite86" 202 | version = "0.2.20" 203 | source = "registry+https://github.com/rust-lang/crates.io-index" 204 | checksum = "77957b295656769bb8ad2b6a6b09d897d94f05c41b069aede1fcdaa675eaea04" 205 | dependencies = [ 206 | "zerocopy", 207 | ] 208 | 209 | [[package]] 210 | name = "proc-macro2" 211 | version = "1.0.89" 212 | source = "registry+https://github.com/rust-lang/crates.io-index" 213 | checksum = "f139b0662de085916d1fb67d2b4169d1addddda1919e696f3252b740b629986e" 214 | dependencies = [ 215 | "unicode-ident", 216 | ] 217 | 218 | [[package]] 219 | name = "quote" 220 | version = "1.0.37" 221 | source = "registry+https://github.com/rust-lang/crates.io-index" 222 | checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" 223 | dependencies = [ 224 | "proc-macro2", 225 | ] 226 | 227 | [[package]] 228 | name = "rand" 229 | version = "0.8.5" 230 | source = "registry+https://github.com/rust-lang/crates.io-index" 231 | checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" 232 | dependencies = [ 233 | "libc", 234 | "rand_chacha", 235 | "rand_core", 236 | ] 237 | 238 | [[package]] 239 | name = "rand_chacha" 240 | version = "0.3.1" 241 | source = "registry+https://github.com/rust-lang/crates.io-index" 242 | checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" 243 | dependencies = [ 244 | "ppv-lite86", 245 | "rand_core", 246 | ] 247 | 248 | [[package]] 249 | name = "rand_core" 250 | version = "0.6.4" 251 | source = "registry+https://github.com/rust-lang/crates.io-index" 252 | checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" 253 | dependencies = [ 254 | "getrandom", 255 | ] 256 | 257 | [[package]] 258 | name = "rustc_version" 259 | version = "0.4.1" 260 | source = "registry+https://github.com/rust-lang/crates.io-index" 261 | checksum = "cfcb3a22ef46e85b45de6ee7e79d063319ebb6594faafcf1c225ea92ab6e9b92" 262 | dependencies = [ 263 | "semver", 264 | ] 265 | 266 | [[package]] 267 | name = "semver" 268 | version = "1.0.23" 269 | source = "registry+https://github.com/rust-lang/crates.io-index" 270 | checksum = "61697e0a1c7e512e84a621326239844a24d8207b4669b41bc18b32ea5cbf988b" 271 | 272 | [[package]] 273 | name = "serde" 274 | version = "1.0.215" 275 | source = "registry+https://github.com/rust-lang/crates.io-index" 276 | checksum = "6513c1ad0b11a9376da888e3e0baa0077f1aed55c17f50e7b2397136129fb88f" 277 | dependencies = [ 278 | "serde_derive", 279 | ] 280 | 281 | [[package]] 282 | name = "serde_derive" 283 | version = "1.0.215" 284 | source = "registry+https://github.com/rust-lang/crates.io-index" 285 | checksum = "ad1e866f866923f252f05c889987993144fb74e722403468a4ebd70c3cd756c0" 286 | dependencies = [ 287 | "proc-macro2", 288 | "quote", 289 | "syn", 290 | ] 291 | 292 | [[package]] 293 | name = "sha2" 294 | version = "0.10.8" 295 | source = "registry+https://github.com/rust-lang/crates.io-index" 296 | checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8" 297 | dependencies = [ 298 | "cfg-if", 299 | "cpufeatures", 300 | "digest", 301 | ] 302 | 303 | [[package]] 304 | name = "signature" 305 | version = "2.2.0" 306 | source = "registry+https://github.com/rust-lang/crates.io-index" 307 | checksum = "77549399552de45a898a580c1b41d445bf730df867cc44e6c0233bbc4b8329de" 308 | dependencies = [ 309 | "rand_core", 310 | ] 311 | 312 | [[package]] 313 | name = "spki" 314 | version = "0.7.3" 315 | source = "registry+https://github.com/rust-lang/crates.io-index" 316 | checksum = "d91ed6c858b01f942cd56b37a94b3e0a1798290327d1236e4d9cf4eaca44d29d" 317 | dependencies = [ 318 | "base64ct", 319 | "der", 320 | ] 321 | 322 | [[package]] 323 | name = "subtle" 324 | version = "2.6.1" 325 | source = "registry+https://github.com/rust-lang/crates.io-index" 326 | checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" 327 | 328 | [[package]] 329 | name = "syn" 330 | version = "2.0.87" 331 | source = "registry+https://github.com/rust-lang/crates.io-index" 332 | checksum = "25aa4ce346d03a6dcd68dd8b4010bcb74e54e62c90c573f394c46eae99aba32d" 333 | dependencies = [ 334 | "proc-macro2", 335 | "quote", 336 | "unicode-ident", 337 | ] 338 | 339 | [[package]] 340 | name = "typenum" 341 | version = "1.17.0" 342 | source = "registry+https://github.com/rust-lang/crates.io-index" 343 | checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" 344 | 345 | [[package]] 346 | name = "unicode-ident" 347 | version = "1.0.13" 348 | source = "registry+https://github.com/rust-lang/crates.io-index" 349 | checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" 350 | 351 | [[package]] 352 | name = "valida-rs" 353 | version = "0.1.0" 354 | source = "git+https://github.com/lita-xyz/valida-rs.git?branch=main#66868f53635b4a1a5d962a5eec7473cdcc41ae38" 355 | dependencies = [ 356 | "bincode", 357 | "getrandom", 358 | "once_cell", 359 | "rand", 360 | "serde", 361 | ] 362 | 363 | [[package]] 364 | name = "version_check" 365 | version = "0.9.5" 366 | source = "registry+https://github.com/rust-lang/crates.io-index" 367 | checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" 368 | 369 | [[package]] 370 | name = "wasi" 371 | version = "0.11.0+wasi-snapshot-preview1" 372 | source = "registry+https://github.com/rust-lang/crates.io-index" 373 | checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" 374 | 375 | [[package]] 376 | name = "zerocopy" 377 | version = "0.7.35" 378 | source = "registry+https://github.com/rust-lang/crates.io-index" 379 | checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" 380 | dependencies = [ 381 | "byteorder", 382 | "zerocopy-derive", 383 | ] 384 | 385 | [[package]] 386 | name = "zerocopy-derive" 387 | version = "0.7.35" 388 | source = "registry+https://github.com/rust-lang/crates.io-index" 389 | checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" 390 | dependencies = [ 391 | "proc-macro2", 392 | "quote", 393 | "syn", 394 | ] 395 | 396 | [[package]] 397 | name = "zeroize" 398 | version = "1.8.1" 399 | source = "registry+https://github.com/rust-lang/crates.io-index" 400 | checksum = "ced3678a2879b30306d323f4542626697a464a97c0a07c9aebf7ebca65cd4dde" 401 | -------------------------------------------------------------------------------- /ed25519/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "ed25519" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | [dependencies] 7 | valida-rs = { git = "https://github.com/lita-xyz/valida-rs.git", branch = "main" } 8 | ed25519-dalek = { git = "https://github.com/lita-xyz/curve25519-dalek.git", branch = "main", features = ["rand_core"] } 9 | hex = "0.4" 10 | rand = "0.8" 11 | -------------------------------------------------------------------------------- /ed25519/README.md: -------------------------------------------------------------------------------- 1 | # Ed25519 in Valida 2 | 3 | This is a simple program that verifies an Ed25519 signature in Valida. 4 | 5 | ## System requirements 6 | 7 | This template supports x86-64 Linux. [`rustup`](https://www.rust-lang.org/tools/install) is required. Arch Linux and Ubuntu are specifically supported, with other platforms possibly requiring some tinkering to make work. 8 | 9 | ## Toolchain installation 10 | 11 | To run this project in the Valida VM, you need the Valida toolchain installed. Go to [LLVM Valida releases](https://github.com/lita-xyz/llvm-valida-releases/releases) to find the latest release. Download the release tarball, extract it, `cd` into the extracted folder, and run `sudo ./install.sh`. 12 | 13 | ## Entering the Valida shell 14 | 15 | To put the Valida toolchain on your PATH, you can enter the Valida shell by running `valida-shell` in your shell. The above installation process should have resulted in `valida-shell` being on your `PATH`. 16 | 17 | ## Usage 18 | 19 | Build the project, from the root directory of this repo: 20 | 21 | ``` 22 | cargo +valida build --release 23 | ``` 24 | 25 | To run the program, in the Valida shell, from the root directory of this repo: 26 | 27 | ``` 28 | valida> valida run ./target/valida-unknown-baremetal-gnu/release/ed25519 log 29 | ``` 30 | 31 | The `run` command runs the program and verifies the Ed25519 signature and prints the result to the console and the file `log` in the current directory. 32 | -------------------------------------------------------------------------------- /ed25519/src/main.rs: -------------------------------------------------------------------------------- 1 | #![no_main] 2 | 3 | use ed25519_dalek::*; 4 | 5 | use rand::rngs::OsRng; 6 | 7 | fn sign_verify() { 8 | // TestSignVerify 9 | 10 | let good: &[u8] = "test message".as_bytes(); 11 | let bad: &[u8] = "wrong message".as_bytes(); 12 | 13 | let mut csprng = OsRng; 14 | 15 | let signing_key: SigningKey = SigningKey::generate(&mut csprng); 16 | let verifying_key = signing_key.verifying_key(); 17 | let good_sig: Signature = signing_key.sign(good); 18 | let bad_sig: Signature = signing_key.sign(bad); 19 | 20 | // Check that an honestly generated public key is not weak 21 | if verifying_key.is_weak() { 22 | valida_rs::io::println("Key is weak"); 23 | } 24 | 25 | if !signing_key.verify(good, &good_sig).is_ok() { 26 | valida_rs::io::println("Verification of a valid signature failed!"); 27 | } 28 | 29 | if !verifying_key.verify_strict(good, &good_sig).is_ok() { 30 | valida_rs::io::println("Strict verification of a valid signature failed!"); 31 | } 32 | 33 | if !signing_key.verify(good, &bad_sig).is_err() { 34 | valida_rs::io::println("Verification of a signature on a different message passed!"); 35 | } 36 | 37 | if !verifying_key.verify_strict(good, &bad_sig).is_err() { 38 | valida_rs::io::println("Strict verification of a signature on a different message passed!"); 39 | } 40 | 41 | if !signing_key.verify(bad, &good_sig).is_err() { 42 | valida_rs::io::println("Verification of a signature on a different message passed!"); 43 | } 44 | 45 | if !verifying_key.verify_strict(bad, &good_sig).is_err() { 46 | valida_rs::io::println("Strict verification of a signature on a different message passed!"); 47 | } 48 | } 49 | 50 | valida_rs::entrypoint!(main); 51 | 52 | pub fn main() { 53 | sign_verify(); 54 | } 55 | -------------------------------------------------------------------------------- /factorial/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [build] 2 | target = "valida-unknown-baremetal-gnu" 3 | 4 | [target.valida-unknown-baremetal-gnu] 5 | linker = "/valida-toolchain/bin/ld.lld" 6 | rustflags = [ 7 | "-C", "link-arg=/valida-toolchain/ValidaEntryPoint.o", 8 | "-C", "link-arg=--script=/valida-toolchain/valida.ld", 9 | "-C", "link-arg=/valida-toolchain/lib/valida-unknown-baremetal-gnu/libc.a", 10 | "-C", "link-arg=/valida-toolchain/lib/valida-unknown-baremetal-gnu/libm.a", 11 | "-C", "link-arg=--noinhibit-exec", 12 | ] 13 | 14 | [env] 15 | CC_valida_unknown_baremetal_gnu = "/valida-toolchain/bin/clang" 16 | CFLAGS_valida_unknown_baremetal_gnu = "--sysroot=/valida-toolchain/ -isystem /valida-toolchain/include" 17 | -------------------------------------------------------------------------------- /factorial/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 4 4 | 5 | [[package]] 6 | name = "factorial" 7 | version = "0.1.0" 8 | dependencies = [ 9 | "valida-rs", 10 | ] 11 | 12 | [[package]] 13 | name = "valida-rs" 14 | version = "0.1.0" 15 | source = "git+https://github.com/lita-xyz/valida-rs.git?branch=no-deps#23b9906c4466e8fa7e2547e9124f200c20440688" 16 | -------------------------------------------------------------------------------- /factorial/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "factorial" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | [dependencies] 7 | valida-rs = { git = "https://github.com/lita-xyz/valida-rs.git", branch = "no-deps" } -------------------------------------------------------------------------------- /factorial/README.md: -------------------------------------------------------------------------------- 1 | # Factorial in Valida 2 | 3 | This is a simple program that asks the user for a number and then prints the factorial of that number. 4 | 5 | ## System requirements 6 | 7 | This template supports x86-64 Linux. [`rustup`](https://www.rust-lang.org/tools/install) is required. Arch Linux and Ubuntu are specifically supported, with other platforms possibly requiring some tinkering to make work. 8 | 9 | ## Toolchain installation 10 | 11 | To run this project in the Valida VM, you need the Valida toolchain installed. Go to [LLVM Valida releases](https://github.com/lita-xyz/llvm-valida-releases/releases) to find the latest release. Download the release tarball, extract it, `cd` into the extracted folder, and run `sudo ./install.sh`. 12 | 13 | ## Entering the Valida shell 14 | 15 | To put the Valida toolchain on your PATH, you can enter the Valida shell by running `valida-shell` in your shell. The above installation process should have resulted in `valida-shell` being on your `PATH`. 16 | 17 | ## Usage 18 | 19 | Build the project, from the root directory of this repo: 20 | 21 | ``` 22 | cargo +valida build --release 23 | ``` 24 | 25 | To run the program, in the Valida shell, from the root directory of this repo: 26 | 27 | ``` 28 | valida> valida run ./target/valida-unknown-baremetal-gnu/release/factorial log 29 | ``` 30 | 31 | The `run` command runs the program and asks the user for a number, then prints the factorial of that number to the console and the file `log` in the current directory. 32 | -------------------------------------------------------------------------------- /factorial/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly" 3 | components = ["rustc", "rust-src", "rustfmt", "cargo", "clippy", "rust-docs", "rust-analyzer"] 4 | -------------------------------------------------------------------------------- /factorial/src/main.rs: -------------------------------------------------------------------------------- 1 | #![no_main] 2 | 3 | #[no_mangle] 4 | pub fn main() { 5 | println!("Please enter a number from 0 to 12:"); 6 | let num = loop { 7 | match valida_rs::io::read_line::() { 8 | Ok(num) => { 9 | if num > 12 { 10 | println!("Number must be between 0 and 12. Please try again:"); 11 | continue; 12 | } 13 | break num 14 | }, 15 | Err(e) => { 16 | println!("Error reading input: {}. Please try again:", e); 17 | } 18 | } 19 | }; 20 | let mut factorial:u32 = 1; 21 | for i in 1..=num { 22 | factorial *= i; 23 | } 24 | println!("The factorial of {} is {}", num, factorial); 25 | } 26 | -------------------------------------------------------------------------------- /fibonacci/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [build] 2 | target = "valida-unknown-baremetal-gnu" 3 | 4 | [target.valida-unknown-baremetal-gnu] 5 | linker = "/valida-toolchain/bin/ld.lld" 6 | rustflags = [ 7 | "-C", "link-arg=/valida-toolchain/ValidaEntryPoint.o", 8 | "-C", "link-arg=--script=/valida-toolchain/valida.ld", 9 | "-C", "link-arg=/valida-toolchain/lib/valida-unknown-baremetal-gnu/libc.a", 10 | "-C", "link-arg=/valida-toolchain/lib/valida-unknown-baremetal-gnu/libm.a", 11 | "-C", "link-arg=--noinhibit-exec", 12 | ] 13 | 14 | [env] 15 | CC_valida_unknown_baremetal_gnu = "/valida-toolchain/bin/clang" 16 | CFLAGS_valida_unknown_baremetal_gnu = "--sysroot=/valida-toolchain/ -isystem /valida-toolchain/include" 17 | -------------------------------------------------------------------------------- /fibonacci/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 4 4 | 5 | [[package]] 6 | name = "fibonacci" 7 | version = "0.1.0" 8 | dependencies = [ 9 | "valida-rs", 10 | ] 11 | 12 | [[package]] 13 | name = "valida-rs" 14 | version = "0.1.0" 15 | source = "git+https://github.com/lita-xyz/valida-rs.git?branch=no-deps#b2216526d65b777df76d4d8267b98c0cc53b42e0" 16 | -------------------------------------------------------------------------------- /fibonacci/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "fibonacci" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | [dependencies] 7 | valida-rs = { git = "https://github.com/lita-xyz/valida-rs.git", branch = "no-deps" } 8 | -------------------------------------------------------------------------------- /fibonacci/LICENSE.TXT: -------------------------------------------------------------------------------- 1 | Copyright © [2024] Lita Inc. All Rights Reserved. 2 | All rights reserved. 3 | This software and associated documentation files (the “Software”) are owned by Lita Inc. and are protected by copyright law and international treaties. 4 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to use the Software for personal, non-commercial purposes only, subject to the following conditions: 5 | 1. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 2. The Software may not be used for commercial purposes without the express written permission of Lita Inc. 7 | For inquiries regarding commercial use, please contact us at: ops@lita.foundation 8 | 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. -------------------------------------------------------------------------------- /fibonacci/README.md: -------------------------------------------------------------------------------- 1 | # Fibonacci: A template project for Valida 2 | 3 | This is a simple program that calculates the n-th fibonacci number and proves the execution of the computation in Valida. You can use this as a template for your projects which create Valida proofs of execution of Rust code. 4 | 5 | ## Usage 6 | 7 | Build the project, from the root directory of this repo: 8 | 9 | ``` 10 | cargo +valida build --release 11 | ``` 12 | 13 | To run the program, in the Valida shell, from the root directory of this repo: 14 | 15 | ``` 16 | valida> valida run ./target/valida-unknown-baremetal-gnu/release/fibonacci log 17 | ``` 18 | 19 | The `run` command runs the program, prompting for an input, and print the output to the console and the file `log` in the current directory. 20 | 21 | The log file should contain: 22 | 23 | ``` 24 | 25 25 | -th fibonacci number is: 26 | 75025 27 | ``` 28 | -------------------------------------------------------------------------------- /fibonacci/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly" 3 | components = ["rustc", "rust-src", "rustfmt", "cargo", "clippy", "rust-docs", "rust-analyzer"] 4 | -------------------------------------------------------------------------------- /fibonacci/src/main.rs: -------------------------------------------------------------------------------- 1 | #![no_main] 2 | 3 | #[no_mangle] 4 | 5 | pub fn main() { 6 | println!("Please enter a number from 0 to 46:"); 7 | // Read a line from stdin and parse it as an u8. 8 | let n = loop { 9 | match valida_rs::io::read_line::() { 10 | Ok(num) => break num, 11 | Err(e) => { 12 | println!("Error reading input: {}. Please try again:", e); 13 | } 14 | } 15 | }; 16 | // n that is larger than 46 will overflow the u32 type. 17 | if n > 46 { 18 | println!("Error: n is too large. Please enter a number no more than 46."); 19 | return; 20 | } 21 | let mut a: u32 = 0; 22 | let mut b: u32 = 1; 23 | let mut sum: u32; 24 | for _ in 1..n { 25 | sum = a + b; 26 | a = b; 27 | b = sum; 28 | } 29 | 30 | println!("The {}-th fibonacci number is: {}", n, b); 31 | } 32 | -------------------------------------------------------------------------------- /fizzbuzz/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [build] 2 | target = "valida-unknown-baremetal-gnu" 3 | 4 | [target.valida-unknown-baremetal-gnu] 5 | linker = "/valida-toolchain/bin/ld.lld" 6 | rustflags = [ 7 | "-C", "link-arg=/valida-toolchain/ValidaEntryPoint.o", 8 | "-C", "link-arg=--script=/valida-toolchain/valida.ld", 9 | "-C", "link-arg=/valida-toolchain/lib/valida-unknown-baremetal-gnu/libc.a", 10 | "-C", "link-arg=/valida-toolchain/lib/valida-unknown-baremetal-gnu/libm.a", 11 | "-C", "link-arg=--noinhibit-exec", 12 | ] 13 | 14 | [env] 15 | CC_valida_unknown_baremetal_gnu = "/valida-toolchain/bin/clang" 16 | CFLAGS_valida_unknown_baremetal_gnu = "--sysroot=/valida-toolchain/ -isystem /valida-toolchain/include" 17 | -------------------------------------------------------------------------------- /fizzbuzz/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 4 4 | 5 | [[package]] 6 | name = "fizzbuzz" 7 | version = "0.1.0" 8 | dependencies = [ 9 | "valida-rs", 10 | ] 11 | 12 | [[package]] 13 | name = "valida-rs" 14 | version = "0.1.0" 15 | source = "git+https://github.com/lita-xyz/valida-rs.git?branch=no-deps#23b9906c4466e8fa7e2547e9124f200c20440688" 16 | -------------------------------------------------------------------------------- /fizzbuzz/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "fizzbuzz" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | [dependencies] 7 | valida-rs = { git = "https://github.com/lita-xyz/valida-rs.git", branch = "no-deps" } 8 | -------------------------------------------------------------------------------- /fizzbuzz/README.md: -------------------------------------------------------------------------------- 1 | # Fizz Buzz in Valida 2 | 3 | Fizz buzz is a word game. Players take turns to count incrementally, replacing any number divisible by three with the word "fizz", and any number divisible by five with the word "buzz", and any number divisible by both three and five with the word "fizzbuzz". 4 | ## System requirements 5 | 6 | This template supports x86-64 Linux. [`rustup`](https://www.rust-lang.org/tools/install) is required. Arch Linux and Ubuntu are specifically supported, with other platforms possibly requiring some tinkering to make work. 7 | 8 | ## Toolchain installation 9 | 10 | To run this project in the Valida VM, you need the Valida toolchain installed. Go to [LLVM Valida releases](https://github.com/lita-xyz/llvm-valida-releases/releases) to find the latest release. Download the release tarball, extract it, `cd` into the extracted folder, and run `sudo ./install.sh`. 11 | 12 | ## Entering the Valida shell 13 | 14 | To put the Valida toolchain on your PATH, you can enter the Valida shell by running `valida-shell` in your shell. The above installation process should have resulted in `valida-shell` being on your `PATH`. 15 | 16 | ## Usage 17 | 18 | Build the project, from the root directory of this repo: 19 | 20 | ``` 21 | cargo +valida build --release 22 | ``` 23 | 24 | To run the program, in the Valida shell, from the root directory of this repo: 25 | 26 | ``` 27 | valida> valida run ./target/valida-unknown-baremetal-gnu/release/fizzbuzz log 28 | ``` 29 | 30 | The `run` command runs the program and asks the user for a number, then prints the numbers from 1 to that number, replacing any number divisible by three with the word "fizz", and any number divisible by five with the word "buzz", and any number divisible by both three and five with the word "fizzbuzz", to the console and the file `log` in the current directory. -------------------------------------------------------------------------------- /fizzbuzz/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly" 3 | components = ["rustc", "rust-src", "rustfmt", "cargo", "clippy", "rust-docs", "rust-analyzer"] 4 | -------------------------------------------------------------------------------- /fizzbuzz/src/main.rs: -------------------------------------------------------------------------------- 1 | #![no_main] 2 | 3 | #[no_mangle] 4 | pub fn main() { 5 | println!("Please enter a number:"); 6 | let n = loop { 7 | match valida_rs::io::read_line::() { 8 | Ok(num) => break num, 9 | Err(e) => { 10 | println!("Error reading input: {}. Please try again:", e); 11 | } 12 | } 13 | }; 14 | 15 | for i in 1..=n { 16 | match (i % 3, i % 5) { 17 | (0, 0) => println!("FizzBuzz"), 18 | (0, _) => println!("Fizz"), 19 | (_, 0) => println!("Buzz"), 20 | _ => println!("{}", i) 21 | } 22 | } 23 | } -------------------------------------------------------------------------------- /grep/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [build] 2 | target = "valida-unknown-baremetal-gnu" 3 | 4 | [target.valida-unknown-baremetal-gnu] 5 | linker = "/valida-toolchain/bin/ld.lld" 6 | rustflags = [ 7 | "-C", "link-arg=/valida-toolchain/ValidaEntryPoint.o", 8 | "-C", "link-arg=--script=/valida-toolchain/valida.ld", 9 | "-C", "link-arg=/valida-toolchain/lib/valida-unknown-baremetal-gnu/libc.a", 10 | "-C", "link-arg=/valida-toolchain/lib/valida-unknown-baremetal-gnu/libm.a", 11 | "-C", "link-arg=--noinhibit-exec", 12 | ] 13 | 14 | [env] 15 | CC_valida_unknown_baremetal_gnu = "/valida-toolchain/bin/clang" 16 | CFLAGS_valida_unknown_baremetal_gnu = "--sysroot=/valida-toolchain/ -isystem /valida-toolchain/include" 17 | -------------------------------------------------------------------------------- /grep/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 4 4 | 5 | [[package]] 6 | name = "grep" 7 | version = "0.1.0" 8 | dependencies = [ 9 | "valida-rs", 10 | ] 11 | 12 | [[package]] 13 | name = "valida-rs" 14 | version = "0.1.0" 15 | source = "git+https://github.com/lita-xyz/valida-rs.git?branch=no-deps#b2216526d65b777df76d4d8267b98c0cc53b42e0" 16 | -------------------------------------------------------------------------------- /grep/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "grep" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | [dependencies] 7 | valida-rs = { git = "https://github.com/lita-xyz/valida-rs.git", branch = "no-deps" } 8 | -------------------------------------------------------------------------------- /grep/README.md: -------------------------------------------------------------------------------- 1 | # Grep in Valida 2 | 3 | This is a simple program which users can enter a query and a string of contents, and the program will return all the words in the contents that contain the query. 4 | 5 | ## System requirements 6 | 7 | This template supports x86-64 Linux. [`rustup`](https://www.rust-lang.org/tools/install) is required. Arch Linux and Ubuntu are specifically supported, with other platforms possibly requiring some tinkering to make work. 8 | 9 | ## Toolchain installation 10 | 11 | To run this project in the Valida VM, you need the Valida toolchain installed. Go to [LLVM Valida releases](https://github.com/lita-xyz/llvm-valida-releases/releases) to find the latest release. Download the release tarball, extract it, `cd` into the extracted folder, and run `sudo ./install.sh`. 12 | 13 | ## Entering the Valida shell 14 | 15 | To put the Valida toolchain on your PATH, you can enter the Valida shell by running `valida-shell` in your shell. The above installation process should have resulted in `valida-shell` being on your `PATH`. 16 | 17 | ## Usage 18 | 19 | Build the project, from the root directory of this repo: 20 | 21 | ``` 22 | cargo +valida build --release 23 | ``` 24 | 25 | To run the program, in the Valida shell, from the root directory of this repo: 26 | 27 | ``` 28 | valida> valida run ./target/valida-unknown-baremetal-gnu/release/grep log 29 | ``` 30 | 31 | The `run` command runs the program and asks the user for a query and contents, then prints the results to the console and the file `log` in the current directory. 32 | -------------------------------------------------------------------------------- /grep/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly" 3 | components = ["rustc", "rust-src", "rustfmt", "cargo", "clippy", "rust-docs", "rust-analyzer"] 4 | -------------------------------------------------------------------------------- /grep/src/main.rs: -------------------------------------------------------------------------------- 1 | #![no_main] 2 | 3 | #[no_mangle] 4 | 5 | // searches a string for a substring and returns the match 6 | pub fn main() { 7 | println!("Please enter the substring to search for:"); 8 | let query = valida_rs::io::read_line::().unwrap(); 9 | println!("Please enter the contents to search in:"); 10 | let contents = valida_rs::io::read_line::().unwrap(); 11 | 12 | println!("Searching for '{}' in '{}'.", query, contents); 13 | 14 | let results = search_case_insensitive(&query, contents.as_str()); 15 | println!("Words that contain '{}': {:?}", query, results); 16 | } 17 | 18 | pub fn search_case_insensitive<'a>( 19 | query: &str, 20 | contents: &'a str 21 | ) -> Vec<&'a str> { 22 | let query = query.to_lowercase(); 23 | let mut results = Vec::new(); 24 | 25 | for line in contents.lines() { 26 | for word in line.split_whitespace() { 27 | if word.to_lowercase().contains(&query) { 28 | results.push(word); 29 | } 30 | } 31 | } 32 | 33 | results 34 | } -------------------------------------------------------------------------------- /guessing_game/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [build] 2 | target = "valida-unknown-baremetal-gnu" 3 | 4 | [target.valida-unknown-baremetal-gnu] 5 | linker = "/valida-toolchain/bin/ld.lld" 6 | rustflags = [ 7 | "-C", "link-arg=/valida-toolchain/ValidaEntryPoint.o", 8 | "-C", "link-arg=--script=/valida-toolchain/valida.ld", 9 | "-C", "link-arg=/valida-toolchain/lib/valida-unknown-baremetal-gnu/libc.a", 10 | "-C", "link-arg=/valida-toolchain/lib/valida-unknown-baremetal-gnu/libm.a", 11 | "-C", "link-arg=--noinhibit-exec", 12 | ] 13 | 14 | [env] 15 | CC_valida_unknown_baremetal_gnu = "/valida-toolchain/bin/clang" 16 | CFLAGS_valida_unknown_baremetal_gnu = "--sysroot=/valida-toolchain/ -isystem /valida-toolchain/include" 17 | -------------------------------------------------------------------------------- /guessing_game/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | -------------------------------------------------------------------------------- /guessing_game/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "bincode" 7 | version = "1.3.3" 8 | source = "registry+https://github.com/rust-lang/crates.io-index" 9 | checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad" 10 | dependencies = [ 11 | "serde", 12 | ] 13 | 14 | [[package]] 15 | name = "byteorder" 16 | version = "1.5.0" 17 | source = "registry+https://github.com/rust-lang/crates.io-index" 18 | checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" 19 | 20 | [[package]] 21 | name = "cfg-if" 22 | version = "1.0.0" 23 | source = "registry+https://github.com/rust-lang/crates.io-index" 24 | checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" 25 | 26 | [[package]] 27 | name = "getrandom" 28 | version = "0.2.15" 29 | source = "registry+https://github.com/rust-lang/crates.io-index" 30 | checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" 31 | dependencies = [ 32 | "cfg-if", 33 | "libc", 34 | "wasi", 35 | ] 36 | 37 | [[package]] 38 | name = "guessing_game" 39 | version = "0.1.0" 40 | dependencies = [ 41 | "getrandom", 42 | "rand", 43 | "valida-rs", 44 | ] 45 | 46 | [[package]] 47 | name = "libc" 48 | version = "0.2.161" 49 | source = "registry+https://github.com/rust-lang/crates.io-index" 50 | checksum = "8e9489c2807c139ffd9c1794f4af0ebe86a828db53ecdc7fea2111d0fed085d1" 51 | 52 | [[package]] 53 | name = "once_cell" 54 | version = "1.20.2" 55 | source = "registry+https://github.com/rust-lang/crates.io-index" 56 | checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" 57 | 58 | [[package]] 59 | name = "ppv-lite86" 60 | version = "0.2.20" 61 | source = "registry+https://github.com/rust-lang/crates.io-index" 62 | checksum = "77957b295656769bb8ad2b6a6b09d897d94f05c41b069aede1fcdaa675eaea04" 63 | dependencies = [ 64 | "zerocopy", 65 | ] 66 | 67 | [[package]] 68 | name = "proc-macro2" 69 | version = "1.0.89" 70 | source = "registry+https://github.com/rust-lang/crates.io-index" 71 | checksum = "f139b0662de085916d1fb67d2b4169d1addddda1919e696f3252b740b629986e" 72 | dependencies = [ 73 | "unicode-ident", 74 | ] 75 | 76 | [[package]] 77 | name = "quote" 78 | version = "1.0.37" 79 | source = "registry+https://github.com/rust-lang/crates.io-index" 80 | checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" 81 | dependencies = [ 82 | "proc-macro2", 83 | ] 84 | 85 | [[package]] 86 | name = "rand" 87 | version = "0.8.5" 88 | source = "registry+https://github.com/rust-lang/crates.io-index" 89 | checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" 90 | dependencies = [ 91 | "libc", 92 | "rand_chacha", 93 | "rand_core", 94 | ] 95 | 96 | [[package]] 97 | name = "rand_chacha" 98 | version = "0.3.1" 99 | source = "registry+https://github.com/rust-lang/crates.io-index" 100 | checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" 101 | dependencies = [ 102 | "ppv-lite86", 103 | "rand_core", 104 | ] 105 | 106 | [[package]] 107 | name = "rand_core" 108 | version = "0.6.4" 109 | source = "registry+https://github.com/rust-lang/crates.io-index" 110 | checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" 111 | dependencies = [ 112 | "getrandom", 113 | ] 114 | 115 | [[package]] 116 | name = "serde" 117 | version = "1.0.214" 118 | source = "registry+https://github.com/rust-lang/crates.io-index" 119 | checksum = "f55c3193aca71c12ad7890f1785d2b73e1b9f63a0bbc353c08ef26fe03fc56b5" 120 | dependencies = [ 121 | "serde_derive", 122 | ] 123 | 124 | [[package]] 125 | name = "serde_derive" 126 | version = "1.0.214" 127 | source = "registry+https://github.com/rust-lang/crates.io-index" 128 | checksum = "de523f781f095e28fa605cdce0f8307e451cc0fd14e2eb4cd2e98a355b147766" 129 | dependencies = [ 130 | "proc-macro2", 131 | "quote", 132 | "syn", 133 | ] 134 | 135 | [[package]] 136 | name = "syn" 137 | version = "2.0.87" 138 | source = "registry+https://github.com/rust-lang/crates.io-index" 139 | checksum = "25aa4ce346d03a6dcd68dd8b4010bcb74e54e62c90c573f394c46eae99aba32d" 140 | dependencies = [ 141 | "proc-macro2", 142 | "quote", 143 | "unicode-ident", 144 | ] 145 | 146 | [[package]] 147 | name = "unicode-ident" 148 | version = "1.0.13" 149 | source = "registry+https://github.com/rust-lang/crates.io-index" 150 | checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" 151 | 152 | [[package]] 153 | name = "valida-rs" 154 | version = "0.1.0" 155 | source = "git+https://github.com/lita-xyz/valida-rs.git?branch=main#5230828e5b83880f9fcb6fb93cec278d4896695f" 156 | dependencies = [ 157 | "bincode", 158 | "getrandom", 159 | "once_cell", 160 | "rand", 161 | "serde", 162 | ] 163 | 164 | [[package]] 165 | name = "wasi" 166 | version = "0.11.0+wasi-snapshot-preview1" 167 | source = "registry+https://github.com/rust-lang/crates.io-index" 168 | checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" 169 | 170 | [[package]] 171 | name = "zerocopy" 172 | version = "0.7.35" 173 | source = "registry+https://github.com/rust-lang/crates.io-index" 174 | checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" 175 | dependencies = [ 176 | "byteorder", 177 | "zerocopy-derive", 178 | ] 179 | 180 | [[package]] 181 | name = "zerocopy-derive" 182 | version = "0.7.35" 183 | source = "registry+https://github.com/rust-lang/crates.io-index" 184 | checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" 185 | dependencies = [ 186 | "proc-macro2", 187 | "quote", 188 | "syn", 189 | ] 190 | -------------------------------------------------------------------------------- /guessing_game/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "guessing_game" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | [dependencies] 7 | valida-rs = { git = "https://github.com/lita-xyz/valida-rs.git", branch = "main" } 8 | getrandom = "0.2.15" 9 | rand = "0.8.5" -------------------------------------------------------------------------------- /guessing_game/LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /guessing_game/README.md: -------------------------------------------------------------------------------- 1 | # A random number guessing game on Valida 2 | 3 | ## System requirements 4 | 5 | This template supports x86-64 Linux. [`rustup`](https://www.rust-lang.org/tools/install) is required. Arch Linux and Ubuntu are specifically supported, with other platforms possibly requiring some tinkering to make work. 6 | 7 | ## Toolchain installation 8 | 9 | To run this project in the Valida VM, you need the Valida toolchain installed. Go to [LLVM Valida releases](https://github.com/lita-xyz/llvm-valida-releases/releases) to find the latest release. Download the release tarball, extract it, `cd` into the extracted folder, and run `sudo ./install.sh`. 10 | 11 | ## Entering the Valida shell 12 | 13 | To put the Valida toolchain on your PATH, you can enter the Valida shell by running `valida-shell` in your shell. The above installation process should have resulted in `valida-shell` being on your `PATH`. 14 | 15 | ## Usage 16 | 17 | Build the project, from the root directory of this repo: 18 | 19 | ``` 20 | cargo +valida build --release 21 | ``` 22 | 23 | To run the program, in the Valida shell, from the root directory of this repo: 24 | 25 | ``` 26 | valida> valida run ./target/valida-unknown-baremetal-gnu/release/guessing_game log 27 | ``` 28 | 29 | The `run` command runs the program, and starts the game. The log of the game contains the game history. 30 | -------------------------------------------------------------------------------- /guessing_game/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly" 3 | components = ["rustc", "rust-src", "rustfmt", "cargo", "clippy", "rust-docs", "rust-analyzer"] 4 | -------------------------------------------------------------------------------- /guessing_game/src/main.rs: -------------------------------------------------------------------------------- 1 | #![no_main] 2 | 3 | use rand::Rng; 4 | use std::cmp::Ordering; 5 | 6 | valida_rs::entrypoint!(main); 7 | 8 | fn main() { 9 | println!("Guess the number!"); 10 | 11 | let secret_number = rand::thread_rng().gen_range(1..=100); 12 | 13 | loop { 14 | println!("Please input your guess between 1 and 100."); 15 | 16 | let guess = match valida_rs::io::read_line::() { 17 | Ok(num) => num, 18 | Err(e) => { 19 | println!("Error reading input: {}", e); 20 | continue; 21 | } 22 | }; 23 | 24 | println!("You guessed: {}", guess); 25 | 26 | match guess.cmp(&secret_number) { 27 | Ordering::Less => println!("Too small!"), 28 | Ordering::Greater => println!("Too big!"), 29 | Ordering::Equal => { 30 | println!("You win!"); 31 | break; 32 | } 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /hello_world/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [build] 2 | target = "valida-unknown-baremetal-gnu" 3 | 4 | [target.valida-unknown-baremetal-gnu] 5 | linker = "/valida-toolchain/bin/ld.lld" 6 | rustflags = [ 7 | "-C", "link-arg=/valida-toolchain/ValidaEntryPoint.o", 8 | "-C", "link-arg=--script=/valida-toolchain/valida.ld", 9 | "-C", "link-arg=/valida-toolchain/lib/valida-unknown-baremetal-gnu/libc.a", 10 | "-C", "link-arg=/valida-toolchain/lib/valida-unknown-baremetal-gnu/libm.a", 11 | "-C", "link-arg=--noinhibit-exec", 12 | ] 13 | 14 | [env] 15 | CC_valida_unknown_baremetal_gnu = "/valida-toolchain/bin/clang" 16 | CFLAGS_valida_unknown_baremetal_gnu = "--sysroot=/valida-toolchain/ -isystem /valida-toolchain/include" 17 | -------------------------------------------------------------------------------- /hello_world/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 4 4 | 5 | [[package]] 6 | name = "hello_world" 7 | version = "0.1.0" 8 | dependencies = [ 9 | "valida-rs", 10 | ] 11 | 12 | [[package]] 13 | name = "valida-rs" 14 | version = "0.1.0" 15 | source = "git+https://github.com/lita-xyz/valida-rs.git?branch=no-deps#b2216526d65b777df76d4d8267b98c0cc53b42e0" 16 | -------------------------------------------------------------------------------- /hello_world/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "hello_world" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | [dependencies] 7 | valida-rs = { git = "https://github.com/lita-xyz/valida-rs.git", branch = "no-deps" } 8 | -------------------------------------------------------------------------------- /hello_world/README.md: -------------------------------------------------------------------------------- 1 | # Hello World in Valida 2 | 3 | This is a simple program that prints "Hello, World!" in Valida. You can use this as a template for your projects which create Valida proofs of execution of Rust code. 4 | 5 | ## System requirements 6 | 7 | This template supports x86-64 Linux. [`rustup`](https://www.rust-lang.org/tools/install) is required. Arch Linux and Ubuntu are specifically supported, with other platforms possibly requiring some tinkering to make work. 8 | 9 | ## Toolchain installation 10 | 11 | To run this project in the Valida VM, you need the Valida toolchain installed. Go to [LLVM Valida releases](https://github.com/lita-xyz/llvm-valida-releases/releases) to find the latest release. Download the release tarball, extract it, `cd` into the extracted folder, and run `sudo ./install.sh`. 12 | 13 | ## Entering the Valida shell 14 | 15 | To put the Valida toolchain on your PATH, you can enter the Valida shell by running `valida-shell` in your shell. The above installation process should have resulted in `valida-shell` being on your `PATH`. 16 | 17 | ## Usage 18 | 19 | Build the project, from the root directory of this repo: 20 | 21 | ``` 22 | cargo +valida build --release 23 | ``` 24 | 25 | To run the program, in the Valida shell, from the root directory of this repo: 26 | 27 | ``` 28 | valida> valida run ./target/valida-unknown-baremetal-gnu/release/my-project log 29 | ``` 30 | 31 | The `run` command runs the program and print "Hello, World!" to the console and the file `log` in the current directory. 32 | -------------------------------------------------------------------------------- /hello_world/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly" 3 | components = ["rustc", "rust-src", "rustfmt", "cargo", "clippy", "rust-docs", "rust-analyzer"] 4 | -------------------------------------------------------------------------------- /hello_world/src/main.rs: -------------------------------------------------------------------------------- 1 | #![no_main] 2 | 3 | #[no_mangle] 4 | 5 | pub fn main() { 6 | println!("Hello, world!"); 7 | } 8 | -------------------------------------------------------------------------------- /json_contains/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [build] 2 | target = "valida-unknown-baremetal-gnu" 3 | 4 | [target.valida-unknown-baremetal-gnu] 5 | linker = "/valida-toolchain/bin/ld.lld" 6 | rustflags = [ 7 | "-C", "link-arg=/valida-toolchain/ValidaEntryPoint.o", 8 | "-C", "link-arg=--script=/valida-toolchain/valida.ld", 9 | "-C", "link-arg=/valida-toolchain/lib/valida-unknown-baremetal-gnu/libc.a", 10 | "-C", "link-arg=/valida-toolchain/lib/valida-unknown-baremetal-gnu/libm.a", 11 | "-C", "link-arg=--noinhibit-exec", 12 | ] 13 | 14 | [env] 15 | CC_valida_unknown_baremetal_gnu = "/valida-toolchain/bin/clang" 16 | CFLAGS_valida_unknown_baremetal_gnu = "--sysroot=/valida-toolchain/ -isystem /valida-toolchain/include" 17 | -------------------------------------------------------------------------------- /json_contains/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /log 3 | -------------------------------------------------------------------------------- /json_contains/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 4 4 | 5 | [[package]] 6 | name = "bincode" 7 | version = "1.3.3" 8 | source = "registry+https://github.com/rust-lang/crates.io-index" 9 | checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad" 10 | dependencies = [ 11 | "serde", 12 | ] 13 | 14 | [[package]] 15 | name = "byteorder" 16 | version = "1.5.0" 17 | source = "registry+https://github.com/rust-lang/crates.io-index" 18 | checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" 19 | 20 | [[package]] 21 | name = "cfg-if" 22 | version = "1.0.0" 23 | source = "registry+https://github.com/rust-lang/crates.io-index" 24 | checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" 25 | 26 | [[package]] 27 | name = "equivalent" 28 | version = "1.0.1" 29 | source = "registry+https://github.com/rust-lang/crates.io-index" 30 | checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" 31 | 32 | [[package]] 33 | name = "getrandom" 34 | version = "0.2.15" 35 | source = "registry+https://github.com/rust-lang/crates.io-index" 36 | checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" 37 | dependencies = [ 38 | "cfg-if", 39 | "libc", 40 | "wasi", 41 | ] 42 | 43 | [[package]] 44 | name = "hashbrown" 45 | version = "0.15.0" 46 | source = "registry+https://github.com/rust-lang/crates.io-index" 47 | checksum = "1e087f84d4f86bf4b218b927129862374b72199ae7d8657835f1e89000eea4fb" 48 | 49 | [[package]] 50 | name = "indexmap" 51 | version = "2.6.0" 52 | source = "registry+https://github.com/rust-lang/crates.io-index" 53 | checksum = "707907fe3c25f5424cce2cb7e1cbcafee6bdbe735ca90ef77c29e84591e5b9da" 54 | dependencies = [ 55 | "equivalent", 56 | "hashbrown", 57 | ] 58 | 59 | [[package]] 60 | name = "itoa" 61 | version = "1.0.11" 62 | source = "registry+https://github.com/rust-lang/crates.io-index" 63 | checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" 64 | 65 | [[package]] 66 | name = "json_contains" 67 | version = "0.1.0" 68 | dependencies = [ 69 | "jsonpath_lib", 70 | "serde", 71 | "serde_json", 72 | "valida-rs", 73 | ] 74 | 75 | [[package]] 76 | name = "jsonpath_lib" 77 | version = "0.3.0" 78 | source = "registry+https://github.com/rust-lang/crates.io-index" 79 | checksum = "eaa63191d68230cccb81c5aa23abd53ed64d83337cacbb25a7b8c7979523774f" 80 | dependencies = [ 81 | "log", 82 | "serde", 83 | "serde_json", 84 | ] 85 | 86 | [[package]] 87 | name = "libc" 88 | version = "0.2.161" 89 | source = "registry+https://github.com/rust-lang/crates.io-index" 90 | checksum = "8e9489c2807c139ffd9c1794f4af0ebe86a828db53ecdc7fea2111d0fed085d1" 91 | 92 | [[package]] 93 | name = "log" 94 | version = "0.4.22" 95 | source = "registry+https://github.com/rust-lang/crates.io-index" 96 | checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" 97 | 98 | [[package]] 99 | name = "memchr" 100 | version = "2.7.4" 101 | source = "registry+https://github.com/rust-lang/crates.io-index" 102 | checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" 103 | 104 | [[package]] 105 | name = "once_cell" 106 | version = "1.20.2" 107 | source = "registry+https://github.com/rust-lang/crates.io-index" 108 | checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" 109 | 110 | [[package]] 111 | name = "ppv-lite86" 112 | version = "0.2.20" 113 | source = "registry+https://github.com/rust-lang/crates.io-index" 114 | checksum = "77957b295656769bb8ad2b6a6b09d897d94f05c41b069aede1fcdaa675eaea04" 115 | dependencies = [ 116 | "zerocopy", 117 | ] 118 | 119 | [[package]] 120 | name = "proc-macro2" 121 | version = "1.0.88" 122 | source = "registry+https://github.com/rust-lang/crates.io-index" 123 | checksum = "7c3a7fc5db1e57d5a779a352c8cdb57b29aa4c40cc69c3a68a7fedc815fbf2f9" 124 | dependencies = [ 125 | "unicode-ident", 126 | ] 127 | 128 | [[package]] 129 | name = "quote" 130 | version = "1.0.37" 131 | source = "registry+https://github.com/rust-lang/crates.io-index" 132 | checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" 133 | dependencies = [ 134 | "proc-macro2", 135 | ] 136 | 137 | [[package]] 138 | name = "rand" 139 | version = "0.8.5" 140 | source = "registry+https://github.com/rust-lang/crates.io-index" 141 | checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" 142 | dependencies = [ 143 | "libc", 144 | "rand_chacha", 145 | "rand_core", 146 | ] 147 | 148 | [[package]] 149 | name = "rand_chacha" 150 | version = "0.3.1" 151 | source = "registry+https://github.com/rust-lang/crates.io-index" 152 | checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" 153 | dependencies = [ 154 | "ppv-lite86", 155 | "rand_core", 156 | ] 157 | 158 | [[package]] 159 | name = "rand_core" 160 | version = "0.6.4" 161 | source = "registry+https://github.com/rust-lang/crates.io-index" 162 | checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" 163 | dependencies = [ 164 | "getrandom", 165 | ] 166 | 167 | [[package]] 168 | name = "ryu" 169 | version = "1.0.18" 170 | source = "registry+https://github.com/rust-lang/crates.io-index" 171 | checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" 172 | 173 | [[package]] 174 | name = "serde" 175 | version = "1.0.210" 176 | source = "registry+https://github.com/rust-lang/crates.io-index" 177 | checksum = "c8e3592472072e6e22e0a54d5904d9febf8508f65fb8552499a1abc7d1078c3a" 178 | dependencies = [ 179 | "serde_derive", 180 | ] 181 | 182 | [[package]] 183 | name = "serde_derive" 184 | version = "1.0.210" 185 | source = "registry+https://github.com/rust-lang/crates.io-index" 186 | checksum = "243902eda00fad750862fc144cea25caca5e20d615af0a81bee94ca738f1df1f" 187 | dependencies = [ 188 | "proc-macro2", 189 | "quote", 190 | "syn", 191 | ] 192 | 193 | [[package]] 194 | name = "serde_json" 195 | version = "1.0.132" 196 | source = "registry+https://github.com/rust-lang/crates.io-index" 197 | checksum = "d726bfaff4b320266d395898905d0eba0345aae23b54aee3a737e260fd46db03" 198 | dependencies = [ 199 | "indexmap", 200 | "itoa", 201 | "memchr", 202 | "ryu", 203 | "serde", 204 | ] 205 | 206 | [[package]] 207 | name = "syn" 208 | version = "2.0.82" 209 | source = "registry+https://github.com/rust-lang/crates.io-index" 210 | checksum = "83540f837a8afc019423a8edb95b52a8effe46957ee402287f4292fae35be021" 211 | dependencies = [ 212 | "proc-macro2", 213 | "quote", 214 | "unicode-ident", 215 | ] 216 | 217 | [[package]] 218 | name = "unicode-ident" 219 | version = "1.0.13" 220 | source = "registry+https://github.com/rust-lang/crates.io-index" 221 | checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" 222 | 223 | [[package]] 224 | name = "valida-rs" 225 | version = "0.1.0" 226 | source = "git+https://github.com/lita-xyz/valida-rs.git?branch=main#84df845d672aa72f7aa0d591caa3e5201dac2b75" 227 | dependencies = [ 228 | "bincode", 229 | "getrandom", 230 | "once_cell", 231 | "rand", 232 | "serde", 233 | ] 234 | 235 | [[package]] 236 | name = "wasi" 237 | version = "0.11.0+wasi-snapshot-preview1" 238 | source = "registry+https://github.com/rust-lang/crates.io-index" 239 | checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" 240 | 241 | [[package]] 242 | name = "zerocopy" 243 | version = "0.7.35" 244 | source = "registry+https://github.com/rust-lang/crates.io-index" 245 | checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" 246 | dependencies = [ 247 | "byteorder", 248 | "zerocopy-derive", 249 | ] 250 | 251 | [[package]] 252 | name = "zerocopy-derive" 253 | version = "0.7.35" 254 | source = "registry+https://github.com/rust-lang/crates.io-index" 255 | checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" 256 | dependencies = [ 257 | "proc-macro2", 258 | "quote", 259 | "syn", 260 | ] 261 | -------------------------------------------------------------------------------- /json_contains/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "json_contains" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | [dependencies] 7 | valida-rs = { git = "https://github.com/lita-xyz/valida-rs.git", branch = "main" } 8 | serde_json = { version = "1", default-features = false } 9 | serde = { version = "1", default-features = false, features = ["derive"] } 10 | jsonpath_lib = "0.3.0" 11 | -------------------------------------------------------------------------------- /json_contains/README.md: -------------------------------------------------------------------------------- 1 | # Prove a JsonPath contains a value 2 | 3 | ## System requirements 4 | 5 | This template supports x86-64 Linux. [`rustup`](https://www.rust-lang.org/tools/install) is required. Arch Linux and Ubuntu are specifically supported, with other platforms possibly requiring some tinkering to make work. 6 | 7 | ## Toolchain installation 8 | 9 | To run this project in the Valida VM, you need the Valida toolchain installed. Go to [LLVM Valida releases](https://github.com/lita-xyz/llvm-valida-releases/releases) to find the latest release. Download the release tarball, extract it, `cd` into the extracted folder, and run `sudo ./install.sh`. 10 | 11 | ## Entering the Valida shell 12 | 13 | To put the Valida toolchain on your PATH, you can enter the Valida shell by running `valida-shell` in your shell. The above installation process should have resulted in `valida-shell` being on your `PATH`. 14 | 15 | ## Usage 16 | 17 | Build the project, from the root directory of this repo: 18 | 19 | ```bash 20 | valida> cargo +valida build --release 21 | ``` 22 | 23 | Run the program in Valida: 24 | 25 | ```bash 26 | valida> 27 | valida> echo '{"path":"$.foo.bar","expected":{"baz":1},"json":{"foo":{"bar":{"baz":1}}}}' | valida run target/valida-unknown-baremetal-gnu/release/json_contains log 28 | path: $.foo.bar 29 | contains json: {"baz":1} 30 | ``` 31 | -------------------------------------------------------------------------------- /json_contains/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly" 3 | components = ["rustc", "rust-src", "rustfmt", "cargo", "clippy", "rust-docs", "rust-analyzer"] 4 | -------------------------------------------------------------------------------- /json_contains/src/main.rs: -------------------------------------------------------------------------------- 1 | //! A simple check to prove if a JSON object contains a path and or value. 2 | //! This code is for illustrative purposes only it should not be assumed to be correct. 3 | #![no_main] 4 | 5 | use valida_rs::io::InputTape; 6 | use serde::Deserialize; 7 | use serde_json::Value; 8 | 9 | valida_rs::entrypoint!(main); 10 | 11 | fn main() { 12 | let mut de = serde_json::Deserializer::from_reader(InputTape); 13 | let Input { 14 | path, 15 | json, 16 | expected, 17 | } = Input::deserialize(&mut de).expect("could not parse input"); 18 | 19 | validate_json_path(&path, &expected, &json); 20 | println!("path: {}", path); 21 | println!("contains json: {}", expected); 22 | } 23 | 24 | #[derive(Deserialize)] 25 | struct Input { 26 | path: String, 27 | json: Value, 28 | expected: Value, 29 | } 30 | 31 | fn validate_json_path(path: &str, expected: &Value, json: &Value) { 32 | let compiled = jsonpath_lib::Compiled::compile(path).expect("Invalid JSONPath expression"); 33 | let results = compiled.select(json).expect("Failed to evaluate JSONPath"); 34 | 35 | match expected { 36 | Value::Array(expected) => { 37 | assert_eq!(results.len(), expected.len()); 38 | for (result, expected) in results.iter().zip(expected.iter()) { 39 | assert_eq!(result, &expected); 40 | } 41 | } 42 | value => { 43 | assert_eq!(results.len(), 1); 44 | assert_eq!(results[0], value); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /palindrome/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [build] 2 | target = "valida-unknown-baremetal-gnu" 3 | 4 | [target.valida-unknown-baremetal-gnu] 5 | linker = "/valida-toolchain/bin/ld.lld" 6 | rustflags = [ 7 | "-C", "link-arg=/valida-toolchain/ValidaEntryPoint.o", 8 | "-C", "link-arg=--script=/valida-toolchain/valida.ld", 9 | "-C", "link-arg=/valida-toolchain/lib/valida-unknown-baremetal-gnu/libc.a", 10 | "-C", "link-arg=/valida-toolchain/lib/valida-unknown-baremetal-gnu/libm.a", 11 | "-C", "link-arg=--noinhibit-exec", 12 | ] 13 | 14 | [env] 15 | CC_valida_unknown_baremetal_gnu = "/valida-toolchain/bin/clang" 16 | CFLAGS_valida_unknown_baremetal_gnu = "--sysroot=/valida-toolchain/ -isystem /valida-toolchain/include" 17 | -------------------------------------------------------------------------------- /palindrome/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 4 4 | 5 | [[package]] 6 | name = "palindrome" 7 | version = "0.1.0" 8 | dependencies = [ 9 | "valida-rs", 10 | ] 11 | 12 | [[package]] 13 | name = "valida-rs" 14 | version = "0.1.0" 15 | source = "git+https://github.com/lita-xyz/valida-rs.git?branch=no-deps#23b9906c4466e8fa7e2547e9124f200c20440688" 16 | -------------------------------------------------------------------------------- /palindrome/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "palindrome" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | [dependencies] 7 | valida-rs = { git = "https://github.com/lita-xyz/valida-rs.git", branch = "no-deps" } 8 | -------------------------------------------------------------------------------- /palindrome/README.md: -------------------------------------------------------------------------------- 1 | # Palindrome in Valida 2 | 3 | This is a simple program that asks the user for a word or phrase and then checks if it is a palindrome. 4 | 5 | ## System requirements 6 | 7 | This template supports x86-64 Linux. [`rustup`](https://www.rust-lang.org/tools/install) is required. Arch Linux and Ubuntu are specifically supported, with other platforms possibly requiring some tinkering to make work. 8 | 9 | ## Toolchain installation 10 | 11 | To run this project in the Valida VM, you need the Valida toolchain installed. Go to [LLVM Valida releases](https://github.com/lita-xyz/llvm-valida-releases/releases) to find the latest release. Download the release tarball, extract it, `cd` into the extracted folder, and run `sudo ./install.sh`. 12 | 13 | ## Entering the Valida shell 14 | 15 | To put the Valida toolchain on your PATH, you can enter the Valida shell by running `valida-shell` in your shell. The above installation process should have resulted in `valida-shell` being on your `PATH`. 16 | 17 | ## Usage 18 | 19 | Build the project, from the root directory of this repo: 20 | 21 | ``` 22 | cargo +valida build --release 23 | ``` 24 | 25 | To run the program, in the Valida shell, from the root directory of this repo: 26 | 27 | ``` 28 | valida> valida run ./target/valida-unknown-baremetal-gnu/release/palindrome log 29 | ``` 30 | 31 | The `run` command runs the program and asks the user for a word or phrase, then prints whether it is a palindrome to the console and the file `log` in the current directory. -------------------------------------------------------------------------------- /palindrome/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly" 3 | components = ["rustc", "rust-src", "rustfmt", "cargo", "clippy", "rust-docs", "rust-analyzer"] 4 | -------------------------------------------------------------------------------- /palindrome/src/main.rs: -------------------------------------------------------------------------------- 1 | #![no_main] 2 | 3 | #[no_mangle] 4 | pub fn main() { 5 | println!("Please enter a word or phrase:"); 6 | let word = match valida_rs::io::read_line::() { 7 | Ok(w) => w, 8 | Err(e) => { 9 | println!("Error reading input: {}", e); 10 | return; 11 | } 12 | }; 13 | let word_no_spaces = word.chars().filter(|c| !c.is_whitespace()).collect::(); 14 | let is_palindrome = word_no_spaces.chars().eq(word_no_spaces.chars().rev()); 15 | println!("Is '{}' a palindrome? {}", word, is_palindrome); 16 | } 17 | -------------------------------------------------------------------------------- /prime_factorization/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [build] 2 | target = "valida-unknown-baremetal-gnu" 3 | 4 | [target.valida-unknown-baremetal-gnu] 5 | linker = "/valida-toolchain/bin/ld.lld" 6 | rustflags = [ 7 | "-C", "link-arg=/valida-toolchain/DelendumEntryPoint.o", 8 | "-C", "link-arg=--script=/valida-toolchain/valida.ld", 9 | "-C", "link-arg=/valida-toolchain/lib/delendum-unknown-baremetal-gnu/libc.a", 10 | "-C", "link-arg=/valida-toolchain/lib/delendum-unknown-baremetal-gnu/libm.a", 11 | "-C", "link-arg=--noinhibit-exec", 12 | ] 13 | 14 | [env] 15 | CC_delendum_unknown_baremetal_gnu = "/valida-toolchain/bin/clang" 16 | CFLAGS_delendum_unknown_baremetal_gnu = "--sysroot=/valida-toolchain/ -isystem /valida-toolchain/include" 17 | -------------------------------------------------------------------------------- /prime_factorization/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 4 4 | 5 | [[package]] 6 | name = "bincode" 7 | version = "1.3.3" 8 | source = "registry+https://github.com/rust-lang/crates.io-index" 9 | checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad" 10 | dependencies = [ 11 | "serde", 12 | ] 13 | 14 | [[package]] 15 | name = "byteorder" 16 | version = "1.5.0" 17 | source = "registry+https://github.com/rust-lang/crates.io-index" 18 | checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" 19 | 20 | [[package]] 21 | name = "cfg-if" 22 | version = "1.0.0" 23 | source = "registry+https://github.com/rust-lang/crates.io-index" 24 | checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" 25 | 26 | [[package]] 27 | name = "getrandom" 28 | version = "0.2.15" 29 | source = "registry+https://github.com/rust-lang/crates.io-index" 30 | checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" 31 | dependencies = [ 32 | "cfg-if", 33 | "libc", 34 | "wasi", 35 | ] 36 | 37 | [[package]] 38 | name = "libc" 39 | version = "0.2.161" 40 | source = "registry+https://github.com/rust-lang/crates.io-index" 41 | checksum = "8e9489c2807c139ffd9c1794f4af0ebe86a828db53ecdc7fea2111d0fed085d1" 42 | 43 | [[package]] 44 | name = "once_cell" 45 | version = "1.20.2" 46 | source = "registry+https://github.com/rust-lang/crates.io-index" 47 | checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" 48 | 49 | [[package]] 50 | name = "ppv-lite86" 51 | version = "0.2.20" 52 | source = "registry+https://github.com/rust-lang/crates.io-index" 53 | checksum = "77957b295656769bb8ad2b6a6b09d897d94f05c41b069aede1fcdaa675eaea04" 54 | dependencies = [ 55 | "zerocopy", 56 | ] 57 | 58 | [[package]] 59 | name = "prime_factorization" 60 | version = "0.1.0" 61 | dependencies = [ 62 | "getrandom", 63 | "valida-rs", 64 | ] 65 | 66 | [[package]] 67 | name = "proc-macro2" 68 | version = "1.0.89" 69 | source = "registry+https://github.com/rust-lang/crates.io-index" 70 | checksum = "f139b0662de085916d1fb67d2b4169d1addddda1919e696f3252b740b629986e" 71 | dependencies = [ 72 | "unicode-ident", 73 | ] 74 | 75 | [[package]] 76 | name = "quote" 77 | version = "1.0.37" 78 | source = "registry+https://github.com/rust-lang/crates.io-index" 79 | checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" 80 | dependencies = [ 81 | "proc-macro2", 82 | ] 83 | 84 | [[package]] 85 | name = "rand" 86 | version = "0.8.5" 87 | source = "registry+https://github.com/rust-lang/crates.io-index" 88 | checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" 89 | dependencies = [ 90 | "libc", 91 | "rand_chacha", 92 | "rand_core", 93 | ] 94 | 95 | [[package]] 96 | name = "rand_chacha" 97 | version = "0.3.1" 98 | source = "registry+https://github.com/rust-lang/crates.io-index" 99 | checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" 100 | dependencies = [ 101 | "ppv-lite86", 102 | "rand_core", 103 | ] 104 | 105 | [[package]] 106 | name = "rand_core" 107 | version = "0.6.4" 108 | source = "registry+https://github.com/rust-lang/crates.io-index" 109 | checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" 110 | dependencies = [ 111 | "getrandom", 112 | ] 113 | 114 | [[package]] 115 | name = "serde" 116 | version = "1.0.213" 117 | source = "registry+https://github.com/rust-lang/crates.io-index" 118 | checksum = "3ea7893ff5e2466df8d720bb615088341b295f849602c6956047f8f80f0e9bc1" 119 | dependencies = [ 120 | "serde_derive", 121 | ] 122 | 123 | [[package]] 124 | name = "serde_derive" 125 | version = "1.0.213" 126 | source = "registry+https://github.com/rust-lang/crates.io-index" 127 | checksum = "7e85ad2009c50b58e87caa8cd6dac16bdf511bbfb7af6c33df902396aa480fa5" 128 | dependencies = [ 129 | "proc-macro2", 130 | "quote", 131 | "syn", 132 | ] 133 | 134 | [[package]] 135 | name = "syn" 136 | version = "2.0.82" 137 | source = "registry+https://github.com/rust-lang/crates.io-index" 138 | checksum = "83540f837a8afc019423a8edb95b52a8effe46957ee402287f4292fae35be021" 139 | dependencies = [ 140 | "proc-macro2", 141 | "quote", 142 | "unicode-ident", 143 | ] 144 | 145 | [[package]] 146 | name = "unicode-ident" 147 | version = "1.0.13" 148 | source = "registry+https://github.com/rust-lang/crates.io-index" 149 | checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" 150 | 151 | [[package]] 152 | name = "valida-rs" 153 | version = "0.1.0" 154 | source = "git+https://github.com/lita-xyz/valida-rs.git?branch=main#84df845d672aa72f7aa0d591caa3e5201dac2b75" 155 | dependencies = [ 156 | "bincode", 157 | "getrandom", 158 | "once_cell", 159 | "rand", 160 | "serde", 161 | ] 162 | 163 | [[package]] 164 | name = "wasi" 165 | version = "0.11.0+wasi-snapshot-preview1" 166 | source = "registry+https://github.com/rust-lang/crates.io-index" 167 | checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" 168 | 169 | [[package]] 170 | name = "zerocopy" 171 | version = "0.7.35" 172 | source = "registry+https://github.com/rust-lang/crates.io-index" 173 | checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" 174 | dependencies = [ 175 | "byteorder", 176 | "zerocopy-derive", 177 | ] 178 | 179 | [[package]] 180 | name = "zerocopy-derive" 181 | version = "0.7.35" 182 | source = "registry+https://github.com/rust-lang/crates.io-index" 183 | checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" 184 | dependencies = [ 185 | "proc-macro2", 186 | "quote", 187 | "syn", 188 | ] 189 | -------------------------------------------------------------------------------- /prime_factorization/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "prime_factorization" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | [dependencies] 7 | valida-rs = { git = "https://github.com/lita-xyz/valida-rs.git", branch = "main" } 8 | getrandom = "0.2.15" 9 | -------------------------------------------------------------------------------- /prime_factorization/README.md: -------------------------------------------------------------------------------- 1 | # Prime factorization example for Valida 2 | 3 | This is a simple program that verifies prime factorizations in Valida. You can use this program to prove knowledge of the prime factorization of a 32-bit number. 4 | 5 | ## Usage 6 | 7 | Build the project, from the root directory of this repo, in the Valida shell: 8 | 9 | ``` 10 | valida> cargo +valida build 11 | ``` 12 | 13 | To run the program, in the Valida shell, from the root directory of this repo: 14 | 15 | ``` 16 | valida> valida run ./target/valida-unknown-baremetal-gnu/debug/prime_factorization log 17 | ``` 18 | -------------------------------------------------------------------------------- /prime_factorization/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly" 3 | components = ["rustc", "rust-src", "rustfmt", "cargo", "clippy", "rust-docs", "rust-analyzer"] 4 | -------------------------------------------------------------------------------- /prime_factorization/src/lib.rs: -------------------------------------------------------------------------------- 1 | pub fn read_number() -> u32 { 2 | loop { 3 | match valida_rs::io::read_line::() { 4 | Ok(num) => break num, 5 | Err(e) => { 6 | valida_rs::io::println(&format!("Error reading input: {}. Please try again:", e)); 7 | } 8 | } 9 | } 10 | } 11 | 12 | pub fn check_prime_factorization(x: u32, ys: &[u32]) -> bool { 13 | let mut z = 1; 14 | for y in ys { 15 | if !is_prime(*y) { 16 | return false; 17 | } 18 | z *= *y; 19 | } 20 | z == x 21 | } 22 | 23 | pub fn is_prime(x: u32) -> bool { 24 | for y in 2..x { 25 | if x % y == 0 { 26 | return false; 27 | } 28 | } 29 | true 30 | } 31 | -------------------------------------------------------------------------------- /prime_factorization/src/main.rs: -------------------------------------------------------------------------------- 1 | #![no_main] 2 | 3 | use prime_factorization::{read_number, check_prime_factorization}; 4 | 5 | valida_rs::entrypoint!(main); 6 | 7 | pub fn main() { 8 | valida_rs::io::println("Please enter a 32-bit number:"); 9 | // Read a line from stdin and parse it as a u32. 10 | let x = read_number(); 11 | 12 | valida_rs::io::println("Please enter the number of prime factors (with multiplicity):"); 13 | let n = read_number(); 14 | 15 | let mut ys = vec![]; 16 | for _i in 0..n { 17 | valida_rs::io::println("Please enter the next prime factor:"); 18 | ys.push(read_number()); 19 | } 20 | 21 | if check_prime_factorization(x, ys.as_ref()) { 22 | valida_rs::io::println("Verified prime factorization of:"); 23 | valida_rs::io::println(&x.to_string()); 24 | } else { 25 | valida_rs::io::println("Failed to verify prime factorization"); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /rust_test_script.bash: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # This script tests the crates in the rust-examples directory. 4 | # It builds each crate and runs it on the Valida VM and the x86_64-unknown-linux-gnu host, comparing the VM output to the host output. 5 | # It skips any crates that don't have a input file in the test_data directory. 6 | # For the guessing game crate, it is actually random on the host machine so we need to give it the expected output by hand. 7 | # To add a new crate to the test, add a directory with the same name as the crate to the test_data directory and put the input file in it. 8 | # If there is no input, add an empty input file. 9 | # If the crate has randomness, you will need to give it the expected output by hand. Add the crate name to the RANDOM_CRATES array below. 10 | # Make sure that you have set the vm_executable variable to the path to the valida-vm executable. 11 | # If your host is not x86_64 linux, you will need to change the target in the cargo run command. 12 | # Run the script in the `rust-examples` directory with: bash rust_test_script.bash 13 | # To test a specific crate: bash rust_test_script.bash -c 14 | 15 | # configuration 16 | test_data_dir='test_data' 17 | crates_dir='.' 18 | # path to the valida-vm executable 19 | vm_executable='/valida-toolchain/bin/valida' 20 | 21 | # utilities 22 | function fail { 23 | echo $1; 24 | exit 1 25 | } 26 | 27 | # global sanity check 28 | test -d "$test_data_dir" || fail "the test data directory '${test_data_dir}' does not exist, please create it and put the test input data in it" 29 | test -f "$vm_executable" || fail "the vm executable '${vm_executable}' is not a file, please correctly specify the path to the valida-vm executable in the test script" 30 | test -x "$vm_executable" || fail "the vm executable '${vm_executable}' is not executable, please correctly specify the path to the valida-vm executable in the test script" 31 | 32 | # Parse command line arguments 33 | crate_to_test="" 34 | while getopts "c:" opt; do 35 | case $opt in 36 | c) 37 | crate_to_test="$OPTARG" 38 | ;; 39 | \?) 40 | echo "Invalid option: -$OPTARG" >&2 41 | echo "Usage: $0 [-c crate_name]" >&2 42 | exit 1 43 | ;; 44 | esac 45 | done 46 | 47 | # If a specific crate was specified, verify it exists 48 | if [ -n "$crate_to_test" ]; then 49 | if [ ! -d "$test_data_dir/$crate_to_test" ]; then 50 | fail "crate '$crate_to_test' not found in test data directory" 51 | fi 52 | fi 53 | 54 | if [ -n "$crate_to_test" ]; then 55 | crate_test_dirs=("$test_data_dir/$crate_to_test") 56 | else 57 | crate_test_dirs=("$test_data_dir"/*) 58 | fi 59 | 60 | for crate_test_dir in "${crate_test_dirs[@]}" 61 | do { 62 | crate=$(basename "${crate_test_dir}") 63 | 64 | # build crate, silently to avoid polluting the output 65 | echo "building ${crate}" 66 | pushd "${crates_dir}/${crate}" 67 | cargo clean --quiet 68 | if ! cargo +valida build --quiet 69 | then 70 | echo "failed to build ${crate}" 71 | popd 72 | continue 73 | fi 74 | popd 75 | 76 | # test crate. Print the diff (empty if equal) and output a success message if actual output matches expected output. 77 | echo "testing ${crate}" 78 | # Guessing game is actually random on host, so we need to give it the expected output by hand, not running it on the host 79 | if [ "$crate" != "guessing_game" ]; then 80 | # For any crates that don't have randomness, run on native host to generate expected output 81 | cargo run --quiet --manifest-path "${crates_dir}/${crate}/Cargo.toml" --target x86_64-unknown-linux-gnu log\ 82 | < "${crate_test_dir}/input" > "${crate_test_dir}/expected_output" 83 | fi 84 | 85 | # Run on Valida VM to generate the actual output 86 | "$vm_executable" run "${crates_dir}/${crate}/target/valida-unknown-baremetal-gnu/debug/${crate}" log \ 87 | < "${crate_test_dir}/input" > "${crate_test_dir}/actual_output" 88 | diff <(tr -d '\n' < "${crate_test_dir}/actual_output") <(tr -d '\n' < "${crate_test_dir}/expected_output") && echo "${crate} execution test passed" 89 | 90 | # Prove and verify 91 | echo "PROVING $crate" 92 | "$vm_executable" prove "${crates_dir}/${crate}/target/valida-unknown-baremetal-gnu/debug/${crate}" proof \ 93 | < "${crate_test_dir}/input" > "${crate_test_dir}/actual_output" 94 | echo "VERIFYING $crate" 95 | "$vm_executable" verify "${crates_dir}/${crate}/target/valida-unknown-baremetal-gnu/debug/${crate}" proof -o log 96 | } 97 | done 98 | -------------------------------------------------------------------------------- /secp256k1/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [build] 2 | target = "valida-unknown-baremetal-gnu" 3 | 4 | [target.valida-unknown-baremetal-gnu] 5 | linker = "/valida-toolchain/bin/ld.lld" 6 | rustflags = [ 7 | "-C", "link-arg=/valida-toolchain/ValidaEntryPoint.o", 8 | "-C", "link-arg=--script=/valida-toolchain/valida.ld", 9 | "-C", "link-arg=/valida-toolchain/lib/valida-unknown-baremetal-gnu/libc.a", 10 | "-C", "link-arg=/valida-toolchain/lib/valida-unknown-baremetal-gnu/libm.a", 11 | "-C", "link-arg=--noinhibit-exec", 12 | ] 13 | 14 | [env] 15 | CC_valida_unknown_baremetal_gnu = "/valida-toolchain/bin/clang" 16 | CFLAGS_valida_unknown_baremetal_gnu = "--sysroot=/valida-toolchain/ -isystem /valida-toolchain/include" 17 | -------------------------------------------------------------------------------- /secp256k1/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 4 4 | 5 | [[package]] 6 | name = "arrayvec" 7 | version = "0.7.6" 8 | source = "registry+https://github.com/rust-lang/crates.io-index" 9 | checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50" 10 | 11 | [[package]] 12 | name = "bincode" 13 | version = "1.3.3" 14 | source = "registry+https://github.com/rust-lang/crates.io-index" 15 | checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad" 16 | dependencies = [ 17 | "serde", 18 | ] 19 | 20 | [[package]] 21 | name = "bitcoin_hashes" 22 | version = "0.14.0" 23 | source = "registry+https://github.com/rust-lang/crates.io-index" 24 | checksum = "bb18c03d0db0247e147a21a6faafd5a7eb851c743db062de72018b6b7e8e4d16" 25 | dependencies = [ 26 | "hex-conservative", 27 | ] 28 | 29 | [[package]] 30 | name = "bitflags" 31 | version = "2.6.0" 32 | source = "registry+https://github.com/rust-lang/crates.io-index" 33 | checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" 34 | 35 | [[package]] 36 | name = "byteorder" 37 | version = "1.5.0" 38 | source = "registry+https://github.com/rust-lang/crates.io-index" 39 | checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" 40 | 41 | [[package]] 42 | name = "cc" 43 | version = "1.2.1" 44 | source = "registry+https://github.com/rust-lang/crates.io-index" 45 | checksum = "fd9de9f2205d5ef3fd67e685b0df337994ddd4495e2a28d185500d0e1edfea47" 46 | dependencies = [ 47 | "shlex", 48 | ] 49 | 50 | [[package]] 51 | name = "cfg-if" 52 | version = "1.0.0" 53 | source = "registry+https://github.com/rust-lang/crates.io-index" 54 | checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" 55 | 56 | [[package]] 57 | name = "errno" 58 | version = "0.3.9" 59 | source = "registry+https://github.com/rust-lang/crates.io-index" 60 | checksum = "534c5cf6194dfab3db3242765c03bbe257cf92f22b38f6bc0c58d59108a820ba" 61 | dependencies = [ 62 | "libc", 63 | "windows-sys 0.52.0", 64 | ] 65 | 66 | [[package]] 67 | name = "fastrand" 68 | version = "2.2.0" 69 | source = "registry+https://github.com/rust-lang/crates.io-index" 70 | checksum = "486f806e73c5707928240ddc295403b1b93c96a02038563881c4a2fd84b81ac4" 71 | 72 | [[package]] 73 | name = "filedescriptor" 74 | version = "0.8.2" 75 | source = "registry+https://github.com/rust-lang/crates.io-index" 76 | checksum = "7199d965852c3bac31f779ef99cbb4537f80e952e2d6aa0ffeb30cce00f4f46e" 77 | dependencies = [ 78 | "libc", 79 | "thiserror", 80 | "winapi", 81 | ] 82 | 83 | [[package]] 84 | name = "gag" 85 | version = "1.0.0" 86 | source = "registry+https://github.com/rust-lang/crates.io-index" 87 | checksum = "a713bee13966e9fbffdf7193af71d54a6b35a0bb34997cd6c9519ebeb5005972" 88 | dependencies = [ 89 | "filedescriptor", 90 | "tempfile", 91 | ] 92 | 93 | [[package]] 94 | name = "getrandom" 95 | version = "0.2.15" 96 | source = "registry+https://github.com/rust-lang/crates.io-index" 97 | checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" 98 | dependencies = [ 99 | "cfg-if", 100 | "libc", 101 | "wasi", 102 | ] 103 | 104 | [[package]] 105 | name = "hex-conservative" 106 | version = "0.2.1" 107 | source = "registry+https://github.com/rust-lang/crates.io-index" 108 | checksum = "5313b072ce3c597065a808dbf612c4c8e8590bdbf8b579508bf7a762c5eae6cd" 109 | dependencies = [ 110 | "arrayvec", 111 | ] 112 | 113 | [[package]] 114 | name = "libc" 115 | version = "0.2.162" 116 | source = "registry+https://github.com/rust-lang/crates.io-index" 117 | checksum = "18d287de67fe55fd7e1581fe933d965a5a9477b38e949cfa9f8574ef01506398" 118 | 119 | [[package]] 120 | name = "linux-raw-sys" 121 | version = "0.4.14" 122 | source = "registry+https://github.com/rust-lang/crates.io-index" 123 | checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89" 124 | 125 | [[package]] 126 | name = "once_cell" 127 | version = "1.20.2" 128 | source = "registry+https://github.com/rust-lang/crates.io-index" 129 | checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" 130 | 131 | [[package]] 132 | name = "ppv-lite86" 133 | version = "0.2.20" 134 | source = "registry+https://github.com/rust-lang/crates.io-index" 135 | checksum = "77957b295656769bb8ad2b6a6b09d897d94f05c41b069aede1fcdaa675eaea04" 136 | dependencies = [ 137 | "zerocopy", 138 | ] 139 | 140 | [[package]] 141 | name = "proc-macro2" 142 | version = "1.0.89" 143 | source = "registry+https://github.com/rust-lang/crates.io-index" 144 | checksum = "f139b0662de085916d1fb67d2b4169d1addddda1919e696f3252b740b629986e" 145 | dependencies = [ 146 | "unicode-ident", 147 | ] 148 | 149 | [[package]] 150 | name = "quote" 151 | version = "1.0.37" 152 | source = "registry+https://github.com/rust-lang/crates.io-index" 153 | checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" 154 | dependencies = [ 155 | "proc-macro2", 156 | ] 157 | 158 | [[package]] 159 | name = "rand" 160 | version = "0.8.5" 161 | source = "registry+https://github.com/rust-lang/crates.io-index" 162 | checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" 163 | dependencies = [ 164 | "libc", 165 | "rand_chacha", 166 | "rand_core", 167 | ] 168 | 169 | [[package]] 170 | name = "rand_chacha" 171 | version = "0.3.1" 172 | source = "registry+https://github.com/rust-lang/crates.io-index" 173 | checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" 174 | dependencies = [ 175 | "ppv-lite86", 176 | "rand_core", 177 | ] 178 | 179 | [[package]] 180 | name = "rand_core" 181 | version = "0.6.4" 182 | source = "registry+https://github.com/rust-lang/crates.io-index" 183 | checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" 184 | dependencies = [ 185 | "getrandom", 186 | ] 187 | 188 | [[package]] 189 | name = "rustix" 190 | version = "0.38.41" 191 | source = "registry+https://github.com/rust-lang/crates.io-index" 192 | checksum = "d7f649912bc1495e167a6edee79151c84b1bad49748cb4f1f1167f459f6224f6" 193 | dependencies = [ 194 | "bitflags", 195 | "errno", 196 | "libc", 197 | "linux-raw-sys", 198 | "windows-sys 0.52.0", 199 | ] 200 | 201 | [[package]] 202 | name = "secp256k1" 203 | version = "0.1.0" 204 | dependencies = [ 205 | "getrandom", 206 | "rand", 207 | "secp256k1 0.29.1", 208 | "valida-rs", 209 | ] 210 | 211 | [[package]] 212 | name = "secp256k1" 213 | version = "0.29.1" 214 | source = "registry+https://github.com/rust-lang/crates.io-index" 215 | checksum = "9465315bc9d4566e1724f0fffcbcc446268cb522e60f9a27bcded6b19c108113" 216 | dependencies = [ 217 | "bitcoin_hashes", 218 | "rand", 219 | "secp256k1-sys", 220 | ] 221 | 222 | [[package]] 223 | name = "secp256k1-sys" 224 | version = "0.10.1" 225 | source = "registry+https://github.com/rust-lang/crates.io-index" 226 | checksum = "d4387882333d3aa8cb20530a17c69a3752e97837832f34f6dccc760e715001d9" 227 | dependencies = [ 228 | "cc", 229 | ] 230 | 231 | [[package]] 232 | name = "serde" 233 | version = "1.0.215" 234 | source = "registry+https://github.com/rust-lang/crates.io-index" 235 | checksum = "6513c1ad0b11a9376da888e3e0baa0077f1aed55c17f50e7b2397136129fb88f" 236 | dependencies = [ 237 | "serde_derive", 238 | ] 239 | 240 | [[package]] 241 | name = "serde_derive" 242 | version = "1.0.215" 243 | source = "registry+https://github.com/rust-lang/crates.io-index" 244 | checksum = "ad1e866f866923f252f05c889987993144fb74e722403468a4ebd70c3cd756c0" 245 | dependencies = [ 246 | "proc-macro2", 247 | "quote", 248 | "syn", 249 | ] 250 | 251 | [[package]] 252 | name = "shlex" 253 | version = "1.3.0" 254 | source = "registry+https://github.com/rust-lang/crates.io-index" 255 | checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" 256 | 257 | [[package]] 258 | name = "syn" 259 | version = "2.0.87" 260 | source = "registry+https://github.com/rust-lang/crates.io-index" 261 | checksum = "25aa4ce346d03a6dcd68dd8b4010bcb74e54e62c90c573f394c46eae99aba32d" 262 | dependencies = [ 263 | "proc-macro2", 264 | "quote", 265 | "unicode-ident", 266 | ] 267 | 268 | [[package]] 269 | name = "tempfile" 270 | version = "3.14.0" 271 | source = "registry+https://github.com/rust-lang/crates.io-index" 272 | checksum = "28cce251fcbc87fac86a866eeb0d6c2d536fc16d06f184bb61aeae11aa4cee0c" 273 | dependencies = [ 274 | "cfg-if", 275 | "fastrand", 276 | "once_cell", 277 | "rustix", 278 | "windows-sys 0.59.0", 279 | ] 280 | 281 | [[package]] 282 | name = "thiserror" 283 | version = "1.0.69" 284 | source = "registry+https://github.com/rust-lang/crates.io-index" 285 | checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52" 286 | dependencies = [ 287 | "thiserror-impl", 288 | ] 289 | 290 | [[package]] 291 | name = "thiserror-impl" 292 | version = "1.0.69" 293 | source = "registry+https://github.com/rust-lang/crates.io-index" 294 | checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1" 295 | dependencies = [ 296 | "proc-macro2", 297 | "quote", 298 | "syn", 299 | ] 300 | 301 | [[package]] 302 | name = "unicode-ident" 303 | version = "1.0.13" 304 | source = "registry+https://github.com/rust-lang/crates.io-index" 305 | checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" 306 | 307 | [[package]] 308 | name = "valida-rs" 309 | version = "0.1.0" 310 | source = "git+https://github.com/lita-xyz/valida-rs.git?branch=main#f780cefbb5299fc1f5e505a27b2c6eeb02b67f2f" 311 | dependencies = [ 312 | "bincode", 313 | "gag", 314 | "getrandom", 315 | "once_cell", 316 | "rand", 317 | "serde", 318 | "tempfile", 319 | ] 320 | 321 | [[package]] 322 | name = "wasi" 323 | version = "0.11.0+wasi-snapshot-preview1" 324 | source = "registry+https://github.com/rust-lang/crates.io-index" 325 | checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" 326 | 327 | [[package]] 328 | name = "winapi" 329 | version = "0.3.9" 330 | source = "registry+https://github.com/rust-lang/crates.io-index" 331 | checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" 332 | dependencies = [ 333 | "winapi-i686-pc-windows-gnu", 334 | "winapi-x86_64-pc-windows-gnu", 335 | ] 336 | 337 | [[package]] 338 | name = "winapi-i686-pc-windows-gnu" 339 | version = "0.4.0" 340 | source = "registry+https://github.com/rust-lang/crates.io-index" 341 | checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" 342 | 343 | [[package]] 344 | name = "winapi-x86_64-pc-windows-gnu" 345 | version = "0.4.0" 346 | source = "registry+https://github.com/rust-lang/crates.io-index" 347 | checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" 348 | 349 | [[package]] 350 | name = "windows-sys" 351 | version = "0.52.0" 352 | source = "registry+https://github.com/rust-lang/crates.io-index" 353 | checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" 354 | dependencies = [ 355 | "windows-targets", 356 | ] 357 | 358 | [[package]] 359 | name = "windows-sys" 360 | version = "0.59.0" 361 | source = "registry+https://github.com/rust-lang/crates.io-index" 362 | checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" 363 | dependencies = [ 364 | "windows-targets", 365 | ] 366 | 367 | [[package]] 368 | name = "windows-targets" 369 | version = "0.52.6" 370 | source = "registry+https://github.com/rust-lang/crates.io-index" 371 | checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" 372 | dependencies = [ 373 | "windows_aarch64_gnullvm", 374 | "windows_aarch64_msvc", 375 | "windows_i686_gnu", 376 | "windows_i686_gnullvm", 377 | "windows_i686_msvc", 378 | "windows_x86_64_gnu", 379 | "windows_x86_64_gnullvm", 380 | "windows_x86_64_msvc", 381 | ] 382 | 383 | [[package]] 384 | name = "windows_aarch64_gnullvm" 385 | version = "0.52.6" 386 | source = "registry+https://github.com/rust-lang/crates.io-index" 387 | checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" 388 | 389 | [[package]] 390 | name = "windows_aarch64_msvc" 391 | version = "0.52.6" 392 | source = "registry+https://github.com/rust-lang/crates.io-index" 393 | checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" 394 | 395 | [[package]] 396 | name = "windows_i686_gnu" 397 | version = "0.52.6" 398 | source = "registry+https://github.com/rust-lang/crates.io-index" 399 | checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" 400 | 401 | [[package]] 402 | name = "windows_i686_gnullvm" 403 | version = "0.52.6" 404 | source = "registry+https://github.com/rust-lang/crates.io-index" 405 | checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" 406 | 407 | [[package]] 408 | name = "windows_i686_msvc" 409 | version = "0.52.6" 410 | source = "registry+https://github.com/rust-lang/crates.io-index" 411 | checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" 412 | 413 | [[package]] 414 | name = "windows_x86_64_gnu" 415 | version = "0.52.6" 416 | source = "registry+https://github.com/rust-lang/crates.io-index" 417 | checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" 418 | 419 | [[package]] 420 | name = "windows_x86_64_gnullvm" 421 | version = "0.52.6" 422 | source = "registry+https://github.com/rust-lang/crates.io-index" 423 | checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" 424 | 425 | [[package]] 426 | name = "windows_x86_64_msvc" 427 | version = "0.52.6" 428 | source = "registry+https://github.com/rust-lang/crates.io-index" 429 | checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" 430 | 431 | [[package]] 432 | name = "zerocopy" 433 | version = "0.7.35" 434 | source = "registry+https://github.com/rust-lang/crates.io-index" 435 | checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" 436 | dependencies = [ 437 | "byteorder", 438 | "zerocopy-derive", 439 | ] 440 | 441 | [[package]] 442 | name = "zerocopy-derive" 443 | version = "0.7.35" 444 | source = "registry+https://github.com/rust-lang/crates.io-index" 445 | checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" 446 | dependencies = [ 447 | "proc-macro2", 448 | "quote", 449 | "syn", 450 | ] 451 | -------------------------------------------------------------------------------- /secp256k1/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "secp256k1" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | [dependencies] 7 | getrandom = "0.2.15" 8 | rand = "0.8.5" 9 | valida-rs = { git = "https://github.com/lita-xyz/valida-rs.git", branch = "main" } 10 | secp256k1 = { version = "0.29.1", features = ["rand-std", "hashes"] } -------------------------------------------------------------------------------- /secp256k1/README.md: -------------------------------------------------------------------------------- 1 | # secp256k1 in Valida 2 | 3 | This is a simple program that verifies a secp256k1 signature in Valida. 4 | 5 | ## System requirements 6 | 7 | This template supports x86-64 Linux. [`rustup`](https://www.rust-lang.org/tools/install) is required. Arch Linux and Ubuntu are specifically supported, with other platforms possibly requiring some tinkering to make work. 8 | 9 | ## Toolchain installation 10 | 11 | To run this project in the Valida VM, you need the Valida toolchain installed. Go to [LLVM Valida releases](https://github.com/lita-xyz/llvm-valida-releases/releases) to find the latest release. Download the release tarball, extract it, `cd` into the extracted folder, and run `sudo ./install.sh`. 12 | 13 | ## Entering the Valida shell 14 | 15 | To put the Valida toolchain on your PATH, you can enter the Valida shell by running `valida-shell` in your shell. The above installation process should have resulted in `valida-shell` being on your `PATH`. 16 | 17 | ## Usage 18 | 19 | Build the project, from the root directory of this repo: 20 | 21 | ``` 22 | cargo +valida build --release 23 | ``` 24 | 25 | To run the program, in the Valida shell, from the root directory of this repo: 26 | 27 | ``` 28 | valida> valida run ./target/valida-unknown-baremetal-gnu/release/secp256k1 log 29 | ``` 30 | 31 | The `run` command runs the program and verifies the secp256k1 signature and prints the result to the console and the file `log` in the current directory. 32 | -------------------------------------------------------------------------------- /secp256k1/src/main.rs: -------------------------------------------------------------------------------- 1 | #![no_main] 2 | 3 | valida_rs::entrypoint!(main); 4 | 5 | use secp256k1::ecdsa::Signature; 6 | use secp256k1::hashes::{sha256, Hash}; 7 | use secp256k1::{Message, PublicKey, Secp256k1}; 8 | 9 | use secp256k1::rand::rngs::OsRng; 10 | 11 | const MSG: &str = "Hello World!"; 12 | 13 | fn print_fresh_keypair_and_signature() { 14 | use std::io::Write; 15 | 16 | let secp = Secp256k1::new(); 17 | let (secret_key, public_key) = secp.generate_keypair(&mut OsRng); 18 | let digest = sha256::Hash::hash(MSG.as_bytes()); 19 | let message = Message::from_digest(digest.to_byte_array()); 20 | 21 | let serialized_public_key = public_key.serialize(); 22 | let signature = secp.sign_ecdsa(&message, &secret_key); 23 | 24 | std::io::stdout().write(&serialized_public_key).unwrap(); // 33 bytes 25 | std::io::stdout().write(secret_key.as_ref()).unwrap(); // 32 bytes 26 | std::io::stdout() 27 | .write(&signature.serialize_compact()) 28 | .unwrap(); // 64 bytes 29 | 30 | // use 31 | // xxd -p -c1 output | sed 's/^/0x/' | paste -sd ',' - 32 | // to dump concatenated keys and signature on host 33 | } 34 | 35 | fn verify_signature_based_on_hardcoded_signature_and_public_key() { 36 | // -- secret key was 37 | // ce e986 baaf a29f ac83 4743 d6ae ea32 38 | // 42e2 7062 3a22 e090 9c3d ee16 3bf0 7016 39 | // e3 40 | 41 | const PUBLIC_KEY_SERIALIZED: [u8; 33] = [ 42 | 0x03, 0xce, 0x00, 0xba, 0xd2, 0xd8, 0xf1, 0x46, 0xe1, 0xd6, 0x70, 0x79, 0xb7, 0xfb, 0x40, 43 | 0xf2, 0xff, 0xe7, 0x2d, 0xe9, 0xcd, 0x15, 0xe0, 0xd2, 0xfb, 0xd0, 0x01, 0xf8, 0xe8, 0x5a, 44 | 0xab, 0xb9, 0xfa, 45 | ]; 46 | 47 | const SIGNATURE_SERIALIZED: [u8; 64] = [ 48 | 0xe7, 0x69, 0x93, 0xac, 0x34, 0x45, 0xa4, 0x95, 0x5f, 0x89, 0x18, 0xf7, 0xec, 0xfb, 0x5c, 49 | 0xd9, 0xf2, 0x4c, 0xc9, 0x51, 0x07, 0xfe, 0x92, 0x21, 0xd4, 0x5c, 0xa6, 0xc9, 0x11, 0x47, 50 | 0x12, 0xd3, 0x3a, 0xe8, 0x8e, 0x2d, 0x8c, 0xc8, 0xfe, 0x44, 0xdd, 0xfb, 0xfd, 0xcc, 0xba, 51 | 0xfe, 0x1a, 0x0f, 0x9b, 0xb0, 0xa9, 0x05, 0x40, 0x35, 0xd7, 0x17, 0x86, 0xd2, 0x5b, 0xcf, 52 | 0x9e, 0xa2, 0x7e, 0x25, 53 | ]; 54 | 55 | let secp = Secp256k1::new(); 56 | 57 | let digest = sha256::Hash::hash(MSG.as_bytes()); 58 | let message = Message::from_digest(digest.to_byte_array()); 59 | 60 | let public_key = PublicKey::from_slice(&PUBLIC_KEY_SERIALIZED).unwrap(); 61 | let sig = Signature::from_compact(&SIGNATURE_SERIALIZED).unwrap(); 62 | 63 | assert!(secp.verify_ecdsa(&message, &sig, &public_key).is_ok()); 64 | } 65 | 66 | pub fn main() { 67 | print_fresh_keypair_and_signature(); 68 | 69 | // just to make sure that verification with harcoded data passes on host 70 | verify_signature_based_on_hardcoded_signature_and_public_key(); 71 | } 72 | -------------------------------------------------------------------------------- /sha256/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [build] 2 | target = "valida-unknown-baremetal-gnu" 3 | 4 | [target.valida-unknown-baremetal-gnu] 5 | linker = "/valida-toolchain/bin/ld.lld" 6 | rustflags = [ 7 | "-C", "link-arg=/valida-toolchain/ValidaEntryPoint.o", 8 | "-C", "link-arg=--script=/valida-toolchain/valida.ld", 9 | "-C", "link-arg=/valida-toolchain/lib/valida-unknown-baremetal-gnu/libc.a", 10 | "-C", "link-arg=/valida-toolchain/lib/valida-unknown-baremetal-gnu/libm.a", 11 | "-C", "link-arg=--noinhibit-exec", 12 | ] 13 | 14 | [env] 15 | CC_valida_unknown_baremetal_gnu = "/valida-toolchain/bin/clang" 16 | CFLAGS_valida_unknown_baremetal_gnu = "--sysroot=/valida-toolchain/ -isystem /valida-toolchain/include" 17 | -------------------------------------------------------------------------------- /sha256/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 4 4 | 5 | [[package]] 6 | name = "block-buffer" 7 | version = "0.10.4" 8 | source = "registry+https://github.com/rust-lang/crates.io-index" 9 | checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" 10 | dependencies = [ 11 | "generic-array", 12 | ] 13 | 14 | [[package]] 15 | name = "cfg-if" 16 | version = "1.0.0" 17 | source = "registry+https://github.com/rust-lang/crates.io-index" 18 | checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" 19 | 20 | [[package]] 21 | name = "cpufeatures" 22 | version = "0.2.16" 23 | source = "registry+https://github.com/rust-lang/crates.io-index" 24 | checksum = "16b80225097f2e5ae4e7179dd2266824648f3e2f49d9134d584b76389d31c4c3" 25 | dependencies = [ 26 | "libc", 27 | ] 28 | 29 | [[package]] 30 | name = "crypto-common" 31 | version = "0.1.6" 32 | source = "registry+https://github.com/rust-lang/crates.io-index" 33 | checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" 34 | dependencies = [ 35 | "generic-array", 36 | "typenum", 37 | ] 38 | 39 | [[package]] 40 | name = "digest" 41 | version = "0.10.7" 42 | source = "registry+https://github.com/rust-lang/crates.io-index" 43 | checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" 44 | dependencies = [ 45 | "block-buffer", 46 | "crypto-common", 47 | ] 48 | 49 | [[package]] 50 | name = "generic-array" 51 | version = "0.14.7" 52 | source = "registry+https://github.com/rust-lang/crates.io-index" 53 | checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" 54 | dependencies = [ 55 | "typenum", 56 | "version_check", 57 | ] 58 | 59 | [[package]] 60 | name = "libc" 61 | version = "0.2.165" 62 | source = "registry+https://github.com/rust-lang/crates.io-index" 63 | checksum = "fcb4d3d38eab6c5239a362fa8bae48c03baf980a6e7079f063942d563ef3533e" 64 | 65 | [[package]] 66 | name = "sha2" 67 | version = "0.10.8" 68 | source = "registry+https://github.com/rust-lang/crates.io-index" 69 | checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8" 70 | dependencies = [ 71 | "cfg-if", 72 | "cpufeatures", 73 | "digest", 74 | ] 75 | 76 | [[package]] 77 | name = "sha256" 78 | version = "0.1.0" 79 | dependencies = [ 80 | "sha2", 81 | "valida-rs", 82 | ] 83 | 84 | [[package]] 85 | name = "typenum" 86 | version = "1.17.0" 87 | source = "registry+https://github.com/rust-lang/crates.io-index" 88 | checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" 89 | 90 | [[package]] 91 | name = "valida-rs" 92 | version = "0.1.0" 93 | source = "git+https://github.com/lita-xyz/valida-rs.git?branch=no-deps#b2216526d65b777df76d4d8267b98c0cc53b42e0" 94 | 95 | [[package]] 96 | name = "version_check" 97 | version = "0.9.5" 98 | source = "registry+https://github.com/rust-lang/crates.io-index" 99 | checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" 100 | -------------------------------------------------------------------------------- /sha256/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "sha256" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | [dependencies] 7 | valida-rs = { git = "https://github.com/lita-xyz/valida-rs.git", branch = "no-deps" } 8 | sha2 = "0.10" -------------------------------------------------------------------------------- /sha256/README.md: -------------------------------------------------------------------------------- 1 | # SHA256 in Valida 2 | 3 | This is a simple program that computes the SHA256 hash of a given input in Valida. 4 | 5 | ## System requirements 6 | 7 | This template supports x86-64 Linux. [`rustup`](https://www.rust-lang.org/tools/install) is required. Arch Linux and Ubuntu are specifically supported, with other platforms possibly requiring some tinkering to make work. 8 | 9 | ## Toolchain installation 10 | 11 | To run this project in the Valida VM, you need the Valida toolchain installed. Go to [LLVM Valida releases](https://github.com/lita-xyz/llvm-valida-releases/releases) to find the latest release. Download the release tarball, extract it, `cd` into the extracted folder, and run `sudo ./install.sh`. 12 | 13 | ## Entering the Valida shell 14 | 15 | To put the Valida toolchain on your PATH, you can enter the Valida shell by running `valida-shell` in your shell. The above installation process should have resulted in `valida-shell` being on your `PATH`. 16 | 17 | ## Usage 18 | 19 | Build the project, from the root directory of this repo: 20 | 21 | ``` 22 | cargo +valida build --release 23 | ``` 24 | 25 | To run the program, in the Valida shell, from the root directory of this repo: 26 | 27 | ``` 28 | valida> valida run ./target/valida-unknown-baremetal-gnu/release/sha256 log 29 | ``` 30 | 31 | The `run` command runs the program and prints the SHA256 hash to the console and the file `log` in the current directory. 32 | -------------------------------------------------------------------------------- /sha256/src/main.rs: -------------------------------------------------------------------------------- 1 | #![no_main] 2 | 3 | use sha2::{Sha256, Digest}; 4 | 5 | #[no_mangle] 6 | pub fn main() { 7 | // Read a 32-byte input 8 | let input = match valida_rs::io::read_line::() { 9 | Ok(w) => w, 10 | Err(e) => { 11 | valida_rs::io::println(&format!("Error reading input: {}", e)); 12 | return; 13 | } 14 | }; 15 | // Create SHA-256 hasher and compute hash 16 | let mut hasher = Sha256::new(); 17 | hasher.update(&input.to_be_bytes()); 18 | let result = hasher.finalize(); 19 | 20 | // Output the hash 21 | println!("{:?}", result); 22 | } 23 | -------------------------------------------------------------------------------- /simple_calculator/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [build] 2 | target = "valida-unknown-baremetal-gnu" 3 | 4 | [target.valida-unknown-baremetal-gnu] 5 | linker = "/valida-toolchain/bin/ld.lld" 6 | rustflags = [ 7 | "-C", "link-arg=/valida-toolchain/ValidaEntryPoint.o", 8 | "-C", "link-arg=--script=/valida-toolchain/valida.ld", 9 | "-C", "link-arg=/valida-toolchain/lib/valida-unknown-baremetal-gnu/libc.a", 10 | "-C", "link-arg=/valida-toolchain/lib/valida-unknown-baremetal-gnu/libm.a", 11 | "-C", "link-arg=--noinhibit-exec", 12 | ] 13 | 14 | [env] 15 | CC_valida_unknown_baremetal_gnu = "/valida-toolchain/bin/clang" 16 | CFLAGS_valida_unknown_baremetal_gnu = "--sysroot=/valida-toolchain/ -isystem /valida-toolchain/include" 17 | -------------------------------------------------------------------------------- /simple_calculator/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 4 4 | 5 | [[package]] 6 | name = "simple_calculator" 7 | version = "0.1.0" 8 | dependencies = [ 9 | "valida-rs", 10 | ] 11 | 12 | [[package]] 13 | name = "valida-rs" 14 | version = "0.1.0" 15 | source = "git+https://github.com/lita-xyz/valida-rs.git?branch=no-deps#b2216526d65b777df76d4d8267b98c0cc53b42e0" 16 | -------------------------------------------------------------------------------- /simple_calculator/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "simple_calculator" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | [dependencies] 7 | valida-rs = { git = "https://github.com/lita-xyz/valida-rs.git", branch = "no-deps" } -------------------------------------------------------------------------------- /simple_calculator/README.md: -------------------------------------------------------------------------------- 1 | # A simple calculator program on Valida 2 | 3 | ## System requirements 4 | 5 | This template supports x86-64 Linux. [`rustup`](https://www.rust-lang.org/tools/install) is required. Arch Linux and Ubuntu are specifically supported, with other platforms possibly requiring some tinkering to make work. 6 | 7 | ## Toolchain installation 8 | 9 | To run this project in the Valida VM, you need the Valida toolchain installed. Go to [LLVM Valida releases](https://github.com/lita-xyz/llvm-valida-releases/releases) to find the latest release. Download the release tarball, extract it, `cd` into the extracted folder, and run `sudo ./install.sh`. 10 | 11 | ## Entering the Valida shell 12 | 13 | To put the Valida toolchain on your PATH, you can enter the Valida shell by running `valida-shell` in your shell. The above installation process should have resulted in `valida-shell` being on your `PATH`. 14 | 15 | ## Usage 16 | 17 | Build the project, from the root directory of this repo: 18 | 19 | ``` 20 | cargo +valida build --release 21 | ``` 22 | 23 | To run the program, in the Valida shell, from the root directory of this repo: 24 | 25 | ``` 26 | valida> valida run ./target/valida-unknown-baremetal-gnu/release/calculator log 27 | ``` 28 | 29 | The `run` command runs the program, and starts the program. The log of the program contains the program history. 30 | -------------------------------------------------------------------------------- /simple_calculator/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly" 3 | components = ["rustc", "rust-src", "rustfmt", "cargo", "clippy", "rust-docs", "rust-analyzer"] 4 | -------------------------------------------------------------------------------- /simple_calculator/src/main.rs: -------------------------------------------------------------------------------- 1 | #![no_main] 2 | 3 | #[no_mangle] 4 | 5 | fn main() { 6 | println!("Welcome to the Simple Calculator!"); 7 | 8 | loop { 9 | println!("Enter the first number:"); 10 | let num1 = valida_rs::io::read_line::().unwrap(); 11 | 12 | println!("Enter the operation (+, -, *, /):"); 13 | let operation = valida_rs::io::read_line::().unwrap(); 14 | 15 | println!("Enter the second number:"); 16 | let num2 = valida_rs::io::read_line::().unwrap(); 17 | 18 | let result = match operation.as_str() { 19 | "+" => num1 + num2, 20 | "-" => num1 - num2, 21 | "*" => num1 * num2, 22 | "/" => { 23 | if num2 != 0.0 { 24 | num1 / num2 25 | } else { 26 | println!("Error: Division by zero!"); 27 | continue; 28 | } 29 | } 30 | _ => { 31 | println!("Error: Invalid operation!"); 32 | continue; 33 | } 34 | }; 35 | 36 | println!("Result: {}", result); 37 | 38 | println!("Do you want to perform another calculation? (y/n)"); 39 | let continue_calc = valida_rs::io::read_line::().unwrap(); 40 | if continue_calc.to_lowercase() != "y" { 41 | break; 42 | } 43 | } 44 | 45 | println!("Thank you for using the Simple Calculator!"); 46 | } 47 | -------------------------------------------------------------------------------- /sudoku/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [build] 2 | target = "valida-unknown-baremetal-gnu" 3 | 4 | [target.valida-unknown-baremetal-gnu] 5 | linker = "/valida-toolchain/bin/ld.lld" 6 | rustflags = [ 7 | "-C", "link-arg=/valida-toolchain/ValidaEntryPoint.o", 8 | "-C", "link-arg=--script=/valida-toolchain/valida.ld", 9 | "-C", "link-arg=/valida-toolchain/lib/valida-unknown-baremetal-gnu/libc.a", 10 | "-C", "link-arg=/valida-toolchain/lib/valida-unknown-baremetal-gnu/libm.a", 11 | "-C", "link-arg=--noinhibit-exec", 12 | ] 13 | 14 | [env] 15 | CC_valida_unknown_baremetal_gnu = "/valida-toolchain/bin/clang" 16 | CFLAGS_valida_unknown_baremetal_gnu = "--sysroot=/valida-toolchain/ -isystem /valida-toolchain/include" 17 | -------------------------------------------------------------------------------- /sudoku/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "sudoku" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | [dependencies] 7 | valida-rs = { git = "https://github.com/lita-xyz/valida-rs.git", branch = "main" } 8 | getrandom = "0.2.15" 9 | -------------------------------------------------------------------------------- /sudoku/README.md: -------------------------------------------------------------------------------- 1 | # Sudoku in Valida 2 | 3 | This is a program that solves a Sudoku puzzle in Valida. 4 | 5 | ## System requirements 6 | 7 | This template supports x86-64 Linux. [`rustup`](https://www.rust-lang.org/tools/install) is required. Arch Linux and Ubuntu are specifically supported, with other platforms possibly requiring some tinkering to make work. 8 | 9 | ## Toolchain installation 10 | 11 | To run this project in the Valida VM, you need the Valida toolchain installed. Go to [LLVM Valida releases](https://github.com/lita-xyz/llvm-valida-releases/releases) to find the latest release. Download the release tarball, extract it, `cd` into the extracted folder, and run `sudo ./install.sh`. 12 | 13 | ## Entering the Valida shell 14 | 15 | To put the Valida toolchain on your PATH, you can enter the Valida shell by running `valida-shell` in your shell. The above installation process should have resulted in `valida-shell` being on your `PATH`. 16 | 17 | ## Usage 18 | 19 | Build the project, from the root directory of this repo: 20 | 21 | ``` 22 | cargo +valida build --release 23 | ``` 24 | 25 | To run the program, in the Valida shell, from the root directory of this repo: 26 | 27 | ``` 28 | valida> valida run ./target/valida-unknown-baremetal-gnu/release/sudoku log 29 | ``` 30 | 31 | The `run` command runs the program and prints the result to the console and the file `log` in the current directory. 32 | -------------------------------------------------------------------------------- /sudoku/src/main.rs: -------------------------------------------------------------------------------- 1 | #![no_main] 2 | 3 | valida_rs::entrypoint!(main); 4 | 5 | struct Sudoku { 6 | r: [[u16; 9]; 324], 7 | c: [[u16; 4]; 729] 8 | } 9 | 10 | impl Sudoku { 11 | pub fn new() -> Sudoku { 12 | let mut s = Sudoku { r: [[0u16; 9]; 324], c: [[0u16; 4]; 729] }; 13 | let mut nr = [0; 324]; 14 | let mut r = 0; 15 | for i in 0..9 { 16 | for j in 0..9 { 17 | for k in 0..9 { 18 | s.c[r][0] = 9 * i + j; 19 | s.c[r][1] = (i/3*3 + j/3) * 9 + k + 81; 20 | s.c[r][2] = 9 * i + k + 162; 21 | s.c[r][3] = 9 * j + k + 243; 22 | r += 1; 23 | } 24 | } 25 | } 26 | for r in 0..729 { 27 | for c2 in 0..4 { 28 | let k = s.c[r][c2] as usize; 29 | s.r[k][nr[k]] = r as u16; 30 | nr[k] += 1; 31 | } 32 | } 33 | return s; 34 | } 35 | #[inline(always)] 36 | fn forward(&self, sr: &mut [i8], sc: &mut [u8], c: u16, min: &mut u8, min_c: &mut u16) { 37 | for rr in self.r[c as usize] { 38 | let srrr = &mut sr[rr as usize]; 39 | *srrr += 1; 40 | if *srrr == 1 { 41 | for cc in self.c[rr as usize] { 42 | let sccc = &mut sc[cc as usize]; 43 | *sccc -= 1; 44 | if *sccc < *min { 45 | *min = *sccc; 46 | *min_c = cc; 47 | } 48 | } 49 | } 50 | } 51 | } 52 | #[inline(always)] 53 | fn revert(&self, sr: &mut [i8], sc: &mut [u8], c: u16) { 54 | for rr in self.r[c as usize] { 55 | let srrr = &mut sr[rr as usize]; 56 | *srrr -= 1; 57 | if *srrr == 0 { 58 | for i in self.c[rr as usize] { 59 | sc[i as usize] += 1; 60 | } 61 | } 62 | } 63 | } 64 | #[inline(always)] 65 | fn update(&self, sr: &mut [i8], sc: &mut [u8], r: u16, v: i32) -> i32 { 66 | let mut min = 10; 67 | let mut min_c = 0; 68 | for i in self.c[r as usize] { 69 | sc[i as usize] = sc[i as usize].wrapping_add((v<<7) as u8); 70 | } 71 | for c in self.c[r as usize] { 72 | if v > 0 { 73 | self.forward(sr, sc, c, &mut min, &mut min_c); 74 | } else { 75 | self.revert(sr, sc, c); 76 | } 77 | } 78 | return (min as i32)<<16 | min_c as i32; 79 | } 80 | pub fn solve(&self, inp: &str) { 81 | let mut sc = [9u8; 324]; 82 | let mut sr = [0i8; 729]; 83 | let mut cr = [-1i8; 81]; 84 | let mut cc = [-1i16; 81]; 85 | let mut s = [-1i8; 81]; 86 | let mut s8 = [48u8; 81]; 87 | let mut hints = 0; 88 | for i in 0..81 { 89 | let c = inp.as_bytes()[i] as i8; 90 | if c >= '1' as i8 && c <= '9' as i8 { 91 | s[i] = c - '1' as i8; 92 | self.update(&mut sr, &mut sc, (i * 9 + s[i] as usize) as u16, 1); 93 | hints += 1; 94 | s8[i] = c as u8; 95 | } 96 | } 97 | let mut i = 0i32; 98 | let mut dir = 1; 99 | let mut cand: i32 = 10<<16|0; 100 | loop { 101 | while i >= 0 && i < 81 - hints { 102 | if dir == 1 { 103 | let mut min = (cand>>16) as u8; 104 | cc[i as usize] = (cand & 0xffff) as i16; 105 | if min > 1 { 106 | for c in 0..324 { 107 | if sc[c] < min { 108 | min = sc[c]; 109 | cc[i as usize] = c as i16; 110 | if min <= 1 { 111 | break; 112 | } 113 | } 114 | } 115 | } 116 | if min == 0 || min == 10 { 117 | cr[i as usize] = -1; 118 | i -= 1; 119 | dir = -1; 120 | } 121 | } 122 | let c = cc[i as usize] as usize; 123 | if dir == -1 && cr[i as usize] >= 0 { 124 | self.update(&mut sr, &mut sc, self.r[c][cr[i as usize] as usize], -1); 125 | } 126 | let mut r2 = (cr[i as usize] + 1) as usize; 127 | while r2 < 9 && sr[self.r[c][r2] as usize] != 0 { 128 | r2 += 1; 129 | } 130 | if r2 < 9 { 131 | cand = self.update(&mut sr, &mut sc, self.r[c][r2], 1); 132 | cr[i as usize] = r2 as i8; 133 | i += 1; 134 | dir = 1; 135 | } else { 136 | cr[i as usize] = -1; 137 | i -= 1; 138 | dir = -1; 139 | } 140 | } 141 | if i < 0 { 142 | break; 143 | } 144 | for j in 0 .. (i as usize) { 145 | let r = self.r[cc[j] as usize][cr[j] as usize]; 146 | s8[(r/9) as usize] = (r%9 + '1' as u16) as u8; 147 | } 148 | valida_rs::io::println(&format!("{}", std::str::from_utf8(&s8).unwrap())); 149 | i -= 1; dir = -1; 150 | } 151 | } 152 | } 153 | 154 | fn main() { 155 | let hard20: [&str; 20] = [ 156 | "..............3.85..1.2.......5.7.....4...1...9.......5......73..2.1........4...9", 157 | ".......12........3..23..4....18....5.6..7.8.......9.....85.....9...4.5..47...6...", 158 | ".2..5.7..4..1....68....3...2....8..3.4..2.5.....6...1...2.9.....9......57.4...9..", 159 | "........3..1..56...9..4..7......9.5.7.......8.5.4.2....8..2..9...35..1..6........", 160 | "12.3....435....1....4........54..2..6...7.........8.9...31..5.......9.7.....6...8", 161 | "1.......2.9.4...5...6...7...5.9.3.......7.......85..4.7.....6...3...9.8...2.....1", 162 | ".......39.....1..5..3.5.8....8.9...6.7...2...1..4.......9.8..5..2....6..4..7.....", 163 | "12.3.....4.....3....3.5......42..5......8...9.6...5.7...15..2......9..6......7..8", 164 | "..3..6.8....1..2......7...4..9..8.6..3..4...1.7.2.....3....5.....5...6..98.....5.", 165 | "1.......9..67...2..8....4......75.3...5..2....6.3......9....8..6...4...1..25...6.", 166 | "..9...4...7.3...2.8...6...71..8....6....1..7.....56...3....5..1.4.....9...2...7..", 167 | "....9..5..1.....3...23..7....45...7.8.....2.......64...9..1.....8..6......54....7", 168 | "4...3.......6..8..........1....5..9..8....6...7.2........1.27..5.3....4.9........", 169 | "7.8...3.....2.1...5.........4.....263...8.......1...9..9.6....4....7.5...........", 170 | "3.7.4...........918........4.....7.....16.......25..........38..9....5...2.6.....", 171 | "........8..3...4...9..2..6.....79.......612...6.5.2.7...8...5...1.....2.4.5.....3", 172 | ".......1.4.........2...........5.4.7..8...3....1.9....3..4..2...5.1........8.6...", 173 | ".......12....35......6...7.7.....3.....4..8..1...........12.....8.....4..5....6..", 174 | "1.......2.9.4...5...6...7...5.3.4.......6........58.4...2...6...3...9.8.7.......1", 175 | ".....1.2.3...4.5.....6....7..2.....1.8..9..3.4.....8..5....2....9..3.4....67....." 176 | ]; 177 | let s = Sudoku::new(); 178 | let n = 1; 179 | for _ in 0..n { 180 | for j in 0..2 { 181 | s.solve(hard20[j]); 182 | valida_rs::io::println(""); 183 | } 184 | } 185 | } 186 | -------------------------------------------------------------------------------- /test_data/conway/input: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lita-xyz/rust-examples/0fbdaae394b493d664e5c2bfae5c72b819c97de6/test_data/conway/input -------------------------------------------------------------------------------- /test_data/ed25519/input: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lita-xyz/rust-examples/0fbdaae394b493d664e5c2bfae5c72b819c97de6/test_data/ed25519/input -------------------------------------------------------------------------------- /test_data/factorial/input: -------------------------------------------------------------------------------- 1 | 11 -------------------------------------------------------------------------------- /test_data/fibonacci/input: -------------------------------------------------------------------------------- 1 | 35 -------------------------------------------------------------------------------- /test_data/fizzbuzz/input: -------------------------------------------------------------------------------- 1 | 257 -------------------------------------------------------------------------------- /test_data/grep/input: -------------------------------------------------------------------------------- 1 | am 2 | I am not a cat, I am a madam -------------------------------------------------------------------------------- /test_data/guessing_game/expected_output: -------------------------------------------------------------------------------- 1 | Guess the number! 2 | Please input your guess between 1 and 100. 3 | You guessed: 49 4 | You win! 5 | -------------------------------------------------------------------------------- /test_data/guessing_game/input: -------------------------------------------------------------------------------- 1 | 49 -------------------------------------------------------------------------------- /test_data/hello_world/input: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lita-xyz/rust-examples/0fbdaae394b493d664e5c2bfae5c72b819c97de6/test_data/hello_world/input -------------------------------------------------------------------------------- /test_data/json_contains/input: -------------------------------------------------------------------------------- 1 | { 2 | "path": "$.store.book[0].title", 3 | "json": { 4 | "store": { 5 | "book": [ 6 | { 7 | "title": "The Lord of the Rings", 8 | "author": "J.R.R. Tolkien", 9 | "price": 22.99 10 | }, 11 | { 12 | "title": "Dune", 13 | "author": "Frank Herbert", 14 | "price": 19.99 15 | } 16 | ] 17 | } 18 | }, 19 | "expected": "The Lord of the Rings" 20 | } -------------------------------------------------------------------------------- /test_data/palindrome/input: -------------------------------------------------------------------------------- 1 | madam -------------------------------------------------------------------------------- /test_data/prime_factorization/input: -------------------------------------------------------------------------------- 1 | 12 2 | 2 3 | 3 4 | 4 5 | -------------------------------------------------------------------------------- /test_data/secp256k1/input: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lita-xyz/rust-examples/0fbdaae394b493d664e5c2bfae5c72b819c97de6/test_data/secp256k1/input -------------------------------------------------------------------------------- /test_data/sha256/input: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lita-xyz/rust-examples/0fbdaae394b493d664e5c2bfae5c72b819c97de6/test_data/sha256/input -------------------------------------------------------------------------------- /test_data/simple_calculator/input: -------------------------------------------------------------------------------- 1 | 11.23 2 | + 3 | 12.34 4 | -------------------------------------------------------------------------------- /test_data/sudoku/input: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lita-xyz/rust-examples/0fbdaae394b493d664e5c2bfae5c72b819c97de6/test_data/sudoku/input -------------------------------------------------------------------------------- /test_data/unit_tests/input: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lita-xyz/rust-examples/0fbdaae394b493d664e5c2bfae5c72b819c97de6/test_data/unit_tests/input -------------------------------------------------------------------------------- /unit_tests/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [build] 2 | target = "valida-unknown-baremetal-gnu" 3 | 4 | [target.valida-unknown-baremetal-gnu] 5 | linker = "/valida-toolchain/bin/ld.lld" 6 | rustflags = [ 7 | "-C", "link-arg=/valida-toolchain/ValidaEntryPoint.o", 8 | "-C", "link-arg=--script=/valida-toolchain/valida.ld", 9 | "-C", "link-arg=/valida-toolchain/lib/valida-unknown-baremetal-gnu/libc.a", 10 | "-C", "link-arg=/valida-toolchain/lib/valida-unknown-baremetal-gnu/libm.a", 11 | "-C", "link-arg=--noinhibit-exec", 12 | ] 13 | 14 | [env] 15 | CC_valida_unknown_baremetal_gnu = "/valida-toolchain/bin/clang" 16 | CFLAGS_valida_unknown_baremetal_gnu = "--sysroot=/valida-toolchain/ -isystem /valida-toolchain/include" 17 | -------------------------------------------------------------------------------- /unit_tests/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 4 4 | 5 | [[package]] 6 | name = "unit_tests" 7 | version = "0.1.0" 8 | dependencies = [ 9 | "valida-rs", 10 | ] 11 | 12 | [[package]] 13 | name = "valida-rs" 14 | version = "0.1.0" 15 | source = "git+https://github.com/lita-xyz/valida-rs.git?branch=no-deps#b2216526d65b777df76d4d8267b98c0cc53b42e0" 16 | -------------------------------------------------------------------------------- /unit_tests/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "unit_tests" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | [dependencies] 7 | valida-rs = { git = "https://github.com/lita-xyz/valida-rs.git", branch = "no-deps" } 8 | -------------------------------------------------------------------------------- /unit_tests/README.md: -------------------------------------------------------------------------------- 1 | # Unit tests in Valida 2 | 3 | This is a program that tests the Valida standard library. It is a template for writing your own unit tests. 4 | 5 | ## System requirements 6 | 7 | This template supports x86-64 Linux. [`rustup`](https://www.rust-lang.org/tools/install) is required. Arch Linux and Ubuntu are specifically supported, with other platforms possibly requiring some tinkering to make work. 8 | 9 | ## Toolchain installation 10 | 11 | To run this project in the Valida VM, you need the Valida toolchain installed. Go to [LLVM Valida releases](https://github.com/lita-xyz/llvm-valida-releases/releases) to find the latest release. Download the release tarball, extract it, `cd` into the extracted folder, and run `sudo ./install.sh`. 12 | 13 | ## Entering the Valida shell 14 | 15 | To put the Valida toolchain on your PATH, you can enter the Valida shell by running `valida-shell` in your shell. The above installation process should have resulted in `valida-shell` being on your `PATH`. 16 | 17 | ## Usage 18 | 19 | Build the project, from the root directory of this repo: 20 | 21 | ``` 22 | cargo +valida build --release 23 | ``` 24 | 25 | To run the program, in the Valida shell, from the root directory of this repo: 26 | 27 | ``` 28 | valida> valida run ./target/valida-unknown-baremetal-gnu/release/unit_tests log 29 | ``` 30 | 31 | The `run` command runs the program and prints the output to the file `log` in the current directory. 32 | -------------------------------------------------------------------------------- /unit_tests/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly" 3 | components = ["rustc", "rust-src", "rustfmt", "cargo", "clippy", "rust-docs", "rust-analyzer"] 4 | -------------------------------------------------------------------------------- /unit_tests/src/atomics.rs: -------------------------------------------------------------------------------- 1 | pub use core::sync::atomic::{ 2 | AtomicBool, AtomicI16, AtomicI32, AtomicI64, AtomicI8, AtomicIsize, AtomicU16, AtomicU32, 3 | AtomicU64, AtomicU8, AtomicUsize, Ordering, 4 | }; 5 | 6 | // Macro for testing basic store/load operations 7 | #[macro_export] 8 | macro_rules! test_store_load { 9 | ($func_name:ident, $atomic_type:ty, $value:expr) => { 10 | fn $func_name() -> bool { 11 | let atomic = <$atomic_type>::new(false as _); 12 | atomic.store($value, Ordering::SeqCst); 13 | atomic.load(Ordering::SeqCst) == $value 14 | } 15 | }; 16 | } 17 | 18 | // Macro for testing fetch_add operations 19 | #[macro_export] 20 | macro_rules! test_fetch_add { 21 | ($func_name:ident, $atomic_type:ty, $initial:expr, $add:expr) => { 22 | fn $func_name() -> bool { 23 | let atomic = <$atomic_type>::new($initial); 24 | let previous = atomic.fetch_add($add, Ordering::SeqCst); 25 | previous == $initial && atomic.load(Ordering::SeqCst) == $initial + $add 26 | } 27 | }; 28 | } 29 | 30 | // Macro for testing fetch_sub operations 31 | #[macro_export] 32 | macro_rules! test_fetch_sub { 33 | ($func_name:ident, $atomic_type:ty, $initial:expr, $sub:expr) => { 34 | fn $func_name() -> bool { 35 | let atomic = <$atomic_type>::new($initial); 36 | let previous = atomic.fetch_sub($sub, Ordering::SeqCst); 37 | previous == $initial && atomic.load(Ordering::SeqCst) == $initial - $sub 38 | } 39 | }; 40 | } 41 | 42 | // Macro for testing fetch_or operations 43 | #[macro_export] 44 | macro_rules! test_fetch_or { 45 | ($func_name:ident, $atomic_type:ty, $initial:expr, $or:expr) => { 46 | fn $func_name() -> bool { 47 | let atomic = <$atomic_type>::new($initial); 48 | let previous = atomic.fetch_or($or, Ordering::SeqCst); 49 | previous == $initial && atomic.load(Ordering::SeqCst) == ($initial | $or) 50 | } 51 | }; 52 | } -------------------------------------------------------------------------------- /unit_tests/src/main.rs: -------------------------------------------------------------------------------- 1 | #![no_main] 2 | #[macro_use] 3 | mod atomics; 4 | use atomics::*; 5 | 6 | #[no_mangle] 7 | pub fn main() { 8 | // Print without newline 9 | print!("Hello"); 10 | 11 | // Print with newline 12 | println!("Hello"); 13 | 14 | // Print errors to stdout. This is tested to work in Valida when the test was added. 15 | // Commented out because they are not printed to stdout in host and the test output fails with the test script. 16 | // eprintln!("Error message"); 17 | // eprint!("Error without newline"); 18 | // Test format specifiers 19 | let value = "test"; 20 | let number = 42; 21 | let float_num = 3.14159; 22 | 23 | println!("{}", value); // Display trait -> "test" 24 | // commented out because the test script fails because this is different in host. 25 | // println!("{:p}", &value); // Pointer address -> "0x7fff..." 26 | println!("{:b}", number); // Binary -> "101010" 27 | println!("{:x}", number); // Hexadecimal (lowercase) -> "2a" 28 | println!("{:X}", number); // Hexadecimal (uppercase) -> "2A" 29 | println!("{:o}", number); // Octal -> "52" 30 | println!("{:e}", float_num); // Scientific notation -> "3.14159e0" 31 | println!("{:.2}", float_num); // Float with 2 decimal places -> "3.14" 32 | println!("{:>10}", value); // Right-align with width 10 -> " test" 33 | println!("{:<10}", value); // Left-align with width 10 -> "test " 34 | println!("{:^10}", value); // Center-align with width 10 -> " test " 35 | println!("{:0>5}", value); // Pad with zeros on left -> "0test" 36 | 37 | // Test arithmetic 38 | let x = 10; 39 | let y = 20; 40 | // Test addition 41 | let z = x + y; 42 | println!("{}", z); 43 | // Test gti 44 | let q = x > 9; 45 | println!("{}", q); 46 | // Test lti 47 | let r = x < 11; 48 | println!("{}", r); 49 | 50 | // Test vector 51 | let mut v = Vec::new(); 52 | v.push(1); 53 | println!("{:?}", v); 54 | // Test panic. Commented out because it is not shown in host. 55 | // panic!("Test that this panic message is shown"); 56 | 57 | // Run all atomics tests 58 | 59 | // Generate test functions for each atomic type 60 | test_store_load!(test_bool_store_load, AtomicBool, true); 61 | test_store_load!(test_i8_store_load, AtomicI8, 8); 62 | test_store_load!(test_i16_store_load, AtomicI16, 16); 63 | test_store_load!(test_i32_store_load, AtomicI32, 32); 64 | test_store_load!(test_i64_store_load, AtomicI64, 64); 65 | test_store_load!(test_isize_store_load, AtomicIsize, 100); 66 | test_store_load!(test_u8_store_load, AtomicU8, 8); 67 | test_store_load!(test_u16_store_load, AtomicU16, 16); 68 | test_store_load!(test_u32_store_load, AtomicU32, 32); 69 | test_store_load!(test_u64_store_load, AtomicU64, 64); 70 | test_store_load!(test_usize_store_load, AtomicUsize, 100); 71 | 72 | // Generate fetch_add test functions 73 | test_fetch_add!(test_i32_fetch_add, AtomicI32, 32, 1); 74 | test_fetch_add!(test_u64_fetch_add, AtomicU64, 64, 1); 75 | test_fetch_add!(test_usize_fetch_add, AtomicUsize, 100, 5); 76 | 77 | // Generate fetch_sub test functions 78 | test_fetch_sub!(test_i32_fetch_sub, AtomicI32, 32, 1); 79 | test_fetch_sub!(test_u64_fetch_sub, AtomicU64, 64, 1); 80 | test_fetch_sub!(test_usize_fetch_sub, AtomicUsize, 100, 5); 81 | 82 | // Generate fetch_or test functions 83 | test_fetch_or!(test_i32_fetch_or, AtomicI32, 0x0F, 0xF0); 84 | test_fetch_or!(test_u64_fetch_or, AtomicU64, 0x0F, 0xF0); 85 | test_fetch_or!(test_usize_fetch_or, AtomicUsize, 0x0F, 0xF0); 86 | 87 | // Run all store/load tests 88 | let store_load_tests = test_bool_store_load() 89 | && test_i8_store_load() 90 | && test_i16_store_load() 91 | && test_i32_store_load() 92 | && test_i64_store_load() 93 | && test_isize_store_load() 94 | && test_u8_store_load() 95 | && test_u16_store_load() 96 | && test_u32_store_load() 97 | && test_u64_store_load() 98 | && test_usize_store_load(); 99 | 100 | // Run all fetch_add tests 101 | let fetch_add_tests = test_i32_fetch_add() && test_u64_fetch_add() && test_usize_fetch_add(); 102 | 103 | // Run all fetch_sub tests 104 | let fetch_sub_tests = test_i32_fetch_sub() && test_u64_fetch_sub() && test_usize_fetch_sub(); 105 | 106 | // Run all fetch_or tests 107 | let fetch_or_tests = test_i32_fetch_or() && test_u64_fetch_or() && test_usize_fetch_or(); 108 | 109 | if store_load_tests && fetch_add_tests && fetch_sub_tests && fetch_or_tests { 110 | println!("All tests passed"); 111 | } else { 112 | println!("Some test failed"); 113 | } 114 | } 115 | --------------------------------------------------------------------------------