Commit 62455e49 authored by yiwenshao's avatar yiwenshao

add comments for UDF

parent c791641c
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include <crypto/paillier.hh> #include <crypto/paillier.hh>
#include <crypto/arc4.hh> #include <crypto/arc4.hh>
#include <NTL/ZZ.h> #include <NTL/ZZ.h>
#include <assert.h>
using namespace NTL; using namespace NTL;
using std::string; using std::string;
using std::cout; using std::cout;
...@@ -26,12 +27,13 @@ static uint64_t cur_usec() { ...@@ -26,12 +27,13 @@ static uint64_t cur_usec() {
} }
/*static void test_paillier_priv(){ static void test_paillier_priv(){
Paillier_priv * sk; Paillier_priv * sk;
string seed_key="1234567678"; string seed_key="1234567678";
const std::unique_ptr<streamrng<arc4>> prng(new streamrng<arc4>(seed_key)); const std::unique_ptr<streamrng<arc4>> prng(new streamrng<arc4>(seed_key));
urandom u; urandom u;
sk = new Paillier_priv(Paillier_priv::keygen(prng.get(), 1024)); sk = new Paillier_priv(Paillier_priv::keygen(prng.get(), 1024));
for(int i=0;i<1024;i++){ for(int i=0;i<1024;i++){
ZZ pt0 = u.rand_zz_mod(to_ZZ(1) << 20); ZZ pt0 = u.rand_zz_mod(to_ZZ(1) << 20);
ZZ pt1 = u.rand_zz_mod(to_ZZ(1) << 20); ZZ pt1 = u.rand_zz_mod(to_ZZ(1) << 20);
...@@ -45,7 +47,7 @@ static uint64_t cur_usec() { ...@@ -45,7 +47,7 @@ static uint64_t cur_usec() {
cout<<"PASS"<<endl; cout<<"PASS"<<endl;
} }
}*/ }
static void test_paillier_time() { static void test_paillier_time() {
Paillier_priv * sk; Paillier_priv * sk;
...@@ -53,9 +55,9 @@ static void test_paillier_time() { ...@@ -53,9 +55,9 @@ static void test_paillier_time() {
const std::unique_ptr<streamrng<arc4>> prng(new streamrng<arc4>(seed_key)); const std::unique_ptr<streamrng<arc4>> prng(new streamrng<arc4>(seed_key));
urandom u; urandom u;
sk = new Paillier_priv(Paillier_priv::keygen(prng.get(), 1024)); sk = new Paillier_priv(Paillier_priv::keygen(prng.get(), 1024));
ZZ pt0 = u.rand_zz_mod(to_ZZ(1) << 20); ZZ pt0 = u.rand_zz_mod(to_ZZ(1) << 20);
cout<<"numOfTests: "<<numOfTest<<endl;
ZZ enc0; ZZ enc0;
uint64_t start = cur_usec(); uint64_t start = cur_usec();
for(int i=0;i<numOfTest;i++){ for(int i=0;i<numOfTest;i++){
...@@ -72,9 +74,11 @@ static void test_paillier_time() { ...@@ -72,9 +74,11 @@ static void test_paillier_time() {
end = cur_usec(); end = cur_usec();
cout<<"decryption: "<<(end-start)*1.0/numOfTest<<endl; cout<<"decryption: "<<(end-start)*1.0/numOfTest<<endl;
} }
int int
main() { main() {
test_paillier_time(); test_paillier_time();
test_paillier_priv();
return 0; return 0;
} }
...@@ -399,12 +399,11 @@ cryptdb_searchSWP_init(UDF_INIT *const initid, UDF_ARGS *const args, ...@@ -399,12 +399,11 @@ cryptdb_searchSWP_init(UDF_INIT *const initid, UDF_ARGS *const args,
if (args->arg_count != 3 || if (args->arg_count != 3 ||
args->arg_type[0] != STRING_RESULT || args->arg_type[0] != STRING_RESULT ||
args->arg_type[1] != STRING_RESULT || args->arg_type[1] != STRING_RESULT ||
args->arg_type[2] != STRING_RESULT) args->arg_type[2] != STRING_RESULT){
{ strcpy(message, "Usage: cryptdb_searchSWP(string ciphertext,\
strcpy(message, "Usage: cryptdb_searchSWP(string ciphertext, string ciph, string wordKey)"); string ciph, string wordKey)");
return 1; return 1;
} }
Token *const t = new Token(); Token *const t = new Token();
uint64_t ciphLen; uint64_t ciphLen;
...@@ -442,11 +441,16 @@ cryptdb_searchSWP(UDF_INIT *const initid, UDF_ARGS *const args, ...@@ -442,11 +441,16 @@ cryptdb_searchSWP(UDF_INIT *const initid, UDF_ARGS *const args,
} }
/**********************************************************************
*************************Begin UDF for SUM******************************
***********************************************************************
*/
struct agg_state { struct agg_state {
ZZ sum; ZZ sum;/*the results of sum*/
ZZ n2; ZZ n2; /*n2 is the public key and for each group, n2 must be set*/
bool n2_set; bool n2_set;
void *rbuf; void *rbuf;/*convert sum to void* and return*/
}; };
my_bool my_bool
...@@ -455,8 +459,7 @@ cryptdb_agg_init(UDF_INIT *const initid, UDF_ARGS *const args, ...@@ -455,8 +459,7 @@ cryptdb_agg_init(UDF_INIT *const initid, UDF_ARGS *const args,
{ {
if (args->arg_count != 2 || if (args->arg_count != 2 ||
args->arg_type[0] != STRING_RESULT || args->arg_type[0] != STRING_RESULT ||
args->arg_type[1] != STRING_RESULT) args->arg_type[1] != STRING_RESULT){
{
strcpy(message, "Usage: cryptdb_agg(string ciphertext, string pubkey)"); strcpy(message, "Usage: cryptdb_agg(string ciphertext, string pubkey)");
return 1; return 1;
} }
...@@ -469,58 +472,51 @@ cryptdb_agg_init(UDF_INIT *const initid, UDF_ARGS *const args, ...@@ -469,58 +472,51 @@ cryptdb_agg_init(UDF_INIT *const initid, UDF_ARGS *const args,
} }
void void
cryptdb_agg_deinit(UDF_INIT *const initid) cryptdb_agg_deinit(UDF_INIT *const initid){
{
agg_state *const as = reinterpret_cast<agg_state *>(initid->ptr); agg_state *const as = reinterpret_cast<agg_state *>(initid->ptr);
free(as->rbuf); free(as->rbuf);
delete as; delete as;
} }
// When we want to add by zero for HOM values we can multiply our value /* When we want to add by zero for HOM values we can multiply our value
// by 1. by 1.*/
void void
cryptdb_agg_clear(UDF_INIT *const initid, char *const is_null, char *const error) cryptdb_agg_clear(UDF_INIT *const initid, char *const is_null, char *const error){
{
agg_state *const as = reinterpret_cast<agg_state *>(initid->ptr); agg_state *const as = reinterpret_cast<agg_state *>(initid->ptr);
as->sum = to_ZZ(1); as->sum = to_ZZ(1);
as->n2_set = 0; as->n2_set = 0;
} }
//args will be element to add, constant N2 /*args will be element to add, constant N2*/
my_bool my_bool
cryptdb_agg_add(UDF_INIT *const initid, UDF_ARGS *const args, cryptdb_agg_add(UDF_INIT *const initid, UDF_ARGS *const args,
char *const is_null, char *const error) char *const is_null, char *const error){
{
//cerr << "in agg_add \n";
agg_state *const as = reinterpret_cast<agg_state *>(initid->ptr); agg_state *const as = reinterpret_cast<agg_state *>(initid->ptr);
/*for each group, read and set n2*/
if (!as->n2_set) { if (!as->n2_set) {
//cerr << "n2 length is " << args->lengths[1] << "\n";
//cerr << "n2 first byte is " << (int)args->args[1][0] << "\n";
ZZFromBytes(as->n2, ZZFromBytes(as->n2,
reinterpret_cast<const uint8_t *>(args->args[1]), reinterpret_cast<const uint8_t *>(args->args[1]),
args->lengths[1]); args->lengths[1]);
//cerr << "n2 is " << as->n2 << "\n";
as->n2_set = 1; as->n2_set = 1;
} }
/*add current item e, which is converted from args->args[0]*/
ZZ e; ZZ e;
if (NULL == args->args[0]) { if (NULL == args->args[0]) {
e = to_ZZ(1); e = to_ZZ(1);
} else { } else {
ZZFromBytes(e, reinterpret_cast<const uint8_t *>(args->args[0]), ZZFromBytes(e, reinterpret_cast<const uint8_t *>(args->args[0]),
args->lengths[0]); args->lengths[0]);
} }
std::cerr << "element to add " << e << "\n";
//cerr << "element to add " << e << "\n";
MulMod(as->sum, as->sum, e, as->n2); MulMod(as->sum, as->sum, e, as->n2);
//cerr << "sum so far " << as->sum << "\n"; std::cerr << "sum so far " << as->sum << "\n";
return true; return true;
} }
char * char *
cryptdb_agg(UDF_INIT *const initid, UDF_ARGS *const args, char *const result, cryptdb_agg(UDF_INIT *const initid, UDF_ARGS *const args, char *const result,
unsigned long *const length, char *const is_null, char *const error) unsigned long *const length, char *const is_null, char *const error){
{
agg_state *const as = reinterpret_cast<agg_state *>(initid->ptr); agg_state *const as = reinterpret_cast<agg_state *>(initid->ptr);
BytesFromZZ(static_cast<uint8_t *>(as->rbuf), as->sum, BytesFromZZ(static_cast<uint8_t *>(as->rbuf), as->sum,
Paillier_len_bytes); Paillier_len_bytes);
...@@ -528,6 +524,13 @@ cryptdb_agg(UDF_INIT *const initid, UDF_ARGS *const args, char *const result, ...@@ -528,6 +524,13 @@ cryptdb_agg(UDF_INIT *const initid, UDF_ARGS *const args, char *const result,
return static_cast<char *>(as->rbuf); return static_cast<char *>(as->rbuf);
} }
/*
*****************************************************************************
*/
// for update with increment // for update with increment
// > UNUSED // > UNUSED
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment