aboutsummaryrefslogtreecommitdiffstats
path: root/include/mcl/util.hpp
blob: 4acb6d1876d280e4856689da94a697e16af9beca (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#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 <mcl/gmp_util.hpp>

namespace mcl { namespace fp {

/*
    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
*/
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;
}

template<class T>
int compareArray(const T* x, const T* 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;
}

template<class T>
bool isEqualArray(const T* x, const T* y, size_t n)
{
    for (size_t i = 0; i < n; i++) {
        if (x[i] != y[i]) return false;
    }
    return true;
}

template<class T>
bool isZeroArray(const T *x, size_t n)
{
    for (size_t i = 0; i < n; i++) {
        if (x[i]) return false;
    }
    return true;
}

template<class T>
void clearArray(T *x, size_t begin, size_t end)
{
    for (size_t i = begin; i < end; i++) x[i] = 0;
}

template<class T>
void copyArray(T *y, const T *x, size_t n)
{
    for (size_t i = 0; i < n; i++) y[i] = x[i];
}

template<class T>
void toArray(T *y, size_t yn, const mpz_srcptr x)
{
    const int xn = x->_mp_size;
    assert(xn >= 0);
    const T* xp = (const T*)x->_mp_d;
    assert(xn <= (int)yn);
    copyArray(y, xp, xn);
    clearArray(y, xn, yn);
}

/*
    get random value less than in[]
    n = (bitLen + sizeof(T) * 8) / (sizeof(T) * 8)
    input  in[0..n)
    output out[n..n)
    0 <= out < in
*/
template<class RG, class T>
void getRandVal(T *out, RG& rg, const T *in, size_t bitLen)
{
    const size_t TBitN = sizeof(T) * 8;
    const size_t n = (bitLen + TBitN - 1) / TBitN;
    const size_t rem = bitLen & (TBitN - 1);
    for (;;) {
        rg.read(out, n);
        if (rem > 0) out[n - 1] &= (T(1) << rem) - 1;
        if (compareArray(out, in, n) < 0) return;
    }
}

} } // mcl::fp