diff --git a/src/bls12381/fq.rs b/src/bls12381/fq.rs index 451657e4..f647077f 100644 --- a/src/bls12381/fq.rs +++ b/src/bls12381/fq.rs @@ -20,6 +20,7 @@ crate::impl_binops_multiplicative!(Fq, Fq); crate::field_bits!(Fq); crate::serialize_deserialize_primefield!(Fq); crate::impl_from_u64!(Fq); +crate::impl_from_bool!(Fq); use ff::Field; diff --git a/src/bls12381/fr.rs b/src/bls12381/fr.rs index b409ebf4..ee62adc6 100644 --- a/src/bls12381/fr.rs +++ b/src/bls12381/fr.rs @@ -20,6 +20,7 @@ crate::impl_binops_multiplicative!(Fr, Fr); crate::field_bits!(Fr); crate::serialize_deserialize_primefield!(Fr); crate::impl_from_u64!(Fr); +crate::impl_from_bool!(Fr); #[cfg(test)] mod test { diff --git a/src/bn256/fq.rs b/src/bn256/fq.rs index 791c2ada..c82c6e26 100644 --- a/src/bn256/fq.rs +++ b/src/bn256/fq.rs @@ -21,6 +21,7 @@ crate::impl_binops_multiplicative!(Fq, Fq); crate::field_bits!(Fq); crate::serialize_deserialize_primefield!(Fq); crate::impl_from_u64!(Fq); +crate::impl_from_bool!(Fq); use ff::Field; const NEGATIVE_ONE: Fq = Fq::ZERO.sub_const(&Fq::ONE); diff --git a/src/bn256/fr.rs b/src/bn256/fr.rs index ba82383c..986378d0 100644 --- a/src/bn256/fr.rs +++ b/src/bn256/fr.rs @@ -34,6 +34,7 @@ impl From for Fr { } } } +crate::impl_from_bool!(Fr); #[cfg(feature = "bn256-table")] #[rustfmt::skip] diff --git a/src/derive/field/common.rs b/src/derive/field/common.rs index ff3b7772..3a2f7e9a 100644 --- a/src/derive/field/common.rs +++ b/src/derive/field/common.rs @@ -66,6 +66,24 @@ macro_rules! impl_from_u64 { }; } +#[macro_export] +macro_rules! impl_from_bool { + ($field:ident) => { + impl From for $field { + fn from(val: bool) -> $field { + let limbs = std::iter::once(u64::from(val)) + .chain(std::iter::repeat(0)) + .take(Self::NUM_LIMBS) + .collect::>() + .try_into() + .unwrap(); + + $field(limbs) * Self::R2 + } + } + }; +} + /// A macro to help define serialization and deserialization for prime field implementations /// that use `$field::Repr`` representations. This assumes the concerned type implements PrimeField /// (for from_repr, to_repr). diff --git a/src/pluto_eris/fp.rs b/src/pluto_eris/fp.rs index 53d60ff3..d3c1a8da 100644 --- a/src/pluto_eris/fp.rs +++ b/src/pluto_eris/fp.rs @@ -21,6 +21,7 @@ crate::impl_binops_multiplicative!(Fp, Fp); crate::field_bits!(Fp); crate::serialize_deserialize_primefield!(Fp); crate::impl_from_u64!(Fp); +crate::impl_from_bool!(Fp); impl ExtField for Fp { const NON_RESIDUE: Self = Fp::from_raw([ diff --git a/src/pluto_eris/fq.rs b/src/pluto_eris/fq.rs index 78bc8327..c1897e4c 100644 --- a/src/pluto_eris/fq.rs +++ b/src/pluto_eris/fq.rs @@ -20,6 +20,7 @@ crate::impl_binops_multiplicative!(Fq, Fq); crate::field_bits!(Fq); crate::serialize_deserialize_primefield!(Fq); crate::impl_from_u64!(Fq); +crate::impl_from_bool!(Fq); #[cfg(test)] mod test { diff --git a/src/secp256k1/fp.rs b/src/secp256k1/fp.rs index b7228090..c486ce8e 100644 --- a/src/secp256k1/fp.rs +++ b/src/secp256k1/fp.rs @@ -20,6 +20,7 @@ crate::impl_binops_multiplicative!(Fp, Fp); crate::field_bits!(Fp); crate::serialize_deserialize_primefield!(Fp); crate::impl_from_u64!(Fp); +crate::impl_from_bool!(Fp); #[cfg(test)] mod test { diff --git a/src/secp256k1/fq.rs b/src/secp256k1/fq.rs index 50ad683c..0f93e4c9 100644 --- a/src/secp256k1/fq.rs +++ b/src/secp256k1/fq.rs @@ -20,6 +20,7 @@ crate::impl_binops_multiplicative!(Fq, Fq); crate::field_bits!(Fq); crate::serialize_deserialize_primefield!(Fq); crate::impl_from_u64!(Fq); +crate::impl_from_bool!(Fq); #[cfg(test)] mod test { diff --git a/src/secp256r1/fp.rs b/src/secp256r1/fp.rs index 3a740d5f..76324047 100644 --- a/src/secp256r1/fp.rs +++ b/src/secp256r1/fp.rs @@ -20,6 +20,7 @@ crate::impl_binops_multiplicative!(Fp, Fp); crate::field_bits!(Fp); crate::serialize_deserialize_primefield!(Fp); crate::impl_from_u64!(Fp); +crate::impl_from_bool!(Fp); #[cfg(test)] mod test { diff --git a/src/secp256r1/fq.rs b/src/secp256r1/fq.rs index b0445921..ef1c2d35 100644 --- a/src/secp256r1/fq.rs +++ b/src/secp256r1/fq.rs @@ -20,6 +20,7 @@ crate::impl_binops_multiplicative!(Fq, Fq); crate::field_bits!(Fq); crate::serialize_deserialize_primefield!(Fq); crate::impl_from_u64!(Fq); +crate::impl_from_bool!(Fq); #[cfg(test)] mod test {