dev
MITSUNARI Shigeo 10 years ago
parent d55a10e311
commit b7148f7f3b
  1. 3
      include/mcl/fp.hpp
  2. 2
      include/mcl/fp_generator.hpp
  3. 2
      include/mcl/fp_proto.hpp
  4. 93
      include/mcl/op.hpp
  5. 88
      include/mcl/unit.hpp
  6. 2
      src/fp.cpp
  7. 2
      test/base_test.cpp

@ -23,9 +23,8 @@
#include <cybozu/itoa.hpp>
#include <cybozu/atoi.hpp>
#include <cybozu/bitvector.hpp>
#include <mcl/fp_base.hpp>
#include <mcl/op.hpp>
#include <mcl/fp_util.hpp>
#include <mcl/gmp_util.hpp>
#include <mcl/power.hpp>
namespace mcl {

@ -9,7 +9,7 @@
#include <stdio.h>
#include <assert.h>
#include <cybozu/exception.hpp>
#include <mcl/fp_base.hpp>
#include <mcl/op.hpp>
#if (CYBOZU_HOST == CYBOZU_HOST_INTEL) && (CYBOZU_OS_BIT == 64)

@ -6,7 +6,7 @@
@license modified new BSD license
http://opensource.org/licenses/BSD-3-Clause
*/
#include <mcl/fp_base.hpp>
#include <mcl/op.hpp>
#ifdef MCL_USE_LLVM

@ -1,26 +1,14 @@
#pragma once
/**
@file
@brief basic operation
@brief definition of Op
@author MITSUNARI Shigeo(@herumi)
@license modified new BSD license
http://opensource.org/licenses/BSD-3-Clause
*/
#include <assert.h>
#include <cybozu/inttype.hpp>
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4616)
#pragma warning(disable : 4800)
#pragma warning(disable : 4244)
#pragma warning(disable : 4127)
#pragma warning(disable : 4512)
#pragma warning(disable : 4146)
#endif
#include <mcl/gmp_util.hpp>
#ifdef _MSC_VER
#pragma warning(pop)
#endif
#include <mcl/unit.hpp>
#ifndef MCL_MAX_OP_BIT_N
#define MCL_MAX_OP_BIT_N 521
@ -32,12 +20,6 @@ struct FpGenerator;
namespace fp {
#if defined(CYBOZU_OS_BIT) && (CYBOZU_OS_BIT == 32)
typedef uint32_t Unit;
#else
typedef uint64_t Unit;
#endif
const size_t UnitBitN = sizeof(Unit) * 8;
const size_t maxOpUnitN = (MCL_MAX_OP_BIT_N + UnitBitN - 1) / UnitBitN;
struct Op;
@ -114,74 +96,9 @@ struct Op {
void init(const Unit *p, size_t bitLen);
static FpGenerator* createFpGenerator();
static void destroyFpGenerator(FpGenerator *fg);
private:
Op(const Op&);
void operator==(const Op&);
};
/*
get pp such that p * pp = -1 mod M,
where p is prime and M = 1 << 64(or 32).
@param pLow [in] p mod M
T is uint32_t or uint64_t
*/
template<class T>
T getMontgomeryCoeff(T pLow)
{
T ret = 0;
T t = 0;
T x = 1;
for (size_t i = 0; i < sizeof(T) * 8; i++) {
if ((t & 1) == 0) {
t += pLow;
ret += x;
}
t >>= 1;
x <<= 1;
}
return ret;
}
inline int compareArray(const Unit* x, const Unit* y, size_t n)
{
for (size_t i = n - 1; i != size_t(-1); i--) {
if (x[i] < y[i]) return -1;
if (x[i] > y[i]) return 1;
}
return 0;
}
inline bool isEqualArray(const Unit* x, const Unit* y, size_t n)
{
for (size_t i = 0; i < n; i++) {
if (x[i] != y[i]) return false;
}
return true;
}
inline bool isZeroArray(const Unit *x, size_t n)
{
for (size_t i = 0; i < n; i++) {
if (x[i]) return false;
}
return true;
}
inline void clearArray(Unit *x, size_t begin, size_t end)
{
for (size_t i = begin; i < end; i++) x[i] = 0;
}
inline void copyArray(Unit *y, const Unit *x, size_t n)
{
for (size_t i = 0; i < n; i++) y[i] = x[i];
}
inline void toArray(Unit *y, size_t yn, const mpz_srcptr x)
{
const int xn = x->_mp_size;
assert(xn >= 0);
const Unit* xp = (const Unit*)x->_mp_d;
assert(xn <= (int)yn);
copyArray(y, xp, xn);
clearArray(y, xn, yn);
}
} } // mcl::fp

@ -0,0 +1,88 @@
#pragma once
/**
@file
@brief definition of Unit and some functions
@author MITSUNARI Shigeo(@herumi)
@license modified new BSD license
http://opensource.org/licenses/BSD-3-Clause
*/
#include <cybozu/inttype.hpp>
#include <mcl/gmp_util.hpp>
namespace mcl { namespace fp {
#if defined(CYBOZU_OS_BIT) && (CYBOZU_OS_BIT == 32)
typedef uint32_t Unit;
#else
typedef uint64_t Unit;
#endif
const size_t UnitBitN = sizeof(Unit) * 8;
/*
get pp such that p * pp = -1 mod M,
where p is prime and M = 1 << 64(or 32).
@param pLow [in] p mod M
*/
inline Unit getMontgomeryCoeff(Unit pLow)
{
Unit ret = 0;
Unit t = 0;
Unit x = 1;
for (size_t i = 0; i < UnitBitN; i++) {
if ((t & 1) == 0) {
t += pLow;
ret += x;
}
t >>= 1;
x <<= 1;
}
return ret;
}
inline int compareArray(const Unit* x, const Unit* y, size_t n)
{
for (size_t i = n - 1; i != size_t(-1); i--) {
if (x[i] < y[i]) return -1;
if (x[i] > y[i]) return 1;
}
return 0;
}
inline bool isEqualArray(const Unit* x, const Unit* y, size_t n)
{
for (size_t i = 0; i < n; i++) {
if (x[i] != y[i]) return false;
}
return true;
}
inline bool isZeroArray(const Unit *x, size_t n)
{
for (size_t i = 0; i < n; i++) {
if (x[i]) return false;
}
return true;
}
inline void clearArray(Unit *x, size_t begin, size_t end)
{
for (size_t i = begin; i < end; i++) x[i] = 0;
}
inline void copyArray(Unit *y, const Unit *x, size_t n)
{
for (size_t i = 0; i < n; i++) y[i] = x[i];
}
inline void toArray(Unit *y, size_t yn, const mpz_srcptr x)
{
const int xn = x->_mp_size;
assert(xn >= 0);
const Unit* xp = (const Unit*)x->_mp_d;
assert(xn <= (int)yn);
copyArray(y, xp, xn);
clearArray(y, xn, yn);
}
} } // mcl::fp

@ -1,4 +1,4 @@
#include <mcl/fp_base.hpp>
#include <mcl/op.hpp>
#ifdef USE_MONT_FP
#include <mcl/fp_generator.hpp>
#endif

@ -1,6 +1,6 @@
#include <map>
#define MCL_USE_LLVM
#include <mcl/fp_base.hpp>
#include <mcl/op.hpp>
#include <cybozu/test.hpp>
#include <cybozu/benchmark.hpp>
#include <cybozu/xorshift.hpp>

Loading…
Cancel
Save