Skip to content

version 6.2.0 #16

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

Merged
merged 1 commit into from
May 10, 2023
Merged
Show file tree
Hide file tree
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
8 changes: 4 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

cmake_minimum_required( VERSION 3.14 )

project( scl VERSION 6.1.0 DESCRIPTION "Secure Computation Library" )
project( scl VERSION 6.2.0 DESCRIPTION "Secure Computation Library" )

if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
Expand All @@ -39,6 +39,7 @@ set(SCL_SOURCE_FILES
src/scl/util/prg.cc
src/scl/util/sha3.cc
src/scl/util/sha256.cc
src/scl/util/cmdline.cc

src/scl/math/mersenne61.cc
src/scl/math/mersenne127.cc
Expand Down Expand Up @@ -100,6 +101,7 @@ if(CMAKE_BUILD_TYPE MATCHES "Debug")
test/scl/util/test_sha3.cc
test/scl/util/test_sha256.cc
test/scl/util/test_ecdsa.cc
test/scl/util/test_cmdline.cc

test/scl/gf7.cc
test/scl/math/test_mersenne61.cc
Expand Down Expand Up @@ -152,10 +154,8 @@ if(CMAKE_BUILD_TYPE MATCHES "Debug")
include(Catch)
include(${CMAKE_SOURCE_DIR}/cmake/CodeCoverage.cmake)

# Lower the max size of Vec/Mat reads to speed up tests
add_compile_definitions(MAX_VEC_READ_SIZE=1024)
add_compile_definitions(MAX_MAT_READ_SIZE=1024)
add_compile_definitions(SCL_TEST_DATA_DIR="${CMAKE_SOURCE_DIR}/test/data/")
add_compile_definitions(SCL_UTIL_NO_EXIT_ON_ERROR)

add_executable(scl_test ${SCL_SOURCE_FILES} ${SCL_TEST_SOURCE_FILES})
target_link_libraries(scl_test Catch2::Catch2 pthread)
Expand Down
6 changes: 6 additions & 0 deletions RELEASE.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
6.2.0: More functionality for Number
- Add modulo operator to Number.
- Add some mathematical functions that operate on numbers.
- Make Number serializable; add Serializer specialization.
- Add a simple command-line argument parser.

6.1.0: Extend serialization functionality
- Make Write methods return the number of bytes written.
- Make it possible to serialize vectors with arbitrary content.
Expand Down
133 changes: 110 additions & 23 deletions include/scl/math/number.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,78 @@

namespace scl::math {

class Number;

/**
* @brief Compute the least common multiple of two numbers.
* @param a the first number.
* @param b the second number.
* @return \f$lcm(a, b)\f$.
*/
Number LCM(const Number& a, const Number& b);

/**
* @brief Compute the greatest common divisor of two numbers.
* @param a the first number.
* @param b the second number.
* @return \f$gcd(a, b)\f$.
*/
Number GCD(const Number& a, const Number& b);

/**
* @brief Compute the modular inverse of a number.
* @param val the value to invert.
* @param mod the modulus.
* @return \f$val^{-1} \mod mod \f$.
* @throws std::logic_error if \p val is not invertible.
* @throws std::invalid_argument if \p mod is 0.
*/
Number ModInverse(const Number& val, const Number& mod);

/**
* @brief Compute a modular exponentiation.
* @param base the base.
* @param exp the exponent.
* @param mod the modulus.
* @return \f$base^{exp} \mod mod\f$.
*/
Number ModExp(const Number& base, const Number& exp, const Number& mod);

/**
* @brief Arbitrary precision integer.
*/
class Number final : Print<Number> {
public:
/**
* @brief Generate a random Number.
* @param bits the number of bits in the resulting number
* @param prg a prg for generating the random number
* @return a random Number
* @param bits the number of bits in the resulting number.
* @param prg a prg for generating the random number.
* @return a random Number.
*/
static Number Random(std::size_t bits, util::PRG& prg);

/**
* @brief Generate a random prime.
* @param bits the number of bits in the resulting prime.
* @param prg a prg for generating the random prime.
* @return a random prime.
*/
static Number RandomPrime(std::size_t bits, util::PRG& prg);

/**
* @brief Read a Number from a string
* @param str the string
* @return a Number.
*/
static Number FromString(const std::string& str);

/**
* @brief Read a number from a buffer.
* @param buf the buffer.
* @return a Number.
*/
static Number Read(const unsigned char* buf);

/**
* @brief Construct a Number from an int.
* @param value the int
Expand Down Expand Up @@ -85,7 +137,7 @@ class Number final : Print<Number> {
Number copy(number);
swap(*this, copy);
return *this;
};
}

/**
* @brief Move assignment from a Number.
Expand All @@ -95,7 +147,7 @@ class Number final : Print<Number> {
Number& operator=(Number&& number) noexcept {
swap(*this, number);
return *this;
};
}

/**
* @brief In-place addition of two numbers.
Expand All @@ -105,7 +157,7 @@ class Number final : Print<Number> {
Number& operator+=(const Number& number) {
*this = *this + number;
return *this;
};
}

/**
* @brief Add two numbers.
Expand All @@ -122,7 +174,7 @@ class Number final : Print<Number> {
Number& operator-=(const Number& number) {
*this = *this - number;
return *this;
};
}

/**
* @brief Subtract two Numbers.
Expand All @@ -145,7 +197,7 @@ class Number final : Print<Number> {
Number& operator*=(const Number& number) {
*this = *this * number;
return *this;
};
}

/**
* @brief Multiply two Numbers.
Expand All @@ -162,7 +214,7 @@ class Number final : Print<Number> {
Number& operator/=(const Number& number) {
*this = *this / number;
return *this;
};
}

/**
* @brief Divide two Numbers.
Expand All @@ -171,6 +223,23 @@ class Number final : Print<Number> {
*/
Number operator/(const Number& number) const;

/**
* @brief In-place modulo operator.
* @param mod the modulus.
* @return this.
*/
Number& operator%=(const Number& mod) {
*this = *this % mod;
return *this;
}

/**
* @brief Modulo operation.
* @param mod the modulus.
* @return \p this modulo \p mod.
*/
Number operator%(const Number& mod) const;

/**
* @brief In-place left shift.
* @param shift the amount to left shift
Expand All @@ -179,7 +248,7 @@ class Number final : Print<Number> {
Number& operator<<=(int shift) {
*this = *this << shift;
return *this;
};
}

/**
* @brief Perform a left shift of a Number.
Expand All @@ -196,7 +265,7 @@ class Number final : Print<Number> {
Number& operator>>=(int shift) {
*this = *this >> shift;
return *this;
};
}

/**
* @brief Perform a right shift of a Number.
Expand All @@ -213,7 +282,7 @@ class Number final : Print<Number> {
Number& operator^=(const Number& number) {
*this = *this ^ number;
return *this;
};
}

/**
* @brief Exclusive or of two numbers.
Expand All @@ -230,7 +299,7 @@ class Number final : Print<Number> {
Number& operator|=(const Number& number) {
*this = *this | number;
return *this;
};
}

/**
* @brief operator |
Expand All @@ -247,7 +316,7 @@ class Number final : Print<Number> {
Number& operator&=(const Number& number) {
*this = *this & number;
return *this;
};
}

/**
* @brief operator &
Expand Down Expand Up @@ -280,42 +349,47 @@ class Number final : Print<Number> {
*/
friend bool operator==(const Number& lhs, const Number& rhs) {
return lhs.Compare(rhs) == 0;
};
}

/**
* @brief In-equality of two numbers.
*/
friend bool operator!=(const Number& lhs, const Number& rhs) {
return lhs.Compare(rhs) != 0;
};
}

/**
* @brief Strictly less-than of two numbers.
*/
friend bool operator<(const Number& lhs, const Number& rhs) {
return lhs.Compare(rhs) < 0;
};
}

