From 2e168204f073e72549c661f61293beaa82004d7f Mon Sep 17 00:00:00 2001 From: Luke Parker Date: Thu, 30 Jun 2022 03:17:15 -0400 Subject: [PATCH] Implement PrimeFieldBits for dalek-ff-group --- crypto/dalek-ff-group/Cargo.toml | 1 + crypto/dalek-ff-group/src/lib.rs | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/crypto/dalek-ff-group/Cargo.toml b/crypto/dalek-ff-group/Cargo.toml index 0904138f..3b78578f 100644 --- a/crypto/dalek-ff-group/Cargo.toml +++ b/crypto/dalek-ff-group/Cargo.toml @@ -14,6 +14,7 @@ digest = "0.10" subtle = "2.4" +ff = "0.12" group = "0.12" curve25519-dalek = "3.2" diff --git a/crypto/dalek-ff-group/src/lib.rs b/crypto/dalek-ff-group/src/lib.rs index 5340b7e2..e2ed5e75 100644 --- a/crypto/dalek-ff-group/src/lib.rs +++ b/crypto/dalek-ff-group/src/lib.rs @@ -29,7 +29,8 @@ use dalek::{ } }; -use group::{ff::{Field, PrimeField}, Group, GroupEncoding, prime::PrimeGroup}; +use ff::{Field, PrimeField, FieldBits, PrimeFieldBits}; +use group::{Group, GroupEncoding, prime::PrimeGroup}; macro_rules! deref_borrow { ($Source: ident, $Target: ident) => { @@ -190,6 +191,21 @@ impl PrimeField for Scalar { fn root_of_unity() -> Self { unimplemented!() } } +impl PrimeFieldBits for Scalar { + type ReprBits = [u8; 32]; + + fn to_le_bits(&self) -> FieldBits { + self.to_repr().into() + } + + fn char_le_bits() -> FieldBits { + let mut bytes = (Scalar::zero() - Scalar::one()).to_repr(); + bytes[0] += 1; + debug_assert_eq!(Scalar::from_bytes_mod_order(bytes), Scalar::zero()); + bytes.into() + } +} + macro_rules! dalek_group { ( $Point: ident,