From 1d37b87da4192572b0ff18765db4cf756176a7de Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Thu, 20 Jan 2022 19:03:12 +0100 Subject: [PATCH] feat: add 32bit support --- .github/workflows/ci.yml | 13 ++++++------ src/lib.rs | 2 -- src/scalar.rs | 44 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 50 insertions(+), 9 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5beffcb..235989c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -30,11 +30,6 @@ jobs: rust: nightly arch: aarch64 - # 64-bit Linux/arm64 - - target: aarch64-unknown-linux-gnu - rust: nightly - arch: aarch64 - runs-on: ubuntu-18.04 steps: - uses: actions/checkout@v2 @@ -57,6 +52,8 @@ jobs: run: | $HOME/.cargo/bin/cargo test --release --target ${{ matrix.target }} + $HOME/.cargo/bin/cargo test --release --features portable --target ${{ matrix.target }} + # Linux tests linux: needs: set-msrv @@ -90,6 +87,7 @@ jobs: run: sudo apt-get install -y ocl-icd-opencl-dev - run: ${{ matrix.deps }} - run: cargo test --target ${{ matrix.target }} + - run: cargo test --target ${{ matrix.target }} --features portable # macOS tests macos: @@ -110,6 +108,7 @@ jobs: target: x86_64-apple-darwin override: true - run: cargo test + - run: cargo test --features portable # Windows tests windows: @@ -117,7 +116,8 @@ jobs: strategy: matrix: include: - - target: x86_64-pc-windows-gnu + # 64-bit Windows (MSVC) + - target: x86_64-pc-windows-msvc toolchain: stable runs-on: windows-latest @@ -131,6 +131,7 @@ jobs: override: true - uses: msys2/setup-msys2@v2 - run: cargo test --target ${{ matrix.target }} + - run: cargo test --target ${{ matrix.target }} --features portable clippy_check: runs-on: ubuntu-latest diff --git a/src/lib.rs b/src/lib.rs index e6c993a..1c23744 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,8 +8,6 @@ #[cfg(not(target_endian = "little"))] compile_error!("blstrs is only supported on little endian architectures"); -#[cfg(not(target_pointer_width = "64"))] -compile_error!("blstrs is only supported on 64bit architectures"); #[macro_use] mod macros; diff --git a/src/scalar.rs b/src/scalar.rs index b224675..d5c7141 100644 --- a/src/scalar.rs +++ b/src/scalar.rs @@ -41,6 +41,19 @@ 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, @@ -465,10 +478,17 @@ 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 = [u64; 4]; + type ReprBits = ReprBits; + #[cfg(target_pointer_width = "64")] fn to_le_bits(&self) -> FieldBits { let mut limbs = [0u64; 4]; unsafe { blst_uint64_from_fr(limbs.as_mut_ptr(), &self.0) }; @@ -476,7 +496,29 @@ impl PrimeFieldBits for Scalar { FieldBits::new(limbs) } + #[cfg(not(target_pointer_width = "64"))] + fn to_le_bits(&self) -> FieldBits { + 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) + } + fn char_le_bits() -> FieldBits { + #[cfg(not(target_pointer_width = "64"))] + { + FieldBits::new(MODULUS_LIMBS_32) + } + + #[cfg(target_pointer_width = "64")] FieldBits::new(MODULUS) } }