Skip to content

Commit

Permalink
feat: add From bool for fields
Browse files Browse the repository at this point in the history
  • Loading branch information
davidnevadoc committed Jul 25, 2024
1 parent 44e142f commit 1e342e6
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/bls12381/fq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
1 change: 1 addition & 0 deletions src/bls12381/fr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions src/bn256/fq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/bn256/fr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ impl From<u64> for Fr {
}
}
}
crate::impl_from_bool!(Fr);

#[cfg(feature = "bn256-table")]
#[rustfmt::skip]
Expand Down
18 changes: 18 additions & 0 deletions src/derive/field/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,24 @@ macro_rules! impl_from_u64 {
};
}

#[macro_export]
macro_rules! impl_from_bool {
($field:ident) => {
impl From<bool> 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::<Vec<_>>()
.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).
Expand Down
1 change: 1 addition & 0 deletions src/pluto_eris/fp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down
1 change: 1 addition & 0 deletions src/pluto_eris/fq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions src/secp256k1/fp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions src/secp256k1/fq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions src/secp256r1/fp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions src/secp256r1/fq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 1e342e6

Please sign in to comment.