diff --git a/CHANGELOG.md b/CHANGELOG.md index 955e6a8..667eb05 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,18 @@ and this library adheres to Rust's notion of ## [Unreleased] +### Changed +- MSRV is now 1.63.0. +- Migrated to `rand_core 0.9`. +- `ff::Field::random(rng: impl RngCore) -> Self` has been changed back to + `Field::random(rng: &mut R) -> Self`, to enable passing a + trait object as the RNG. +- `ff::Field::try_from_rng` is a new trait method that must be implemented by + downstreams. `Field::random` now has a default implementation that calls it. + +### Removed +- `derive_bits` feature flag (use `bits` instead). + ## [0.13.1] - 2025-03-09 ### Changed - `ff_derive` now works with all odd primes, not just primes that are either diff --git a/Cargo.lock b/Cargo.lock index 8cbc354..ab552c6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -15,21 +15,27 @@ dependencies = [ [[package]] name = "arrayref" -version = "0.3.6" +version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a4c527152e37cf757a3f78aae5a06fbeefdb07ccc535c980a3208ee3060dd544" +checksum = "76a2e8124351fda1ef8aaaa3bbd7ebbcb486bbcd4225aca0aa0d84bb2db8fecb" [[package]] name = "arrayvec" -version = "0.7.2" +version = "0.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8da52d66c7071e2e3fa2a1e5c6d088fec47b593032b254f5e980de8ea54454d6" +checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50" [[package]] name = "autocfg" -version = "1.1.0" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" +checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" + +[[package]] +name = "bitflags" +version = "2.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c8214115b7bf84099f1309324e63141d4c5d7cc26862f97a0a857dbefe165bd" [[package]] name = "bitvec" @@ -45,9 +51,9 @@ dependencies = [ [[package]] name = "blake2b_simd" -version = "1.0.0" +version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72936ee4afc7f8f736d1c38383b56480b5497b4617b4a77bdbf1d2ababc76127" +checksum = "3c2f0dc9a68c6317d884f97cc36cf5a3d20ba14ce404227df55e1af708ab04bc" dependencies = [ "arrayref", "arrayvec", @@ -56,9 +62,9 @@ dependencies = [ [[package]] name = "byteorder" -version = "1.4.3" +version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" [[package]] name = "cfg-if" @@ -68,13 +74,13 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "constant_time_eq" -version = "0.1.5" +version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc" +checksum = "21a53c0a4d288377e7415b53dcfc3c04da5cdc2cc95c8d5ac178b58f0b861ad6" [[package]] name = "ff" -version = "0.13.1" +version = "0.14.0-pre.0" dependencies = [ "bitvec", "blake2b_simd", @@ -87,7 +93,7 @@ dependencies = [ [[package]] name = "ff_derive" -version = "0.13.1" +version = "0.14.0-pre.0" dependencies = [ "addchain", "num-bigint", @@ -95,7 +101,7 @@ dependencies = [ "num-traits", "proc-macro2", "quote", - "syn", + "syn 1.0.109", ] [[package]] @@ -106,20 +112,21 @@ checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c" [[package]] name = "getrandom" -version = "0.2.8" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31" +checksum = "43a49c392881ce6d5c3b8cb70f98717b7c07aabbdff06687b9030dbfbe2725f8" dependencies = [ "cfg-if", "libc", "wasi", + "windows-targets", ] [[package]] name = "libc" -version = "0.2.137" +version = "0.2.170" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fc7fcc620a3bff7cdd7a365be3376c97191aeaccc2a603e600951e452615bf89" +checksum = "875b3680cb2f8f71bdcf9a30f38d48282f5d3c95cbf9b3fa57269bb5d5c06828" [[package]] name = "num-bigint" @@ -134,43 +141,45 @@ dependencies = [ [[package]] name = "num-integer" -version = "0.1.45" +version = "0.1.46" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9" +checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f" dependencies = [ - "autocfg", "num-traits", ] [[package]] name = "num-traits" -version = "0.2.15" +version = "0.2.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" dependencies = [ "autocfg", ] [[package]] name = "ppv-lite86" -version = "0.2.16" +version = "0.2.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872" +checksum = "77957b295656769bb8ad2b6a6b09d897d94f05c41b069aede1fcdaa675eaea04" +dependencies = [ + "zerocopy 0.7.35", +] [[package]] name = "proc-macro2" -version = "1.0.47" +version = "1.0.94" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ea3d908b0e36316caf9e9e2c4625cdde190a7e6f440d794667ed17a1855e725" +checksum = "a31971752e70b8b2686d7e46ec17fb38dad4051d94024c88df49b667caea9c84" dependencies = [ "unicode-ident", ] [[package]] name = "quote" -version = "1.0.21" +version = "1.0.39" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179" +checksum = "c1f1914ce909e1658d9907913b4b91947430c7d9be598b15a1912935b8c04801" dependencies = [ "proc-macro2", ] @@ -183,20 +192,20 @@ checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09" [[package]] name = "rand" -version = "0.8.5" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +checksum = "3779b94aeb87e8bd4e834cee3650289ee9e0d5677f976ecdb6d219e5f4f6cd94" dependencies = [ - "libc", "rand_chacha", "rand_core", + "zerocopy 0.8.23", ] [[package]] name = "rand_chacha" -version = "0.3.1" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb" dependencies = [ "ppv-lite86", "rand_core", @@ -204,24 +213,35 @@ dependencies = [ [[package]] name = "rand_core" -version = "0.6.4" +version = "0.9.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +checksum = "99d9a13982dcf210057a8a78572b2217b667c3beacbf3a0d8b454f6f82837d38" dependencies = [ "getrandom", ] [[package]] name = "subtle" -version = "2.4.1" +version = "2.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6bdef32e8150c2a081110b42772ffe7d7c9032b606bc226c8260fd97e0976601" +checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" [[package]] name = "syn" -version = "1.0.103" +version = "1.0.109" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a864042229133ada95abf3b54fdc62ef5ccabe9515b64717bcb9a1919e59445d" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "syn" +version = "2.0.99" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e02e925281e18ffd9d640e234264753c43edc62d64b2d4cf898f1bc5e75f3fc2" dependencies = [ "proc-macro2", "quote", @@ -236,21 +256,138 @@ checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" [[package]] name = "unicode-ident" -version = "1.0.5" +version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ceab39d59e4c9499d4e5a8ee0e2735b891bb7308ac83dfb4e80cad195c9f6f3" +checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" [[package]] name = "wasi" -version = "0.11.0+wasi-snapshot-preview1" +version = "0.13.3+wasi-0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" +checksum = "26816d2e1a4a36a2940b96c5296ce403917633dff8f3440e9b236ed6f6bacad2" +dependencies = [ + "wit-bindgen-rt", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "wit-bindgen-rt" +version = "0.33.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3268f3d866458b787f390cf61f4bbb563b922d091359f9608842999eaee3943c" +dependencies = [ + "bitflags", +] [[package]] name = "wyz" -version = "0.5.0" +version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "30b31594f29d27036c383b53b59ed3476874d518f0efb151b27a4c275141390e" +checksum = "05f360fc0b24296329c78fda852a1e9ae82de9cf7b27dae4b7f62f118f77b9ed" dependencies = [ "tap", ] + +[[package]] +name = "zerocopy" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" +dependencies = [ + "byteorder", + "zerocopy-derive 0.7.35", +] + +[[package]] +name = "zerocopy" +version = "0.8.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd97444d05a4328b90e75e503a34bad781f14e28a823ad3557f0750df1ebcbc6" +dependencies = [ + "zerocopy-derive 0.8.23", +] + +[[package]] +name = "zerocopy-derive" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.99", +] + +[[package]] +name = "zerocopy-derive" +version = "0.8.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6352c01d0edd5db859a63e2605f4ea3183ddbd15e2c4a9e7d32184df75e4f154" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.99", +] diff --git a/Cargo.toml b/Cargo.toml index a6af21f..a5f7f7c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,12 +1,12 @@ [package] name = "ff" -version = "0.13.1" +version = "0.14.0-pre.0" authors = [ "Sean Bowe ", "Jack Grigg ", ] edition = "2021" -rust-version = "1.56" +rust-version = "1.63" description = "Library for building and interfacing with finite fields" readme = "README.md" documentation = "https://docs.rs/ff/" @@ -17,23 +17,20 @@ repository = "https://github.com/zkcrypto/ff" [dependencies] bitvec = { version = "1", default-features = false, optional = true } byteorder = { version = "1", default-features = false, optional = true } -ff_derive = { version = "0.13.1", path = "ff_derive", optional = true } -rand_core = { version = "0.6", default-features = false } +ff_derive = { version = "=0.14.0-pre.0", path = "ff_derive", optional = true } +rand_core = { version = "0.9", default-features = false } subtle = { version = "2.2.1", default-features = false, features = ["i128"] } [dev-dependencies] blake2b_simd = "1" -rand = "0.8" +rand = "0.9" [features] default = ["bits", "std"] alloc = [] -bits = ["bitvec"] +bits = ["bitvec", "ff_derive?/bits"] derive = ["byteorder", "ff_derive"] std = ["alloc"] -# with MSRV 1.60 this could be merged into bits with ff_derive?/bits -# see PR#72 for more information. -derive_bits = ["bits", "ff_derive/bits"] [[test]] name = "derive" diff --git a/README.md b/README.md index caff449..763769f 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,7 @@ And that's it! `Fp` now implements `Field` and `PrimeField`. ## Minimum Supported Rust Version -Requires Rust **1.56** or higher. +Requires Rust **1.63** or higher. Minimum supported Rust version can be changed in the future, but it will be done with a minor version bump. diff --git a/ff_derive/Cargo.toml b/ff_derive/Cargo.toml index 1933785..6ae2aa0 100644 --- a/ff_derive/Cargo.toml +++ b/ff_derive/Cargo.toml @@ -1,12 +1,12 @@ [package] name = "ff_derive" -version = "0.13.1" +version = "0.14.0-pre.0" authors = [ "Sean Bowe ", "Jack Grigg ", ] edition = "2021" -rust-version = "1.56" +rust-version = "1.63" description = "Procedural macro library used to build custom prime field implementations" documentation = "https://docs.rs/ff-derive/" homepage = "https://github.com/zkcrypto/ff" @@ -15,6 +15,7 @@ repository = "https://github.com/zkcrypto/ff" [features] # enabled when generating bitvec code utilizing the version of ff's bitvec +# ff/bits enables this bits = [] [lib] diff --git a/ff_derive/src/lib.rs b/ff_derive/src/lib.rs index 0a0b08d..53bc6ea 100644 --- a/ff_derive/src/lib.rs +++ b/ff_derive/src/lib.rs @@ -1259,12 +1259,12 @@ fn prime_field_impl( const ONE: Self = R; /// Computes a uniformly random element using rejection sampling. - fn random(mut rng: impl ::ff::derive::rand_core::RngCore) -> Self { + fn try_from_rng(rng: &mut R) -> ::core::result::Result { loop { let mut tmp = { let mut repr = [0u64; #limbs]; for i in 0..#limbs { - repr[i] = rng.next_u64(); + repr[i] = rng.try_next_u64()?; } #name(repr) }; @@ -1277,7 +1277,7 @@ fn prime_field_impl( tmp.0[#top_limb_index] &= 0xffffffffffffffffu64.checked_shr(REPR_SHAVE_BITS).unwrap_or(0); if tmp.is_valid() { - return tmp + return Ok(tmp) } } } diff --git a/rust-toolchain.toml b/rust-toolchain.toml index de43b23..3eebdfe 100644 --- a/rust-toolchain.toml +++ b/rust-toolchain.toml @@ -1,3 +1,3 @@ [toolchain] -channel = "1.56.0" +channel = "1.63.0" components = [ "clippy", "rustfmt" ] diff --git a/src/batch.rs b/src/batch.rs index 96ddc5a..2b54a88 100644 --- a/src/batch.rs +++ b/src/batch.rs @@ -86,7 +86,7 @@ impl BatchInverter { let tmp = *scratch * acc; let skip = p.is_zero(); acc = F::conditional_select(&(acc * *p), &acc, skip); - *p = F::conditional_select(&tmp, &p, skip); + *p = F::conditional_select(&tmp, p, skip); } allinv @@ -123,7 +123,7 @@ impl BatchInverter { let p = (element)(item); let skip = p.is_zero(); acc = F::conditional_select(&(acc * *p), &acc, skip); - *p = F::conditional_select(&tmp, &p, skip); + *p = F::conditional_select(&tmp, p, skip); } allinv diff --git a/src/lib.rs b/src/lib.rs index f9eee3c..4ffb982 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,11 +25,12 @@ pub use bitvec::view::BitViewSized; #[cfg(feature = "bits")] use bitvec::{array::BitArray, order::Lsb0}; +use core::convert::Infallible; use core::fmt; use core::iter::{Product, Sum}; use core::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign}; -use rand_core::RngCore; +use rand_core::{RngCore, TryRngCore}; use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption}; /// Bit representation of a field element. @@ -75,7 +76,19 @@ pub trait Field: const ONE: Self; /// Returns an element chosen uniformly at random using a user-provided RNG. - fn random(rng: impl RngCore) -> Self; + fn random(rng: &mut R) -> Self { + Self::try_from_rng(rng) + .map_err(|e: Infallible| e) + .expect("Infallible failed") + + // NOTE: once MSRV gets to 1.82 remove the map_err/expect and use + // let Ok(out) = Self::try_from_rng(rng); + // out + // See: https://blog.rust-lang.org/2024/10/17/Rust-1.82.0.html#omitting-empty-types-in-pattern-matching + } + + /// Returns an element chosen uniformly at random using a user-provided RNG. + fn try_from_rng(rng: &mut R) -> Result; /// Returns true iff this element is zero. fn is_zero(&self) -> Choice { diff --git a/tests/derive.rs b/tests/derive.rs index 5baf435..29800f1 100644 --- a/tests/derive.rs +++ b/tests/derive.rs @@ -31,9 +31,9 @@ mod full_limbs { #[test] fn random_masking_does_not_overflow() { use ff::Field; - use rand::rngs::OsRng; - let _ = F384p::random(OsRng); + let _ = F384p::random(&mut rand::rng()); + let _ = F384p::try_from_rng(&mut rand::rngs::OsRng).unwrap(); } } @@ -153,6 +153,5 @@ fn sqrt() { test(Fp::ZERO); test(Fp::ONE); - use rand::rngs::OsRng; - test(Fp::random(OsRng)); + test(Fp::random(&mut rand::rng())); }