diff --git a/DEVELOPERS.md b/DEVELOPERS.md index 7bca10d6..dbbe1d1f 100644 --- a/DEVELOPERS.md +++ b/DEVELOPERS.md @@ -12,6 +12,8 @@ in `rust/` or any subdirectory, and you will be dropped into a BASH shell with t ## Rust +### Build + To build Ares, start a nix development shell as above. Within the shell, in the `rust/ares` directory, you can run: ```bash @@ -22,6 +24,30 @@ to build the Ares executable. This will place the built executable at `target/de Ares is made to run as an urbit "serf", meaning it is intended to be invoked by a "king" which sends it commands and performs side-effects specified by its output. We use the vere king. Special instructions for building the vere king to invoke Ares are forthcoming. +### Test + +The command to run the Ares suite of unit tests is: + +```bash +cargo test --verbose -- --test-threads=1 +``` + +The tests must be run with `-- --test-threads=1` because Rust does not have any way to specify test setup / teardown functions, nor does it have any way to +specify ordered test dependencies. Therefore, the only way to ensure that tests that share resources don't clobber each other **and** that tests setup / teardown in the right order is to force all unit tests to be single-threaded. + +### Style + +Ares uses the default Rust formatting and style. The CI jobs are configured to reject any code which produces linter or style warnings. Therefore, as a final step before uploading code changes to GitHub, it's recommended to run the following commands: + +```bash +cargo fmt +cargo clippy --all-targets --no-deps -- -D warnings -A clippy::missing_safety_doc +``` + +This will auto-format your code and check for linter warnings. + +### Watch + To watch rust and check for errors, run ```bash diff --git a/README.md b/README.md index 51d44317..d0cc2d65 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,11 @@ A redesigned Mars for the Urth/Mars Urbit runtime. Currently WIP. -Read the [proposal](docs/proposal/proposal-nock-performance.md) and [hypotheses](docs/proposal/hypotheses.md) for an overview. +Read the [proposal](docs/proposal/proposal-nock-performance.md) and [hypotheses](docs/proposal/hypotheses.md) for an overview. Before branching or opening a PR please review the [contribution guidelines](CONTRIBUTING.md). -If you want to hack on Ares, read the [developer guide](DEVELOPERS.md). -Before branching or opening a PR please review the [contribution guidelines](CONTRIBUTING.md). +## Installation + +Dependencies: +* [`libaes_siv`](https://github.com/dfoxfranke/libaes_siv) +* [`openssl`](https://github.com/openssl/openssl) +* [`libsecp256k1`](https://github.com/bitcoin-core/secp256k1) diff --git a/rust/ares/Cargo.lock b/rust/ares/Cargo.lock index cfa6b4f7..7bee7f8d 100644 --- a/rust/ares/Cargo.lock +++ b/rust/ares/Cargo.lock @@ -2,6 +2,15 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "aho-corasick" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0" +dependencies = [ + "memchr", +] + [[package]] name = "anes" version = "0.1.6" @@ -14,6 +23,7 @@ version = "0.1.0" dependencies = [ "ares_macros", "assert_no_alloc", + "autotools", "bitvec", "cc", "criterion", @@ -29,6 +39,7 @@ dependencies = [ "num-traits", "signal-hook", "static_assertions", + "urcrypt-sys", ] [[package]] @@ -36,7 +47,7 @@ name = "ares_macros" version = "0.1.0" dependencies = [ "quote", - "syn", + "syn 1.0.109", ] [[package]] @@ -51,7 +62,7 @@ version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" dependencies = [ - "hermit-abi 0.1.19", + "hermit-abi", "libc", "winapi", ] @@ -62,17 +73,55 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" +[[package]] +name = "autotools" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aef8da1805e028a172334c3b680f93e71126f2327622faef2ec3d893c0a4ad77" +dependencies = [ + "cc", +] + +[[package]] +name = "bindgen" +version = "0.68.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "726e4313eb6ec35d2730258ad4e15b547ee75d6afaa1361a922e78e59b7d8078" +dependencies = [ + "bitflags 2.4.1", + "cexpr", + "clang-sys", + "lazy_static", + "lazycell", + "log", + "peeking_take_while", + "prettyplease", + "proc-macro2", + "quote", + "regex", + "rustc-hash", + "shlex", + "syn 2.0.39", + "which", +] + [[package]] name = "bitflags" version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +[[package]] +name = "bitflags" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "327762f6e5a765692301e5bb513e0d9fef63be86bbc14528052b1cd3e6f03e07" + [[package]] name = "bitvec" -version = "1.0.0" +version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1489fcb93a5bb47da0462ca93ad252ad6af2145cce58d10d46a83931ba9f016b" +checksum = "1bc2832c24239b0141d5674bb9174f9d68a8b5b3f2753311927c172ca46f7e9c" dependencies = [ "funty", "radium", @@ -82,9 +131,9 @@ dependencies = [ [[package]] name = "bumpalo" -version = "3.12.0" +version = "3.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0d261e256854913907f67ed06efbc3338dfe6179796deefc1ff763fc1aee5535" +checksum = "7f30e7476521f6f8af1a1c4c0b8cc94f0bee37d91763d0ca2665f299b6cd8aec" [[package]] name = "cast" @@ -94,9 +143,21 @@ checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5" [[package]] name = "cc" -version = "1.0.79" +version = "1.0.83" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0" +dependencies = [ + "libc", +] + +[[package]] +name = "cexpr" +version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f" +checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766" +dependencies = [ + "nom", +] [[package]] name = "cfg-if" @@ -106,9 +167,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "ciborium" -version = "0.2.0" +version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b0c137568cc60b904a7724001b35ce2630fd00d5d84805fbb608ab89509d788f" +checksum = "effd91f6c78e5a4ace8a5d3c0b6bfaec9e2baaef55f3efc00e45fb2e477ee926" dependencies = [ "ciborium-io", "ciborium-ll", @@ -117,27 +178,38 @@ dependencies = [ [[package]] name = "ciborium-io" -version = "0.2.0" +version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "346de753af073cc87b52b2083a506b38ac176a44cfb05497b622e27be899b369" +checksum = "cdf919175532b369853f5d5e20b26b43112613fd6fe7aee757e35f7a44642656" [[package]] name = "ciborium-ll" -version = "0.2.0" +version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "213030a2b5a4e0c0892b6652260cf6ccac84827b83a85a534e178e3906c4cf1b" +checksum = "defaa24ecc093c77630e6c15e17c51f5e187bf35ee514f4e2d67baaa96dae22b" dependencies = [ "ciborium-io", "half", ] +[[package]] +name = "clang-sys" +version = "1.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c688fc74432808e3eb684cae8830a86be1d66a2bd58e1f248ed0960a590baf6f" +dependencies = [ + "glob", + "libc", + "libloading", +] + [[package]] name = "clap" -version = "3.2.23" +version = "3.2.25" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "71655c45cb9845d3270c9d6df84ebe72b4dad3c2ba3f7023ad47c144e4e473a5" +checksum = "4ea181bf566f71cb9a5d17a59e1871af638180a18fb0035c92ae62b705207123" dependencies = [ - "bitflags", + "bitflags 1.3.2", "clap_lex", "indexmap", "textwrap", @@ -188,21 +260,11 @@ dependencies = [ "itertools", ] -[[package]] -name = "crossbeam-channel" -version = "0.5.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c2dd04ddaf88237dc3b8d8f9a3c1004b506b54b3313403944054d23c0870c521" -dependencies = [ - "cfg-if", - "crossbeam-utils", -] - [[package]] name = "crossbeam-deque" -version = "0.8.2" +version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "715e8152b692bba2d374b53d4875445368fdf21a94751410af607a5ac677d1fc" +checksum = "ce6fd6f855243022dcecf8702fef0c297d4338e226845fe067f6341ad9fa0cef" dependencies = [ "cfg-if", "crossbeam-epoch", @@ -211,9 +273,9 @@ dependencies = [ [[package]] name = "crossbeam-epoch" -version = "0.9.13" +version = "0.9.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "01a9af1f4c2ef74bb8aa1f7e19706bc72d03598c8a570bb5de72243c7a9d9d5a" +checksum = "ae211234986c545741a7dc064309f67ee1e5ad243d0e48335adc0484d960bcc7" dependencies = [ "autocfg", "cfg-if", @@ -224,9 +286,9 @@ dependencies = [ [[package]] name = "crossbeam-utils" -version = "0.8.14" +version = "0.8.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4fb766fa798726286dbbb842f174001dab8abc7b627a1dd86e0b7222a95d929f" +checksum = "5a22b2d63d4d1dc0b7f1b6b2747dd0088008a9be28b6ddf0b1e7d335e3037294" dependencies = [ "cfg-if", ] @@ -237,12 +299,28 @@ version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" +[[package]] +name = "errno" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7c18ee0ed65a5f1f81cac6b1d213b69c35fa47d4252ad41f1486dbd8226fe36e" +dependencies = [ + "libc", + "windows-sys", +] + [[package]] name = "funty" version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c" +[[package]] +name = "glob" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" + [[package]] name = "half" version = "1.8.2" @@ -265,12 +343,12 @@ dependencies = [ ] [[package]] -name = "hermit-abi" -version = "0.2.6" +name = "home" +version = "0.5.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ee512640fe35acbfb4bb779db6f0d80704c2cacfa2e39b601ef3e3f47d1ae4c7" +checksum = "5444c27eef6923071f7ebcc33e3444508466a76f7a2b93da00ed6e19f30c1ddb" dependencies = [ - "libc", + "windows-sys", ] [[package]] @@ -285,9 +363,9 @@ dependencies = [ [[package]] name = "indexmap" -version = "1.9.2" +version = "1.9.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1885e79c1fc4b10f0e172c475f458b7f7b93061064d98c3293e98c5ba0c8b399" +checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99" dependencies = [ "autocfg", "hashbrown", @@ -310,15 +388,15 @@ dependencies = [ [[package]] name = "itoa" -version = "1.0.5" +version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fad582f4b9e86b6caa621cabeb0963332d92eea04729ab12892c2533951e6440" +checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38" [[package]] name = "js-sys" -version = "0.3.60" +version = "0.3.65" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49409df3e3bf0856b916e2ceaca09ee28e6871cf7d9ce97a692cacfdb2a25a47" +checksum = "54c0c35952f67de54bb584e9fd912b3023117cbafc0a77d8f3dee1fb5f572fe8" dependencies = [ "wasm-bindgen", ] @@ -335,21 +413,46 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" +[[package]] +name = "lazycell" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55" + [[package]] name = "libc" -version = "0.2.126" +version = "0.2.150" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836" +checksum = "89d92a4743f9a61002fae18374ed11e7973f530cb3a3255fb354818118b2203c" [[package]] -name = "log" -version = "0.4.17" +name = "libloading" +version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" +checksum = "b67380fd3b2fbe7527a606e18729d21c6f3951633d0500574c4dc22d2d638b9f" dependencies = [ "cfg-if", + "winapi", ] +[[package]] +name = "linux-raw-sys" +version = "0.4.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "969488b55f8ac402214f3f5fd243ebb7206cf82de60d3172994707a4bcc2b829" + +[[package]] +name = "log" +version = "0.4.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" + +[[package]] +name = "memchr" +version = "2.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167" + [[package]] name = "memmap" version = "0.7.0" @@ -362,18 +465,34 @@ dependencies = [ [[package]] name = "memoffset" -version = "0.7.1" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5de893c32cde5f383baa4c04c5d6dbdd735cfd4a794b0debdb2bb1b421da5ff4" +checksum = "5a634b1c61a95585bd15607c6ab0c4e5b226e695ff2800ba0cdccddf208c406c" dependencies = [ "autocfg", ] +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + [[package]] name = "murmur3" version = "0.5.2" source = "git+https://github.com/tloncorp/murmur3?rev=7878a0f#7878a0fbecf465720891b625f1a1fc3e19b6a960" +[[package]] +name = "nom" +version = "7.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" +dependencies = [ + "memchr", + "minimal-lexical", +] + [[package]] name = "num-derive" version = "0.3.3" @@ -382,33 +501,23 @@ checksum = "876a53fff98e03a936a674b29568b0e605f06b29372c2489ff4de23f1949743d" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.109", ] [[package]] name = "num-traits" -version = "0.2.16" +version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f30b0abd723be7e2ffca1272140fac1a2f084c77ec3e123c192b66af1ee9e6c2" +checksum = "39e3200413f237f41ab11ad6d161bc7239c84dcb631773ccd7de3dfe4b5c267c" dependencies = [ "autocfg", ] -[[package]] -name = "num_cpus" -version = "1.15.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fac9e2da13b5eb447a6ce3d392f23a29d8694bff781bf03a16cd9ac8697593b" -dependencies = [ - "hermit-abi 0.2.6", - "libc", -] - [[package]] name = "once_cell" -version = "1.17.0" +version = "1.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6f61fba1741ea2b3d6a1e3178721804bb716a68a6aeba1149b5d52e3d464ea66" +checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" [[package]] name = "oorandom" @@ -418,15 +527,27 @@ checksum = "0ab1bc2a289d34bd04a330323ac98a1b4bc82c9d9fcb1e66b63caa84da26b575" [[package]] name = "os_str_bytes" -version = "6.4.1" +version = "6.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b7820b9daea5457c9f21c69448905d723fbd21136ccf521748f23fd49e723ee" +checksum = "e2355d85b9a3786f481747ced0e0ff2ba35213a1f9bd406ed906554d7af805a1" + +[[package]] +name = "peeking_take_while" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099" + +[[package]] +name = "pkg-config" +version = "0.3.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26072860ba924cbfa98ea39c8c19b4dd6a4a25423dbdf219c1eca91aa0cf6964" [[package]] name = "plotters" -version = "0.3.4" +version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2538b639e642295546c50fcd545198c9d64ee2a38620a628724a3b266d5fbf97" +checksum = "d2c224ba00d7cadd4d5c660deaf2098e5e80e07846537c51f9cfa4be50c1fd45" dependencies = [ "num-traits", "plotters-backend", @@ -437,33 +558,43 @@ dependencies = [ [[package]] name = "plotters-backend" -version = "0.3.4" +version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "193228616381fecdc1224c62e96946dfbc73ff4384fba576e052ff8c1bea8142" +checksum = "9e76628b4d3a7581389a35d5b6e2139607ad7c75b17aed325f210aa91f4a9609" [[package]] name = "plotters-svg" -version = "0.3.3" +version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f9a81d2759aae1dae668f783c308bc5c8ebd191ff4184aaa1b37f65a6ae5a56f" +checksum = "38f6d39893cca0701371e3c27294f09797214b86f1fb951b89ade8ec04e2abab" dependencies = [ "plotters-backend", ] +[[package]] +name = "prettyplease" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae005bd773ab59b4725093fd7df83fd7892f7d8eafb48dbd7de6e024e4215f9d" +dependencies = [ + "proc-macro2", + "syn 2.0.39", +] + [[package]] name = "proc-macro2" -version = "1.0.40" +version = "1.0.69" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd96a1e8ed2596c337f8eae5f24924ec83f5ad5ab21ea8e455d3566c69fbcaf7" +checksum = "134c189feb4956b20f6f547d2cf727d4c0fe06722b20a0eec87ed445a97f92da" dependencies = [ "unicode-ident", ] [[package]] name = "quote" -version = "1.0.20" +version = "1.0.33" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3bcdf212e9776fbcb2d23ab029360416bb1706b1aea2d1a5ba002727cbcab804" +checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" dependencies = [ "proc-macro2", ] @@ -491,9 +622,9 @@ checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" [[package]] name = "rayon" -version = "1.6.1" +version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6db3a213adf02b3bcfd2d3846bb41cb22857d131789e01df434fb7e7bc0759b7" +checksum = "9c27db03db7734835b3f53954b534c91069375ce6ccaa2e065441e07d9b6cdb1" dependencies = [ "either", "rayon-core", @@ -501,36 +632,67 @@ dependencies = [ [[package]] name = "rayon-core" -version = "1.10.2" +version = "1.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "356a0625f1954f730c0201cdab48611198dc6ce21f4acff55089b5a78e6e835b" +checksum = "5ce3fb6ad83f861aac485e76e1985cd109d9a3713802152be56c3b1f0e0658ed" dependencies = [ - "crossbeam-channel", "crossbeam-deque", "crossbeam-utils", - "num_cpus", ] [[package]] name = "regex" -version = "1.7.1" +version = "1.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "380b951a9c5e80ddfd6136919eef32310721aa4aacd4889a8d39124b026ab343" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "48aaa5748ba571fb95cd2c85c09f629215d3a6ece942baa100950af03a34f733" +checksum = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f" dependencies = [ + "aho-corasick", + "memchr", "regex-syntax", ] [[package]] name = "regex-syntax" -version = "0.6.28" +version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848" +checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" + +[[package]] +name = "rustc-hash" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" + +[[package]] +name = "rustix" +version = "0.38.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b426b0506e5d50a7d8dafcf2e81471400deb602392c7dd110815afb4eaf02a3" +dependencies = [ + "bitflags 2.4.1", + "errno", + "libc", + "linux-raw-sys", + "windows-sys", +] [[package]] name = "ryu" -version = "1.0.12" +version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b4b9743ed687d4b4bcedf9ff5eaa7398495ae14e61cba0a295704edbc7decde" +checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741" [[package]] name = "same-file" @@ -543,41 +705,47 @@ dependencies = [ [[package]] name = "scopeguard" -version = "1.1.0" +version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" [[package]] name = "serde" -version = "1.0.147" +version = "1.0.192" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d193d69bae983fc11a79df82342761dfbf28a99fc8d203dca4c3c1b590948965" +checksum = "bca2a08484b285dcb282d0f67b26cadc0df8b19f8c12502c13d966bf9482f001" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.147" +version = "1.0.192" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4f1d362ca8fc9c3e3a7484440752472d68a6caa98f1ab81d99b5dfe517cec852" +checksum = "d6c7207fbec9faa48073f3e3074cbe553af6ea512d7c21ba46e434e70ea9fbc1" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.39", ] [[package]] name = "serde_json" -version = "1.0.91" +version = "1.0.108" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "877c235533714907a8c2464236f5c4b2a17262ef1bd71f38f35ea592c8da6883" +checksum = "3d1c7e3eac408d115102c4c24ad393e0821bb3a5df4d506a80f85f7a742a526b" dependencies = [ "itoa", "ryu", "serde", ] +[[package]] +name = "shlex" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7cee0529a6d40f580e7a5e6c495c8fbfe21b7b52795ed4bb5e62cdf92bc6380" + [[package]] name = "signal-hook" version = "0.3.17" @@ -605,9 +773,20 @@ checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" [[package]] name = "syn" -version = "1.0.98" +version = "1.0.109" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c50aef8a904de4c23c788f104b7dddc7d6f79c647c7c8ce4cc8f73eb0ca773dd" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "syn" +version = "2.0.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23e78b90f2fcf45d3e842032ce32e3f2d1545ba6636271dcbf24fa306d87be7a" dependencies = [ "proc-macro2", "quote", @@ -638,26 +817,35 @@ dependencies = [ [[package]] name = "unicode-ident" -version = "1.0.1" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "urcrypt-sys" +version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5bd2fe26506023ed7b5e1e315add59d6f584c621d037f9368fea9cfb988f368c" +checksum = "ced751f95a527a3458eb67c75e4ae7093d41585edaa7565f5769101502473019" +dependencies = [ + "bindgen", + "pkg-config", +] [[package]] name = "walkdir" -version = "2.3.2" +version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "808cf2735cd4b6866113f648b791c6adc5714537bc222d9347bb203386ffda56" +checksum = "d71d857dc86794ca4c280d616f7da00d2dbfd8cd788846559a6813e6aa4b54ee" dependencies = [ "same-file", - "winapi", "winapi-util", ] [[package]] name = "wasm-bindgen" -version = "0.2.83" +version = "0.2.88" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eaf9f5aceeec8be17c128b2e93e031fb8a4d469bb9c4ae2d7dc1888b26887268" +checksum = "7daec296f25a1bae309c0cd5c29c4b260e510e6d813c286b19eaadf409d40fce" dependencies = [ "cfg-if", "wasm-bindgen-macro", @@ -665,24 +853,24 @@ dependencies = [ [[package]] name = "wasm-bindgen-backend" -version = "0.2.83" +version = "0.2.88" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c8ffb332579b0557b52d268b91feab8df3615f265d5270fec2a8c95b17c1142" +checksum = "e397f4664c0e4e428e8313a469aaa58310d302159845980fd23b0f22a847f217" dependencies = [ "bumpalo", "log", "once_cell", "proc-macro2", "quote", - "syn", + "syn 2.0.39", "wasm-bindgen-shared", ] [[package]] name = "wasm-bindgen-macro" -version = "0.2.83" +version = "0.2.88" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "052be0f94026e6cbc75cdefc9bae13fd6052cdcaf532fa6c45e7ae33a1e6c810" +checksum = "5961017b3b08ad5f3fe39f1e79877f8ee7c23c5e5fd5eb80de95abc41f1f16b2" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -690,33 +878,45 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.83" +version = "0.2.88" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07bc0c051dc5f23e307b13285f9d75df86bfdf816c5721e573dec1f9b8aa193c" +checksum = "c5353b8dab669f5e10f5bd76df26a9360c748f054f862ff5f3f8aae0c7fb3907" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.39", "wasm-bindgen-backend", "wasm-bindgen-shared", ] [[package]] name = "wasm-bindgen-shared" -version = "0.2.83" +version = "0.2.88" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1c38c045535d93ec4f0b4defec448e4291638ee608530863b1e2ba115d4fff7f" +checksum = "0d046c5d029ba91a1ed14da14dca44b68bf2f124cfbaf741c54151fdb3e0750b" [[package]] name = "web-sys" -version = "0.3.60" +version = "0.3.65" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bcda906d8be16e728fd5adc5b729afad4e444e106ab28cd1c7256e54fa61510f" +checksum = "5db499c5f66323272151db0e666cd34f78617522fb0c1604d31a27c50c206a85" dependencies = [ "js-sys", "wasm-bindgen", ] +[[package]] +name = "which" +version = "4.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87ba24419a2078cd2b0f2ede2691b6c66d8e47836da3b6db8265ebad47afbfc7" +dependencies = [ + "either", + "home", + "once_cell", + "rustix", +] + [[package]] name = "winapi" version = "0.3.9" @@ -735,9 +935,9 @@ checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" [[package]] name = "winapi-util" -version = "0.1.5" +version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596" dependencies = [ "winapi", ] @@ -748,11 +948,77 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" + +[[package]] +name = "windows_i686_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" + +[[package]] +name = "windows_i686_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" + [[package]] name = "wyz" -version = "0.5.0" +version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "30b31594f29d27036c383b53b59ed3476874d518f0efb151b27a4c275141390e" +checksum = "05f360fc0b24296329c78fda852a1e9ae82de9cf7b27dae4b7f62f118f77b9ed" dependencies = [ "tap", ] diff --git a/rust/ares/Cargo.toml b/rust/ares/Cargo.toml index ea0f7344..27d11997 100644 --- a/rust/ares/Cargo.toml +++ b/rust/ares/Cargo.toml @@ -29,8 +29,10 @@ num-derive = "0.3" num-traits = "0.2" signal-hook = "0.3" static_assertions = "1.1.0" +urcrypt-sys = "0.1.1" [build-dependencies] +autotools = "0.2.6" cc = "1.0.79" [[bin]] diff --git a/rust/ares/src/interpreter.rs b/rust/ares/src/interpreter.rs index 62f88dad..dfc39e14 100644 --- a/rust/ares/src/interpreter.rs +++ b/rust/ares/src/interpreter.rs @@ -587,7 +587,7 @@ pub fn interpret(context: &mut Context, mut subject: Noun, formula: Noun) -> Res Todo9::ComputeResult => { if let Ok(mut formula) = res.slot_atom(kale.axis) { if !cfg!(feature = "sham_hints") { - if let Some(jet) = context.warm.find_jet( + if let Some((jet, _path)) = context.warm.find_jet( &mut context.stack, &mut res, &mut formula, @@ -1540,3 +1540,46 @@ mod hint { newt.slog(stack, 0u64, tank); } } + +mod debug { + use crate::noun::Noun; + use either::Either::*; + + #[allow(dead_code)] + pub fn assert_normalized(noun: Noun, path: Noun) { + assert_normalized_helper(noun, path, None); + } + + #[allow(dead_code)] + pub fn assert_normalized_depth(noun: Noun, path: Noun, depth: usize) { + assert_normalized_helper(noun, path, Some(depth)); + } + + #[allow(dead_code)] + fn assert_normalized_helper(noun: Noun, path: Noun, depth: Option) { + match noun.as_either_atom_cell() { + Left(atom) => { + if !atom.is_normalized() { + if atom.size() == 1 { + panic!( + "Un-normalized indirect_atom (should be direct) returned from jet for {:?}", + path + ); + } else { + panic!( + "Un-normalized indirect_atom (last word 0) returned from jet for {:?}", + path + ); + } + } + } + Right(cell) => { + if !depth.is_some_and(|d| d == 0) { + let new_depth = depth.map(|x| x - 1); + assert_normalized_helper(cell.head(), path, new_depth); + assert_normalized_helper(cell.tail(), path, new_depth); + } + } + } + } +} diff --git a/rust/ares/src/jets.rs b/rust/ares/src/jets.rs index 8c312921..f2bc3190 100644 --- a/rust/ares/src/jets.rs +++ b/rust/ares/src/jets.rs @@ -6,6 +6,7 @@ pub mod bits; pub mod form; pub mod hash; pub mod list; +pub mod lock; pub mod lute; pub mod math; pub mod nock; @@ -26,6 +27,9 @@ use crate::jets::nock::*; use crate::jets::serial::*; use crate::jets::sort::*; +use crate::jets::lock::aes::*; +use crate::jets::lock::ed::*; +use crate::jets::lock::sha::*; use crate::jets::tree::*; use crate::jets::warm::Warm; use crate::mem::NockStack; @@ -121,9 +125,28 @@ pub fn get_jet(jet_name: Noun) -> Option { tas!(b"cue") => Some(jet_cue), tas!(b"jam") => Some(jet_jam), // + tas!(b"shas") => Some(jet_shas), + tas!(b"shax") => Some(jet_shax), + tas!(b"shay") => Some(jet_shay), + tas!(b"shal") => Some(jet_shal), + tas!(b"sha1") => Some(jet_sha1), + // tas!(b"scow") => Some(jet_scow), // tas!(b"mink") => Some(jet_mink), + // + tas!(b"puck") => Some(jet_puck), + tas!(b"shar") => Some(jet_shar), + tas!(b"sign") => Some(jet_sign), + tas!(b"veri") => Some(jet_veri), + // + tas!(b"siva_en") => Some(jet_siva_en), + tas!(b"siva_de") => Some(jet_siva_de), + tas!(b"sivb_en") => Some(jet_sivb_en), + tas!(b"sivb_de") => Some(jet_sivb_de), + tas!(b"sivc_en") => Some(jet_sivc_en), + tas!(b"sivc_de") => Some(jet_sivc_de), + // _ => { // eprintln!("Unknown jet: {:?}", jet_name); None diff --git a/rust/ares/src/jets/hot.rs b/rust/ares/src/jets/hot.rs index bc2d564a..379aacdb 100644 --- a/rust/ares/src/jets/hot.rs +++ b/rust/ares/src/jets/hot.rs @@ -208,6 +208,67 @@ const TRUE_HOT_STATE: &[(&[Either<&[u8], (u64, u64)>], u64, Jet)] = &[ jet_jam, ), // + ( + &[ + K_139, + Left(b"one"), + Left(b"two"), + Left(b"tri"), + Left(b"shas"), + ], + 1, + jet_shas, + ), + // + ( + &[ + K_139, + Left(b"one"), + Left(b"two"), + Left(b"tri"), + Left(b"shax"), + ], + 1, + jet_shax, + ), + // + ( + &[ + K_139, + Left(b"one"), + Left(b"two"), + Left(b"tri"), + Left(b"shay"), + ], + 1, + jet_shay, + ), + // + ( + &[ + K_139, + Left(b"one"), + Left(b"two"), + Left(b"tri"), + Left(b"shal"), + ], + 1, + jet_shal, + ), + // + ( + &[ + K_139, + Left(b"one"), + Left(b"two"), + Left(b"tri"), + Left(b"sha"), + Left(b"sha1"), + ], + 1, + jet_sha1, + ), + // ( &[ K_139, @@ -358,6 +419,176 @@ const TRUE_HOT_STATE: &[(&[Either<&[u8], (u64, u64)>], u64, Jet)] = &[ 1, jet_ut_rest, ), + // + ( + &[ + K_139, + Left(b"one"), + Left(b"two"), + Left(b"tri"), + Left(b"qua"), + Left(b"pen"), + Left(b"hex"), + Left(b"coed"), + Left(b"ed"), + Left(b"puck"), + ], + 1, + jet_puck, + ), + // + ( + &[ + K_139, + Left(b"one"), + Left(b"two"), + Left(b"tri"), + Left(b"qua"), + Left(b"pen"), + Left(b"hex"), + Left(b"coed"), + Left(b"ed"), + Left(b"shar"), + ], + 1, + jet_shar, + ), + // + ( + &[ + K_139, + Left(b"one"), + Left(b"two"), + Left(b"tri"), + Left(b"qua"), + Left(b"pen"), + Left(b"hex"), + Left(b"coed"), + Left(b"ed"), + Left(b"sign"), + ], + 1, + jet_sign, + ), + // + ( + &[ + K_139, + Left(b"one"), + Left(b"two"), + Left(b"tri"), + Left(b"qua"), + Left(b"pen"), + Left(b"hex"), + Left(b"coed"), + Left(b"ed"), + Left(b"veri"), + ], + 1, + jet_veri, + ), + // + ( + &[ + K_139, + Left(b"one"), + Left(b"two"), + Left(b"tri"), + Left(b"qua"), + Left(b"pen"), + Left(b"hex"), + Left(b"aes"), + Left(b"siva"), + Left(b"en"), + ], + 1, + jet_siva_en, + ), + // + ( + &[ + K_139, + Left(b"one"), + Left(b"two"), + Left(b"tri"), + Left(b"qua"), + Left(b"pen"), + Left(b"hex"), + Left(b"aes"), + Left(b"siva"), + Left(b"de"), + ], + 1, + jet_siva_de, + ), + // + ( + &[ + K_139, + Left(b"one"), + Left(b"two"), + Left(b"tri"), + Left(b"qua"), + Left(b"pen"), + Left(b"hex"), + Left(b"aes"), + Left(b"sivb"), + Left(b"en"), + ], + 1, + jet_sivb_en, + ), + // + ( + &[ + K_139, + Left(b"one"), + Left(b"two"), + Left(b"tri"), + Left(b"qua"), + Left(b"pen"), + Left(b"hex"), + Left(b"aes"), + Left(b"sivb"), + Left(b"de"), + ], + 1, + jet_sivb_de, + ), + // + ( + &[ + K_139, + Left(b"one"), + Left(b"two"), + Left(b"tri"), + Left(b"qua"), + Left(b"pen"), + Left(b"hex"), + Left(b"aes"), + Left(b"sivc"), + Left(b"en"), + ], + 1, + jet_sivc_en, + ), + // + ( + &[ + K_139, + Left(b"one"), + Left(b"two"), + Left(b"tri"), + Left(b"qua"), + Left(b"pen"), + Left(b"hex"), + Left(b"aes"), + Left(b"sivc"), + Left(b"de"), + ], + 1, + jet_sivc_de, + ), ]; #[derive(Copy, Clone)] diff --git a/rust/ares/src/jets/lock.rs b/rust/ares/src/jets/lock.rs new file mode 100644 index 00000000..1604899d --- /dev/null +++ b/rust/ares/src/jets/lock.rs @@ -0,0 +1,3 @@ +pub mod aes; +pub mod ed; +pub mod sha; diff --git a/rust/ares/src/jets/lock/aes.rs b/rust/ares/src/jets/lock/aes.rs new file mode 100644 index 00000000..32ea4a5c --- /dev/null +++ b/rust/ares/src/jets/lock/aes.rs @@ -0,0 +1,516 @@ +use crate::interpreter::Context; +use crate::jets::bits::util::met; +use crate::jets::util::slot; +use crate::jets::{JetErr, Result}; +use crate::noun::{IndirectAtom, Noun}; +use urcrypt_sys::*; + +crate::gdb!(); + +// Note: The Hoon code for these functions doesn't explicitly check key +// sizes. However, the implementations of these functions in urcrypt +// have fixed maximum key sizes, therefore we must punt if the key is +// too large. + +pub fn jet_siva_en(context: &mut Context, subject: Noun) -> Result { + let stack = &mut context.stack; + let txt = slot(subject, 6)?.as_atom()?; + let key = slot(subject, 60)?.as_atom()?; + let atoms = slot(subject, 61)?; + + if met(3, key) > 32 { + Err(JetErr::Punt) + } else { + unsafe { + let (mut _key_ida, key_bytes) = IndirectAtom::new_raw_mut_bytes(stack, 32); + key_bytes[0..key.as_bytes().len()].copy_from_slice(key.as_bytes()); + + util::_siv_en(stack, key_bytes, atoms, txt, urcrypt_aes_siva_en) + } + } +} + +pub fn jet_siva_de(context: &mut Context, subject: Noun) -> Result { + let stack = &mut context.stack; + let iv = slot(subject, 12)?.as_atom()?; + let len = slot(subject, 26)?.as_atom()?; + let txt = slot(subject, 27)?.as_atom()?; + let key = slot(subject, 60)?.as_atom()?; + let ads = slot(subject, 61)?; + + if met(3, key) > 32 { + Err(JetErr::Punt) + } else { + unsafe { + let (mut _key_ida, key_bytes) = IndirectAtom::new_raw_mut_bytes(stack, 32); + key_bytes[0..key.as_bytes().len()].copy_from_slice(key.as_bytes()); + + util::_siv_de(stack, key_bytes, ads, iv, len, txt, urcrypt_aes_siva_de) + } + } +} + +pub fn jet_sivb_en(context: &mut Context, subject: Noun) -> Result { + let stack = &mut context.stack; + let txt = slot(subject, 6)?.as_atom()?; + let key = slot(subject, 60)?.as_atom()?; + let atoms = slot(subject, 61)?; + + if met(3, key) > 48 { + Err(JetErr::Punt) + } else { + unsafe { + let (mut _key_ida, key_bytes) = IndirectAtom::new_raw_mut_bytes(stack, 48); + key_bytes[0..key.as_bytes().len()].copy_from_slice(key.as_bytes()); + + util::_siv_en(stack, key_bytes, atoms, txt, urcrypt_aes_sivb_en) + } + } +} + +pub fn jet_sivb_de(context: &mut Context, subject: Noun) -> Result { + let stack = &mut context.stack; + let iv = slot(subject, 12)?.as_atom()?; + let len = slot(subject, 26)?.as_atom()?; + let txt = slot(subject, 27)?.as_atom()?; + let key = slot(subject, 60)?.as_atom()?; + let ads = slot(subject, 61)?; + + if met(3, key) > 48 { + Err(JetErr::Punt) + } else { + unsafe { + let (mut _key_ida, key_bytes) = IndirectAtom::new_raw_mut_bytes(stack, 48); + key_bytes[0..key.as_bytes().len()].copy_from_slice(key.as_bytes()); + + util::_siv_de(stack, key_bytes, ads, iv, len, txt, urcrypt_aes_sivb_de) + } + } +} + +pub fn jet_sivc_en(context: &mut Context, subject: Noun) -> Result { + let stack = &mut context.stack; + let txt = slot(subject, 6)?.as_atom()?; + let key = slot(subject, 60)?.as_atom()?; + let atoms = slot(subject, 61)?; + + if met(3, key) > 64 { + Err(JetErr::Punt) + } else { + unsafe { + let (mut _key_ida, key_bytes) = IndirectAtom::new_raw_mut_bytes(stack, 64); + key_bytes[0..key.as_bytes().len()].copy_from_slice(key.as_bytes()); + + util::_siv_en(stack, key_bytes, atoms, txt, urcrypt_aes_sivc_en) + } + } +} + +pub fn jet_sivc_de(context: &mut Context, subject: Noun) -> Result { + let stack = &mut context.stack; + let iv = slot(subject, 12)?.as_atom()?; + let len = slot(subject, 26)?.as_atom()?; + let txt = slot(subject, 27)?.as_atom()?; + let key = slot(subject, 60)?.as_atom()?; + let ads = slot(subject, 61)?; + + if met(3, key) > 64 { + Err(JetErr::Punt) + } else { + unsafe { + let (mut _key_ida, key_bytes) = IndirectAtom::new_raw_mut_bytes(stack, 64); + key_bytes[0..key.as_bytes().len()].copy_from_slice(key.as_bytes()); + + util::_siv_de(stack, key_bytes, ads, iv, len, txt, urcrypt_aes_sivc_de) + } + } +} + +mod util { + use crate::interpreter::Error; + use crate::jets::bits::util::met; + use crate::jets::list; + use crate::jets::{JetErr, Result}; + use crate::mem::NockStack; + use crate::noun::{Atom, IndirectAtom, Noun, D, T}; + use std::ptr::null_mut; + use std::result; + use urcrypt_sys::urcrypt_aes_siv_data; + + type UrcryptSiv = unsafe extern "C" fn( + *mut u8, + usize, + *mut urcrypt_aes_siv_data, + usize, + *mut u8, + *mut u8, + *mut u8, + ) -> i32; + + /// Encodes the list of atoms. + /// + /// # Arguments + /// + /// * `stack` - the active NockStack + /// * `atoms` - the list of atoms to allocate + /// + fn _allocate_atoms( + stack: &mut NockStack, + mut atoms: Noun, + ) -> result::Result<&'static mut [urcrypt_aes_siv_data], JetErr> { + if unsafe { atoms.raw_equals(D(0)) } { + return Ok(&mut []); + } + + // measure + let length = list::util::lent(atoms)?; + + // allocate + let siv_data: &mut [urcrypt_aes_siv_data] = unsafe { + let ptr = stack.struct_alloc::(length); + std::slice::from_raw_parts_mut(ptr, length) + }; + + // encode + unsafe { + for item in siv_data.iter_mut().take(length) { + let cell = atoms.as_cell()?; + let head = cell.head().as_atom()?; + let bytes = head.as_bytes(); + let len = met(3, head); + + let (mut atom, buffer) = IndirectAtom::new_raw_mut_bytes(stack, bytes.len()); + buffer[0..len].copy_from_slice(&(bytes[0..len])); + + item.length = bytes.len(); + item.bytes = atom.data_pointer_mut() as *mut u8; + + atoms = cell.tail(); + } + } + + Ok(siv_data) + } + + pub fn _siv_en( + stack: &mut NockStack, + key: &mut [u8], + ads: Noun, + txt: Atom, + fun: UrcryptSiv, + ) -> Result { + unsafe { + let siv_data = _allocate_atoms(stack, ads)?; + + let txt_len = met(3, txt); + let txt_ptr = match txt_len { + 0 => null_mut::(), + _ => { + let (_, txt_bytes) = IndirectAtom::new_raw_mut_bytes(stack, txt_len); + txt_bytes[0..txt_len].copy_from_slice(&(txt.as_bytes()[0..txt_len])); + txt_bytes.as_mut_ptr() + } + }; + + let (mut iv, iv_bytes) = IndirectAtom::new_raw_mut_bytes(stack, 16); + + match txt_len { + 0 => { + fun( + txt_ptr, + txt_len, + siv_data.as_mut_ptr(), + siv_data.len(), + key.as_mut_ptr(), + iv_bytes.as_mut_ptr(), + null_mut::(), + ); + Ok(T(stack, &[iv.normalize_as_atom().as_noun(), D(0), D(0)])) + } + _ => { + let (mut out_atom, out_bytes) = IndirectAtom::new_raw_mut_bytes(stack, txt_len); + fun( + txt_ptr, + txt_len, + siv_data.as_mut_ptr(), + siv_data.len(), + key.as_mut_ptr(), + iv_bytes.as_mut_ptr(), + out_bytes.as_mut_ptr(), + ); + Ok(T( + stack, + &[ + iv.normalize_as_atom().as_noun(), + D(txt_len as u64), + out_atom.normalize_as_atom().as_noun(), + ], + )) + } + } + } + } + + pub fn _siv_de( + stack: &mut NockStack, + key: &mut [u8], + ads: Noun, + iv: Atom, + len: Atom, + txt: Atom, + fun: UrcryptSiv, + ) -> Result { + unsafe { + let txt_len = match len.as_direct() { + Ok(direct) => direct.data() as usize, + Err(_) => return Err(JetErr::Fail(Error::NonDeterministic(D(0)))), + }; + let (_, txt_bytes) = IndirectAtom::new_raw_mut_bytes(stack, txt_len); + txt_bytes[0..txt_len].copy_from_slice(&(txt.as_bytes()[0..txt_len])); + + let (_iv_ida, iv_bytes) = IndirectAtom::new_raw_mut_bytes(stack, 16); + iv_bytes[0..16].copy_from_slice(&(iv.as_bytes()[0..16])); + + let siv_data = _allocate_atoms(stack, ads)?; + + let (mut out_atom, out_bytes) = IndirectAtom::new_raw_mut_bytes(stack, txt_len); + fun( + if txt_len == 0 { + null_mut::() + } else { + txt_bytes.as_mut_ptr() + }, + txt_len, + siv_data.as_mut_ptr(), + siv_data.len(), + key.as_mut_ptr(), + iv_bytes.as_mut_ptr(), + out_bytes.as_mut_ptr(), + ); + Ok(T(stack, &[D(0), out_atom.normalize_as_atom().as_noun()])) + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::jets::util::test::{assert_noun_eq, init_context, A}; + use crate::jets::Jet; + use crate::mem::NockStack; + use crate::noun::{Cell, D, T}; + use ibig::ubig; + + pub fn assert_jet_in_door( + c: &mut Context, + jet: Jet, + sam: &[fn(&mut NockStack) -> Noun], // regular sample + ctx: &[fn(&mut NockStack) -> Noun], // door sample as context + res: Noun, + ) { + let sam: Vec = sam.iter().map(|f| f(&mut c.stack)).collect(); + let ctx: Vec = ctx.iter().map(|f| f(&mut c.stack)).collect(); + let sam = if sam.len() > 1 { + T(&mut c.stack, &sam) + } else { + sam[0] + }; + let ctx = if ctx.len() > 1 { + T(&mut c.stack, &ctx) + } else { + ctx[0] + }; + let pay = Cell::new(&mut c.stack, sam, ctx).as_noun(); + let sbj = Cell::new(&mut c.stack, D(0), pay).as_noun(); + // std::io::stderr().flush().unwrap(); + let jet_res = jet(c, sbj).unwrap(); + // std::io::stderr().flush().unwrap(); + assert_noun_eq(&mut c.stack, jet_res, res); + } + + #[test] + pub fn test_siva_en() { + let c = &mut init_context(); + /* + > (~(en siva:aes:crypto [key=0x0 vec=~]) txt=0x0) + [p=0xb0f7.a0df.be76.c85b.5e29.bb31.aaec.fc77 q=0 r=0x0] + */ + fn sample(_s: &mut NockStack) -> Noun { + D(0) + } + fn context(s: &mut NockStack) -> Noun { + let sample = T(s, &[D(0), D(0)]); + T(s, &[D(0), sample, D(0)]) + } + + let siv = A(&mut c.stack, &ubig!(0xb0f7a0dfbe76c85b5e29bb31aaecfc77)); + let res = T(&mut c.stack, &[siv, D(0), D(0x0)]); + assert_jet_in_door(c, jet_siva_en, &[sample], &[context], res); + + /* RFC 5297 + * https://datatracker.ietf.org/doc/html/rfc5297#appendix-A + */ + fn gate_sample(s: &mut NockStack) -> Noun { + A(s, &ubig!(0x112233445566778899aabbccddee)) + } + fn gate_context(s: &mut NockStack) -> Noun { + let key = A( + s, + &ubig!(_0xfffefdfcfbfaf9f8f7f6f5f4f3f2f1f0f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff), + ); + let a = A( + s, + &ubig!(_0x101112131415161718191a1b1c1d1e1f2021222324252627), + ); + let vec = T(s, &[a, D(0)]); + let sample = T(s, &[key, vec]); + T(s, &[D(0), sample, D(0)]) + } + let iv = A(&mut c.stack, &ubig!(0x85632d07c6e8f37f950acd320a2ecc93)); + let len = D(14); + let cyp = A(&mut c.stack, &ubig!(0x40c02b9690c4dc04daef7f6afe5c)); + let res = T(&mut c.stack, &[iv, len, cyp]); + assert_jet_in_door(c, jet_siva_en, &[gate_sample], &[gate_context], res); + } + + #[test] + pub fn test_sivb_en() { + let c = &mut init_context(); + + /* RFC 5297 + * https://datatracker.ietf.org/doc/html/rfc5297#appendix-A + */ + fn gate_sample(s: &mut NockStack) -> Noun { + A(s, &ubig!(0x112233445566778899aabbccddee)) + } + fn gate_context(s: &mut NockStack) -> Noun { + let key = A(s, &ubig!(_0xfffefdfcfbfaf9f8f7f6f5f4f3f2f1f0f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff00112233445566778899aabbccddeeff)); + let a = A( + s, + &ubig!(_0x101112131415161718191a1b1c1d1e1f2021222324252627), + ); + let vec = T(s, &[a, D(0)]); + let sample = T(s, &[key, vec]); + T(s, &[D(0), sample, D(0)]) + } + let iv = A(&mut c.stack, &ubig!(0x89e869b93256785154f0963962fe0740)); + let len = D(14); + let cyp = A(&mut c.stack, &ubig!(0xf313e667b56478a032b9913e923c)); + let res = T(&mut c.stack, &[iv, len, cyp]); + assert_jet_in_door(c, jet_sivb_en, &[gate_sample], &[gate_context], res); + } + + #[test] + pub fn test_sivc_en() { + let c = &mut init_context(); + + /* RFC 5297 + * https://datatracker.ietf.org/doc/html/rfc5297#appendix-A + */ + fn gate_sample(s: &mut NockStack) -> Noun { + A(s, &ubig!(0x112233445566778899aabbccddee)) + } + fn gate_context(s: &mut NockStack) -> Noun { + let key = A(s, &ubig!(_0xfffefdfcfbfaf9f8f7f6f5f4f3f2f1f0f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff)); + let a = A( + s, + &ubig!(_0x101112131415161718191a1b1c1d1e1f2021222324252627), + ); + let vec = T(s, &[a, D(0)]); + let sample = T(s, &[key, vec]); + T(s, &[D(0), sample, D(0)]) + } + let iv = A(&mut c.stack, &ubig!(0x724dfb2eaf94dbb19b0ba3a299a0801e)); + let len = D(14); + let cyp = A(&mut c.stack, &ubig!(0x1206291a35ad3db0212773440fd0)); + let res = T(&mut c.stack, &[iv, len, cyp]); + assert_jet_in_door(c, jet_sivc_en, &[gate_sample], &[gate_context], res); + } + + #[test] + pub fn test_siva_de() { + let c = &mut init_context(); + + /* RFC 5297 + * https://datatracker.ietf.org/doc/html/rfc5297#appendix-A + */ + fn gate_sample(s: &mut NockStack) -> Noun { + let iv = A(s, &ubig!(0x85632d07c6e8f37f950acd320a2ecc93)); + let len = D(14); + let cyp = A(s, &ubig!(0x40c02b9690c4dc04daef7f6afe5c)); + T(s, &[iv, len, cyp]) + } + fn gate_context(s: &mut NockStack) -> Noun { + let key = A( + s, + &ubig!(_0xfffefdfcfbfaf9f8f7f6f5f4f3f2f1f0f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff), + ); + let a = A( + s, + &ubig!(_0x101112131415161718191a1b1c1d1e1f2021222324252627), + ); + let vec = T(s, &[a, D(0)]); + let sample = T(s, &[key, vec]); + T(s, &[D(0), sample, D(0)]) + } + let txt = A(&mut c.stack, &ubig!(0x112233445566778899aabbccddee)); + let res = T(&mut c.stack, &[D(0), txt]); + assert_jet_in_door(c, jet_siva_de, &[gate_sample], &[gate_context], res); + } + + #[test] + pub fn test_sivb_de() { + let c = &mut init_context(); + + /* RFC 5297 + * https://datatracker.ietf.org/doc/html/rfc5297#appendix-A + */ + fn gate_sample(s: &mut NockStack) -> Noun { + let iv = A(s, &ubig!(0x89e869b93256785154f0963962fe0740)); + let len = D(14); + let cyp = A(s, &ubig!(0xf313e667b56478a032b9913e923c)); + T(s, &[iv, len, cyp]) + } + fn gate_context(s: &mut NockStack) -> Noun { + let key = A(s, &ubig!(_0xfffefdfcfbfaf9f8f7f6f5f4f3f2f1f0f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff00112233445566778899aabbccddeeff)); + let a = A( + s, + &ubig!(_0x101112131415161718191a1b1c1d1e1f2021222324252627), + ); + let vec = T(s, &[a, D(0)]); + let sample = T(s, &[key, vec]); + T(s, &[D(0), sample, D(0)]) + } + let txt = A(&mut c.stack, &ubig!(0x112233445566778899aabbccddee)); + let res = T(&mut c.stack, &[D(0), txt]); + assert_jet_in_door(c, jet_sivb_de, &[gate_sample], &[gate_context], res); + } + + #[test] + pub fn test_sivc_de() { + let c = &mut init_context(); + + /* RFC 5297 + * https://datatracker.ietf.org/doc/html/rfc5297#appendix-A + */ + fn gate_sample(s: &mut NockStack) -> Noun { + let iv = A(s, &ubig!(0x724dfb2eaf94dbb19b0ba3a299a0801e)); + let len = D(14); + let cyp = A(s, &ubig!(0x1206291a35ad3db0212773440fd0)); + T(s, &[iv, len, cyp]) + } + fn gate_context(s: &mut NockStack) -> Noun { + let key = A(s, &ubig!(_0xfffefdfcfbfaf9f8f7f6f5f4f3f2f1f0f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff)); + let a = A( + s, + &ubig!(_0x101112131415161718191a1b1c1d1e1f2021222324252627), + ); + let vec = T(s, &[a, D(0)]); + let sample = T(s, &[key, vec]); + T(s, &[D(0), sample, D(0)]) + } + let txt = A(&mut c.stack, &ubig!(0x112233445566778899aabbccddee)); + let res = T(&mut c.stack, &[D(0), txt]); + assert_jet_in_door(c, jet_sivc_de, &[gate_sample], &[gate_context], res); + } +} diff --git a/rust/ares/src/jets/lock/ed.rs b/rust/ares/src/jets/lock/ed.rs new file mode 100644 index 00000000..96511acf --- /dev/null +++ b/rust/ares/src/jets/lock/ed.rs @@ -0,0 +1,257 @@ +use crate::interpreter::{Context, Error}; +use crate::jets::bits::util::met; +use crate::jets::util::slot; +use crate::jets::{JetErr, Result}; +use crate::noun::{IndirectAtom, Noun, D, NO, YES}; +use urcrypt_sys::*; + +crate::gdb!(); + +pub fn jet_puck(context: &mut Context, subject: Noun) -> Result { + let stack = &mut context.stack; + let sed = slot(subject, 6)?.as_atom()?; + + if met(3, sed) > 32 { + return Err(JetErr::Fail(Error::Deterministic(D(0)))); + } + + unsafe { + // allocate 32 byte buffer on + let sed_buffer = &mut [0u8; 32] as *mut u8; + let sed_bytes = sed.as_bytes(); + + // we need to copy because the atom might be less than 32 bytes and urcrypt expects a + // 32-byte buffer + std::ptr::copy_nonoverlapping(sed_bytes.as_ptr(), sed_buffer, sed_bytes.len()); + + let (mut pub_ida, pub_key) = IndirectAtom::new_raw_mut_bytes(stack, 32); + urcrypt_ed_puck(sed_buffer as *const u8, pub_key.as_mut_ptr()); + + Ok(pub_ida.normalize_as_atom().as_noun()) + } +} + +pub fn jet_shar(context: &mut Context, subject: Noun) -> Result { + let stack = &mut context.stack; + let pub_key = slot(subject, 12)?.as_atom()?; + let sec_key = slot(subject, 13)?.as_atom()?; + + if met(3, sec_key) > 32 { + // sek is size checked by +puck via +suck + return Err(JetErr::Fail(Error::Deterministic(D(0)))); + } + if met(3, pub_key) > 32 { + // pub is not size checked in Hoon, but it must be 32 bytes or less for + // ucrypt. Therefore, punt on larger values. + return Err(JetErr::Punt); + } + + unsafe { + let (_, public) = IndirectAtom::new_raw_mut_bytes(stack, 32); + let (_, secret) = IndirectAtom::new_raw_mut_bytes(stack, 32); + + let pub_bytes = pub_key.as_bytes(); + let sec_bytes = sec_key.as_bytes(); + + public[0..pub_bytes.len()].copy_from_slice(pub_bytes); + secret[0..sec_bytes.len()].copy_from_slice(sec_bytes); + + let (mut shar_ida, shar) = IndirectAtom::new_raw_mut_bytes(stack, 32); + urcrypt_ed_shar(public.as_ptr(), secret.as_ptr(), shar.as_mut_ptr()); + + Ok(shar_ida.normalize_as_atom().as_noun()) + } +} + +pub fn jet_sign(context: &mut Context, subject: Noun) -> Result { + let stack = &mut context.stack; + let msg = slot(subject, 12)?.as_atom()?; + let sed = slot(subject, 13)?.as_atom()?; + + unsafe { + let sed_bytes = sed.as_bytes(); + if sed_bytes.len() > 32 { + return Err(JetErr::Fail(Error::Deterministic(D(0)))); + }; + + let msg_bytes = &(msg.as_bytes())[0..met(3, msg)]; // drop trailing zeros + + let (mut _seed_ida, seed) = IndirectAtom::new_raw_mut_bytes(stack, 32); + seed.copy_from_slice(sed_bytes); + + let (mut sig_ida, sig) = IndirectAtom::new_raw_mut_bytes(stack, 64); + urcrypt_ed_sign( + msg_bytes.as_ptr(), + msg_bytes.len(), + seed.as_ptr(), + sig.as_mut_ptr(), + ); + sig.reverse(); // LSB first + + Ok(sig_ida.normalize_as_atom().as_noun()) + } +} + +pub fn jet_veri(context: &mut Context, subject: Noun) -> Result { + let stack = &mut context.stack; + let sig = slot(subject, 12)?.as_atom()?; + let msg = slot(subject, 26)?.as_atom()?; + let puk = slot(subject, 27)?.as_atom()?; + + unsafe { + // Both are size checked by Hoon, but without crashing + let sig_bytes = sig.as_bytes(); + if sig_bytes.len() > 64 { + return Ok(NO); + }; + + let pub_bytes = puk.as_bytes(); + if pub_bytes.len() > 32 { + return Ok(NO); + }; + + let (mut _sig_ida, signature) = IndirectAtom::new_raw_mut_bytes(stack, 64); + signature.copy_from_slice(sig_bytes); + let (mut _pub_ida, public_key) = IndirectAtom::new_raw_mut_bytes(stack, 32); + public_key.copy_from_slice(pub_bytes); + + let message = &(msg.as_bytes())[0..met(3, msg)]; // drop trailing zeros + + let valid = urcrypt_ed_veri( + message.as_ptr(), + message.len(), + public_key.as_ptr(), + signature.as_ptr(), + ); + + Ok(if valid { YES } else { NO }) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::jets::util::test::{assert_jet, assert_jet_err, init_context, A}; + use crate::noun::{D, T}; + use ibig::ubig; + + // XX: Should use the test vectors from Section 7.1 of RFC 8032: + // https://tools.ietf.org/html/rfc8032#section-7.1 + + #[test] + fn test_puck() { + let c = &mut init_context(); + + let sam = A( + &mut c.stack, + &ubig!(_0x607fae1c03ac3b701969327b69c54944c42cec92f44a84ba605afdef9db1619d), + ); + let ret = A( + &mut c.stack, + &ubig!(_0x1a5107f7681a02af2523a6daf372e10e3a0764c9d3fe4bd5b70ab18201985ad7), + ); + assert_jet(c, jet_puck, sam, ret); + } + + #[test] + fn test_shar() { + let c = &mut init_context(); + + let sam = T(&mut c.stack, &[D(234), D(234)]); + let ret = A( + &mut c.stack, + &ubig!(_0x6ecd5779a47841207a2cd0c9d085796aa646842885a332adac540027d768c1c5), + ); + assert_jet(c, jet_shar, sam, ret); + + let sam = A( + &mut c.stack, + &ubig!(_0xfb099b0acc4d1ce37f9982a2ed331245e0cdfdf6979364b7676a142b8233e53b), + ); + assert_jet_err(c, jet_shar, sam, JetErr::Fail(Error::Deterministic(D(0)))); + } + + #[test] + fn test_sign() { + let c = &mut init_context(); + + unsafe { + let message = D(0x72); + + let sed_ubig = + ubig!(_0x4ccd089b28ff96da9db6c346ec114e0f5b8a319f35aba624da8cf6ed4fb8a6fb); + let sed_bytes = sed_ubig.to_be_bytes(); + let seed = + IndirectAtom::new_raw_bytes(&mut c.stack, sed_bytes.len(), sed_bytes.as_ptr()) + .as_noun(); + + let sam = T(&mut c.stack, &[message, seed]); + let ret = A(&mut c.stack, &ubig!(_0x92a009a9f0d4cab8720e820b5f642540a2b27b5416503f8fb3762223ebdb69da085ac1e43e15996e458f3613d0f11d8c387b2eaeb4302aeeb00d291612bb0c00)); + assert_jet(c, jet_sign, sam, ret); + + let msg_ubig = ubig!(_0xaf82); + let msg_bytes = msg_ubig.to_be_bytes(); + let message = + IndirectAtom::new_raw_bytes(&mut c.stack, msg_bytes.len(), msg_bytes.as_ptr()) + .as_noun(); + + let sed_ubig = + ubig!(_0xc5aa8df43f9f837bedb7442f31dcb7b166d38535076f094b85ce3a2e0b4458f7); + let sed_bytes = sed_ubig.to_be_bytes(); + let seed = + IndirectAtom::new_raw_bytes(&mut c.stack, sed_bytes.len(), sed_bytes.as_ptr()) + .as_noun(); + + let sam = T(&mut c.stack, &[message, seed]); + let ret = A(&mut c.stack, &ubig!(_0x6291d657deec24024827e69c3abe01a30ce548a284743a445e3680d7db5ac3ac18ff9b538d16f290ae67f760984dc6594a7c15e9716ed28dc027beceea1ec40a)); + assert_jet(c, jet_sign, sam, ret); + } + } + + #[test] + fn test_veri() { + let c = &mut init_context(); + + unsafe { + let sig_ubig = ubig!(_0x92a009a9f0d4cab8720e820b5f642540a2b27b5416503f8fb3762223ebdb69da085ac1e43e15996e458f3613d0f11d8c387b2eaeb4302aeeb00d291612bb0c00); + let sig_bytes = sig_ubig.to_be_bytes(); + let signature = + IndirectAtom::new_raw_bytes(&mut c.stack, sig_bytes.len(), sig_bytes.as_ptr()) + .as_noun(); + + let message = D(0x72); + + let pub_ubig = + ubig!(_0x3d4017c3e843895a92b70aa74d1b7ebc9c982ccf2ec4968cc0cd55f12af4660c); + let pub_bytes = pub_ubig.to_be_bytes(); + let public_key = + IndirectAtom::new_raw_bytes(&mut c.stack, pub_bytes.len(), pub_bytes.as_ptr()) + .as_noun(); + + let sam = T(&mut c.stack, &[signature, message, public_key]); + assert_jet(c, jet_veri, sam, YES); + + let sig_ubig = ubig!(_0x6291d657deec24024827e69c3abe01a30ce548a284743a445e3680d7db5ac3ac18ff9b538d16f290ae67f760984dc6594a7c15e9716ed28dc027beceea1ec40a); + let sig_bytes = sig_ubig.to_be_bytes(); + let signature = + IndirectAtom::new_raw_bytes(&mut c.stack, sig_bytes.len(), sig_bytes.as_ptr()) + .as_noun(); + + let msg_ubig = ubig!(0xaf82); + let msg_bytes = msg_ubig.to_be_bytes(); + let message = + IndirectAtom::new_raw_bytes(&mut c.stack, msg_bytes.len(), msg_bytes.as_ptr()) + .as_noun(); + + let pub_ubig = + ubig!(_0xfc51cd8e6218a1a38da47ed00230f0580816ed13ba3303ac5deb911548908025); + let pub_bytes = pub_ubig.to_be_bytes(); + let public_key = + IndirectAtom::new_raw_bytes(&mut c.stack, pub_bytes.len(), pub_bytes.as_ptr()) + .as_noun(); + + let sam = T(&mut c.stack, &[signature, message, public_key]); + assert_jet(c, jet_veri, sam, YES); + } + } +} diff --git a/rust/ares/src/jets/lock/sha.rs b/rust/ares/src/jets/lock/sha.rs new file mode 100644 index 00000000..a2d87f34 --- /dev/null +++ b/rust/ares/src/jets/lock/sha.rs @@ -0,0 +1,350 @@ +use crate::interpreter::{Context, Error}; +use crate::jets::bits::util::met; +use crate::jets::util::slot; +use crate::jets::{JetErr, Result}; +use crate::noun::{IndirectAtom, Noun, D}; +use urcrypt_sys::*; + +crate::gdb!(); + +pub fn jet_shas(context: &mut Context, subject: Noun) -> Result { + let stack = &mut context.stack; + let sam = slot(subject, 6)?; + let sal = slot(sam, 2)?.as_atom()?; + let ruz = slot(sam, 3)?.as_atom()?; + + let sal_bytes = &(sal.as_bytes())[0..met(3, sal)]; // drop trailing zeros + let (mut _salt_ida, salt) = unsafe { IndirectAtom::new_raw_mut_bytes(stack, sal_bytes.len()) }; + salt.copy_from_slice(sal_bytes); + + let message = &(ruz.as_bytes())[0..met(3, ruz)]; // drop trailing zeros + + unsafe { + let (mut out_ida, out) = IndirectAtom::new_raw_mut_bytes(stack, 32); + urcrypt_shas( + salt.as_mut_ptr(), + salt.len(), + message.as_ptr(), + message.len(), + out.as_mut_ptr(), + ); + Ok(out_ida.normalize_as_atom().as_noun()) + } +} + +pub fn jet_shax(context: &mut Context, subject: Noun) -> Result { + let stack = &mut context.stack; + let sam = slot(subject, 6)?; + let msg = sam.as_atom()?; + let len = met(3, msg); + + unsafe { + let (mut ida, out) = IndirectAtom::new_raw_mut_bytes(stack, 32); + urcrypt_shay(msg.as_bytes().as_ptr(), len, out.as_mut_ptr()); + Ok(ida.normalize_as_atom().as_noun()) + } +} + +pub fn jet_shay(context: &mut Context, subject: Noun) -> Result { + let stack = &mut context.stack; + let sam = slot(subject, 6)?; + let wid = slot(sam, 2)?.as_atom()?; + let dat = slot(sam, 3)?.as_atom()?; + + let width = match wid.as_direct() { + Ok(direct) => direct.data() as usize, + Err(_) => return Err(JetErr::Fail(Error::NonDeterministic(D(0)))), + }; + + let message = dat.as_bytes(); + + unsafe { + let (mut out_ida, out) = IndirectAtom::new_raw_mut_bytes(stack, 32); + urcrypt_shay(message.as_ptr(), width, out.as_mut_ptr()); + Ok(out_ida.normalize_as_atom().as_noun()) + } +} + +pub fn jet_shal(context: &mut Context, subject: Noun) -> Result { + let stack = &mut context.stack; + let sam = slot(subject, 6)?; + let wid = slot(sam, 2)?.as_atom()?; + let dat = slot(sam, 3)?.as_atom()?; + + let width = match wid.as_direct() { + Ok(direct) => direct.data() as usize, + Err(_) => return Err(JetErr::Fail(Error::NonDeterministic(D(0)))), + }; + + let message = &(dat.as_bytes())[0..met(3, dat)]; // drop trailing zeros + + unsafe { + let (mut ida, out) = IndirectAtom::new_raw_mut_bytes(stack, 64); + urcrypt_shal(message.as_ptr(), width, out.as_mut_ptr()); + Ok(ida.normalize_as_atom().as_noun()) + } +} + +pub fn jet_sha1(context: &mut Context, subject: Noun) -> Result { + let stack = &mut context.stack; + let sam = slot(subject, 6)?; + let wid = slot(sam, 2)?.as_atom()?; + let dat = slot(sam, 3)?.as_atom()?; + + let width = match wid.as_direct() { + Ok(direct) => direct.data() as usize, + Err(_) => return Err(JetErr::Fail(Error::NonDeterministic(D(0)))), + }; + + unsafe { + let msg_bytes = dat.as_bytes(); + let (mut _msg_ida, msg) = IndirectAtom::new_raw_mut_bytes(stack, msg_bytes.len()); + msg.copy_from_slice(msg_bytes); + + let (mut out_ida, out) = IndirectAtom::new_raw_mut_bytes(stack, 20); + urcrypt_sha1(msg.as_mut_ptr(), width, out.as_mut_ptr()); + Ok(out_ida.normalize_as_atom().as_noun()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::jets::util::test::{assert_jet, assert_jet_err, assert_jet_ubig, init_context, A}; + use crate::jets::JetErr; + use crate::noun::{D, DIRECT_MAX, T}; + use ibig::ubig; + + #[test] + fn test_shas() { + let c = &mut init_context(); + + let sam = T(&mut c.stack, &[D(1), D(1)]); + assert_jet_ubig( + c, + jet_shas, + sam, + ubig!(_0x547da92584bc986e5784edb746c29504bfd6b34572c83b7b96440ca77d35cdfc), + ); + + let sam = T(&mut c.stack, &[D(2), D(2)]); + assert_jet_ubig( + c, + jet_shas, + sam, + ubig!(_0x4cf01fe7cc56ef70d17735322488de0d31857afcfe451e199abe6295f78f5328), + ); + + let a = A( + &mut c.stack, + &ubig!(_0xa1d6eb6ef33f233ae6980ca7c4fc65f90fe1bdee11c730d41607b4747c83de72), + ); + let b = A( + &mut c.stack, + &ubig!(_0xa1d6eb6ef33f233ae6980ca7c4fc65f90fe1bdee11c730d41607b4747c83de73), + ); + let sam = T(&mut c.stack, &[a, b]); + assert_jet_ubig( + c, + jet_shas, + sam, + ubig!(_0xf7569a89650553ef13f9a8f0bb751fd42b70a4821be6bc1cbe197af33ce4843c), + ); + } + + #[test] + fn test_shax() { + let c = &mut init_context(); + + assert_jet_ubig( + c, + jet_shax, + D(7303014), // 'foo' + ubig!(_0xaee76662885e8af9a0bf8364702d42133441301d3c459bf98fc6ff686bb4262c), + ); + + let a = A( + &mut c.stack, + &ubig!(_0xaee76662885e8af9a0bf8364702d42133441301d3c459bf98fc6ff686bb4262c), + ); + assert_jet_ubig( + c, + jet_shax, + a, + ubig!(_0x9ee26e46c2028aa4a9c463aa722b82ed8bf6e185c3e5a5a69814a2c78fe8adc7), + ); + + assert_jet_ubig( + c, + jet_shax, + D(123456789), + ubig!(_0xa1d6eb6ef33f233ae6980ca7c4fc65f90fe1bdee11c730d41607b4747c83de72), + ); + + let a = A( + &mut c.stack, + &ubig!(_0xa1d6eb6ef33f233ae6980ca7c4fc65f90fe1bdee11c730d41607b4747c83de72), + ); + assert_jet_ubig( + c, + jet_shax, + a, + ubig!(_0xf90f3184d7347a20cfdd2d5f7ac5c82eb9ab7af54c9419fbc18832c5a33360c9), + ) + } + + #[test] + fn test_shay() { + let c = &mut init_context(); + + let sam = T(&mut c.stack, &[D(1), D(0)]); + let ret = A( + &mut c.stack, + &ubig!(_0x1da0af1706a31185763837b33f1d90782c0a78bbe644a59c987ab3ff9c0b346e), + ); + assert_jet(c, jet_shay, sam, ret); + + let sam = T(&mut c.stack, &[D(0), D(1)]); + let ret = A( + &mut c.stack, + &ubig!(_0x55b852781b9995a44c939b64e441ae2724b96f99c8f4fb9a141cfc9842c4b0e3), + ); + assert_jet(c, jet_shay, sam, ret); + + let sam = T(&mut c.stack, &[D(1), D(478560413032)]); // [1 'hello'] + let ret = A( + &mut c.stack, + &ubig!(_0x23b14de6713b28aadf8f95026636eb6ab63e99c952bceb401fa4f1642640a9aa), + ); + assert_jet(c, jet_shay, sam, ret); + + let sam = T(&mut c.stack, &[D(2), D(478560413032)]); // [2 'hello'] + let ret = A( + &mut c.stack, + &ubig!(_0xde1e7ee30cecf453f1d77c08a125fdc6a4bbac72c01dd7a1e21cd0d22f7e2f37), + ); + assert_jet(c, jet_shay, sam, ret); + + let big = DIRECT_MAX + 1; + let ida = unsafe { + IndirectAtom::new_raw_bytes(&mut c.stack, 8, &big as *const u64 as *const u8) + }; + let sam = T(&mut c.stack, &[ida.as_noun(), D(478560413032)]); + assert_jet_err( + c, + jet_shay, + sam, + JetErr::Fail(Error::NonDeterministic(D(0))), + ); + + let big: u128 = (DIRECT_MAX as u128) << 64; + let ida = unsafe { + IndirectAtom::new_raw_bytes(&mut c.stack, 8, &big as *const u128 as *const u8) + }; + let sam = T(&mut c.stack, &[ida.as_noun(), D(478560413032)]); + assert_jet_err( + c, + jet_shay, + sam, + JetErr::Fail(Error::NonDeterministic(D(0))), + ); + } + + #[test] + fn test_shal() { + let c = &mut init_context(); + + let sam = T(&mut c.stack, &[D(1), D(1)]); + assert_jet_ubig( + c, + jet_shal, + sam, + ubig!(_0x39e3d936c6e31eaac08fcfcfe7bb443460c61c0bd5b74408c8bcc35a6b8d6f5700bdcddeaa4b466ae65f8fb67f67ca62dc34149e1d44d213ddfbc13668b6547b) + ); + + let sam = T(&mut c.stack, &[D(1), D(2)]); + assert_jet_ubig( + c, + jet_shal, + sam, + ubig!(_0xcadc698fca01cf2935f760278554b4e61f35453975a5bb45389003159bc8485b7018dd8152d9cc23b6e9dd91b107380b9d14ddbf9cc037ee53a857b6c948b8fa) + ); + + let wid = A( + &mut c.stack, + &ubig!(_0xa1d6eb6ef33f233ae6980ca7c4fc65f90fe1bdee11c730d41607b4747c83de72), + ); + let dat = A( + &mut c.stack, + &ubig!(_0xa1d6eb6ef33f233ae6980ca7c4fc65f90fe1bdee11c730d41607b4747c83de73), + ); + let sam = T(&mut c.stack, &[wid, dat]); + assert_jet_err( + c, + jet_shal, + sam, + JetErr::Fail(Error::NonDeterministic(D(0))), + ); + + let wid = A( + &mut c.stack, + &ubig!(_0xa1d6eb6ef33f233ae6980ca7c4fc65f90fe1bdee11c730d41607b4747c83de72), + ); + let sam = T(&mut c.stack, &[wid, D(1)]); + assert_jet_err( + c, + jet_shal, + sam, + JetErr::Fail(Error::NonDeterministic(D(0))), + ); + } + + #[test] + fn test_sha1() { + let c = &mut init_context(); + + let sam = T(&mut c.stack, &[D(1), D(1)]); + assert_jet_ubig( + c, + jet_sha1, + sam, + ubig!(_0xbf8b4530d8d246dd74ac53a13471bba17941dff7), + ); + + let sam = T(&mut c.stack, &[D(1), D(2)]); + assert_jet_ubig( + c, + jet_sha1, + sam, + ubig!(_0xc4ea21bb365bbeeaf5f2c654883e56d11e43c44e), + ); + + let wid = A( + &mut c.stack, + &ubig!(_0xa1d6eb6ef33f233ae6980ca7c4fc65f90fe1bdee11c730d41607b4747c83de72), + ); + let dat = A( + &mut c.stack, + &ubig!(_0xa1d6eb6ef33f233ae6980ca7c4fc65f90fe1bdee11c730d41607b4747c83de73), + ); + let sam = T(&mut c.stack, &[wid, dat]); + assert_jet_err( + c, + jet_sha1, + sam, + JetErr::Fail(Error::NonDeterministic(D(0))), + ); + + let wid = A( + &mut c.stack, + &ubig!(_0xa1d6eb6ef33f233ae6980ca7c4fc65f90fe1bdee11c730d41607b4747c83de72), + ); + let sam = T(&mut c.stack, &[wid, D(1)]); + assert_jet_err( + c, + jet_sha1, + sam, + JetErr::Fail(Error::NonDeterministic(D(0))), + ); + } +} diff --git a/rust/ares/src/jets/warm.rs b/rust/ares/src/jets/warm.rs index d355f09f..3b7e4c91 100644 --- a/rust/ares/src/jets/warm.rs +++ b/rust/ares/src/jets/warm.rs @@ -133,11 +133,16 @@ impl Warm { /// Walk through the linked list of WarmEntry objects and do a partial check /// against the subject using Batteries (walk to root of parent batteries). /// If there's a match, then we've found a valid jet. - pub fn find_jet(&mut self, stack: &mut NockStack, s: &mut Noun, f: &mut Noun) -> Option { + pub fn find_jet( + &mut self, + stack: &mut NockStack, + s: &mut Noun, + f: &mut Noun, + ) -> Option<(Jet, Noun)> { let warm_it = self.0.lookup(stack, f)?; - for (_path, batteries, jet) in warm_it { + for (path, batteries, jet) in warm_it { if batteries.matches(stack, *s) { - return Some(jet); + return Some((jet, path)); } } None diff --git a/rust/ares/src/noun.rs b/rust/ares/src/noun.rs index 664474a5..3e4b1c2e 100644 --- a/rust/ares/src/noun.rs +++ b/rust/ares/src/noun.rs @@ -733,6 +733,20 @@ impl Atom { unsafe { is_indirect_atom(self.raw) } } + pub fn is_normalized(&self) -> bool { + unsafe { + if let Some(indirect) = self.indirect() { + if (indirect.size() == 1 && *indirect.data_pointer() <= DIRECT_MAX) + || *indirect.data_pointer().add(indirect.size() - 1) == 0 + { + return false; + } + } // nothing to do for direct atom + }; + + true + } + pub fn as_direct(&self) -> Result { if self.is_direct() { unsafe { Ok(self.direct) } @@ -757,6 +771,18 @@ impl Atom { } } + pub fn as_noun(self) -> Noun { + Noun { atom: self } + } + + pub fn as_bytes(&self) -> &[u8] { + if self.is_direct() { + unsafe { self.direct.as_bytes() } + } else { + unsafe { self.indirect.as_bytes() } + } + } + pub fn as_u64(self) -> Result { if self.is_direct() { Ok(unsafe { self.direct.data() }) @@ -845,18 +871,6 @@ impl Atom { *self } } - - pub fn as_noun(self) -> Noun { - Noun { atom: self } - } - - pub fn as_bytes(&self) -> &[u8] { - if self.is_direct() { - unsafe { self.direct.as_bytes() } - } else { - unsafe { self.indirect.as_bytes() } - } - } } impl fmt::Display for Atom { diff --git a/rust/ares/src/snapshot/pma.rs b/rust/ares/src/snapshot/pma.rs index 855835ee..28fba4bc 100644 --- a/rust/ares/src/snapshot/pma.rs +++ b/rust/ares/src/snapshot/pma.rs @@ -121,210 +121,3 @@ impl Snapshot for Pma { } } } - -#[cfg(test)] -mod tests { - use super::*; - use crate::noun::IndirectAtom; - - fn clean_test_dir(path: &str) { - if let Err(err) = std::fs::remove_dir_all(path) { - if err.kind() != std::io::ErrorKind::NotFound { - panic!("failed to remove dir: {}", err); - } - } - } - - #[test] - fn test_pma_unit() { - let path = "/tmp/ares_pma_test_unit"; - clean_test_dir(path); - - unsafe { - test_pma(path); - } - - clean_test_dir(path); - } - - #[test] - fn test_pma_sanity() { - let path = "/tmp/ares_pma_test_sanity"; - clean_test_dir(path); - - unsafe { - let stack = &mut NockStack::new(8 << 10 << 10, 0); - let root = IndirectAtom::new_raw(stack, 1, &0xffff_ffff_ffff_ffff).as_noun(); - let mut base: *mut u8; - let mut new_alloc: *mut u8; - - assert!(0 == pma_init(path)); - - // 2 allocations of every size - // assert that slots / pages are correct number of bytes apart - base = pma_malloc::(16); - let alloc_16 = pma_malloc::(16); - assert!(alloc_16 == (base.add(16))); - assert!(0 != (alloc_16 as u64 % 4096)); - - base = pma_malloc::(32); - let alloc_32 = pma_malloc::(32); - assert!(alloc_32 == (base.add(32))); - assert!(0 != (alloc_32 as u64 % 4096)); - - base = pma_malloc::(64); - let alloc_64 = pma_malloc::(64); - assert!(alloc_64 == (base.add(64))); - assert!(0 != (alloc_64 as u64 % 4096)); - - base = pma_malloc::(128); - let alloc_128 = pma_malloc::(128); - assert!(alloc_128 == (base.add(128))); - assert!(0 != (alloc_128 as u64 % 4096)); - - base = pma_malloc::(256); - let alloc_256 = pma_malloc::(256); - assert!(alloc_256 == (base.add(256))); - assert!(0 != (alloc_256 as u64 % 4096)); - - base = pma_malloc::(512); - let alloc_512 = pma_malloc::(512); - assert!(alloc_512 == (base.add(512))); - assert!(0 != (alloc_512 as u64 % 4096)); - - base = pma_malloc::(1024); - let alloc_1024 = pma_malloc::(1024); - assert!(alloc_1024 == (base.add(1024))); - assert!(0 != (alloc_1024 as u64 % 4096)); - - base = pma_malloc::(2048); - let alloc_2048 = pma_malloc::(2048); - assert!(alloc_2048 == (base.add(4096))); - assert!(0 == (alloc_2048 as u64 % 4096)); - - base = pma_malloc::(4096); - let alloc_4096 = pma_malloc::(4096); - assert!(alloc_4096 == (base.add(4096))); - assert!(0 == (alloc_4096 as u64 % 4096)); - - base = pma_malloc::(8192); - let alloc_8192 = pma_malloc::(8192); - assert!(alloc_8192 == (base.add(8192))); - assert!(0 == (alloc_8192 as u64 % 4096)); - - // sync - // check that everything is still where it should be - *alloc_16 = 0x01; - *alloc_32 = 0x02; - *alloc_64 = 0x03; - *alloc_128 = 0x04; - *alloc_256 = 0x05; - *alloc_512 = 0x06; - *alloc_1024 = 0x07; - *alloc_2048 = 0x08; - *alloc_4096 = 0x09; - *alloc_8192 = 0x0a; - - assert!(0 == pma_sync(1, 1, root)); - - assert!(0x01 == *alloc_16); - assert!(0x02 == *alloc_32); - assert!(0x03 == *alloc_64); - assert!(0x04 == *alloc_128); - assert!(0x05 == *alloc_256); - assert!(0x06 == *alloc_512); - assert!(0x07 == *alloc_1024); - assert!(0x08 == *alloc_2048); - assert!(0x09 == *alloc_4096); - assert!(0x0a == *alloc_8192); - - // close PMA - // load - // check that everything is still where it should be - assert!(0 == pma_close(2, 2, root)); - - let root_state = pma_load(path); - assert!(2 == root_state.0); - assert!(2 == root_state.1); - assert!(root_state.2.raw_equals(root)); - - assert!(0x01 == *alloc_16); - assert!(0x02 == *alloc_32); - assert!(0x03 == *alloc_64); - assert!(0x04 == *alloc_128); - assert!(0x05 == *alloc_256); - assert!(0x06 == *alloc_512); - assert!(0x07 == *alloc_1024); - assert!(0x08 == *alloc_2048); - assert!(0x09 == *alloc_4096); - assert!(0x0a == *alloc_8192); - - // free 1-page allocation - // sync - // make new 1-page allocation - // sync - // check that page is being re-used - assert!(0 == pma_free(alloc_4096)); - assert!(0 == pma_sync(3, 3, root)); - new_alloc = pma_malloc(4096); - assert!(new_alloc == alloc_4096); - - // free 2-page allocation - // sync - // make new 2-page allocation - // sync - // check that page run is being re-used - assert!(0 == pma_free(alloc_8192)); - assert!(0 == pma_sync(4, 4, root)); - new_alloc = pma_malloc(8192); - assert!(new_alloc == alloc_8192); - - // free 2-page allocation - // make new 2-page allocation - // sync - // check that page run is NOT being re-used - assert!(0 == pma_sync(5, 5, root)); - assert!(0 == pma_free(alloc_8192)); - new_alloc = pma_malloc(8192); - assert!(new_alloc != alloc_8192); - - // multiple syncs - // close - // load - // everything is where it should be - assert!(0 == pma_sync(6, 6, root)); - assert!(0 == pma_sync(7, 7, root)); - assert!(0 == pma_close(8, 8, root)); - - let root_state = pma_load(path); - assert!(8 == root_state.0); - assert!(8 == root_state.1); - assert!(root_state.2.raw_equals(root)); - - assert!(0x01 == *alloc_16); - assert!(0x02 == *alloc_32); - assert!(0x03 == *alloc_64); - assert!(0x04 == *alloc_128); - assert!(0x05 == *alloc_256); - assert!(0x06 == *alloc_512); - assert!(0x07 == *alloc_1024); - assert!(0x08 == *alloc_2048); - - // free many allocations - // close - assert!(0 == pma_free(alloc_16)); - assert!(0 == pma_free(alloc_32)); - assert!(0 == pma_free(alloc_64)); - assert!(0 == pma_free(alloc_128)); - assert!(0 == pma_free(alloc_256)); - assert!(0 == pma_free(alloc_512)); - assert!(0 == pma_free(alloc_1024)); - assert!(0 == pma_free(alloc_2048)); - assert!(0 == pma_free(base)); - assert!(0 == pma_free(new_alloc)); - assert!(0 == pma_close(9, 9, root)); - } - - clean_test_dir(path); - } -} diff --git a/rust/flake.lock b/rust/flake.lock index 96487ead..c97117b1 100644 --- a/rust/flake.lock +++ b/rust/flake.lock @@ -8,11 +8,11 @@ "rust-analyzer-src": "rust-analyzer-src" }, "locked": { - "lastModified": 1700547770, - "narHash": "sha256-dN/j67tAzK8tZUxLoPBkQhLlAOzOpaQ0Y8dI7B4Al1E=", + "lastModified": 1700806945, + "narHash": "sha256-mV25HkKFHDmjp+FEmneLYaRCB7wHOGuZlDFdHUCKAJI=", "owner": "nix-community", "repo": "fenix", - "rev": "eea8e81ef5d410b2c9bef8bc90d18188a379958b", + "rev": "7ab9ec16d364b564da3aa0e73887b0af133eef59", "type": "github" }, "original": { @@ -34,25 +34,23 @@ "type": "github" }, "original": { - "owner": "numtide", - "repo": "flake-utils", - "type": "github" + "id": "flake-utils", + "type": "indirect" } }, "nixpkgs": { "locked": { - "lastModified": 1700390070, - "narHash": "sha256-de9KYi8rSJpqvBfNwscWdalIJXPo8NjdIZcEJum1mH0=", + "lastModified": 1700794826, + "narHash": "sha256-RyJTnTNKhO0yqRpDISk03I/4A67/dp96YRxc86YOPgU=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "e4ad989506ec7d71f7302cc3067abd82730a4beb", + "rev": "5a09cb4b393d58f9ed0d9ca1555016a8543c2ac8", "type": "github" }, "original": { - "owner": "NixOS", + "id": "nixpkgs", "ref": "nixos-unstable", - "repo": "nixpkgs", - "type": "github" + "type": "indirect" } }, "root": { @@ -65,11 +63,11 @@ "rust-analyzer-src": { "flake": false, "locked": { - "lastModified": 1700502585, - "narHash": "sha256-e+67TsyI2akFHITDD+gz1Paf7vY3mH5mRnZq5Md8VfA=", + "lastModified": 1700744506, + "narHash": "sha256-zE+ncUiKUJjMYVgvLsRnphyk+lPB4fta+4eiXKz4t4I=", "owner": "rust-lang", "repo": "rust-analyzer", - "rev": "1a5cee17311fa0bb55757b382be918520b6ac2b4", + "rev": "cccc7ca2c630865239f68af480878824041c7c05", "type": "github" }, "original": { diff --git a/rust/flake.nix b/rust/flake.nix index 28f997a6..3f2587aa 100644 --- a/rust/flake.nix +++ b/rust/flake.nix @@ -1,19 +1,26 @@ { inputs = { - nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + nixpkgs.url = "nixpkgs/nixos-unstable"; fenix = { url = "github:nix-community/fenix"; inputs.nixpkgs.follows = "nixpkgs"; }; - flake-utils.url = "github:numtide/flake-utils"; + flake-utils.url = "flake-utils"; }; - outputs = {self, fenix, flake-utils, nixpkgs}: - let supportedSystems = ["x86_64-linux" "x86_64-darwin" "aarch64-darwin"]; - in flake-utils.lib.eachSystem supportedSystems - (system: - let pkgs = import nixpkgs { inherit system; }; - in { devShells.default = pkgs.mkShell { + outputs = {self, fenix, flake-utils, nixpkgs}: + let + supportedSystems = ["x86_64-linux" "x86_64-darwin" "aarch64-darwin"]; + in flake-utils.lib.eachSystem supportedSystems (system: + let + pkgs = import nixpkgs { + inherit system; + overlays = [(import ./nix/overlay.nix)]; + }; + parsedSystem = pkgs.lib.systems.parse.mkSystemFromString system; + in { + devShells.default = pkgs.mkShell { + LIBCLANG_PATH = "${pkgs.llvmPackages.libclang.lib}/lib"; buildInputs = [ (fenix.packages.${system}.complete.withComponents [ "cargo" @@ -22,12 +29,15 @@ "rustfmt" "rust-src" ]) + pkgs.autoconf-archive pkgs.cargo-watch - pkgs.gdb pkgs.iconv - ]; + pkgs.llvmPackages.clang + pkgs.pkg-config + pkgs.urcrypt + ] ++ + (nixpkgs.lib.lists.optional (parsedSystem.kernel.name != "darwin") pkgs.gdb); # nixpkgs won't build gdb for darwin }; } ); } - diff --git a/rust/nix/libaes_siv.nix b/rust/nix/libaes_siv.nix new file mode 100644 index 00000000..4e6382f3 --- /dev/null +++ b/rust/nix/libaes_siv.nix @@ -0,0 +1,17 @@ +{stdenv, fetchFromGitHub, cmake, asciidoc, openssl, libxml2, libxslt, docbook_xml_dtd_45, docbook_xsl}: +stdenv.mkDerivation { + pname = "libaes_siv"; + version = "1.latest"; + + src = fetchFromGitHub { + owner = "dfoxfranke"; + repo = "libaes_siv"; + rev = "9681279cfaa6e6399bb7ca3afbbc27fc2e19df4b"; + hash = "sha256:1g4wy0m5wpqx7z6nillppkh5zki9fkx9rdw149qcxh7mc5vlszzi"; + }; + + buildInputs = [cmake openssl]; + + cmakeFlags = [ "DDISABLE_DOCS" ]; + +} diff --git a/rust/nix/overlay.nix b/rust/nix/overlay.nix new file mode 100644 index 00000000..ea5040ac --- /dev/null +++ b/rust/nix/overlay.nix @@ -0,0 +1,4 @@ +self: super: { + libaes_siv = self.callPackage ./libaes_siv.nix {}; + urcrypt = self.callPackage ./urcrypt.nix {}; +} diff --git a/rust/nix/urcrypt.nix b/rust/nix/urcrypt.nix new file mode 100644 index 00000000..48f9df80 --- /dev/null +++ b/rust/nix/urcrypt.nix @@ -0,0 +1,20 @@ +{ stdenv, fetchFromGitHub, autoreconfHook, pkg-config, openssl, cryptopp, secp256k1, libaes_siv }: +let rev = "375fa7e6a730d8aa517ca981b2b7b505bf4e1103"; +in stdenv.mkDerivation { + pname = "urcrypt"; + version = "git-${rev}"; + src = fetchFromGitHub { + inherit rev; + owner = "urbit"; + repo = "urcrypt"; + hash = "sha256:1c3cqmwr5mys4v9y0834hxqfr6aynm2gav7730bjzfvrdc21ijqa"; + }; + + preConfigure = '' + ./autogen.sh + ''; + + nativeBuildInputs = [autoreconfHook pkg-config]; + + buildInputs = [openssl cryptopp secp256k1 libaes_siv]; +}