Rust bindings (#4)

* working build

* Move bindings.rs to root directory

* tidy build script

* Add initial rust safe bindings

* import fewer c stuff in bindings

* remove unnecessary blst definitions in bindings

* remove bindgen build dependency

* improve interface

* Remove more stuff from bindings

* Add a simple test

* Update error type

* Update verify_kzg_proof interface

* Return array instead of vec

* link with no-pie

* impl Send and Sync for KzgSettings; change mut pointers to const

* Remove no-pie linking

* Make FIELD_ELEMENTS_PER_BLOB a compile time variable

* Add load_trusted_setup method

* Cleanup

* Add failure case to bytes_to_bls_field

* Fix conditional compilation and ensure canonical blobs in test

* Add test vectors

* Remove blobk_commitment test case

* Change function signature

* Copy and delete instead of rename in build script

* Make consts public

* Add error conditions for invalid trusted setup

* Lowercase renamings

* Make blob public

* Add benchmarks

* Run test vectors only for mainnet spec

* Add README

* clippy

* Add rust CI checks

* actually add CI checks

* Fix CI

* Fix workflow again
This commit is contained in:
Pawan Dhananjay 2023-01-12 23:20:12 +05:30 committed by GitHub
parent 42a4419641
commit 2c151d7f7e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 1655 additions and 0 deletions

View File

@ -0,0 +1,31 @@
name: Rust bindings tests
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
test-rust-bindings:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
submodules: recursive
- name: Get latest version of stable rust
run: |
rustup update stable
- name: Build and Test (mainnet preset)
run: |
cd bindings/rust
cargo clean
cargo test --all --release --tests
- name: Build and Test (minimal preset)
run: |
cd bindings/rust
cargo clean
cargo test --all --release --features="minimal-spec" --tests

1
.gitignore vendored
View File

@ -6,6 +6,7 @@ inc/blst_aux.h*
.vscode/
.clang-format
*bindings/*/*.so
*bindings/rust/target
*bindings/csharp/*.exe
*bindings/csharp/*.dll
__pycache__

2
bindings/rust/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
src/consts.rs
target/

658
bindings/rust/Cargo.lock generated Normal file
View File

@ -0,0 +1,658 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[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"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "bumpalo"
version = "3.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "572f695136211188308f16ad2ca5c851a712c464060ae6974944458eb83880ba"
[[package]]
name = "c-kzg"
version = "0.1.0"
dependencies = [
"criterion",
"hex",
"libc",
"rand",
"serde_json",
]
[[package]]
name = "cast"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5"
[[package]]
name = "cfg-if"
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 = "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",
"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 = "either"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797"
[[package]]
name = "getrandom"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[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 = "hex"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
[[package]]
name = "indexmap"
version = "1.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1885e79c1fc4b10f0e172c475f458b7f7b93061064d98c3293e98c5ba0c8b399"
dependencies = [
"autocfg",
"hashbrown",
]
[[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.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4217ad341ebadf8d8e724e264f13e593e0648f5b3e94b3896a5df283be015ecc"
[[package]]
name = "js-sys"
version = "0.3.60"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49409df3e3bf0856b916e2ceaca09ee28e6871cf7d9ce97a692cacfdb2a25a47"
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.137"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc7fcc620a3bff7cdd7a365be3376c97191aeaccc2a603e600951e452615bf89"
[[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 = "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.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6f61fba1741ea2b3d6a1e3178721804bb716a68a6aeba1149b5d52e3d464ea66"
[[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 = "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 = "ppv-lite86"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
[[package]]
name = "proc-macro2"
version = "1.0.49"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57a8eca9f9c4ffde41714334dee777596264c7825420f521abc92b5b5deb63a5"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8856d8364d252a14d474036ea1358d63c9e6965c8e5c1885c18f73d70bff9c7b"
dependencies = [
"proc-macro2",
]
[[package]]
name = "rand"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
]
[[package]]
name = "rand_chacha"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
dependencies = [
"getrandom",
]
[[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.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cac410af5d00ab6884528b4ab69d1e8e146e8d471201800fa1b4524126de6ad3"
dependencies = [
"crossbeam-channel",
"crossbeam-deque",
"crossbeam-utils",
"num_cpus",
]
[[package]]
name = "regex"
version = "1.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e076559ef8e241f2ae3479e36f97bd5741c0330689e217ad51ce2c76808b868a"
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.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09"
[[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.150"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e326c9ec8042f1b5da33252c8a37e9ffbd2c9bef0155215b6e6c80c790e05f91"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.150"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42a3df25b0713732468deadad63ab9da1f1fd75a48a15024b50363f128db627e"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.89"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "020ff22c755c2ed3f8cf162dbb41a7268d934702f3ed3631656ea597e08fc3db"
dependencies = [
"itoa",
"ryu",
"serde",
]
[[package]]
name = "syn"
version = "1.0.107"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1f4064b5b16e03ae50984a5a8ed5d4f8803e6bc1fd170a3cda91a1be4b18e3f5"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "textwrap"
version = "0.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "222a222a5bfe1bba4a77b45ec488a741b3cb8872e5e499451fd7d0129c9c7c3d"
[[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 = "unicode-ident"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "84a22b9f218b40614adcb3f4ff08b703773ad44fa9423e4e0d346d5db86e4ebc"
[[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 = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "wasm-bindgen"
version = "0.2.83"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eaf9f5aceeec8be17c128b2e93e031fb8a4d469bb9c4ae2d7dc1888b26887268"
dependencies = [
"cfg-if",
"wasm-bindgen-macro",
]
[[package]]
name = "wasm-bindgen-backend"
version = "0.2.83"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c8ffb332579b0557b52d268b91feab8df3615f265d5270fec2a8c95b17c1142"
dependencies = [
"bumpalo",
"log",
"once_cell",
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.83"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "052be0f94026e6cbc75cdefc9bae13fd6052cdcaf532fa6c45e7ae33a1e6c810"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
]
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.83"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "07bc0c051dc5f23e307b13285f9d75df86bfdf816c5721e573dec1f9b8aa193c"
dependencies = [
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.83"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1c38c045535d93ec4f0b4defec448e4291638ee608530863b1e2ba115d4fff7f"
[[package]]
name = "web-sys"
version = "0.3.60"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bcda906d8be16e728fd5adc5b729afad4e444e106ab28cd1c7256e54fa61510f"
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"

24
bindings/rust/Cargo.toml Normal file
View File

@ -0,0 +1,24 @@
[package]
name = "c-kzg"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features]
default = ["mainnet-spec"]
mainnet-spec = []
minimal-spec = []
[dependencies]
libc = "0.2"
hex = "0.4.2"
[dev-dependencies]
rand = "0.8.5"
serde_json = "1.0.89"
criterion = "0.4"
[[bench]]
name = "kzg_benches"
harness = false

23
bindings/rust/README.md Normal file
View File

@ -0,0 +1,23 @@
# Rust bindings
Generates the rust bindings for the c-kzg library.
## Build
```
cargo build --release
```
Build with `--features="minimal-spec"` to set the `FIELD_ELEMENTS_PER_BLOB` compile time parameter to the pre-determined minimal spec value.
## Test
```
cargo test --release
```
## Benchmark
```
cargo bench
```

View File

@ -0,0 +1,65 @@
use std::path::PathBuf;
use c_kzg::*;
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use rand::{rngs::ThreadRng, Rng};
use std::sync::Arc;
fn generate_random_blob_for_bench(rng: &mut ThreadRng) -> Blob {
let mut arr: Blob = [0; BYTES_PER_BLOB];
rng.fill(&mut arr[..]);
// Ensure that the blob is canonical by ensuring that
// each field element contained in the blob is < BLS_MODULUS
for i in 0..FIELD_ELEMENTS_PER_BLOB {
arr[i * BYTES_PER_FIELD_ELEMENT + BYTES_PER_FIELD_ELEMENT - 1] = 0;
}
arr
}
pub fn criterion_benchmark(c: &mut Criterion) {
let mut rng = rand::thread_rng();
let trusted_setup_file = PathBuf::from("../../src/trusted_setup.txt");
assert!(trusted_setup_file.exists());
let kzg_settings = Arc::new(KzgSettings::load_trusted_setup_file(trusted_setup_file).unwrap());
let blob = generate_random_blob_for_bench(&mut rng);
c.bench_function("blob_to_kzg_commitment", |b| {
b.iter(|| KzgCommitment::blob_to_kzg_commitment(blob, &kzg_settings))
});
for num_blobs in [4, 8, 16].iter() {
let mut group = c.benchmark_group("kzg operations");
let blobs: Vec<Blob> = (0..*num_blobs)
.map(|_| generate_random_blob_for_bench(&mut rng))
.collect();
group.bench_with_input(
BenchmarkId::new("compute_aggregate_kzg_proof", *num_blobs),
&blobs,
|b, blobs| b.iter(|| KzgProof::compute_aggregate_kzg_proof(blobs, &kzg_settings)),
);
let kzg_commitments: Vec<KzgCommitment> = blobs
.clone()
.into_iter()
.map(|blob| KzgCommitment::blob_to_kzg_commitment(blob, &kzg_settings))
.collect();
let proof = KzgProof::compute_aggregate_kzg_proof(&blobs, &kzg_settings).unwrap();
group.bench_with_input(
BenchmarkId::new("verify_aggregate_kzg_proof", *num_blobs),
&blobs,
|b, blobs| {
b.iter(|| {
proof
.verify_aggregate_kzg_proof(&blobs, &kzg_commitments, &kzg_settings)
.unwrap()
})
},
);
}
}
criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);

91
bindings/rust/build.rs Normal file
View File

@ -0,0 +1,91 @@
use std::env;
use std::path::{Path, PathBuf};
use std::process::Command;
const MAINNET_FIELD_ELEMENTS_PER_BLOB: usize = 4096;
const MINIMAL_FIELD_ELEMENTS_PER_BLOB: usize = 4;
fn move_file(src: &Path, dst: &Path) -> Result<(), String> {
std::fs::copy(src, dst)
.map_err(|_| format!("Failed to copy {} to {}", src.display(), dst.display()))?;
std::fs::remove_file(src)
.map_err(|_| format!("Failed to remove file {} from source", src.display()))?;
Ok(())
}
fn main() {
let root_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()).join("../../");
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
// Ensure libblst exists in `OUT_DIR`
// Assuming blst submodule exists
Command::new("make")
.current_dir(root_dir.join("src"))
.arg("blst")
.status()
.unwrap();
move_file(
root_dir.join("lib/libblst.a").as_path(),
out_dir.join("libblst.a").as_path(),
)
.unwrap();
let field_elements_per_blob = if cfg!(feature = "minimal-spec") {
MINIMAL_FIELD_ELEMENTS_PER_BLOB
} else {
MAINNET_FIELD_ELEMENTS_PER_BLOB
};
eprintln!("Using FIELD_ELEMENTS_PER_BLOB={}", field_elements_per_blob);
// Deleting any existing assembly and object files to ensure that compiling with a different
// feature flag changes the final linked library file.
let obj_file = root_dir.join("src/c_kzg_4844.o");
if obj_file.exists() {
std::fs::remove_file(obj_file).unwrap();
}
// Ensure libckzg exists in `OUT_DIR`
Command::new("make")
.current_dir(root_dir.join("src"))
.arg("all")
.arg(format!(
"FIELD_ELEMENTS_PER_BLOB={}",
field_elements_per_blob
))
.status()
.unwrap();
Command::new("ar")
.current_dir(&root_dir.join("src"))
.args(["crus", "libckzg.a", "c_kzg_4844.o"])
.status()
.unwrap();
move_file(
root_dir.join("src/libckzg.a").as_path(),
out_dir.join("libckzg.a").as_path(),
)
.unwrap();
println!("cargo:rustc-link-search={}", out_dir.display());
println!("cargo:rustc-link-search={}", out_dir.display());
println!("cargo:rustc-link-lib=static=ckzg");
println!("cargo:rustc-link-lib=static=blst");
// Write the compile time variable to a consts.rs file to be imported to the bindings module.
let const_file = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()).join("src/consts.rs");
std::fs::write(
const_file,
format!(
"pub const FIELD_ELEMENTS_PER_BLOB: usize = {};",
field_elements_per_blob
),
)
.unwrap();
// Cleanup
let obj_file = root_dir.join("src/c_kzg_4844.o");
if obj_file.exists() {
std::fs::remove_file(obj_file).unwrap();
}
}

View File

@ -0,0 +1,293 @@
/* automatically generated by rust-bindgen 0.61.0 */
include!("./consts.rs");
use libc::FILE;
pub const BYTES_PER_COMMITMENT: usize = 48;
pub const BYTES_PER_PROOF: usize = 48;
pub const BYTES_PER_FIELD_ELEMENT: usize = 32;
pub const BYTES_PER_BLOB: usize = FIELD_ELEMENTS_PER_BLOB * BYTES_PER_FIELD_ELEMENT;
pub type byte = u8;
pub type limb_t = u64;
#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct blst_scalar {
pub b: [byte; 32usize],
}
#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct blst_fr {
pub l: [limb_t; 4usize],
}
#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct blst_fp {
pub l: [limb_t; 6usize],
}
#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct blst_fp2 {
pub fp: [blst_fp; 2usize],
}
#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct blst_fp6 {
pub fp2: [blst_fp2; 3usize],
}
#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct blst_fp12 {
pub fp6: [blst_fp6; 2usize],
}
#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct blst_p1 {
pub x: blst_fp,
pub y: blst_fp,
pub z: blst_fp,
}
#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct blst_p1_affine {
pub x: blst_fp,
pub y: blst_fp,
}
#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct blst_p2 {
pub x: blst_fp2,
pub y: blst_fp2,
pub z: blst_fp2,
}
#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct blst_p2_affine {
pub x: blst_fp2,
pub y: blst_fp2,
}
pub const FIAT_SHAMIR_PROTOCOL_DOMAIN: [u8; 16usize] = [
70, 83, 66, 76, 79, 66, 86, 69, 82, 73, 70, 89, 95, 86, 49, 95,
];
pub type g1_t = blst_p1;
pub type g2_t = blst_p2;
pub type fr_t = blst_fr;
pub type KZGCommitment = g1_t;
pub type KZGProof = g1_t;
pub type BLSFieldElement = fr_t;
pub type Blob = [u8; BYTES_PER_BLOB];
#[repr(u32)]
#[doc = " The common return type for all routines in which something can go wrong."]
#[doc = ""]
#[doc = " @warning In the case of @p C_KZG_OK or @p C_KZG_BADARGS, the caller can assume that all memory allocated by the"]
#[doc = " called routines has been deallocated. However, in the case of @p C_KZG_ERROR or @p C_KZG_MALLOC being returned, these"]
#[doc = " are unrecoverable and memory may have been leaked."]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum C_KZG_RET {
#[doc = "< Success!"]
C_KZG_OK = 0,
#[doc = "< The supplied data is invalid in some way"]
C_KZG_BADARGS = 1,
#[doc = "< Internal error - this should never occur and may indicate a bug in the library"]
C_KZG_ERROR = 2,
#[doc = "< Could not allocate memory"]
C_KZG_MALLOC = 3,
}
#[doc = " Stores the setup and parameters needed for performing FFTs."]
#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct FFTSettings {
#[doc = "< The maximum size of FFT these settings support, a power of 2."]
pub max_width: u64,
#[doc = "< Ascending powers of the root of unity, size `width + 1`."]
pub expanded_roots_of_unity: *const fr_t,
#[doc = "< Descending powers of the root of unity, size `width + 1`."]
pub reverse_roots_of_unity: *const fr_t,
#[doc = "< Powers of the root of unity in bit-reversal permutation, size `width`."]
pub roots_of_unity: *const fr_t,
}
#[test]
fn bindgen_test_layout_FFTSettings() {
const UNINIT: ::std::mem::MaybeUninit<FFTSettings> = ::std::mem::MaybeUninit::uninit();
let ptr = UNINIT.as_ptr();
assert_eq!(
::std::mem::size_of::<FFTSettings>(),
32usize,
concat!("Size of: ", stringify!(FFTSettings))
);
assert_eq!(
::std::mem::align_of::<FFTSettings>(),
8usize,
concat!("Alignment of ", stringify!(FFTSettings))
);
assert_eq!(
unsafe { ::std::ptr::addr_of!((*ptr).max_width) as usize - ptr as usize },
0usize,
concat!(
"Offset of field: ",
stringify!(FFTSettings),
"::",
stringify!(max_width)
)
);
assert_eq!(
unsafe { ::std::ptr::addr_of!((*ptr).expanded_roots_of_unity) as usize - ptr as usize },
8usize,
concat!(
"Offset of field: ",
stringify!(FFTSettings),
"::",
stringify!(expanded_roots_of_unity)
)
);
assert_eq!(
unsafe { ::std::ptr::addr_of!((*ptr).reverse_roots_of_unity) as usize - ptr as usize },
16usize,
concat!(
"Offset of field: ",
stringify!(FFTSettings),
"::",
stringify!(reverse_roots_of_unity)
)
);
assert_eq!(
unsafe { ::std::ptr::addr_of!((*ptr).roots_of_unity) as usize - ptr as usize },
24usize,
concat!(
"Offset of field: ",
stringify!(FFTSettings),
"::",
stringify!(roots_of_unity)
)
);
}
#[doc = " Stores the setup and parameters needed for computing KZG proofs."]
#[repr(C)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct KZGSettings {
#[doc = "< The corresponding settings for performing FFTs"]
pub fs: *const FFTSettings,
#[doc = "< G1 group elements from the trusted setup, in Lagrange form bit-reversal permutation"]
pub g1_values: *const g1_t,
#[doc = "< G2 group elements from the trusted setup; both arrays have FIELD_ELEMENTS_PER_BLOB elements"]
pub g2_values: *const g2_t,
}
/// Safety: FFTSettings is initialized once on calling `load_trusted_setup`. After
/// that, the struct is never modified. The memory for the arrays within `FFTSettings` and
/// `g1_values` and `g2_values` are only freed on calling `free_trusted_setup` which only happens
/// when we drop the struct.
unsafe impl Sync for KZGSettings {}
unsafe impl Send for KZGSettings {}
#[test]
fn bindgen_test_layout_KZGSettings() {
const UNINIT: ::std::mem::MaybeUninit<KZGSettings> = ::std::mem::MaybeUninit::uninit();
let ptr = UNINIT.as_ptr();
assert_eq!(
::std::mem::size_of::<KZGSettings>(),
24usize,
concat!("Size of: ", stringify!(KZGSettings))
);
assert_eq!(
::std::mem::align_of::<KZGSettings>(),
8usize,
concat!("Alignment of ", stringify!(KZGSettings))
);
assert_eq!(
unsafe { ::std::ptr::addr_of!((*ptr).fs) as usize - ptr as usize },
0usize,
concat!(
"Offset of field: ",
stringify!(KZGSettings),
"::",
stringify!(fs)
)
);
assert_eq!(
unsafe { ::std::ptr::addr_of!((*ptr).g1_values) as usize - ptr as usize },
8usize,
concat!(
"Offset of field: ",
stringify!(KZGSettings),
"::",
stringify!(g1_values)
)
);
assert_eq!(
unsafe { ::std::ptr::addr_of!((*ptr).g2_values) as usize - ptr as usize },
16usize,
concat!(
"Offset of field: ",
stringify!(KZGSettings),
"::",
stringify!(g2_values)
)
);
}
extern "C" {
#[doc = " Interface functions"]
pub fn bytes_to_g1(out: *mut g1_t, in_: *const u8) -> C_KZG_RET;
}
extern "C" {
pub fn bytes_from_g1(out: *mut u8, in_: *const g1_t);
}
extern "C" {
pub fn bytes_to_bls_field(out: *mut BLSFieldElement, in_: *const u8) -> C_KZG_RET;
}
extern "C" {
pub fn load_trusted_setup_file(out: *mut KZGSettings, in_: *mut FILE) -> C_KZG_RET;
}
extern "C" {
pub fn load_trusted_setup(
out: *mut KZGSettings,
g1_bytes: *const u8, /* n1 * 48 bytes */
n1: usize,
g2_bytes: *const u8, /* n2 * 96 bytes */
n2: usize,
) -> C_KZG_RET;
}
extern "C" {
pub fn free_trusted_setup(s: *mut KZGSettings);
}
extern "C" {
pub fn compute_aggregate_kzg_proof(
out: *mut KZGProof,
blobs: *const u8, // pointer to the first byte in a 2D array ([[u8; BYTES_PER_BLOB]])
n: usize,
s: *const KZGSettings,
) -> C_KZG_RET;
}
extern "C" {
pub fn verify_aggregate_kzg_proof(
out: *mut bool,
blobs: *const u8, // pointer to the first byte in a 2D array ([[u8; BYTES_PER_BLOB]])
expected_kzg_commitments: *const KZGCommitment,
n: usize,
kzg_aggregated_proof: *const KZGProof,
s: *const KZGSettings,
) -> C_KZG_RET;
}
extern "C" {
pub fn blob_to_kzg_commitment(out: *mut KZGCommitment, blob: *mut u8, s: *const KZGSettings);
}
extern "C" {
pub fn verify_kzg_proof(
out: *mut bool,
polynomial_kzg: *const KZGCommitment,
z: *const u8,
y: *const u8,
kzg_proof: *const KZGProof,
s: *const KZGSettings,
) -> C_KZG_RET;
}

