diff --git a/Cargo.lock b/Cargo.lock
index 2ffb984..d28eaa5 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2,6 +2,33 @@
# It is not intended for manual editing.
version = 3
+[[package]]
+name = "adversary"
+version = "0.1.0"
+dependencies = [
+ "criterion",
+ "pest",
+ "pest_derive",
+ "pyo3",
+]
+
+[[package]]
+name = "anes"
+version = "0.1.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299"
+
+[[package]]
+name = "atty"
+version = "0.2.14"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
+dependencies = [
+ "hermit-abi 0.1.19",
+ "libc",
+ "winapi",
+]
+
[[package]]
name = "autocfg"
version = "1.1.0"
@@ -24,13 +51,16 @@ dependencies = [
]
[[package]]
-name = "brute_lower_bounds"
-version = "0.1.0"
-dependencies = [
- "pest",
- "pest_derive",
- "pyo3",
-]
+name = "bumpalo"
+version = "3.12.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0d261e256854913907f67ed06efbc3338dfe6179796deefc1ff763fc1aee5535"
+
+[[package]]
+name = "cast"
+version = "0.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5"
[[package]]
name = "cfg-if"
@@ -38,6 +68,54 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
+[[package]]
+name = "ciborium"
+version = "0.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b0c137568cc60b904a7724001b35ce2630fd00d5d84805fbb608ab89509d788f"
+dependencies = [
+ "ciborium-io",
+ "ciborium-ll",
+ "serde",
+]
+
+[[package]]
+name = "ciborium-io"
+version = "0.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "346de753af073cc87b52b2083a506b38ac176a44cfb05497b622e27be899b369"
+
+[[package]]
+name = "ciborium-ll"
+version = "0.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "213030a2b5a4e0c0892b6652260cf6ccac84827b83a85a534e178e3906c4cf1b"
+dependencies = [
+ "ciborium-io",
+ "half",
+]
+
+[[package]]
+name = "clap"
+version = "3.2.23"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "71655c45cb9845d3270c9d6df84ebe72b4dad3c2ba3f7023ad47c144e4e473a5"
+dependencies = [
+ "bitflags",
+ "clap_lex",
+ "indexmap",
+ "textwrap",
+]
+
+[[package]]
+name = "clap_lex"
+version = "0.2.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2850f2f5a82cbf437dd5af4d49848fbdfc27c157c3d010345776f952765261c5"
+dependencies = [
+ "os_str_bytes",
+]
+
[[package]]
name = "cpufeatures"
version = "0.2.5"
@@ -47,6 +125,85 @@ dependencies = [
"libc",
]
+[[package]]
+name = "criterion"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e7c76e09c1aae2bc52b3d2f29e13c6572553b30c4aa1b8a49fd70de6412654cb"
+dependencies = [
+ "anes",
+ "atty",
+ "cast",
+ "ciborium",
+ "clap",
+ "criterion-plot",
+ "itertools",
+ "lazy_static",
+ "num-traits",
+ "oorandom",
+ "plotters",
+ "rayon",
+ "regex",
+ "serde",
+ "serde_derive",
+ "serde_json",
+ "tinytemplate",
+ "walkdir",
+]
+
+[[package]]
+name = "criterion-plot"
+version = "0.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6b50826342786a51a89e2da3a28f1c32b06e387201bc2d19791f622c673706b1"
+dependencies = [
+ "cast",
+ "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"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "715e8152b692bba2d374b53d4875445368fdf21a94751410af607a5ac677d1fc"
+dependencies = [
+ "cfg-if",
+ "crossbeam-epoch",
+ "crossbeam-utils",
+]
+
+[[package]]
+name = "crossbeam-epoch"
+version = "0.9.13"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "01a9af1f4c2ef74bb8aa1f7e19706bc72d03598c8a570bb5de72243c7a9d9d5a"
+dependencies = [
+ "autocfg",
+ "cfg-if",
+ "crossbeam-utils",
+ "memoffset 0.7.1",
+ "scopeguard",
+]
+
+[[package]]
+name = "crossbeam-utils"
+version = "0.8.14"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4fb766fa798726286dbbb842f174001dab8abc7b627a1dd86e0b7222a95d929f"
+dependencies = [
+ "cfg-if",
+]
+
[[package]]
name = "crypto-common"
version = "0.1.6"
@@ -67,6 +224,12 @@ dependencies = [
"crypto-common",
]
+[[package]]
+name = "either"
+version = "1.8.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91"
+
[[package]]
name = "generic-array"
version = "0.14.6"
@@ -77,12 +240,82 @@ dependencies = [
"version_check",
]
+[[package]]
+name = "half"
+version = "1.8.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "eabb4a44450da02c90444cf74558da904edde8fb4e9035a9a6a4e15445af0bd7"
+
+[[package]]
+name = "hashbrown"
+version = "0.12.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
+
+[[package]]
+name = "hermit-abi"
+version = "0.1.19"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
+dependencies = [
+ "libc",
+]
+
+[[package]]
+name = "hermit-abi"
+version = "0.2.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ee512640fe35acbfb4bb779db6f0d80704c2cacfa2e39b601ef3e3f47d1ae4c7"
+dependencies = [
+ "libc",
+]
+
+[[package]]
+name = "indexmap"
+version = "1.9.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1885e79c1fc4b10f0e172c475f458b7f7b93061064d98c3293e98c5ba0c8b399"
+dependencies = [
+ "autocfg",
+ "hashbrown",
+]
+
[[package]]
name = "indoc"
version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bfa799dd5ed20a7e349f3b4639aa80d74549c81716d9ec4f994c9b5815598306"
+[[package]]
+name = "itertools"
+version = "0.10.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473"
+dependencies = [
+ "either",
+]
+
+[[package]]
+name = "itoa"
+version = "1.0.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "fad582f4b9e86b6caa621cabeb0963332d92eea04729ab12892c2533951e6440"
+
+[[package]]
+name = "js-sys"
+version = "0.3.61"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "445dde2150c55e483f3d8416706b97ec8e8237c307e5b7b4b8dd15e6af2a0730"
+dependencies = [
+ "wasm-bindgen",
+]
+
+[[package]]
+name = "lazy_static"
+version = "1.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
+
[[package]]
name = "libc"
version = "0.2.139"
@@ -99,6 +332,24 @@ dependencies = [
"scopeguard",
]
+[[package]]
+name = "log"
+version = "0.4.17"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e"
+dependencies = [
+ "cfg-if",
+]
+
+[[package]]
+name = "memoffset"
+version = "0.7.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5de893c32cde5f383baa4c04c5d6dbdd735cfd4a794b0debdb2bb1b421da5ff4"
+dependencies = [
+ "autocfg",
+]
+
[[package]]
name = "memoffset"
version = "0.8.0"
@@ -108,12 +359,43 @@ dependencies = [
"autocfg",
]
+[[package]]
+name = "num-traits"
+version = "0.2.15"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd"
+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.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3"
+[[package]]
+name = "oorandom"
+version = "11.1.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0ab1bc2a289d34bd04a330323ac98a1b4bc82c9d9fcb1e66b63caa84da26b575"
+
+[[package]]
+name = "os_str_bytes"
+version = "6.4.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9b7820b9daea5457c9f21c69448905d723fbd21136ccf521748f23fd49e723ee"
+
[[package]]
name = "parking_lot"
version = "0.12.1"
@@ -181,6 +463,34 @@ dependencies = [
"sha2",
]
+[[package]]
+name = "plotters"
+version = "0.3.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2538b639e642295546c50fcd545198c9d64ee2a38620a628724a3b266d5fbf97"
+dependencies = [
+ "num-traits",
+ "plotters-backend",
+ "plotters-svg",
+ "wasm-bindgen",
+ "web-sys",
+]
+
+[[package]]
+name = "plotters-backend"
+version = "0.3.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "193228616381fecdc1224c62e96946dfbc73ff4384fba576e052ff8c1bea8142"
+
+[[package]]
+name = "plotters-svg"
+version = "0.3.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f9a81d2759aae1dae668f783c308bc5c8ebd191ff4184aaa1b37f65a6ae5a56f"
+dependencies = [
+ "plotters-backend",
+]
+
[[package]]
name = "proc-macro2"
version = "1.0.51"
@@ -199,7 +509,7 @@ dependencies = [
"cfg-if",
"indoc",
"libc",
- "memoffset",
+ "memoffset 0.8.0",
"parking_lot",
"pyo3-build-config",
"pyo3-ffi",
@@ -259,6 +569,28 @@ dependencies = [
"proc-macro2",
]
+[[package]]
+name = "rayon"
+version = "1.6.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6db3a213adf02b3bcfd2d3846bb41cb22857d131789e01df434fb7e7bc0759b7"
+dependencies = [
+ "either",
+ "rayon-core",
+]
+
+[[package]]
+name = "rayon-core"
+version = "1.10.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "356a0625f1954f730c0201cdab48611198dc6ce21f4acff55089b5a78e6e835b"
+dependencies = [
+ "crossbeam-channel",
+ "crossbeam-deque",
+ "crossbeam-utils",
+ "num_cpus",
+]
+
[[package]]
name = "redox_syscall"
version = "0.2.16"
@@ -268,12 +600,73 @@ dependencies = [
"bitflags",
]
+[[package]]
+name = "regex"
+version = "1.7.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "48aaa5748ba571fb95cd2c85c09f629215d3a6ece942baa100950af03a34f733"
+dependencies = [
+ "regex-syntax",
+]
+
+[[package]]
+name = "regex-syntax"
+version = "0.6.28"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848"
+
+[[package]]
+name = "ryu"
+version = "1.0.12"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7b4b9743ed687d4b4bcedf9ff5eaa7398495ae14e61cba0a295704edbc7decde"
+
+[[package]]
+name = "same-file"
+version = "1.0.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"
+dependencies = [
+ "winapi-util",
+]
+
[[package]]
name = "scopeguard"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
+[[package]]
+name = "serde"
+version = "1.0.152"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bb7d1f0d3021d347a83e556fc4683dea2ea09d87bccdf88ff5c12545d89d5efb"
+dependencies = [
+ "serde_derive",
+]
+
+[[package]]
+name = "serde_derive"
+version = "1.0.152"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "af487d118eecd09402d70a5d72551860e788df87b464af30e5ea6a38c75c541e"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
+name = "serde_json"
+version = "1.0.93"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cad406b69c91885b5107daf2c29572f6c8cdb3c66826821e286c533490c0bc76"
+dependencies = [
+ "itoa",
+ "ryu",
+ "serde",
+]
+
[[package]]
name = "sha2"
version = "0.10.6"
@@ -308,6 +701,12 @@ version = "0.12.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ae9980cab1db3fceee2f6c6f643d5d8de2997c58ee8d25fb0cc8a9e9e7348e5"
+[[package]]
+name = "textwrap"
+version = "0.16.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "222a222a5bfe1bba4a77b45ec488a741b3cb8872e5e499451fd7d0129c9c7c3d"
+
[[package]]
name = "thiserror"
version = "1.0.38"
@@ -328,6 +727,16 @@ dependencies = [
"syn",
]
+[[package]]
+name = "tinytemplate"
+version = "1.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "be4d6b5f19ff7664e8c98d03e2139cb510db9b0a60b55f8e8709b689d939b6bc"
+dependencies = [
+ "serde",
+ "serde_json",
+]
+
[[package]]
name = "typenum"
version = "1.16.0"
@@ -358,6 +767,112 @@ version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
+[[package]]
+name = "walkdir"
+version = "2.3.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "808cf2735cd4b6866113f648b791c6adc5714537bc222d9347bb203386ffda56"
+dependencies = [
+ "same-file",
+ "winapi",
+ "winapi-util",
+]
+
+[[package]]
+name = "wasm-bindgen"
+version = "0.2.84"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "31f8dcbc21f30d9b8f2ea926ecb58f6b91192c17e9d33594b3df58b2007ca53b"
+dependencies = [
+ "cfg-if",
+ "wasm-bindgen-macro",
+]
+
+[[package]]
+name = "wasm-bindgen-backend"
+version = "0.2.84"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "95ce90fd5bcc06af55a641a86428ee4229e44e07033963a2290a8e241607ccb9"
+dependencies = [
+ "bumpalo",
+ "log",
+ "once_cell",
+ "proc-macro2",
+ "quote",
+ "syn",
+ "wasm-bindgen-shared",
+]
+
+[[package]]
+name = "wasm-bindgen-macro"
+version = "0.2.84"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4c21f77c0bedc37fd5dc21f897894a5ca01e7bb159884559461862ae90c0b4c5"
+dependencies = [
+ "quote",
+ "wasm-bindgen-macro-support",
+]
+
+[[package]]
+name = "wasm-bindgen-macro-support"
+version = "0.2.84"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2aff81306fcac3c7515ad4e177f521b5c9a15f2b08f4e32d823066102f35a5f6"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+ "wasm-bindgen-backend",
+ "wasm-bindgen-shared",
+]
+
+[[package]]
+name = "wasm-bindgen-shared"
+version = "0.2.84"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0046fef7e28c3804e5e38bfa31ea2a0f73905319b677e57ebe37e49358989b5d"
+
+[[package]]
+name = "web-sys"
+version = "0.3.61"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e33b99f4b23ba3eec1a53ac264e35a755f00e966e0065077d6027c0f575b0b97"
+dependencies = [
+ "js-sys",
+ "wasm-bindgen",
+]
+
+[[package]]
+name = "winapi"
+version = "0.3.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
+dependencies = [
+ "winapi-i686-pc-windows-gnu",
+ "winapi-x86_64-pc-windows-gnu",
+]
+
+[[package]]
+name = "winapi-i686-pc-windows-gnu"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
+
+[[package]]
+name = "winapi-util"
+version = "0.1.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
+dependencies = [
+ "winapi",
+]
+
+[[package]]
+name = "winapi-x86_64-pc-windows-gnu"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
+
[[package]]
name = "windows-sys"
version = "0.45.0"
diff --git a/Cargo.toml b/Cargo.toml
index 9720086..a022880 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,14 +1,21 @@
[package]
-name = "brute_lower_bounds"
+name = "adversary"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
name = "adversary"
-crate-type = ["cdylib"]
+crate-type = ["cdylib", "rlib"]
[dependencies]
pest = "2.5.5"
pest_derive = "2.5.5"
pyo3 = { version = "0.18.0", features = ["extension-module"] }
+
+[dev-dependencies]
+criterion = "0.4.0"
+
+[[bench]]
+name = "search"
+harness = false
\ No newline at end of file
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..e4974ad
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,11 @@
+criterion:
+ cargo bench
+
+flamegraph:
+ cargo flamegraph --bench search -- --bench
+
+flamegraph-debug:
+ cargo flamegraph --dev -o debug_flamegraph.svg --bench search -- --bench
+
+
+.PHONY: criterion flamegraph flamegraph-debug
\ No newline at end of file
diff --git a/benches/search.rs b/benches/search.rs
new file mode 100644
index 0000000..69a0957
--- /dev/null
+++ b/benches/search.rs
@@ -0,0 +1,23 @@
+#[macro_use]
+extern crate criterion;
+extern crate adversary;
+
+use criterion::{criterion_group, criterion_main, Criterion};
+
+pub fn criterion_benchmark(c: &mut Criterion) {
+ pyo3::prepare_freethreaded_python();
+ pyo3::Python::with_gil(|_py| {
+ let obj = adversary::Prover::py_new(
+ "= (ham x) k".to_string(),
+ "= (ham y) (+ k 1)".to_string(),
+ "<= ham (^ x y) p".to_string(),
+ )
+ .unwrap();
+ c.bench_function("find_bounds", |b| {
+ b.iter(|| criterion::black_box(obj.find_bounds(10, 5, 3)));
+ });
+ })
+}
+
+criterion_group!(benches, criterion_benchmark);
+criterion_main!(benches);
diff --git a/flamegraph.svg b/flamegraph.svg
new file mode 100644
index 0000000..92b6ff6
--- /dev/null
+++ b/flamegraph.svg
@@ -0,0 +1,491 @@
+
\ No newline at end of file
diff --git a/pyproject.toml b/pyproject.toml
index e850234..2ca1cd0 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -3,7 +3,7 @@ requires = ["maturin>=0.14,<0.15"]
build-backend = "maturin"
[project]
-name = "brute_lower_bounds"
+name = "adversary"
requires-python = ">=3.7"
classifiers = [
"Programming Language :: Rust",
diff --git a/src/lib.rs b/src/lib.rs
index 9887ca2..a79e3f0 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -12,7 +12,7 @@ use vm::VmCode;
const CACHE_SIZE_LIMIT: usize = 10_000_000;
#[pyclass]
-struct BoundsResult {
+pub struct BoundsResult {
#[pyo3(get)]
min_x_relations: u64,
#[pyo3(get)]
@@ -32,7 +32,7 @@ struct BoundsResult {
}
#[pyclass]
-struct Prover {
+pub struct Prover {
a_description: VmCode,
b_description: VmCode,
relationship: VmCode,
@@ -41,9 +41,9 @@ struct Prover {
struct CachedSetIterator<'code> {
cache: Vec,
filter: &'code VmCode,
- n: u8,
- p: u8,
- k: u8,
+ n: u32,
+ p: u32,
+ k: u32,
cached: bool,
counter: u64,
cache_counter: usize,
@@ -52,7 +52,7 @@ struct CachedSetIterator<'code> {
}
impl<'code> CachedSetIterator<'code> {
- fn create_x(filter: &'code VmCode, n: u8, p: u8, k: u8) -> Self {
+ fn create_x(filter: &'code VmCode, n: u32, p: u32, k: u32) -> Self {
CachedSetIterator {
cache: Vec::with_capacity(CACHE_SIZE_LIMIT),
filter,
@@ -67,7 +67,7 @@ impl<'code> CachedSetIterator<'code> {
}
}
- fn create_y(filter: &'code VmCode, n: u8, p: u8, k: u8) -> Self {
+ fn create_y(filter: &'code VmCode, n: u32, p: u32, k: u32) -> Self {
CachedSetIterator {
cache: Vec::with_capacity(CACHE_SIZE_LIMIT),
filter,
@@ -106,40 +106,44 @@ impl<'code> Iterator for &mut CachedSetIterator<'code> {
self.reset();
return None;
}
-
- if self.counter == self.top {
- self.cached = true;
- self.reset();
- return None;
}
- let included = if self.is_x {
- vm::Vm::load(
- self.filter,
- vm::Registers::load(self.counter, 0, self.n, self.p, self.k),
- )
- .run()
- .unwrap_bool()
- } else {
- vm::Vm::load(
- self.filter,
- vm::Registers::load(0, self.counter, self.n, self.p, self.k),
- )
- .run()
- .unwrap_bool()
- };
+ loop {
+ if self.counter == self.top {
+ self.cached = true;
+ self.reset();
+ return None;
+ }
- self.counter += 1;
- return Some(result);
+ let included = if self.is_x {
+ vm::Vm::load(
+ self.filter,
+ vm::Registers::load(self.counter, 0, self.n, self.p, self.k),
+ )
+ .run()
+ .unwrap_bool()
+ } else {
+ vm::Vm::load(
+ self.filter,
+ vm::Registers::load(0, self.counter, self.n, self.p, self.k),
+ )
+ .run()
+ .unwrap_bool()
+ };
+
+ if !included {
+ self.counter += 1;
+ continue;
+ } else {
+ let result = self.counter;
+ if self.cache.len() < CACHE_SIZE_LIMIT {
+ self.cache.push(result);
+ }
+
+ self.counter += 1;
+ return Some(result);
+ }
}
-
- let result = self.counter;
- if self.cache.len() < CACHE_SIZE_LIMIT {
- self.cache.push(result);
- }
-
- self.counter += 1;
- Some(result)
}
}
@@ -152,7 +156,7 @@ struct FixedHammingWeight {
}
impl FixedHammingWeight {
- fn new(bits: u8, ones: u8) -> Self {
+ fn new(bits: u32, ones: u32) -> Self {
FixedHammingWeight {
permutation: (1 << ones) - 1,
top: ((1 << ones) - 1) << (bits - ones),
@@ -183,11 +187,11 @@ impl Iterator for FixedHammingWeight {
#[pymethods]
impl Prover {
/// Initializes a searcher for lower bounds.
- ///
+ ///
/// This object prepares the search for lower bounds according to Ambainis et al.'s
/// adversary method. The bounds themselves for specific values of the parameters can
/// be found with the associated method `find_bounds`.
- ///
+ ///
/// Arguments:
/// a_description A description of elements that should belong to the A set,
/// as specified discriminating on x [string]
@@ -197,7 +201,7 @@ impl Prover {
/// depending on x and y, and resolving to true if x is related to
/// y [string]
#[new]
- fn py_new(
+ pub fn py_new(
a_description: String,
b_description: String,
relationship: String,
@@ -263,13 +267,13 @@ impl Prover {
}
/// Finds a lower bound to the query complexity according to the specified parameters.
- ///
+ ///
/// Arguments:
/// n The maximum number of bits to consider when exhausting bitstrings
/// belonging to either of the specified sets [int]
/// p The number of parallel queries [int]
/// k Parameter accepted in set specifications and relations [int]
- fn find_bounds(&self, n: u8, p: u8, k: u8) -> PyResult {
+ pub fn find_bounds(&self, n: u32, p: u32, k: u32) -> PyResult {
if n > 63 {
return Err(pyo3::exceptions::PyValueError::new_err(
"More than 63 bits is not supported. (Because of `n` parameter.)",
@@ -299,12 +303,6 @@ impl Prover {
let mut joint_bounding_y = 0_u64;
let mut bounding_window = 0_u64;
- let related = |x: u64, y: u64| {
- vm::Vm::load(&self.relationship, vm::Registers::load(x, y, n, p, k))
- .run()
- .unwrap_bool()
- };
-
for window in window_iterator {
let mut max_x_relations = 0_u64;
let mut max_y_relations = 0_u64;
@@ -316,9 +314,15 @@ impl Prover {
let mut max_x_relations_candidate = 0_u64;
for y in &mut b_set_iterator {
- if related(x, y) {
+ let related = vm::Vm::load(
+ &self.relationship,
+ vm::Registers::load(x, y, n, p, k),
+ )
+ .run()
+ .unwrap_bool();
+ if related {
min_x_relations_candidate += 1;
-
+
if (x & window) != (y & window) {
max_x_relations_candidate += 1;
}
@@ -340,7 +344,11 @@ impl Prover {
let mut max_y_relations_candidate = 0_u64;
for x in &mut a_set_iterator {
- if related(x, y) {
+ let related =
+ vm::Vm::load(&self.relationship, vm::Registers::load(x, y, n, p, k))
+ .run()
+ .unwrap_bool();
+ if related {
min_y_relations_candidate += 1;
if (x & window) != (y & window) {
@@ -366,6 +374,8 @@ impl Prover {
joint_bounding_x = bounding_x_candidate;
joint_bounding_y = bounding_y_candidate;
bounding_window = window;
+
+ Python::with_gil(|py| py.check_signals())?;
}
}
@@ -414,3 +424,17 @@ fn test_hamming_strings() {
assert!(x.count_ones() <= 3);
}
}
+
+#[test]
+fn test_full_run() {
+ pyo3::prepare_freethreaded_python();
+ pyo3::Python::with_gil(|_py| {
+ let obj = Prover::py_new(
+ "= (ham x) k".to_string(),
+ "= (ham y) (+ k 1)".to_string(),
+ "<= ham (^ x y) p".to_string(),
+ )
+ .unwrap();
+ std::hint::black_box(obj.find_bounds(10, 5, 3)).expect("Success");
+ })
+}
\ No newline at end of file
diff --git a/src/vm/mod.rs b/src/vm/mod.rs
index bdd4e25..991a53d 100644
--- a/src/vm/mod.rs
+++ b/src/vm/mod.rs
@@ -21,9 +21,9 @@ pub struct Vm<'code> {
pub struct Registers {
x: i64,
y: i64,
- n: u32,
- p: u32,
- k: u32,
+ n: i64,
+ p: i64,
+ k: i64,
}
#[derive(Debug)]
@@ -62,6 +62,7 @@ impl VmOutput {
}
}
+ #[allow(unused)]
pub fn unwrap_arithmetic(self) -> f64 {
match self {
VmOutput::Boolean(_) => panic!("Expected arithmetic, got boolean value."),
@@ -74,13 +75,13 @@ impl VmOutput {
}
impl Registers {
- pub fn load(x: u64, y: u64, n: u8, p: u8, k: u8) -> Self {
+ pub fn load(x: u64, y: u64, n: u32, p: u32, k: u32) -> Self {
Self {
x: x as i64,
y: y as i64,
- n: n as u32,
- p: p as u32,
- k: k as u32,
+ n: n as i64,
+ p: p as i64,
+ k: k as i64,
}
}
}
@@ -91,6 +92,7 @@ pub fn compile_boolean(expression: ast::BooleanExpression) -> VmCode {
VmCode { code }
}
+#[allow(unused)]
pub fn compile_arithmetic(expression: ast::ArithmeticExpression) -> VmCode {
let mut code = Vec::::new();
compile_expression(Expression::Arithmetic(expression), &mut code);
@@ -334,9 +336,9 @@ impl<'code> Vm<'code> {
VmOutput::Arithmetic(ArithmeticValue::Integer(match var {
ast::Variable::X => registers.x,
ast::Variable::Y => registers.y,
- ast::Variable::N => registers.n as i64,
- ast::Variable::P => registers.p as i64,
- ast::Variable::K => registers.k as i64,
+ ast::Variable::N => registers.n,
+ ast::Variable::P => registers.p,
+ ast::Variable::K => registers.k,
}))
}
OpCode::Literal(lit) => VmOutput::Arithmetic(ArithmeticValue::Integer(*lit)),
diff --git a/test/bounds.py b/test/bounds.py
new file mode 100644
index 0000000..ec707b8
--- /dev/null
+++ b/test/bounds.py
@@ -0,0 +1,13 @@
+import adversary
+from math import sqrt
+
+prover = adversary.Prover("= (ham x) k", "= (ham y) (+ k 1)", "<= ham (^ x y) p")
+bounds = prover.find_bounds(16, 5, 3);
+lower_bound = sqrt(bounds.min_x_relations * bounds.min_y_relations / bounds.max_joint_relations)
+print("m =", bounds.min_x_relations)
+print(f"From x={bounds.single_bounding_x:b}")
+print("m' =", bounds.min_y_relations)
+print(f"From y={bounds.single_bounding_y:b}")
+print("ℓ·ℓ' =", bounds.max_joint_relations)
+print(f"From x={bounds.joint_bounding_x:b} and y={bounds.joint_bounding_y:b} and window={bounds.bounding_window:b}")
+print(f"Query lower bounds = {lower_bound}")
\ No newline at end of file