Skip to content

Commit

Permalink
Implemented NOT and improved tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hdvanegasm committed Dec 13, 2023
1 parent 0b65245 commit ede2ff3
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 17 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,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`.
- [\#713](https://github.com/arkworks-rs/algebra/pull/713) (`ark-ff`) Add support for bitwise operations AND, OR, NOT, and XOR between `BigInteger`.

### Breaking changes

Expand Down
15 changes: 14 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};
use core::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Not};

use crate::{
bits::{BitIteratorBE, BitIteratorLE},
Expand Down Expand Up @@ -738,6 +738,18 @@ impl<const N: usize> BitOr for BigInt<N> {
}
}

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

fn not(self) -> Self::Output {
let mut result = Self::zero();
for i in 0..N {
result.0[i] = !self.0[i];
}
result
}
}

/// Compute the signed modulo operation on a u64 representation, returning the result.
/// If n % modulus > modulus / 2, return modulus - n
/// # Example
Expand Down Expand Up @@ -800,6 +812,7 @@ pub trait BigInteger:
+ BitAnd<Self>
+ BitOrAssign<Self>
+ BitOr<Self>
+ Not

This comment has been minimized.

Copy link
@mmaker

mmaker Dec 13, 2023

Member

Not here is going to be confusing -because I should perform modular operations - I don't think it needs to be in the trait definition.

This comment has been minimized.

Copy link
@mmaker

mmaker Dec 13, 2023

Member

give me a sec to pimp your PR

This comment has been minimized.

Copy link
@hdvanegasm

hdvanegasm Dec 13, 2023

Author Contributor

Not here is going to be confusing -because I should perform modular operations - I don't think it needs to be in the trait definition.

Are you suggesting to remove it from the trait definition and keep the impl for BigInt<N>?

{
/// Number of 64-bit limbs representing `Self`.
const NUM_LIMBS: usize;
Expand Down
40 changes: 25 additions & 15 deletions ff/src/biginteger/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,44 +53,54 @@ fn biginteger_arithmetic_test<B: BigInteger>(a: B, b: B, zero: B) {

// Test for BigInt's bitwise operations
fn biginteger_bitwise_ops_test<B: BigInteger>() {
let mut rng = ark_std::test_rng();

// Test XOR
// a xor a = 0
let a: BigInt<4> = BigInt::from(4_u64);
let a: BigInt<4> = UniformRand::rand(&mut rng);
let a_clone = a.clone();
assert_eq!(a ^ a_clone, BigInt::from(0_u64));

// Testing a xor b xor b
let a: BigInt<4> = BigInt::from(4_u64);
let b = BigInt::from(5_u64);
let a: BigInt<4> = UniformRand::rand(&mut rng);
let b: BigInt<4> = UniformRand::rand(&mut rng);
let a_clone = a.clone();
let b_clone = b.clone();
let xor_ab = a ^ b;
assert_eq!(xor_ab ^ b_clone, BigInt::from(4_u64));
assert_eq!(xor_ab ^ b_clone, a_clone);

// Test OR
// 1 or 1 = 1
let a: BigInt<4> = BigInt::from(1_u64);
// a or a = a
let a: BigInt<4> = UniformRand::rand(&mut rng);
let a_clone = a.clone();
assert_eq!(a | a_clone, BigInt::from(1_u64));
assert_eq!(a | a_clone, a);

// Testing a or b or b
let a: BigInt<4> = BigInt::from(4_u64);
let b = BigInt::from(5_u64);
let a: BigInt<4> = UniformRand::rand(&mut rng);
let b: BigInt<4> = UniformRand::rand(&mut rng);
let b_clone = b.clone();
let or_ab = a | b;
assert_eq!(or_ab | b_clone, BigInt::from(5_u64));
assert_eq!(or_ab | b_clone, a | b);

// Test AND
// a and a = a
let a: BigInt<4> = BigInt::from(2_u64);
let a: BigInt<4> = UniformRand::rand(&mut rng);
let a_clone = a.clone();
assert_eq!(a & a_clone, BigInt::from(2_u64));
assert_eq!(a & a_clone, a);

// Testing a and a and b.
let a: BigInt<4> = BigInt::from(4_u64);
let b = BigInt::from(5_u64);
let a: BigInt<4> = UniformRand::rand(&mut rng);
let b: BigInt<4> = UniformRand::rand(&mut rng);
let b_clone = b.clone();
let and_ab = a & b;
assert_eq!(and_ab & b_clone, BigInt::from(4_u64));
assert_eq!(and_ab & b_clone, a & b);

// Testing De Morgan's law
let a: BigInt<4> = UniformRand::rand(&mut rng);
let b = UniformRand::rand(&mut rng);
let de_morgan_lhs = !(a | b);
let de_morgan_rhs = (!a) & (!b);
assert_eq!(de_morgan_lhs, de_morgan_rhs);
}

// Test correctness of BigInteger's bit values
Expand Down

0 comments on commit ede2ff3

Please sign in to comment.