Пример #1
0
static void
test_value_move_ctor ()
{
    rw_info (0, __FILE__, __LINE__, "value move constructor");

#define INTEGER_CONSTANT        256

    std::tuple<int> it1 (INTEGER_CONSTANT);
    test (__LINE__, it1, INTEGER_CONSTANT);
    const int c = std::rand ();
    int i = c;   // move semantics can alter source value
    std::tuple<int> it2 (i);   // temporary source value
    test (__LINE__, it2, c);

    const std::tuple<int> it3 (INTEGER_CONSTANT);
    test (__LINE__, it3, INTEGER_CONSTANT);
    i = c;
    const std::tuple<int> it4 (i);
    test (__LINE__, it4, c);

    std::tuple<const int> ct1 (INTEGER_CONSTANT);
    test (__LINE__, ct1, INTEGER_CONSTANT);
    i = c;
    std::tuple<const int> ct2 (i);
    test (__LINE__, ct2, c);

    // ill-formed for tuples with element types containing references

    std::tuple<std::tuple<int> > nt (it1);
    test (__LINE__, nt, it1);

    std::tuple<long, const char*> pt (123456789L, "string");
    test (__LINE__, pt, 123456789L, (const char*) "string");

    const UserDefined src (c);
    UserDefined tmp (src);
    UserDefined::reset ();
    std::tuple<UserDefined> ut (tmp);
    UserDefined::expect.move_ctor = 1;
    test (__LINE__, ut, src);

    tmp = src;  ++UserDefined::expect.copy_asgn;
    std::tuple<bool, char, int, double, void*, UserDefined>
        bt (true, 'a', INTEGER_CONSTANT, 3.14159, (void*) 0, tmp);
    ++UserDefined::expect.move_ctor;
    test (__LINE__, bt,
          true, 'a', INTEGER_CONSTANT, 3.14159, (void*) 0, src);
}
Пример #2
0
static void
test_homo_move_ctor ()
{
    rw_info (0, __FILE__, __LINE__,
             "move constructor (homogenous tuples)");

    std::tuple<> et (std::tuple<> ()); _RWSTD_UNUSED (et);

    const int ci = std::rand ();

    std::tuple<int> it1 (ci);
    std::tuple<int> it2 (std::move (it1));
    test (__LINE__, it2, ci);

    std::tuple<const int> ct1 (ci);
    std::tuple<const int> ct2 = std::move (ct1);
    test (__LINE__, ct2, ci);

    std::tuple<std::tuple<int> > nt1 (it1);
    std::tuple<std::tuple<int> > nt2 = std::move (nt1);
    test (__LINE__, nt2, it1);

    std::tuple<long, const char*> pt1 (1234567890L, "string");
    std::tuple<long, const char*> pt2 (std::move (pt1));
    test (__LINE__, pt2, 1234567890L, (const char*) "string");

    const UserDefined ud (ci);
    std::tuple<UserDefined> ut1 (ud);
    UserDefined::reset ();
    std::tuple<UserDefined> ut2 (std::move (ut1));
    ++UserDefined::expect.move_ctor;
    test (__LINE__, ut2, ud);

    std::tuple<bool, char, int, double, void*, UserDefined>
        bt1 (true, 'a', ci, 3.14159, (void*) &ci, ud);
    ++UserDefined::expect.copy_ctor;
    std::tuple<bool, char, int, double, void*, UserDefined>
        bt2 (std::move (bt1));
    ++UserDefined::expect.move_ctor;
    test (__LINE__, bt2, true, 'a', ci, 3.14159, (void*) &ci, ud);
}
Пример #3
0
// Elliptic Curve Integrated Encryption Scheme
void ecies_ex () {
	// Degree 163 Binary Field from fips186-2
	use_NIST_B_233 ();
	EC_Domain_Parameters dp = NIST_B_233;

	ECPrivKey sk (dp);// generate a private key from the domain parameters
	ECPubKey pk (sk);// calculate the public key the private key

	// plaintext p1 = a, b, c, d
	OCTETSTR p1(4); p1[0]='a'; p1[1]='b'; p1[2]='c'; p1[3]='d';
	int i;
	for (i=0; i<4; i++) {
		std::cout << p1[i];
	}
	std::cout << std::endl;

	ECIES ct1 (p1, pk); // encrypt using the public key

	std::cout << "ct1: " << std::endl << ct1 << std::endl;
	OCTETSTR p2;
	try { // try to catch any exceptions if the tag is invalid
		p2 = ct1.decrypt(sk); // decrypt using the private key
	} catch (borzoiException e) { // print the error message and exit
		e.debug_print ();
		return;
	}
	for (i=0; i<4; i++) {
		std::cout << p2[i];
	}
	std::cout << std::endl;


	// DER encoding
	DER der_str (ct1);
	HexEncoder hex_str (der_str);

	// You might save the DER ecoded ciphertext to a file or send it over 
	// the network here.
	std::cout << "DER Encoding: " << hex_str << std::endl;
	
	ECIES ct2;
	try { // try to catch any DER parsing errors
		ct2 = der_str.toECIES (); // decode the DER string
	} catch (borzoiException e) { // print the error message and exit
		e.debug_print ();
		return;
	}

	std::cout << "ct2: " << std::endl << ct2 << std::endl;

	OCTETSTR p3;
	try { // try to catch any exceptions if the tag is invalid
		p3 = ct2.decrypt(sk); // decrypt using the private key
	} catch (borzoiException e) { // print the error message and exit
		e.debug_print ();
		return;
	}
	for (i=0; i<4; i++) {
		std::cout << p3[i];
	}
	std::cout << std::endl;

}
int main(int argc, char **argv)
{
    /* On our trusted system we generate a new key
     * (or read one in) and encrypt the secret data set.
     */

    long m=0, p=2, r=1; // Native plaintext space
                            // Computations will be 'modulo p'
    long L=16;          // Levels
    long c=3;           // Columns in key switching matrix
    long w=64;          // Hamming weight of secret key
    long d=0;
    long security = 128;
    ZZX G;
    m = FindM(security,L,c,p, d, 0, 0);


    FHEcontext context(m, p, r);
    // initialize context
    buildModChain(context, L, c);
    // modify the context, adding primes to the modulus chain
    FHESecKey secretKey(context);
    // construct a secret key structure
    const FHEPubKey& publicKey = secretKey;
    // an "upcast": FHESecKey is a subclass of FHEPubKey

    //if(0 == d)
    G = context.alMod.getFactorsOverZZ()[0];

    secretKey.GenSecKey(w);
    // actually generate a secret key with Hamming weight w

    addSome1DMatrices(secretKey);
    cout << "Generated key..." << endl;

    EncryptedArray ea(context, G);
    // constuct an Encrypted array object ea that is
    // associated with the given context and the polynomial G

    long nslots = ea.size();
    cout << "Vector Size " << nslots << endl;;

    vector<long> v1;
    for(int i = 0 ; i < nslots; i++) {
        v1.push_back(1);
       //v1.push_back(i*2);
    }
    Ctxt ct1(publicKey);
    startFHEtimer("ea.encrypt1");
    ea.encrypt(ct1, publicKey, v1);
    stopFHEtimer("ea.encrypt1");
        
    vector<long> v2;
    Ctxt ct2(publicKey);
    for(int i = 0 ; i < nslots; i++) {
        v2.push_back(1);
        //v2.push_back(i*3);
    }
    startFHEtimer("ea.encrypt2");
    ea.encrypt(ct2, publicKey, v2);
    stopFHEtimer("ea.encrypt2");

    // v1.mul(v2); // c3.multiplyBy(c2) 
    // ct2.multiplyBy(ct1);              
    // CheckCtxt(ct2, "c3*=c2");
    // debugCompare(ea,secretKey,v1,ct2);

    // On the public (untrusted) system we
    // can now perform our computation

    Ctxt ctSum = ct1;
    Ctxt ctProd = ct1;

    startFHEtimer("sum");
    ctSum += ct2;
    stopFHEtimer("sum");
    //ctProd *= ct2;
    startFHEtimer("product");
    ctProd *= ct2;
    //ctProd.multiplyBy(ct2);
    stopFHEtimer("product");
    //ea.mat_mul(ctProd,ct2);
    vector<long> res;

    startFHEtimer("decryptsum");
    ea.decrypt(ctSum, secretKey, res);
    stopFHEtimer("decryptsum");
    //cout << "All computations are modulo " << p << "." << endl;
    for (unsigned int i = 0; i<res.size(); i++){
        cout<< res[i];
    }

    for(unsigned int i = 0; i < res.size(); i++) {
        cout << v1[i] << " + " << v2[i] << " = " << res[i] << endl;
    }

    startFHEtimer("decryptproduct");
    ea.decrypt(ctProd, secretKey, res);
    stopFHEtimer("decryptproduct");
    for (unsigned int i = 0; i<res.size(); i++){
        cout<< res[i];
    }
    
    for(unsigned int i = 0; i < res.size(); i++) {
        cout << v1[i] << " * " << v2[i] << " = " << res[i] << endl;
    }
    printAllTimers();
    cout << endl;
    cout << "All computations are modulo " << p << "." << endl;
    return 0;
}