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

Remove dependency on cfg(target_pointer_width) in src/scalar.rs #35

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
67 changes: 11 additions & 56 deletions src/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use core::{
};

use blst::*;
use byte_slice_cast::AsByteSlice;
use ff::{Field, FieldBits, PrimeField, PrimeFieldBits};
use rand_core::RngCore;
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption};
Expand Down Expand Up @@ -41,19 +40,6 @@ const MODULUS: [u64; 4] = [
0x73ed_a753_299d_7d48,
];

/// The modulus as u32 limbs.
#[cfg(not(target_pointer_width = "64"))]
const MODULUS_LIMBS_32: [u32; 8] = [
0x0000_0001,
0xffff_ffff,
0xfffe_5bfe,
0x53bd_a402,
0x09a1_d805,
0x3339_d808,
0x299d_7d48,
0x73ed_a753,
];

// Little-endian non-Montgomery form not reduced mod p.
const MODULUS_REPR: [u8; 32] = [
0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x5b, 0xfe, 0xff, 0x02, 0xa4, 0xbd, 0x53,
Expand Down Expand Up @@ -478,62 +464,31 @@ impl PrimeField for Scalar {
}
}

#[cfg(not(target_pointer_width = "64"))]
type ReprBits = [u32; 8];

#[cfg(target_pointer_width = "64")]
type ReprBits = [u64; 4];

impl PrimeFieldBits for Scalar {
// Representation in non-Montgomery form.
type ReprBits = ReprBits;
type ReprBits = [u8; 32];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this change doesn't quite work for us, as I am trying to mirror this: https://github.com/zkcrypto/bls12_381/blob/main/src/scalar.rs#L729


#[cfg(target_pointer_width = "64")]
fn to_le_bits(&self) -> FieldBits<Self::ReprBits> {
let mut limbs = [0u64; 4];
unsafe { blst_uint64_from_fr(limbs.as_mut_ptr(), &self.0) };
let mut out = blst_scalar::default();
unsafe { blst_scalar_from_fr(&mut out, &self.0) };

FieldBits::new(limbs)
}

#[cfg(not(target_pointer_width = "64"))]
fn to_le_bits(&self) -> FieldBits<Self::ReprBits> {
let bytes = self.to_bytes_le();
let limbs = [
u32::from_le_bytes(bytes[0..4].try_into().unwrap()),
u32::from_le_bytes(bytes[4..8].try_into().unwrap()),
u32::from_le_bytes(bytes[8..12].try_into().unwrap()),
u32::from_le_bytes(bytes[12..16].try_into().unwrap()),
u32::from_le_bytes(bytes[16..20].try_into().unwrap()),
u32::from_le_bytes(bytes[20..24].try_into().unwrap()),
u32::from_le_bytes(bytes[24..28].try_into().unwrap()),
u32::from_le_bytes(bytes[28..32].try_into().unwrap()),
];
FieldBits::new(limbs)
FieldBits::new(out.b)
}

fn char_le_bits() -> FieldBits<Self::ReprBits> {
#[cfg(not(target_pointer_width = "64"))]
{
FieldBits::new(MODULUS_LIMBS_32)
}

#[cfg(target_pointer_width = "64")]
FieldBits::new(MODULUS)
FieldBits::new(MODULUS_REPR)
}
}

impl Scalar {
/// Attempts to convert a little-endian byte representation of
/// a scalar into a `Scalar`, failing if the input is not canonical.
pub fn from_bytes_le(bytes: &[u8; 32]) -> CtOption<Scalar> {
let is_some =
Choice::from(unsafe { blst_scalar_fr_check(&blst_scalar { b: *bytes }) as u8 });

let s = bytes.as_ptr() as *const blst_scalar;
let is_some = Choice::from(unsafe { blst_scalar_fr_check(s) as u8 });
let mut out = blst_fr::default();
let bytes_u64 = u64s_from_bytes(bytes);

unsafe { blst_fr_from_uint64(&mut out, bytes_u64.as_ptr()) };
unsafe { blst_fr_from_scalar(&mut out, s) };

CtOption::new(Scalar(out), is_some)
}
Expand All @@ -550,9 +505,9 @@ impl Scalar {
/// little-endian byte order.
#[inline]
pub fn to_bytes_le(&self) -> [u8; 32] {
let mut out = [0u64; 4];
unsafe { blst_uint64_from_fr(out.as_mut_ptr(), &self.0) };
out.as_byte_slice().try_into().unwrap()
let mut out = blst_scalar::default();
unsafe { blst_scalar_from_fr(&mut out, &self.0) };
out.b
}

/// Converts an element of `Scalar` into a byte representation in
Expand Down