void Universe::calculatePlanetsAcceleration(Planet* mReference, QuadTreeNode* mCurrentNode) { if (mCurrentNode->northWest == NULL) { //My node is a leaf if (mCurrentNode->PlanetInfo->centerOfMass != atlas::math::Vector(0.0f) && mCurrentNode->PlanetInfo->centerOfMass != mReference->mPosition) //The node isn't empty and isn't the same planet as mReference mReference->calculateNetAcceleration(mCurrentNode->PlanetInfo); return; } else { float boundingBoxSide, distancePlanets; boundingBoxSide = abs(mCurrentNode->boundary.bottomRight.x - mCurrentNode->boundary.topLeft.x); distancePlanets = getModulus(mCurrentNode->PlanetInfo->centerOfMass - mReference->mPosition); if (boundingBoxSide / distancePlanets < RATIO) { mReference->calculateNetAcceleration(mCurrentNode->PlanetInfo); return; } else { calculatePlanetsAcceleration(mReference, mCurrentNode->northWest); calculatePlanetsAcceleration(mReference, mCurrentNode->northEast); calculatePlanetsAcceleration(mReference, mCurrentNode->southWest); calculatePlanetsAcceleration(mReference, mCurrentNode->southEast); } } }
void Planet::calculateNetAcceleration(QuadTreeNodeData * mOtherPlanet) { atlas::math::Vector r = (mPosition - mOtherPlanet->centerOfMass); atlas::math::Vector mNormalRadius = getNormal(r); atlas::math::Vector mPerpendicular = getPerpendicular(mNormalRadius); float rModulus = getModulus(r); if (rModulus <= 0.05f) return; thetadotdot -= (2.0f * (mAngVelocity * getModulus(mRadialVelocity)) / rModulus); rdotdot += mAngVelocity * mAngVelocity * rModulus - ((G * mOtherPlanet->totalMass) / (rModulus * rModulus)); mRadialResultant += mNormalRadius; mTangenResultant += mPerpendicular * rModulus; }
/** * Adds an integral value to this modulus number. * * @param number An integral value * @return A reference to this modulus number */ ModulusNumber& ModulusNumber::operator+=(const IndexType number) { int64_t i = containedNumber; i += number; containedNumber = i % getModulus(); return *this; }
uint16_t encryptNumber(uint32_t num){ uint16_t e = getDeviceID(); uint32_t n = getModulus(); uint32_t result = 1; for(uint16_t j = 0; j < e; j++){ result = (result * num % n); } result = (result % n); return (uint16_t)result; }
/** * Subtracts an integral value from this modulus number. * * @param number An integral value * @return A reference to this modulus number */ ModulusNumber& ModulusNumber::operator-=(IndexType number) { // Make sure the given number is within the modulus. const IndexType modulus = getModulus(); number %= modulus; /* * Add the modulus to the contained number if subtraction of * the given value will produce a negative number. */ int64_t i = containedNumber; if (i < number) { // Add the modulus to the contained number. i += modulus; } // Subract the give value from the contained number. containedNumber = i - number; return *this; }
/* * generate an RSA signature key * * e is fixed at 3, without discussion. That would not be wise if these * keys were to be used for encryption, but for signatures there are some * real speed advantages. * See also: https://www.imperialviolet.org/2012/03/16/rsae.html */ void rsasigkey(int nbits, char *configdir, char *password) { SECStatus rv; PK11RSAGenParams rsaparams = { nbits, (long) E }; secuPWData pwdata = { PW_NONE, NULL }; PK11SlotInfo *slot = NULL; SECKEYPrivateKey *privkey = NULL; SECKEYPublicKey *pubkey = NULL; unsigned char *bundp = NULL; mpz_t n; mpz_t e; size_t bs; char n_str[3 + MAXBITS / 4 + 1]; realtime_t now = realnow(); mpz_init(n); mpz_init(e); if (password == NULL) { pwdata.source = PW_NONE; } else { /* check if passwd == configdir/nsspassword */ size_t cdl = strlen(configdir); size_t pwl = strlen(password); static const char suf[] = "/nsspassword"; if (pwl == cdl + sizeof(suf) - 1 && memeq(password, configdir, cdl) && memeq(password + cdl, suf, sizeof(suf))) pwdata.source = PW_FROMFILE; else pwdata.source = PW_PLAINTEXT; } pwdata.data = password; PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 1); rv = NSS_InitReadWrite(configdir); if (rv != SECSuccess) { fprintf(stderr, "%s: NSS_InitReadWrite(%s) returned %d\n", me, configdir, PR_GetError()); exit(1); } #ifdef FIPS_CHECK if (PK11_IsFIPS() && !FIPSCHECK_verify(NULL, NULL)) { fprintf(stderr, "FIPS HMAC integrity verification test failed.\n"); exit(1); } #endif if (PK11_IsFIPS() && !password) { fprintf(stderr, "%s: On FIPS mode a password is required\n", me); exit(1); } PK11_SetPasswordFunc(GetModulePassword); /* Good for now but someone may want to use a hardware token */ slot = PK11_GetInternalKeySlot(); /* In which case this may be better */ /* slot = PK11_GetBestSlot(CKM_RSA_PKCS_KEY_PAIR_GEN, password ? &pwdata : NULL); */ /* or the user may specify the name of a token. */ #if 0 if (PK11_IsFIPS() || !PK11_IsInternal(slot)) { rv = PK11_Authenticate(slot, PR_FALSE, &pwdata); if (rv != SECSuccess) { fprintf(stderr, "%s: could not authenticate to token '%s'\n", me, PK11_GetTokenName(slot)); return; } } #endif /* 0 */ /* Do some random-number initialization. */ UpdateNSS_RNG(); /* Log in to the token */ if (password) { rv = PK11_Authenticate(slot, PR_FALSE, &pwdata); if (rv != SECSuccess) { fprintf(stderr, "%s: could not authenticate to token '%s'\n", me, PK11_GetTokenName(slot)); return; } } privkey = PK11_GenerateKeyPair(slot, CKM_RSA_PKCS_KEY_PAIR_GEN, &rsaparams, &pubkey, PR_TRUE, password ? PR_TRUE : PR_FALSE, &pwdata); /* inTheToken, isSensitive, passwordCallbackFunction */ if (!privkey) { fprintf(stderr, "%s: key pair generation failed: \"%d\"\n", me, PORT_GetError()); return; } /*privkey->wincx = &pwdata;*/ PORT_Assert(pubkey != NULL); fprintf(stderr, "Generated RSA key pair using the NSS database\n"); SECItemToHex(getModulus(pubkey), n_str); assert(!mpz_set_str(n, n_str, 16)); /* and the output */ report("output...\n"); /* deliberate extra newline */ printf("\t# RSA %d bits %s %s", nbits, outputhostname, ctime(&now.real_secs)); /* ctime provides \n */ printf("\t# for signatures only, UNSAFE FOR ENCRYPTION\n"); bundp = bundle(E, n, &bs); printf("\t#pubkey=%s\n", conv(bundp, bs, 's')); /* RFC2537ish format */ printf("\tModulus: %s\n", hexOut(getModulus(pubkey))); printf("\tPublicExponent: %s\n", hexOut(getPublicExponent(pubkey))); SECItem *ckaID = PK11_MakeIDFromPubKey(getModulus(pubkey)); if (ckaID != NULL) { printf("\t# everything after this point is CKA_ID in hex format - not the real values \n"); printf("\tPrivateExponent: %s\n", hexOut(ckaID)); printf("\tPrime1: %s\n", hexOut(ckaID)); printf("\tPrime2: %s\n", hexOut(ckaID)); printf("\tExponent1: %s\n", hexOut(ckaID)); printf("\tExponent2: %s\n", hexOut(ckaID)); printf("\tCoefficient: %s\n", hexOut(ckaID)); printf("\tCKAIDNSS: %s\n", hexOut(ckaID)); SECITEM_FreeItem(ckaID, PR_TRUE); } if (privkey) SECKEY_DestroyPrivateKey(privkey); if (pubkey) SECKEY_DestroyPublicKey(pubkey); (void) NSS_Shutdown(); (void) PR_Cleanup(); }