void StringTest::testConstructor() { cxxtools::String s1; CXXTOOLS_UNIT_ASSERT(s1 == cxxtools::String(L"")); cxxtools::String s2(L"abcde"); CXXTOOLS_UNIT_ASSERT(s2 == L"abcde"); cxxtools::String s3(L"abcde", 3); CXXTOOLS_UNIT_ASSERT(s3 == L"abc"); cxxtools::String s4(3, 'x'); CXXTOOLS_UNIT_ASSERT(s4 == L"xxx"); cxxtools::String s5(s2); CXXTOOLS_UNIT_ASSERT(s5 == L"abcde"); cxxtools::String s6(s2, 1); CXXTOOLS_UNIT_ASSERT(s6 == L"bcde"); cxxtools::String s7(s2, 1, 3); CXXTOOLS_UNIT_ASSERT(s7 == L"bcd"); cxxtools::String s10; CXXTOOLS_UNIT_ASSERT(s10 == cxxtools::String(L"")); const cxxtools::Char c11[] = { 'a', 'b', 'c', 'd', 'e', '\0' }; cxxtools::String s11(c11); CXXTOOLS_UNIT_ASSERT(s11 == c11); const cxxtools::Char c12[] = { 'a', 'b', 'c', '\0' }; cxxtools::String s12(L"abcde", 3); CXXTOOLS_UNIT_ASSERT(s12 == c12); const cxxtools::Char c13[] = { 'x', 'x', 'x', '\0' }; cxxtools::String s13(3, 'x'); CXXTOOLS_UNIT_ASSERT(s13 == c13); const cxxtools::Char c14[] = { 'a', 'b', 'c', 'd', 'e', '\0' }; cxxtools::String s14(s11); CXXTOOLS_UNIT_ASSERT(s14 == c14); const cxxtools::Char c15[] = { 'b', 'c', 'd', 'e', '\0' }; cxxtools::String s15(s11, 1); CXXTOOLS_UNIT_ASSERT(s15 == c15); const cxxtools::Char c16[] = { 'b', 'c', 'd', '\0' }; cxxtools::String s16(s11, 1, 3); CXXTOOLS_UNIT_ASSERT(s16 == c16); cxxtools::String s20(s2.begin(), s2.end()); CXXTOOLS_UNIT_ASSERT(s20 == L"abcde"); }
std::string Identity::decrypt(const Identity &from,const void *cdata,unsigned int len) const { unsigned char key[64]; unsigned char mac[32]; if (len < 16) return std::string(); if (!agree(from,key,sizeof(key))) return std::string(); for(int i=0;i<8;++i) key[i + 32] ^= ((const unsigned char *)cdata)[i]; // apply IV to HMAC key HMAC::sha256(key + 32,32,((const char *)cdata) + 16,(unsigned int)(len - 16),mac); for(int i=0;i<8;++i) { if (((const unsigned char *)cdata)[i + 8] != mac[i]) return std::string(); } char *decbuf = new char[len - 16]; try { Salsa20 s20(key,256,cdata); // first 8 bytes are IV len -= 16; s20.decrypt((const char *)cdata + 16,decbuf,len); std::string decompressed; if (Utils::decompress((const char *)decbuf,(const char *)decbuf + len,Utils::StringAppendOutput(decompressed))) { delete [] decbuf; return decompressed; } else { delete [] decbuf; return std::string(); } } catch ( ... ) { delete [] decbuf; return std::string(); } }
void Packet::armor(const void *key,bool encryptPayload) { unsigned char mangledKey[32]; unsigned char macKey[32]; unsigned char mac[16]; const unsigned int payloadLen = size() - ZT_PACKET_IDX_VERB; unsigned char *const payload = field(ZT_PACKET_IDX_VERB,payloadLen); // Set flag now, since it affects key mangle function setCipher(encryptPayload ? ZT_PROTO_CIPHER_SUITE__C25519_POLY1305_SALSA2012 : ZT_PROTO_CIPHER_SUITE__C25519_POLY1305_NONE); _salsa20MangleKey((const unsigned char *)key,mangledKey); Salsa20 s20(mangledKey,256,field(ZT_PACKET_IDX_IV,8)/*,ZT_PROTO_SALSA20_ROUNDS*/); // MAC key is always the first 32 bytes of the Salsa20 key stream // This is the same construction DJB's NaCl library uses s20.encrypt12(ZERO_KEY,macKey,sizeof(macKey)); if (encryptPayload) s20.encrypt12(payload,payload,payloadLen); Poly1305::compute(mac,payload,payloadLen,macKey); memcpy(field(ZT_PACKET_IDX_MAC,8),mac,8); }
bool Packet::dearmor(const void *key) { unsigned char mangledKey[32]; unsigned char macKey[32]; unsigned char mac[16]; const unsigned int payloadLen = size() - ZT_PACKET_IDX_VERB; unsigned char *const payload = field(ZT_PACKET_IDX_VERB,payloadLen); unsigned int cs = cipher(); if ((cs == ZT_PROTO_CIPHER_SUITE__C25519_POLY1305_NONE)||(cs == ZT_PROTO_CIPHER_SUITE__C25519_POLY1305_SALSA2012)) { _salsa20MangleKey((const unsigned char *)key,mangledKey); Salsa20 s20(mangledKey,256,field(ZT_PACKET_IDX_IV,8)/*,ZT_PROTO_SALSA20_ROUNDS*/); s20.encrypt12(ZERO_KEY,macKey,sizeof(macKey)); Poly1305::compute(mac,payload,payloadLen,macKey); if (!Utils::secureEq(mac,field(ZT_PACKET_IDX_MAC,8),8)) return false; if (cs == ZT_PROTO_CIPHER_SUITE__C25519_POLY1305_SALSA2012) s20.decrypt12(payload,payload,payloadLen); return true; } else return false; // unrecognized cipher suite }
std::string Identity::encrypt(const Identity &to,const void *data,unsigned int len) const { unsigned char key[64]; unsigned char mac[32]; unsigned char iv[8]; if (!agree(to,key,sizeof(key))) return std::string(); Utils::getSecureRandom(iv,8); for(int i=0;i<8;++i) key[i + 32] ^= iv[i]; // perturb HMAC key with IV so IV is effectively included in HMAC Salsa20 s20(key,256,iv); std::string compressed; compressed.reserve(len); Utils::compress((const char *)data,(const char *)data + len,Utils::StringAppendOutput(compressed)); if (!compressed.length()) return std::string(); char *encrypted = new char[compressed.length() + 16]; try { s20.encrypt(compressed.data(),encrypted + 16,(unsigned int)compressed.length()); HMAC::sha256(key + 32,32,encrypted + 16,(unsigned int)compressed.length(),mac); for(int i=0;i<8;++i) encrypted[i] = iv[i]; for(int i=0;i<8;++i) encrypted[i + 8] = mac[i]; std::string s(encrypted,compressed.length() + 16); delete [] encrypted; return s; } catch ( ... ) { delete [] encrypted; return std::string(); } }
int main(int argc, char **argv) { /* This test case applies a prescribed vortex field in a unit cube to * test the re-meshing techinique of the surface mesh. * * OBS.: - comment stepSL() on Simulator3D::stepALE * - switch to tetrahedralize( (char*) "QYYAp",&in,&out ) on * Model3D::mesh3DPoints * * Since the field is prescribed, there is no need of calculating the * convection in a Euleurian way (stepSL) and the insertion of nodes on * the 3D mesh. * * */ PetscInitializeNoArguments(); int iter = 1; double d1 = 0.0; // surface tangent velocity u_n=u-u_t double d2 = 0.0; // surface smooth cord (fujiwara) double dt = 0.02; double T = 3.0; double time = 0; string meshFile = "sphere.msh"; const char *vtkFolder = "./vtk/"; const char *mshFolder = "./msh/"; const char *datFolder = "./dat/"; string meshDir = (string) getenv("DATA_DIR"); meshDir += "/gmsh/3d/sphere/vortex/" + meshFile; const char *mesh = meshDir.c_str(); Model3D m1; Simulator3D s1; const char *mesh1 = mesh; m1.readMSH(mesh1); m1.setInterfaceBC(); m1.setTriEdge(); m1.mesh2Dto3D("QYYAp"); m1.setMapping(); #if NUMGLEU == 5 m1.setMiniElement(); #else m1.setQuadElement(); #endif m1.setSurfaceConfig(); m1.setInitSurfaceVolume(); m1.setInitSurfaceArea(); s1(m1); s1.setDt(dt); s1.setD1(d1); s1.setD2(d2); // initial conditions s1.stepImposedPeriodicField("3d",T,s1.getTime()); // X,Y and Z --> Sol(n+1) int nReMesh = 1; while( time < T ) { for( int j=0;j<nReMesh;j++ ) { cout << color(none,magenta,black); cout << "____________________________________ Iteration: " << iter << endl << endl; cout << resetColor(); InOut save(m1,s1); // cria objeto de gravacao save.printSimulationReport(); time = s1.getTime(); // time step: n+1/4 Simulator3D s20(m1,s1); double stepTime = dt/4.0; s20.stepImposedPeriodicField("3d",T,time+stepTime,stepTime); // SolOld(n) --> Sol(n+1/2) s20.saveOldData(); // Sol(n+1/2) --> SolOld(n+1/2) // time step: n+1/2 Simulator3D s30(m1,s20); stepTime = dt/2.0; s30.stepImposedPeriodicField("3d",T,time+stepTime,stepTime); // SolOld(n) --> Sol(n+1/2) s30.saveOldData(); // Sol(n+1/2) --> SolOld(n+1/2) s30.stepALE(); // SolOld(n+1/2) --> ALE(n+1/2) // time step: n using ALE(n+1/2) s1.setUALE(s30.getUALE()); s1.setVALE(s30.getVALE()); s1.setWALE(s30.getWALE()); s1.movePoints(); m1.setNormalAndKappa(); double field = cos(3.14159265358*time/T); cout << endl; cout << " | T: " << T << endl; cout << " | dt: " << dt << endl; cout << " | time: " << time << endl; cout << " | iter: " << iter << endl; cout << " | field: " << field << endl; cout << endl; save.saveMSH(mshFolder,"newMesh",iter); save.saveVTK(vtkFolder,"sim",iter); save.saveVTKSurface(vtkFolder,"sim",iter); save.saveBubbleInfo(datFolder); s1.saveOldData(); // Sol(n+1) --> SolOld(n) s1.timeStep(); cout << color(none,magenta,black); cout << "________________________________________ END of " << iter << endl << endl;; cout << resetColor(); iter++; } Model3D mOld = m1; /* *********** MESH TREATMENT ************* */ // set normal and kappa values m1.initMeshParameters(); // surface operations //m1.smoothPointsByCurvature(); m1.insertPointsByLength("flat"); m1.contractEdgesByLength("flat"); if( time > 1.9 ) m1.contractEdgesByLength("flat",0.65); if( time > 2.2 ) m1.contractEdgesByLength2("flat",0.7); if( time > 2.3 ) m1.contractEdgesByLength2("flat",0.8); if( time > 2.8 ) m1.contractEdgesByLength2("flat",1.2); //m1.removePointsByLength(); m1.flipTriangleEdges(); //m1.removePointsByNeighbourCheck(); //m1.checkAngleBetweenPlanes(); /* **************************************** */ m1.setInterfaceBC(); m1.setMiniElement(); m1.restoreMappingArrays(); m1.setSurfaceVolume(); m1.setSurfaceArea(); m1.triMeshStats(); // computing velocity field X^(n+1),time+1 at new nodes too! Simulator3D s2(m1,s1); s2.stepImposedPeriodicField("3d",T,time); // X,Y and Z --> Sol(n+1) s2.saveOldData(); s1 = s2; s1.setCentroidVelPos(); InOut saveEnd(m1,s1); // cria objeto de gravacao saveEnd.printMeshReport(); saveEnd.saveMeshInfo(datFolder); } PetscFinalize(); return 0; }
void V3TSP::selfTestStates() { // Linear test -- coords all along the x-axis { V3TSP::StateVec states; TspTestState s10(10,0); TspTestState s60(60,0); TspTestState s20(20,0); TspTestState s100(100,0); TspTestState s5(5,0); states.push_back(&s10); states.push_back(&s60); states.push_back(&s20); states.push_back(&s100); states.push_back(&s5); V3TSP::StateVec result; tspSort(states, &result); V3TSP::StateVec expect; expect.push_back(&s100); expect.push_back(&s60); expect.push_back(&s20); expect.push_back(&s10); expect.push_back(&s5); if (expect != result) { for (V3TSP::StateVec::iterator it = result.begin(); it != result.end(); ++it) { const TspTestState* statep = dynamic_cast<const TspTestState*>(*it); cout<<statep->xpos()<<" "; } cout<<endl; v3fatalSrc("TSP linear self-test fail. Result (above) did not match expectation."); } } // Second test. Coords are distributed in 2D space. // Test that tspSort() will rotate the list for minimum cost. { V3TSP::StateVec states; TspTestState a(0,0); TspTestState b(100,0); TspTestState c(200,0); TspTestState d(200,100); TspTestState e(150,150); TspTestState f(0,150); TspTestState g(0,100); states.push_back(&a); states.push_back(&b); states.push_back(&c); states.push_back(&d); states.push_back(&e); states.push_back(&f); states.push_back(&g); V3TSP::StateVec result; tspSort(states, &result); V3TSP::StateVec expect; expect.push_back(&f); expect.push_back(&g); expect.push_back(&a); expect.push_back(&b); expect.push_back(&c); expect.push_back(&d); expect.push_back(&e); if (expect != result) { for (V3TSP::StateVec::iterator it = result.begin(); it != result.end(); ++it) { const TspTestState* statep = dynamic_cast<const TspTestState*>(*it); cout<<statep->xpos()<<","<<statep->ypos()<<" "; } cout<<endl; v3fatalSrc("TSP 2d cycle=false self-test fail. Result (above) did not match expectation."); } } }