forked from data61/cuda-fixnum
-
Notifications
You must be signed in to change notification settings - Fork 8
/
paillier_encrypt.cu
48 lines (40 loc) · 1.3 KB
/
paillier_encrypt.cu
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
#pragma once
#include "functions/quorem_preinv.cu"
#include "functions/multi_modexp.cu"
#include "modnum/modnum_monty_cios.cu"
namespace cuFIXNUM {
template< typename fixnum >
class paillier_encrypt {
public:
__device__ paillier_encrypt(fixnum n_)
: n(n_), n_sqr(square(n_)), pow(n_sqr, n_), mod_n2(n_sqr) { }
/*
* NB: In reality, the values r^n should be calculated out-of-band or
* stock-piled and piped into an encryption function.
*/
__device__ void operator()(fixnum &ctxt, fixnum m, fixnum r) const {
// TODO: test this properly
//assert(fixnum::slot_layout::laneIdx() < fixnum::SLOT_WIDTH/2 || m == 0);
fixnum::mul_lo(m, m, n);
fixnum::incr_cy(m);
pow(r, r);
fixnum c_hi, c_lo;
fixnum::mul_wide(c_hi, c_lo, m, r);
mod_n2(ctxt, c_hi, c_lo);
}
private:
typedef modnum_monty_cios<fixnum> modnum;
fixnum n;
fixnum n_sqr;
modexp<modnum> pow;
quorem_preinv<fixnum> mod_n2;
// TODO: It is flipping stupid that this is necessary.
__device__ fixnum square(fixnum n) {
fixnum n2;
// TODO: test this properly
//assert(fixnum::slot_layout::laneIdx() < fixnum::SLOT_WIDTH/2 || n == 0);
fixnum::sqr_lo(n2, n);
return n2;
}
};
} // End namespace cuFIXNUM