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

Deprecated divn and implemented core::ops::Shr #736

Merged
merged 15 commits into from
Dec 20, 2023
Merged
67 changes: 1 addition & 66 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -395,69 +395,4 @@ jobs:
--exclude ark-algebra-test-templates \
--exclude ark-algebra-bench-templates \
--exclude ark-poly-benches \
--target aarch64-unknown-none"

test_against_curves:
name: Test against curves
runs-on: ubuntu-latest
env:
RUSTFLAGS: -Dwarnings
strategy:
matrix:
curve:
- bls12_377
- bls12_381
- bn254
- pallas
- bw6_761
- mnt4_298
- mnt6_298
- ed_on_bls12_377
steps:
- name: Checkout curves
uses: actions/checkout@v4
with:
repository: arkworks-rs/curves
path: curves

- name: Checkout algebra
uses: actions/checkout@v4
with:
path: ark-algebra

- name: Install Rust
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true

- name: Patch cargo.toml
run: |
if grep -q "\[patch.crates-io\]" curves/Cargo.toml ; then
MATCH=$(awk '/\[patch.crates-io\]/{ print NR; exit }' curves/Cargo.toml);
sed -i "$MATCH,\$d" curves/Cargo.toml
fi
{
echo "[patch.crates-io]";
echo "ark-ff = { path = '../ark-algebra/ff' }";
echo "ark-serialize = { path = '../ark-algebra/serialize' }";
echo "ark-ff-macros = { path = '../ark-algebra/ff-macros' }";
echo "ark-ff-asm = { path = '../ark-algebra/ff-asm' }";
echo "ark-ec = { path = '../ark-algebra/ec' }";
echo "ark-algebra-bench-templates = { path = '../ark-algebra/bench-templates' }"
echo "ark-algebra-test-templates = { path = '../ark-algebra/test-templates' }"
echo "ark-bls12-377 = { git = 'https://github.com/arkworks-rs/curves' }"
echo "ark-bls12-381 = { git = 'https://github.com/arkworks-rs/curves' }"
echo "ark-bn254 = { git = 'https://github.com/arkworks-rs/curves' }"
echo "ark-pallas = { git = 'https://github.com/arkworks-rs/curves' }"
echo "ark-bw6-761 = { git = 'https://github.com/arkworks-rs/curves' }"
echo "ark-mnt4-298 = { git = 'https://github.com/arkworks-rs/curves' }"
echo "ark-mnt6-298 = { git = 'https://github.com/arkworks-rs/curves' }"
echo "ark-ed-on-bls12-377 = { git = 'https://github.com/arkworks-rs/curves' }"
echo "ark-std = { git = 'https://github.com/arkworks-rs/std' }"
echo "ark-r1cs-std = { git = 'https://github.com/arkworks-rs/r1cs-std' }"
} >> curves/Cargo.toml