/**
* @brief Less-than-or-equal of two numbers.
*/
friend bool operator<=(const Number& lhs, const Number& rhs) {
return lhs.Compare(rhs) <= 0;
};
}

/**
* @brief Strictly greater-than of two numbers.
*/
friend bool operator>(const Number& lhs, const Number& rhs) {
return lhs.Compare(rhs) > 0;
};
}

/**
* @brief Greater-than-or-equal of two numbers.
*/
friend bool operator>=(const Number& lhs, const Number& rhs) {
return lhs.Compare(rhs) >= 0;
};
}

/**
* @brief Get the size of this number in bytes.
*/
std::size_t ByteSize() const;

/**
* @brief Get the size of this Number in bits.
Expand All @@ -340,15 +414,21 @@ class Number final : Print<Number> {
*/
bool Odd() const {
return TestBit(0);
};
}

/**
* @brief Test if this Number is even.
* @return true if this Number is even.
*/
bool Even() const {
return !Odd();
};
}

/**
* @brief Write this number to a buffer.
* @param buf the buffer.
*/
void Write(unsigned char* buf) const;

/**
* @brief Return a string representation of this Number.
Expand All @@ -362,10 +442,17 @@ class Number final : Print<Number> {
friend void swap(Number& first, Number& second) {
using std::swap;
swap(first.m_value, second.m_value);
};
}

private:
mpz_t m_value;

friend Number LCM(const Number& a, const Number& b);
friend Number GCD(const Number& a, const Number& b);
friend Number ModInverse(const Number& val, const Number& mod);
friend Number ModExp(const Number& base,
const Number& exp,
const Number& mod);
};

} // namespace scl::math
Expand Down
Loading