From f5413a93cb2fe0b2677e65c3ef5e7e6fc26c222f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Fri, 12 Jul 2024 11:03:13 +0100 Subject: [PATCH 1/2] add optional arbitrary impls --- Cargo.toml | 10 +++++++++- rust-toolchain.toml | 2 +- src/fr.rs | 1 + src/lib.rs | 4 +++- 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index da6566a..15107ad 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,12 +13,19 @@ repository = "https://github.com/zkcrypto/jubjub" version = "0.10.0" edition = "2021" +[dependencies.arbitrary] +version = "1.3" +features = ["derive"] +optional = true + [dependencies.bitvec] version = "1" default-features = false [dependencies.bls12_381] -version = "0.8" +# upstreamed in https://github.com/zkcrypto/bls12_381/pull/138 +git = "https://github.com/heliaxdev/bls12_381.git" +rev = "d96b104d0dc035af160fd6505e967cb37a553320" default-features = false [dependencies.ff] @@ -49,6 +56,7 @@ default-features = false default = ["alloc", "bits"] alloc = ["ff/alloc", "group/alloc"] bits = ["ff/bits"] +arbitrary = ["dep:arbitrary", "bls12_381/arbitrary"] [[bench]] name = "fq_bench" 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/fr.rs b/src/fr.rs index 870f58e..57e4f96 100644 --- a/src/fr.rs +++ b/src/fr.rs @@ -19,6 +19,7 @@ use crate::util::{adc, mac, sbb}; // The internal representation of this type is four 64-bit unsigned // integers in little-endian order. Elements of Fr are always in // Montgomery form; i.e., Fr(a) = aR mod r, with R = 2^256. +#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))] #[derive(Clone, Copy, Eq)] pub struct Fr(pub(crate) [u64; 4]); diff --git a/src/lib.rs b/src/lib.rs index 014173a..77b7cc4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,7 +16,7 @@ //! //! This crate uses the `subtle` crate to perform constant-time operations. -#![no_std] +#![cfg_attr(not(feature = "arbitrary"), no_std)] // Catch documentation errors caused by code changes. #![deny(rustdoc::broken_intra_doc_links)] #![deny(missing_debug_implementations)] @@ -135,6 +135,7 @@ impl ConditionallySelectable for AffinePoint { /// * Add it to an `ExtendedPoint`, `AffineNielsPoint` or `ExtendedNielsPoint`. /// * Double it using `double()`. /// * Compare it with another extended point using `PartialEq` or `ct_eq()`. +#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))] #[derive(Clone, Copy, Debug, Eq)] pub struct ExtendedPoint { u: Fq, @@ -1118,6 +1119,7 @@ impl_binops_multiplicative_mixed!(AffinePoint, Fr, ExtendedPoint); /// This represents a point in the prime-order subgroup of Jubjub, in extended /// coordinates. +#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))] #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] pub struct SubgroupPoint(ExtendedPoint); From 7bf633000af9fff960cefaf8bd9d984c341c5763 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Fri, 12 Jul 2024 14:38:45 +0100 Subject: [PATCH 2/2] avoid invalid arbitrary ExtendedPoint where z.is_zero --- src/lib.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 77b7cc4..b1cfb80 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -140,11 +140,21 @@ impl ConditionallySelectable for AffinePoint { pub struct ExtendedPoint { u: Fq, v: Fq, + #[cfg_attr(feature = "arbitrary", arbitrary(with = arbitrary_non_zero_fq))] z: Fq, t1: Fq, t2: Fq, } +#[cfg(feature = "arbitrary")] +fn arbitrary_non_zero_fq(u: &mut arbitrary::Unstructured) -> arbitrary::Result { + let raw: Fq = arbitrary::Arbitrary::arbitrary(u)?; + if bool::from(raw.is_zero()) { + return Err(arbitrary::Error::IncorrectFormat); + } + Ok(raw) +} + impl fmt::Display for ExtendedPoint { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{:?}", self)