Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add From<bool> for Fields #171

Merged
merged 1 commit into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading