aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMITSUNARI Shigeo <herumi@nifty.com>2017-04-08 08:25:00 +0800
committerMITSUNARI Shigeo <herumi@nifty.com>2017-04-08 13:35:10 +0800
commit22ea6ac90222d3e4b3a8549c55779388ede78301 (patch)
tree1f240ec930f350b49dece7f05e80c6261adbab02 /src
parent84a05e93499d6a01abdf95a634e5521e51b361ee (diff)
downloadtangerine-mcl-22ea6ac90222d3e4b3a8549c55779388ede78301.tar.gz
tangerine-mcl-22ea6ac90222d3e4b3a8549c55779388ede78301.tar.zst
tangerine-mcl-22ea6ac90222d3e4b3a8549c55779388ede78301.zip
add copyByteToUnitAsLE and copyUnitToByteAsLE
_
Diffstat (limited to 'src')
-rw-r--r--src/fp.cpp55
1 files changed, 53 insertions, 2 deletions
diff --git a/src/fp.cpp b/src/fp.cpp
index 490699e..ed6601d 100644
--- a/src/fp.cpp
+++ b/src/fp.cpp
@@ -1,6 +1,7 @@
#include <mcl/op.hpp>
#include <mcl/util.hpp>
#include <cybozu/crypto.hpp>
+#include <cybozu/endian.hpp>
#include "conversion.hpp"
#include "fp_generator.hpp"
#include "low_func.hpp"
@@ -36,6 +37,22 @@ void Op::destroyFpGenerator(FpGenerator *)
}
#endif
+inline void setUnitAsLE(void *p, Unit x)
+{
+#if CYBOZU_OS_BIT == 32
+ cybozu::Set32bitAsLE(p, x);
+#else
+ cybozu::Set64bitAsLE(p, x);
+#endif
+}
+inline Unit getUnitAsLE(const void *p)
+{
+#if CYBOZU_OS_BIT == 32
+ return cybozu::Get32bitAsLE(p);
+#else
+ return cybozu::Get64bitAsLE(p);
+#endif
+}
/*
use prefix if base conflicts with prefix
*/
@@ -468,6 +485,38 @@ void arrayToStr(std::string& str, const Unit *x, size_t n, int base, bool withPr
}
}
+void copyUnitToByteAsLE(void *dst, const Unit *src, size_t byteSize)
+{
+ uint8_t *p = reinterpret_cast<uint8_t*>(dst);
+ while (byteSize >= sizeof(Unit)) {
+ setUnitAsLE(p, *src++);
+ p += sizeof(Unit);
+ byteSize -= sizeof(Unit);
+ }
+ if (byteSize == 0) return;
+ Unit x = *src;
+ while (byteSize) {
+ *p++ = static_cast<uint8_t>(x);
+ x >>= 8;
+ byteSize--;
+ }
+}
+
+void copyByteToUnitAsLE(Unit *dst, const void *src, size_t byteSize)
+{
+ const uint8_t *p = reinterpret_cast<const uint8_t*>(src);
+ while (byteSize >= sizeof(Unit)) {
+ *dst++ = getUnitAsLE(p);
+ p += sizeof(Unit);
+ byteSize -= sizeof(Unit);
+ }
+ if (byteSize == 0) return;
+ Unit x = 0;
+ for (size_t i = 0; i < byteSize; i++) {
+ x |= Unit(p[i]) << (i * 8);
+ }
+ *dst = x;
+}
void copyAndMask(Unit *y, const void *x, size_t xByteSize, const Op& op, bool doMask)
{
const size_t fpByteSize = sizeof(Unit) * op.N;
@@ -475,8 +524,10 @@ void copyAndMask(Unit *y, const void *x, size_t xByteSize, const Op& op, bool do
if (!doMask) throw cybozu::Exception("fp:copyAndMask:bad size") << xByteSize << fpByteSize;
xByteSize = fpByteSize;
}
- memcpy(y, x, xByteSize);
- memset((char *)y + xByteSize, 0, fpByteSize - xByteSize);
+ copyByteToUnitAsLE(y, x, xByteSize);
+ for (size_t i = (xByteSize + sizeof(Unit) - 1) / sizeof(Unit); i < op.N; i++) {
+ y[i] = 0;
+ }
if (!doMask) {
if (isGreaterOrEqualArray(y, op.p, op.N)) throw cybozu::Exception("fp:copyAndMask:large x");
return;