428
bindings/rust/src/lib.rs Normal file
View File

@ -0,0 +1,428 @@
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
mod bindings;
use bindings::{g1_t, C_KZG_RET};
use libc::fopen;
use std::ffi::CString;
use std::mem::MaybeUninit;
use std::os::unix::prelude::OsStrExt;
use std::path::PathBuf;
pub use bindings::{
Blob, BYTES_PER_BLOB, BYTES_PER_COMMITMENT, BYTES_PER_FIELD_ELEMENT, BYTES_PER_PROOF,
FIAT_SHAMIR_PROTOCOL_DOMAIN, FIELD_ELEMENTS_PER_BLOB,
};
pub const BYTES_PER_G1_POINT: usize = 48;
pub const BYTES_PER_G2_POINT: usize = 96;
/// Number of G2 points required for the kzg trusted setup.
/// 65 is fixed and is used for providing multiproofs up to 64 field elements.
const NUM_G2_POINTS: usize = 65;
#[derive(Debug)]
pub enum Error {
/// The KZG proof is invalid.
InvalidKzgProof(String),
/// The KZG commitment is invalid.
InvalidKzgCommitment(String),
/// The provided trusted setup is invalid.
InvalidTrustedSetup(String),
/// The underlying c-kzg library returned an error.
CError(C_KZG_RET),
}
pub fn bytes_to_g1(bytes: &[u8]) -> Result<g1_t, Error> {
let mut g1_point = MaybeUninit::<g1_t>::uninit();
unsafe {
let res = bindings::bytes_to_g1(g1_point.as_mut_ptr(), bytes.as_ptr());
if let C_KZG_RET::C_KZG_OK = res {
Ok(g1_point.assume_init())
} else {
Err(Error::CError(res))
}
}
}
pub fn bytes_from_g1(g1_point: g1_t) -> [u8; BYTES_PER_G1_POINT] {
let mut bytes = [0; 48];
unsafe { bindings::bytes_from_g1(bytes.as_mut_ptr(), &g1_point) }
bytes
}
#[derive(Debug, Clone, Copy)]
pub struct BlsFieldElement(bindings::BLSFieldElement);
impl BlsFieldElement {
pub fn bytes_to_bls_field(bytes: [u8; BYTES_PER_FIELD_ELEMENT]) -> Result<Self, Error> {
let mut bls_field_element = MaybeUninit::<bindings::BLSFieldElement>::uninit();
unsafe {
let res = bindings::bytes_to_bls_field(bls_field_element.as_mut_ptr(), bytes.as_ptr());
if let C_KZG_RET::C_KZG_OK = res {
Ok(Self(bls_field_element.assume_init()))
} else {
Err(Error::CError(res))
}
}
}
}
/// Holds the parameters of a kzg trusted setup ceremony.
pub struct KzgSettings(bindings::KZGSettings);
impl KzgSettings {
/// Initializes a trusted setup from `FIELD_ELEMENTS_PER_BLOB` g1 points
/// and 65 g2 points in byte format.
pub fn load_trusted_setup(
g1_bytes: Vec<[u8; BYTES_PER_G1_POINT]>,
g2_bytes: Vec<[u8; BYTES_PER_G2_POINT]>,
) -> Result<Self, Error> {
if g1_bytes.len() != FIELD_ELEMENTS_PER_BLOB {
return Err(Error::InvalidTrustedSetup(format!(
"Invalid number of g1 points in trusted setup. Expected {} got {}",
FIELD_ELEMENTS_PER_BLOB,
g1_bytes.len()
)));
}
if g2_bytes.len() != NUM_G2_POINTS {
return Err(Error::InvalidTrustedSetup(format!(
"Invalid number of g2 points in trusted setup. Expected {} got {}",
NUM_G2_POINTS,
g2_bytes.len()
)));
}
let mut kzg_settings = MaybeUninit::<bindings::KZGSettings>::uninit();
unsafe {
let n1 = g1_bytes.len();
let n2 = g2_bytes.len();
let res = bindings::load_trusted_setup(
kzg_settings.as_mut_ptr(),
g1_bytes.as_ptr() as *const u8,
n1,
g2_bytes.as_ptr() as *const u8,
n2,
);
if let C_KZG_RET::C_KZG_OK = res {
Ok(Self(kzg_settings.assume_init()))
} else {
Err(Error::InvalidTrustedSetup(format!(
"Invalid trusted setup: {:?}",
res
)))
}
}
}
/// Loads the trusted setup parameters from a file. The file format is as follows:
///
/// FIELD_ELEMENTS_PER_BLOB
/// 65 # This is fixed and is used for providing multiproofs up to 64 field elements.
/// FIELD_ELEMENT_PER_BLOB g1 byte values
/// 65 g2 byte values
pub fn load_trusted_setup_file(file_path: PathBuf) -> Result<Self, Error> {
let file_path = CString::new(file_path.as_os_str().as_bytes()).map_err(|e| {
Error::InvalidTrustedSetup(format!("Invalid trusted setup file: {:?}", e))
})?;
let mut kzg_settings = MaybeUninit::<bindings::KZGSettings>::uninit();
unsafe {
let file_ptr = fopen(file_path.as_ptr(), &('r' as libc::c_char));
let res = bindings::load_trusted_setup_file(kzg_settings.as_mut_ptr(), file_ptr);
if let C_KZG_RET::C_KZG_OK = res {
Ok(Self(kzg_settings.assume_init()))
} else {
Err(Error::InvalidTrustedSetup(format!(
"Invalid trusted setup: {:?}",
res
)))
}
}
}
}
impl Drop for KzgSettings {
fn drop(&mut self) {
unsafe { bindings::free_trusted_setup(&mut self.0) }
}
}
pub struct KzgProof(bindings::KZGProof);
impl KzgProof {
pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
if bytes.len() != BYTES_PER_PROOF {
return Err(Error::InvalidKzgProof(format!(
"Invalid byte length. Expected {} got {}",
BYTES_PER_PROOF,
bytes.len(),
)));
}
let mut proof_bytes = [0; BYTES_PER_PROOF];
proof_bytes.copy_from_slice(bytes);
Ok(Self(bytes_to_g1(bytes)?))
}
pub fn to_bytes(&self) -> [u8; BYTES_PER_G1_POINT] {
bytes_from_g1(self.0)
}
pub fn as_hex_string(&self) -> String {
hex::encode(self.to_bytes())
}
pub fn compute_aggregate_kzg_proof(
blobs: &[Blob],
kzg_settings: &KzgSettings,
) -> Result<Self, Error> {
let mut kzg_proof = MaybeUninit::<bindings::KZGProof>::uninit();
unsafe {
let res = bindings::compute_aggregate_kzg_proof(
kzg_proof.as_mut_ptr(),
blobs.as_ptr() as *const u8,
blobs.len(),
&kzg_settings.0,
);
if let C_KZG_RET::C_KZG_OK = res {
Ok(Self(kzg_proof.assume_init()))
} else {
Err(Error::CError(res))
}
}
}
pub fn verify_aggregate_kzg_proof(
&self,
blobs: &[Blob],
expected_kzg_commitments: &[KzgCommitment],
kzg_settings: &KzgSettings,
) -> Result<bool, Error> {
let mut verified: MaybeUninit<bool> = MaybeUninit::uninit();
unsafe {
// TODO: pass without allocating a vec
let res = bindings::verify_aggregate_kzg_proof(
verified.as_mut_ptr(),
blobs.as_ptr() as *const u8,
expected_kzg_commitments
.iter()
.map(|c| c.0)
.collect::<Vec<_>>()
.as_ptr(),
blobs.len(),
&self.0,
&kzg_settings.0,
);
if let C_KZG_RET::C_KZG_OK = res {
Ok(verified.assume_init())
} else {
Err(Error::CError(res))
}
}
}
pub fn verify_kzg_proof(
&self,
kzg_commitment: KzgCommitment,
z: [u8; BYTES_PER_FIELD_ELEMENT],
y: [u8; BYTES_PER_FIELD_ELEMENT],
kzg_settings: &KzgSettings,
) -> Result<bool, Error> {
let mut verified: MaybeUninit<bool> = MaybeUninit::uninit();
unsafe {
let res = bindings::verify_kzg_proof(
verified.as_mut_ptr(),
&kzg_commitment.0,
z.as_ptr(),
y.as_ptr(),
&self.0,
&kzg_settings.0,
);
if let C_KZG_RET::C_KZG_OK = res {
Ok(verified.assume_init())
} else {
Err(Error::CError(res))
}
}
}
}
pub struct KzgCommitment(bindings::KZGCommitment);
impl KzgCommitment {
pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
if bytes.len() != BYTES_PER_COMMITMENT {
return Err(Error::InvalidKzgCommitment(format!(
"Invalid byte length. Expected {} got {}",
BYTES_PER_PROOF,
bytes.len(),
)));
}
let mut proof_bytes = [0; BYTES_PER_COMMITMENT];
proof_bytes.copy_from_slice(bytes);
Ok(Self(bytes_to_g1(bytes)?))
}
pub fn to_bytes(&self) -> [u8; BYTES_PER_G1_POINT] {
bytes_from_g1(self.0)
}
pub fn as_hex_string(&self) -> String {
hex::encode(self.to_bytes())
}
pub fn blob_to_kzg_commitment(mut blob: Blob, kzg_settings: &KzgSettings) -> Self {
let mut kzg_commitment: MaybeUninit<bindings::KZGCommitment> = MaybeUninit::uninit();
unsafe {
bindings::blob_to_kzg_commitment(
kzg_commitment.as_mut_ptr(),
blob.as_mut_ptr(),
&kzg_settings.0,
);
Self(kzg_commitment.assume_init())
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use rand::{rngs::ThreadRng, Rng};
fn generate_random_blob(rng: &mut ThreadRng) -> Blob {
let mut arr: Blob = [0; BYTES_PER_BLOB];
rng.fill(&mut arr[..]);
// Ensure that the blob is canonical by ensuring that
// each field element contained in the blob is < BLS_MODULUS
for i in 0..FIELD_ELEMENTS_PER_BLOB {
arr[i * BYTES_PER_FIELD_ELEMENT + BYTES_PER_FIELD_ELEMENT - 1] = 0;
}
arr
}
fn test_simple(trusted_setup_file: PathBuf) {
let mut rng = rand::thread_rng();
assert!(trusted_setup_file.exists());
let kzg_settings = KzgSettings::load_trusted_setup_file(trusted_setup_file).unwrap();
let num_blobs: usize = rng.gen_range(0..16);
let mut blobs: Vec<Blob> = (0..num_blobs)
.map(|_| generate_random_blob(&mut rng))
.collect();
let kzg_commitments: Vec<KzgCommitment> = blobs
.clone()
.into_iter()
.map(|blob| KzgCommitment::blob_to_kzg_commitment(blob, &kzg_settings))
.collect();
let kzg_proof = KzgProof::compute_aggregate_kzg_proof(&blobs, &kzg_settings).unwrap();
assert!(kzg_proof
.verify_aggregate_kzg_proof(&blobs, &kzg_commitments, &kzg_settings)
.unwrap());
let incorrect_blob = generate_random_blob(&mut rng);
blobs.pop();
blobs.push(incorrect_blob);
assert!(!kzg_proof
.verify_aggregate_kzg_proof(&blobs, &kzg_commitments, &kzg_settings)
.unwrap());
}
#[test]
fn test_end_to_end() {
let trusted_setup_file = if cfg!(feature = "minimal-spec") {
PathBuf::from("../../src/trusted_setup_4.txt")
} else {
PathBuf::from("../../src/trusted_setup.txt")
};
test_simple(trusted_setup_file);
}
#[cfg(not(feature = "minimal-spec"))]
#[test]
fn test_compute_agg_proof() {
let trusted_setup_file = PathBuf::from("../../src/trusted_setup.txt");
assert!(trusted_setup_file.exists());
let kzg_settings = KzgSettings::load_trusted_setup_file(trusted_setup_file).unwrap();
let test_file = PathBuf::from("test_vectors/public_agg_proof.json");
let json_data: serde_json::Value =
serde_json::from_reader(std::fs::File::open(test_file).unwrap()).unwrap();
let tests = json_data.get("TestCases").unwrap().as_array().unwrap();
for test in tests.iter() {
let expected_proof = test.get("Proof").unwrap().as_str().unwrap();
let expected_kzg_commitments = test
.get("Commitments")
.unwrap()
.as_array()
.unwrap()
.iter()
.map(|data| data.as_str().unwrap())
.collect::<Vec<_>>();
let blobs = test
.get("Polynomials")
.unwrap()
.as_array()
.unwrap()
.iter()
.map(|data| {
let data = data.as_str().unwrap();
let blob = hex::decode(data).unwrap();
let mut blob_data = [0; BYTES_PER_BLOB];
blob_data.copy_from_slice(&blob);
blob_data
})
.collect::<Vec<_>>();
let proof = KzgProof::compute_aggregate_kzg_proof(&blobs, &kzg_settings).unwrap();
assert_eq!(proof.as_hex_string(), expected_proof);
for (i, blob) in blobs.into_iter().enumerate() {
let commitment = KzgCommitment::blob_to_kzg_commitment(blob, &kzg_settings);
assert_eq!(
commitment.as_hex_string().as_str(),
expected_kzg_commitments[i]
);
}
}
}
#[cfg(not(feature = "minimal-spec"))]
#[test]
fn test_verify_kzg_proof() {
let trusted_setup_file = PathBuf::from("../../src/trusted_setup.txt");
assert!(trusted_setup_file.exists());
let kzg_settings = KzgSettings::load_trusted_setup_file(trusted_setup_file).unwrap();
let test_file = PathBuf::from("test_vectors/public_verify_kzg_proof.json");
let json_data: serde_json::Value =
serde_json::from_reader(std::fs::File::open(test_file).unwrap()).unwrap();
let tests = json_data.get("TestCases").unwrap().as_array().unwrap();
for test in tests.iter() {
let proof = test.get("Proof").unwrap().as_str().unwrap();
let kzg_proof = KzgProof::from_bytes(&hex::decode(proof).unwrap()).unwrap();
let commitment = test.get("Commitment").unwrap().as_str().unwrap();
let kzg_commitment =
KzgCommitment::from_bytes(&hex::decode(commitment).unwrap()).unwrap();
let z = test.get("InputPoint").unwrap().as_str().unwrap();
let mut z_bytes = [0; BYTES_PER_FIELD_ELEMENT];
z_bytes.copy_from_slice(&hex::decode(z).unwrap());
let y = test.get("ClaimedValue").unwrap().as_str().unwrap();
let mut y_bytes = [0; BYTES_PER_FIELD_ELEMENT];
y_bytes.copy_from_slice(&hex::decode(y).unwrap());
assert!(kzg_proof
.verify_kzg_proof(kzg_commitment, z_bytes, y_bytes, &kzg_settings)
.unwrap());
}
}
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long