- name: Test on ${{ matrix.curve }}
run: "cd curves/${{ matrix.curve }} && cargo test --all-features"
--target aarch64-unknown-none"
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- [\#689](https://github.com/arkworks-rs/algebra/pull/689) (`ark-serialize`) Add `CanonicalSerialize` and `CanonicalDeserialize` impls for `VecDeque` and `LinkedList`.
- [\#693](https://github.com/arkworks-rs/algebra/pull/693) (`ark-serialize`) Add `serialize_to_vec!` convenience macro.
- [\#713](https://github.com/arkworks-rs/algebra/pull/713) (`ark-ff`) Add support for bitwise operations AND, OR, and XOR between `BigInteger`.
- [\#736](https://github.com/arkworks-rs/algebra/pull/736) (`ark-ff`) Deprecate the use of `divn()`. Instead, it is implemented the trait `core::ops::Shr`.

### Breaking changes

Expand Down
12 changes: 6 additions & 6 deletions curves/bls12_381/src/fields/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,7 @@ fn test_fq_repr_div2() {
}

#[test]
fn test_fq_repr_divn() {
fn test_fq_repr_shr() {
let mut a = BigInt::new([
0xaa5cdd6172847ffd,
0x43242c06aed55287,
Expand All @@ -827,7 +827,7 @@ fn test_fq_repr_divn() {
0x16080cf4071e0b05,
0x1225f2901aea514e,
]);
a.divn(0);
a >>= 0;
assert_eq!(
a,
BigInt::new([
Expand All @@ -839,7 +839,7 @@ fn test_fq_repr_divn() {
0x1225f2901aea514e,
])
);
a.divn(1);
a >>= 1;
assert_eq!(
a,
BigInt::new([
Expand All @@ -851,7 +851,7 @@ fn test_fq_repr_divn() {
0x912f9480d7528a7,
])
);
a.divn(50);
a >>= 50;
assert_eq!(
a,
BigInt::new([
Expand All @@ -863,7 +863,7 @@ fn test_fq_repr_divn() {
0x244,
])
);
a.divn(130);
a >>= 130;
assert_eq!(
a,
BigInt::new([
Expand All @@ -875,7 +875,7 @@ fn test_fq_repr_divn() {
0x0,
])
);
a.divn(64);
a >>= 64;
assert_eq!(
a,
BigInt::new([0x4067a038f0582e2a, 0x2f9480d7528a70b0, 0x91, 0x0, 0x0, 0x0])
Expand Down
2 changes: 1 addition & 1 deletion ec/src/scalar_mul/variable_base/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ fn msm_bigint<V: VariableBaseMSM>(

// We right-shift by w_start, thus getting rid of the
// lower bits.
scalar.divn(w_start as u32);
scalar >>= w_start as u32;

// We mod the remaining bits by 2^{window size}, thus taking `c` bits.
let scalar = scalar.as_ref()[0] % (1 << c);
Expand Down
53 changes: 52 additions & 1 deletion ff/src/biginteger/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use core::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Not};
use core::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Not, Shr, ShrAssign};

use crate::{
bits::{BitIteratorBE, BitIteratorLE},
Expand Down Expand Up @@ -729,6 +729,54 @@
}
}

impl<const N: usize> ShrAssign<u32> for BigInt<N> {
/// Computes the bitwise shift right operation in place.
///
/// Differently from the built-in numeric types (u8, u32, u64, etc.) this
/// operation does *not* return an underflow error if the number of bits
/// shifted is larger than N * 64. Instead the result will be saturated to
/// zero.
fn shr_assign(&mut self, mut rhs: u32) {
if rhs >= (64 * N) as u32 {
return Self::from(0u64);

Check failure on line 741 in ff/src/biginteger/mod.rs

View workflow job for this annotation

GitHub Actions / Check no_std

mismatched types

Check failure on line 741 in ff/src/biginteger/mod.rs

View workflow job for this annotation

GitHub Actions / Test (nightly)

mismatched types

Check failure on line 741 in ff/src/biginteger/mod.rs

View workflow job for this annotation

GitHub Actions / Test (stable)

mismatched types

Check failure on line 741 in ff/src/biginteger/mod.rs

View workflow job for this annotation

GitHub Actions / Test assembly

mismatched types
}

while rhs >= 64 {
let mut t = 0;
for i in 0..N {
core::mem::swap(&mut t, &mut self[N - i - 1]);

Check failure on line 747 in ff/src/biginteger/mod.rs

View workflow job for this annotation

GitHub Actions / Check no_std

cannot index into a value of type `&mut biginteger::BigInt<N>`

Check failure on line 747 in ff/src/biginteger/mod.rs

View workflow job for this annotation

GitHub Actions / Test (nightly)

cannot index into a value of type `&mut biginteger::BigInt<N>`

Check failure on line 747 in ff/src/biginteger/mod.rs

View workflow job for this annotation

GitHub Actions / Test (stable)

cannot index into a value of type `&mut biginteger::BigInt<N>`

Check failure on line 747 in ff/src/biginteger/mod.rs

View workflow job for this annotation

GitHub Actions / Test assembly

cannot index into a value of type `&mut biginteger::BigInt<N>`
}
rhs -= 64;
}

if rhs > 0 {
let mut t = 0;
for i in 0..N {
let a = &mut self[N - i - 1];

Check failure on line 755 in ff/src/biginteger/mod.rs

View workflow job for this annotation

GitHub Actions / Check no_std

cannot index into a value of type `&mut biginteger::BigInt<N>`

Check failure on line 755 in ff/src/biginteger/mod.rs

View workflow job for this annotation

GitHub Actions / Test (nightly)

cannot index into a value of type `&mut biginteger::BigInt<N>`

Check failure on line 755 in ff/src/biginteger/mod.rs

View workflow job for this annotation

GitHub Actions / Test (stable)

cannot index into a value of type `&mut biginteger::BigInt<N>`

Check failure on line 755 in ff/src/biginteger/mod.rs

View workflow job for this annotation

GitHub Actions / Test assembly

cannot index into a value of type `&mut biginteger::BigInt<N>`
let t2 = *a << (64 - rhs);
*a >>= rhs;
*a |= t;
t = t2;
}
}
}
}
Pratyush marked this conversation as resolved.
Show resolved Hide resolved

impl<const N: usize> Shr<u32> for BigInt<N> {
type Output = Self;

/// Computes bitwise shift right operation.
///
/// Differently from the built-in numeric types (u8, u32, u64, etc.) this
/// operation does *not* return an underflow error if the number of bits
/// shifted is larger than N * 64. Instead the result will be saturated to
/// zero.
fn shr(mut self, mut rhs: u32) -> Self::Output {
self >>= rhs;
self
}
}

impl<const N: usize> Not for BigInt<N> {
type Output = Self;

Expand Down Expand Up @@ -809,6 +857,8 @@
+ for<'a> BitOrAssign<&'a Self>
+ BitOr<Self, Output = Self>
+ for<'a> BitOr<&'a Self, Output = Self>
+ Shr<u32, Output = Self>
Pratyush marked this conversation as resolved.
Show resolved Hide resolved
+ ShrAssign<u32>
{
/// Number of 64-bit limbs representing `Self`.
const NUM_LIMBS: usize;
Expand Down Expand Up @@ -947,6 +997,7 @@
/// div.divn(5);
/// assert_eq!(div, B::from(0u64));
/// ```
#[deprecated(since = "0.4.2", note = "please use the operator `>>` instead")]
fn divn(&mut self, amt: u32);

/// Returns true iff this number is odd.
Expand Down
24 changes: 23 additions & 1 deletion ff/src/biginteger/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,27 @@ fn biginteger_arithmetic_test<B: BigInteger>(a: B, b: B, zero: B) {
assert_eq!(a_mul2, a_plus_a);
}

fn biginteger_shr<B: BigInteger>() {
let mut rng = ark_std::test_rng();
let a = B::rand(&mut rng);
assert_eq!(a >> 0, a);

// Binary simple test
let a = B::from(256u64);
assert_eq!(a >> 2, B::from(64u64));

// Test saturated underflow
let a = B::from(1u64);
assert_eq!(a >> 5, B::from(0u64));

// Test null bits
let a = B::rand(&mut rng);
let b = a >> 3;
assert_eq!(b.get_bit(B::NUM_LIMBS * 64 - 1), false);
assert_eq!(b.get_bit(B::NUM_LIMBS * 64 - 2), false);
assert_eq!(b.get_bit(B::NUM_LIMBS * 64 - 3), false);
}

// Test for BigInt's bitwise operations
fn biginteger_bitwise_ops_test<B: BigInteger>() {
let mut rng = ark_std::test_rng();
Expand Down Expand Up @@ -139,7 +160,8 @@ fn test_biginteger<B: BigInteger>(zero: B) {
biginteger_arithmetic_test(a, b, zero);
biginteger_bits_test::<B>();
biginteger_conversion_test::<B>();
biginteger_bitwise_ops_test::<B>()
biginteger_bitwise_ops_test::<B>();
biginteger_shr::<B>();
}

#[test]
Expand Down
Loading