From 1a1554224a6bdfb59aea6ea7cf2d58d2d8687bf0 Mon Sep 17 00:00:00 2001 From: Agost Biro Date: Wed, 5 Jun 2024 22:45:50 +0200 Subject: [PATCH] Add Rust bindings to Go VDF --- Cargo.lock | 500 +++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 3 +- crates/vdf/Cargo.toml | 8 + crates/vdf/build.rs | 2 + crates/vdf/src/lib.rs | 16 ++ crates/vdf/src/lib.udl | 4 + vdf/.gitignore | 1 + vdf/generate.sh | 15 ++ vdf/go.mod | 3 + vdf/test.sh | 18 ++ vdf/vdf.go | 19 ++ vdf/vdf_test.go | 17 ++ 12 files changed, 605 insertions(+), 1 deletion(-) create mode 100644 crates/vdf/src/lib.udl create mode 100644 vdf/.gitignore create mode 100755 vdf/generate.sh create mode 100644 vdf/go.mod create mode 100755 vdf/test.sh create mode 100644 vdf/vdf.go create mode 100644 vdf/vdf_test.go diff --git a/Cargo.lock b/Cargo.lock index 0e172c3..eee764f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,18 +17,126 @@ version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299" +[[package]] +name = "anstream" +version = "0.6.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "418c75fa768af9c03be99d17643f93f79bbba589895012a80e3452a19ddda15b" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + [[package]] name = "anstyle" version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "038dfcf04a5feb68e9c60b21c9625a54c2c0616e79b72b0fd87075a056ae1d1b" +[[package]] +name = "anstyle-parse" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c03a11a9034d92058ceb6ee011ce58af4a9bf61491aa7e1e59ecd24bd40d22d4" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad186efb764318d35165f1758e7dcef3b10628e26d41a44bc5550652e6804391" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61a38449feb7068f52bb06c12759005cf459ee52bb4adc1d5a7c4322d716fb19" +dependencies = [ + "anstyle", + "windows-sys", +] + +[[package]] +name = "anyhow" +version = "1.0.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da" + +[[package]] +name = "askama" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b79091df18a97caea757e28cd2d5fda49c6cd4bd01ddffd7ff01ace0c0ad2c28" +dependencies = [ + "askama_derive", + "askama_escape", +] + +[[package]] +name = "askama_derive" +version = "0.12.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19fe8d6cb13c4714962c072ea496f3392015f0989b1a2847bb4b2d9effd71d83" +dependencies = [ + "askama_parser", + "basic-toml", + "mime", + "mime_guess", + "proc-macro2", + "quote", + "serde", + "syn", +] + +[[package]] +name = "askama_escape" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "619743e34b5ba4e9703bba34deac3427c72507c7159f5fd030aea8cac0cfe341" + +[[package]] +name = "askama_parser" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acb1161c6b64d1c3d83108213c2a2533a342ac225aabd0bda218278c2ddb00c0" +dependencies = [ + "nom", +] + [[package]] name = "autocfg" version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" +[[package]] +name = "basic-toml" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "823388e228f614e9558c6804262db37960ec8821856535f5c3f59913140558f8" +dependencies = [ + "serde", +] + +[[package]] +name = "bincode" +version = "1.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad" +dependencies = [ + "serde", +] + [[package]] name = "bit-vec" version = "0.5.1" @@ -74,6 +182,44 @@ version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" +[[package]] +name = "bytes" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "514de17de45fdb8dc022b1a7975556c53c86f9f0aa5f534b98977b171857c2c9" + +[[package]] +name = "camino" +version = "1.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0ec6b951b160caa93cc0c7b209e5a3bff7aae9062213451ac99493cd844c239" +dependencies = [ + "serde", +] + +[[package]] +name = "cargo-platform" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24b1f0365a6c6bb4020cd05806fd0d33c44d38046b8bd7f0e40814b9763cabfc" +dependencies = [ + "serde", +] + +[[package]] +name = "cargo_metadata" +version = "0.15.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eee4243f1f26fc7a42710e7439c149e2b10b05472f88090acce52632f231a73a" +dependencies = [ + "camino", + "cargo-platform", + "semver", + "serde", + "serde_json", + "thiserror", +] + [[package]] name = "cast" version = "0.3.0" @@ -120,6 +266,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "90bc066a67923782aa8515dbaea16946c5bcc5addbd668bb80af688e53e548a0" dependencies = [ "clap_builder", + "clap_derive", ] [[package]] @@ -128,8 +275,22 @@ version = "4.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ae129e2e766ae0ec03484e609954119f123cc1fe650337e155d03b022f24f7b4" dependencies = [ + "anstream", "anstyle", "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "528131438037fd55894f62d6e9f068b8f45ac57ffa77517819645d10aed04f64" +dependencies = [ + "heck 0.5.0", + "proc-macro2", + "quote", + "syn", ] [[package]] @@ -147,6 +308,12 @@ dependencies = [ "num-traits", ] +[[package]] +name = "colorchoice" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b6a852b24ab71dffc585bcb46eaf7959d175cb865a7152e35b348d1b2960422" + [[package]] name = "criterion" version = "0.5.1" @@ -235,6 +402,15 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed" +[[package]] +name = "fs-err" +version = "2.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88a41f105fe1d5b6b34b2055e3dc59bb79b46b48b2040b9e6c7b4b5de097aa41" +dependencies = [ + "autocfg", +] + [[package]] name = "generic-array" version = "0.12.4" @@ -244,6 +420,23 @@ dependencies = [ "typenum", ] +[[package]] +name = "glob" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" + +[[package]] +name = "goblin" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d6b4de4a8eb6c46a8c77e1d3be942cb9a8bf073c22374578e5ba4b08ed0ff68" +dependencies = [ + "log", + "plain", + "scroll", +] + [[package]] name = "half" version = "2.4.1" @@ -254,6 +447,18 @@ dependencies = [ "crunchy", ] +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + [[package]] name = "hermit-abi" version = "0.3.9" @@ -277,6 +482,12 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "is_terminal_polyfill" +version = "1.70.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8478577c03552c21db0e2724ffb8986a5ce7af88107e6be5d2ee6e158c12800" + [[package]] name = "itertools" version = "0.10.5" @@ -319,6 +530,38 @@ version = "2.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6c8640c5d730cb13ebd907d8d04b52f55ac9a2eec55b440c8892f40d56c76c1d" +[[package]] +name = "mime" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" + +[[package]] +name = "mime_guess" +version = "2.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4192263c238a5f0d0c6bfd21f336a313a4ce1c450542449ca191bb657b4642ef" +dependencies = [ + "mime", + "unicase", +] + +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + +[[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-traits" version = "0.2.19" @@ -334,6 +577,12 @@ version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" +[[package]] +name = "oneshot-uniffi" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c548d5c78976f6955d72d0ced18c48ca07030f7a1d4024529fedd7c1c01b29c" + [[package]] name = "oorandom" version = "11.1.3" @@ -346,6 +595,18 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2839e79665f131bdb5782e51f2c6c9599c133c6098982a54c794358bf432529c" +[[package]] +name = "paste" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" + +[[package]] +name = "plain" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4596b6d070b27117e987119b4dac604f3c58cfb0b191112e24771b2faeac1a6" + [[package]] name = "plotters" version = "0.3.6" @@ -456,6 +717,35 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "scroll" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04c565b551bafbef4157586fa379538366e4385d42082f255bfd96e4fe8519da" +dependencies = [ + "scroll_derive", +] + +[[package]] +name = "scroll_derive" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1db149f81d46d2deba7cd3c50772474707729550221e69588478ebf9ada425ae" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "semver" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61697e0a1c7e512e84a621326239844a24d8207b4669b41bc18b32ea5cbf988b" +dependencies = [ + "serde", +] + [[package]] name = "serde" version = "1.0.203" @@ -499,6 +789,24 @@ dependencies = [ "opaque-debug", ] +[[package]] +name = "siphasher" +version = "0.3.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38b58827f4464d87d377d175e90bf58eb00fd8716ff0a62f80356b5e61555d0d" + +[[package]] +name = "static_assertions" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + [[package]] name = "syn" version = "2.0.66" @@ -510,6 +818,26 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "thiserror" +version = "1.0.61" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c546c80d6be4bc6a00c0f01730c08df82eaa7a7a61f11d656526506112cc1709" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.61" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46c3384250002a6d5af4d114f2845d37b57521033f30d5c3f46c4d70e1197533" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "tinytemplate" version = "1.2.1" @@ -520,18 +848,174 @@ dependencies = [ "serde_json", ] +[[package]] +name = "toml" +version = "0.5.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4f7f0dd8d50a853a531c426359045b1998f04219d88799810762cd4ad314234" +dependencies = [ + "serde", +] + [[package]] name = "typenum" version = "1.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" +[[package]] +name = "unicase" +version = "2.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7d2d4dafb69621809a81864c9c1b864479e1235c0dd4e199924b9742439ed89" +dependencies = [ + "version_check", +] + [[package]] name = "unicode-ident" version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" +[[package]] +name = "uniffi" +version = "0.25.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21345172d31092fd48c47fd56c53d4ae9e41c4b1f559fb8c38c1ab1685fd919f" +dependencies = [ + "anyhow", + "camino", + "clap", + "uniffi_bindgen", + "uniffi_build", + "uniffi_core", + "uniffi_macros", +] + +[[package]] +name = "uniffi_bindgen" +version = "0.25.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd992f2929a053829d5875af1eff2ee3d7a7001cb3b9a46cc7895f2caede6940" +dependencies = [ + "anyhow", + "askama", + "camino", + "cargo_metadata", + "clap", + "fs-err", + "glob", + "goblin", + "heck 0.4.1", + "once_cell", + "paste", + "serde", + "toml", + "uniffi_meta", + "uniffi_testing", + "uniffi_udl", +] + +[[package]] +name = "uniffi_build" +version = "0.25.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "001964dd3682d600084b3aaf75acf9c3426699bc27b65e96bb32d175a31c74e9" +dependencies = [ + "anyhow", + "camino", + "uniffi_bindgen", +] + +[[package]] +name = "uniffi_checksum_derive" +version = "0.25.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55137c122f712d9330fd985d66fa61bdc381752e89c35708c13ce63049a3002c" +dependencies = [ + "quote", + "syn", +] + +[[package]] +name = "uniffi_core" +version = "0.25.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6121a127a3af1665cd90d12dd2b3683c2643c5103281d0fed5838324ca1fad5b" +dependencies = [ + "anyhow", + "bytes", + "camino", + "log", + "once_cell", + "oneshot-uniffi", + "paste", + "static_assertions", +] + +[[package]] +name = "uniffi_macros" +version = "0.25.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11cf7a58f101fcedafa5b77ea037999b88748607f0ef3a33eaa0efc5392e92e4" +dependencies = [ + "bincode", + "camino", + "fs-err", + "once_cell", + "proc-macro2", + "quote", + "serde", + "syn", + "toml", + "uniffi_build", + "uniffi_meta", +] + +[[package]] +name = "uniffi_meta" +version = "0.25.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71dc8573a7b1ac4b71643d6da34888273ebfc03440c525121f1b3634ad3417a2" +dependencies = [ + "anyhow", + "bytes", + "siphasher", + "uniffi_checksum_derive", +] + +[[package]] +name = "uniffi_testing" +version = "0.25.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "118448debffcb676ddbe8c5305fb933ab7e0123753e659a71dc4a693f8d9f23c" +dependencies = [ + "anyhow", + "camino", + "cargo_metadata", + "fs-err", + "once_cell", +] + +[[package]] +name = "uniffi_udl" +version = "0.25.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "889edb7109c6078abe0e53e9b4070cf74a6b3468d141bdf5ef1bd4d1dc24a1c3" +dependencies = [ + "anyhow", + "uniffi_meta", + "uniffi_testing", + "weedle2", +] + +[[package]] +name = "utf8parse" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" + [[package]] name = "vdf" version = "0.1.0" @@ -542,8 +1026,15 @@ dependencies = [ "hex", "num-traits", "sha2", + "uniffi", ] +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + [[package]] name = "walkdir" version = "2.5.0" @@ -618,6 +1109,15 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "weedle2" +version = "4.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e79c5206e1f43a2306fd64bdb95025ee4228960f2e6c5a8b173f3caaf807741" +dependencies = [ + "nom", +] + [[package]] name = "winapi-util" version = "0.1.8" diff --git a/Cargo.toml b/Cargo.toml index 88a1543..229c039 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,8 @@ members = [ ] [profile.release] -rpath = true +# rpath = false is important to make Go bindings work +rpath = false lto = true debug = false # panic = 'abort' diff --git a/crates/vdf/Cargo.toml b/crates/vdf/Cargo.toml index 81eca55..c2fdb7e 100644 --- a/crates/vdf/Cargo.toml +++ b/crates/vdf/Cargo.toml @@ -22,11 +22,19 @@ license = "Apache-2.0" edition = "2018" description = "An implementation of Verifiable Delay Functions (VDFs) in Rust" +[lib] +crate-type = ["lib", "cdylib"] +name = "vdf" + [dependencies] classgroup = { path = "../classgroup", version = "^0.1.0" } num-traits = "0.2" sha2 = "0.8" bit-vec = "0.5" +uniffi = { version= "0.25", features = ["cli"]} + +[build-dependencies] +uniffi = { version = "0.25", features = [ "build" ] } [dev-dependencies] criterion = ">=0.2" diff --git a/crates/vdf/build.rs b/crates/vdf/build.rs index 0fc1d3e..ad96740 100644 --- a/crates/vdf/build.rs +++ b/crates/vdf/build.rs @@ -110,6 +110,8 @@ fn main() { println!("cargo:rustc-link-search=/opt/homebrew/Cellar/gmp/6.3.0/lib"); } + uniffi::generate_scaffolding("src/lib.udl").expect("uniffi generation failed"); + let manifest_path = env::var("OUT_DIR").expect("cargo should have set this"); let mut path = PathBuf::from(&manifest_path); path.push("constants.rs"); diff --git a/crates/vdf/src/lib.rs b/crates/vdf/src/lib.rs index dda54cf..0cd128c 100644 --- a/crates/vdf/src/lib.rs +++ b/crates/vdf/src/lib.rs @@ -95,6 +95,8 @@ mod proof_of_time; mod proof_pietrzak; mod proof_wesolowski; +uniffi::include_scaffolding!("lib"); + /// An empty struct indicating verification failure. /// /// For security reasons, the functions that perform verification *do not* @@ -243,3 +245,17 @@ pub trait VDF: Send + Debug { alleged_solution: &[u8], ) -> Result<(), InvalidProof>; } + +/// Solve and prove with the Wesolowski VDF using the given parameters. +/// Outputs the concatenated solution and proof (in this order). +pub fn wesolowski_solve(int_size_bits: u16, challenge: &[u8], difficulty: u64) -> Vec { + let vdf = WesolowskiVDFParams(int_size_bits).new(); + vdf.solve(challenge, difficulty).expect("invalid difficulty") +} + +/// Verify with the Wesolowski VDF using the given parameters. +/// `alleged_solution` is the output of `wesolowski_solve`. +pub fn wesolowski_verify(int_size_bits: u16, challenge: &[u8], difficulty: u64, alleged_solution: &[u8]) -> bool { + let vdf = WesolowskiVDFParams(int_size_bits).new(); + vdf.verify(challenge, difficulty, alleged_solution).is_ok() +} diff --git a/crates/vdf/src/lib.udl b/crates/vdf/src/lib.udl new file mode 100644 index 0000000..4c48661 --- /dev/null +++ b/crates/vdf/src/lib.udl @@ -0,0 +1,4 @@ +namespace vdf { + sequence wesolowski_solve(u16 int_size_bits, [ByRef] sequence challenge, u64 difficulty); + boolean wesolowski_verify(u16 int_size_bits, [ByRef] sequence challenge, u64 difficulty, [ByRef] sequence alleged_solution); +}; diff --git a/vdf/.gitignore b/vdf/.gitignore new file mode 100644 index 0000000..86d4c2d --- /dev/null +++ b/vdf/.gitignore @@ -0,0 +1 @@ +generated diff --git a/vdf/generate.sh b/vdf/generate.sh new file mode 100755 index 0000000..d57a1f8 --- /dev/null +++ b/vdf/generate.sh @@ -0,0 +1,15 @@ +#!/bin/bash +set -euxo pipefail + +SCRIPT_DIR="${SCRIPT_DIR:-$( cd "../$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )}" +ROOT_DIR="$SCRIPT_DIR" + +RUST_VDF_PACKAGE="$ROOT_DIR/crates/vdf" +BINDINGS_DIR="$ROOT_DIR/vdf" + +# Build the Rust VDF package in release mode +RUSTFLAGS='-L /opt/homebrew/Cellar/gmp/6.3.0/lib' cargo build -p vdf --release + +# Generate Go bindings +pushd "$RUST_VDF_PACKAGE" > /dev/null +uniffi-bindgen-go src/lib.udl -o "$BINDINGS_DIR"/generated diff --git a/vdf/go.mod b/vdf/go.mod new file mode 100644 index 0000000..7369603 --- /dev/null +++ b/vdf/go.mod @@ -0,0 +1,3 @@ +module "source.quilibrium.com/quilibrium/cermonyclient/vdf" + +go 1.20 diff --git a/vdf/test.sh b/vdf/test.sh new file mode 100755 index 0000000..48479d6 --- /dev/null +++ b/vdf/test.sh @@ -0,0 +1,18 @@ +#!/bin/bash +set -euxo pipefail + +SCRIPT_DIR="${SCRIPT_DIR:-$( cd "../$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )}" +ROOT_DIR="$SCRIPT_DIR" + +BINDINGS_DIR="$ROOT_DIR/vdf" +BINARIES_DIR="$ROOT_DIR/target/release" + +go generate + +# Test the generated bindings +pushd "$BINDINGS_DIR" > /dev/null +LD_LIBRARY_PATH="${LD_LIBRARY_PATH:-}:$BINARIES_DIR" \ + CGO_LDFLAGS="-lvdf -L$BINARIES_DIR -ldl" \ + CGO_ENABLED=1 \ + LC_RPATH="$BINARIES_DIR" \ + go test diff --git a/vdf/vdf.go b/vdf/vdf.go new file mode 100644 index 0000000..f8524a6 --- /dev/null +++ b/vdf/vdf.go @@ -0,0 +1,19 @@ +package vdf + +import ( + generated "source.quilibrium.com/quilibrium/cermonyclient/vdf/generated/vdf" +) + +//go:generate ./generate.sh + +// WesolowskiSolve Solve and prove with the Wesolowski VDF using the given parameters. +// Outputs the concatenated solution and proof (in this order). +func WesolowskiSolve(intSizeBits uint16, challenge []uint8, difficulty uint64) []uint8 { + return generated.WesolowskiSolve(intSizeBits, challenge, difficulty) +} + +// WesolowskiVerify Verify with the Wesolowski VDF using the given parameters. +// `allegedSolution` is the output of `WesolowskiSolve`. +func WesolowskiVerify(intSizeBits uint16, challenge []uint8, difficulty uint64, allegedSolution []uint8) bool { + return generated.WesolowskiVerify(intSizeBits, challenge, difficulty, allegedSolution) +} diff --git a/vdf/vdf_test.go b/vdf/vdf_test.go new file mode 100644 index 0000000..a2e1585 --- /dev/null +++ b/vdf/vdf_test.go @@ -0,0 +1,17 @@ +package vdf_test + +import ( + "source.quilibrium.com/quilibrium/cermonyclient/vdf" + "testing" +) + +const intSizeBits = uint16(2048) +const difficulty = uint64(10000) + +func TestProveVerify(t *testing.T) { + solution := vdf.WesolowskiSolve(intSizeBits, []byte{0x01, 0x02, 0x03}, difficulty) + isOk := vdf.WesolowskiVerify(intSizeBits, []byte{0x01, 0x02, 0x03}, difficulty, solution) + if !isOk { + t.Errorf("WesolowskiVerify failed") + } +}