void ScoreState::after_evaluate(DerivativeAccumulator *da) { IMP_OBJECT_LOG; base::Timer t(this, "after_evaluate"); validate_inputs(); validate_outputs(); do_after_evaluate(da); }
int main (int argc, char **argv) { if (argc != 4) { print_help_message(argv); exit(1); } int n, choice; choice = atoi(argv[1]); n = atoi(argv[2]); validate_inputs(n, choice); int *A; A = (int *) malloc(n * sizeof(int)); assert(A != 0); int input_type = atoi(argv[3]); assert(input_type >= 0); assert(input_type <= 4); gen_input(A, n, input_type); int num_iterations = 10; sort_routines(choice, A, n, num_iterations); //insertion_sort(A, n, num_iterations); free(A); return 0; }
static int init(const struct ccdrbg_info *info, struct ccdrbg_state *drbg, size_t entropyLength, const void* entropy, size_t nonceLength, const void* nonce, size_t psLength, const void* ps) { struct ccdrbg_nisthmac_state *state=(struct ccdrbg_nisthmac_state *)drbg; state->bytesLeft = 0; state->custom = info->custom; //we only need to get the custom parameter from the info structure. int rc = validate_inputs(state , entropyLength, 0, psLength); if(rc!=CCDRBG_STATUS_OK){ //clear everything if cannot initialize. The idea is that if the caller doesn't check the output of init() and init() fails, //the system crashes by NULL dereferencing after a call to generate, rather than generating bad random numbers. done(drbg); return rc; } const struct ccdigest_info *di = state->custom->di; state->vsize = di->output_size; state->keysize = di->output_size; state->vptr=state->v; state->nextvptr=state->v+state->vsize; // 7. (V, Key, reseed_counter) = HMAC_DRBG_Instantiate_algorithm (entropy_input, personalization_string). hmac_dbrg_instantiate_algorithm(drbg, entropyLength, entropy, nonceLength, nonce, psLength, ps); #if DRBG_NISTHMAC_DEBUG dumpState("Init: ", state); #endif return CCDRBG_STATUS_OK; }
void ScoreState::before_evaluate() { IMP_OBJECT_LOG; base::Timer t(this, "before_evaluate"); validate_inputs(); validate_outputs(); do_before_evaluate(); }
int argon2_ctx(argon2_context *context, argon2_type type) { /* 1. Validate all inputs */ int result = validate_inputs(context); uint32_t memory_blocks, segment_length; argon2_instance_t instance; if (ARGON2_OK != result) { return result; } if (Argon2_d != type && Argon2_i != type) { return ARGON2_INCORRECT_TYPE; } /* 2. Align memory size */ /* Minimum memory_blocks = 8L blocks, where L is the number of lanes */ memory_blocks = context->m_cost; if (memory_blocks < 2 * ARGON2_SYNC_POINTS * context->lanes) { memory_blocks = 2 * ARGON2_SYNC_POINTS * context->lanes; } segment_length = memory_blocks / (context->lanes * ARGON2_SYNC_POINTS); /* Ensure that all segments have equal length */ memory_blocks = segment_length * (context->lanes * ARGON2_SYNC_POINTS); instance.memory = NULL; instance.passes = context->t_cost; instance.memory_blocks = memory_blocks; instance.segment_length = segment_length; instance.lane_length = segment_length * ARGON2_SYNC_POINTS; instance.lanes = context->lanes; instance.threads = context->threads; instance.type = type; /* 3. Initialization: Hashing inputs, allocating memory, filling first * blocks */ result = initialize(&instance, context); if (ARGON2_OK != result) { return result; } /* 4. Filling memory */ result = fill_memory_blocks(&instance); if (ARGON2_OK != result) { return result; } /* 5. Finalization */ finalize(context, &instance); return ARGON2_OK; }
/* * NIST SP 800-90 March 2007 * 10.2.1.4.2 The Process Steps for Reseeding When a Derivation * Function is Used */ static int reseed(struct ccdrbg_state *rng, unsigned long entropyLength, const void *entropy, unsigned long additionalLength, const void *additional) { int err; uint32_t count; const char *input_string[2]; uint32_t length[2]; struct ccdrbg_nistctr_state *drbg=(struct ccdrbg_nistctr_state *)rng; uint32_t seed_material[CCADRBG_SEEDLEN_INTS(drbg)]; err =validate_inputs(drbg, entropyLength, additionalLength, 0); if(err!=CCDRBG_STATUS_OK) return err; if(drbg->use_df) { /* [1] seed_material = entropy || additional */ input_string[0] = entropy; /* typecast: guaranteed to fit by the above checks */ length[0] = (uint32_t)entropyLength; count = 1; if (additional && additionalLength) { input_string[count] = additional; /* typecast: guaranteed to fit by above checks */ length[count] = (uint32_t)additionalLength; ++count; } /* [2] seed_material = Block_Cipher_df(seed_material, seedlen) */ err = df(drbg, input_string, length, count, (uint8_t *)seed_material, sizeof(seed_material)); if (err) return err; } else { cc_clear(sizeof(seed_material),seed_material); cc_assert(additionalLength==0 || additionalLength==sizeof(seed_material)); //additionalLength is validated above CC_MEMCPY(seed_material, additional, additionalLength); cc_xor(CCADRBG_SEEDLEN(drbg), seed_material, seed_material, entropy); } /* [3] (Key, V) = Update(seed_material, Key, V) */ if (drbg_update(drbg, seed_material)) { return CCDRBG_STATUS_PARAM_ERROR; } /* [4] reseed_counter = 1 */ drbg->reseed_counter = 1; return CCDRBG_STATUS_OK; }
static int reseed(struct ccdrbg_state *drbg, size_t entropyLength, const void *entropy, size_t additionalLength, const void *additional) { struct ccdrbg_nisthmac_state *state = (struct ccdrbg_nisthmac_state *)drbg; int rc = validate_inputs(state, entropyLength, additionalLength, 0); if(rc!=CCDRBG_STATUS_OK) return rc; int rx = hmac_dbrg_update(drbg, entropyLength, entropy, additionalLength, additional, 0, NULL); state->reseed_counter = 1; #if DRBG_NISTHMAC_DEBUG dumpState("Reseed: ", state); #endif return rx; }
/////////////////////////////////////////////////////////////////////////////////////////// // Calculate all SPA parameters and put into structure // Note: All inputs values (listed in header file) must already be in structure /////////////////////////////////////////////////////////////////////////////////////////// int spa_calculate(spa_data *spa) { int result; result = validate_inputs(spa); if (result == 0) { spa->jd = julian_day (spa->year, spa->month, spa->day, spa->hour, spa->minute, spa->second, spa->timezone); calculate_geocentric_sun_right_ascension_and_declination(spa); spa->h = observer_hour_angle(spa->nu, spa->longitude, spa->alpha); spa->xi = sun_equatorial_horizontal_parallax(spa->r); sun_right_ascension_parallax_and_topocentric_dec(spa->latitude, spa->elevation, spa->xi, spa->h, spa->delta, &(spa->del_alpha), &(spa->delta_prime)); spa->alpha_prime = topocentric_sun_right_ascension(spa->alpha, spa->del_alpha); spa->h_prime = topocentric_local_hour_angle(spa->h, spa->del_alpha); spa->e0 = topocentric_elevation_angle(spa->latitude, spa->delta_prime, spa->h_prime); spa->del_e = atmospheric_refraction_correction(spa->pressure, spa->temperature, spa->atmos_refract, spa->e0); spa->e = topocentric_elevation_angle_corrected(spa->e0, spa->del_e); spa->zenith = topocentric_zenith_angle(spa->e); spa->azimuth180 = topocentric_azimuth_angle_neg180_180(spa->h_prime, spa->latitude, spa->delta_prime); spa->azimuth = topocentric_azimuth_angle_zero_360(spa->azimuth180); if ((spa->function == SPA_ZA_INC) || (spa->function == SPA_ALL)) spa->incidence = surface_incidence_angle(spa->zenith, spa->azimuth180, spa->azm_rotation, spa->slope); if ((spa->function == SPA_ZA_RTS) || (spa->function == SPA_ALL)) calculate_eot_and_sun_rise_transit_set(spa); } return result; }
int main(int argc, char **argv) { int r; char user_input = 'n'; pthread_t tid1, tid2; program_name = argv[0]; while ( (next_option = getopt_long(argc, argv, short_options, long_options, NULL) ) != -1 ) { switch ( next_option ) { case 'h': /* -h or --help */ print_usage(stdout, 0); case 'v': /* -v or --version */ printf("%s (Ver 1.0)\n",program_name); printf("Copyright (C) 2012 Cypress Semiconductors Inc. / ATR-LABS\n"); exit(0); case 't': /* -t or --timeout */ timeout_provided = 1; timeout = atoi(optarg); break; case '?': /* Invalid option */ print_usage(stdout, 1); default : /* Something else, unexpected */ abort(); } } validate_inputs(); r = cyusb_open(); if ( r < 0 ) { printf("Error opening library\n"); return -1; } else if ( r == 0 ) { printf("No device found\n"); return 0; } if ( r > 1 ) { printf("More than 1 devices of interest found. Disconnect unwanted devices\n"); return 0; } h1 = cyusb_gethandle(0); if ( cyusb_getvendor(h1) != 0x04b4 ) { printf("Cypress chipset not detected\n"); cyusb_close(); return 0; } r = cyusb_kernel_driver_active(h1, 0); if ( r != 0 ) { printf("kernel driver active. Exitting\n"); cyusb_close(); return 0; } r = cyusb_claim_interface(h1, 0); if ( r != 0 ) { printf("Error in claiming interface\n"); cyusb_close(); return 0; } else printf("Successfully claimed interface\n"); r = pthread_create(&tid1, NULL, reader, NULL); r = pthread_create(&tid2, NULL, writer, NULL); while ( 1) { pause(); } cyusb_close(); return 0; }
int main(int argc, char **argv) { int N; int pid; char tbuf[50]; int r; N = cyusb_open(); if ( N < 0 ) { printf("Error in opening library\n"); return -1; } else if ( N == 0 ) { printf("No device of interest found\n"); return 0; } else printf("No of devices of interest found = %d\n",N); logfd = open(logfile, O_WRONLY | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR ); if ( logfd < 0 ) { printf("Error opening log file for output %s\n",logfile); cyusb_close(); return -2; } program_name = argv[0]; while ( (next_option = getopt_long(argc, argv, short_options, long_options, NULL) ) != -1 ) { switch ( next_option ) { case 'h': /* -h or --help */ print_usage(stdout, 0); case 'v': /* -v or --version */ printf("cyusbd (Ver 1.0)\n"); printf("Copyright (C) 2012 Cypress Semiconductors / ATR-LABS\n"); exit(0); case '?': /* Invalid option */ print_usage(stdout, 1); default : /* Something else, unexpected */ cyusb_close(); abort(); } } validate_inputs(); pid = getpid(); pidfd = open(pidfile, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR ); if ( pidfd < 0 ) { printf("Error opening pid file for output %s\n",pidfile); cyusb_close(); return -3; } else { memset(tbuf,' ',50); sprintf(tbuf,"%d\n",pid); write(pidfd, tbuf, strlen(tbuf)); close(pidfd); } signal(SIGUSR1,handle_sigusr1); /* Signal to handle events received from the kernel */ signal(SIGUSR2,handle_sigusr2); /* Signal to stop this daemon and exit gracefully */ signal(SIGINT, handle_sigusr2); /* Ctrl_C will also stop this daemon and exit gracefully */ while (1) { pause(); } return 0; }
int main(int argc, char **argv) { int r; struct libusb_device_descriptor desc; char user_input = 'n'; program_name = argv[0]; while ( (next_option = getopt_long(argc, argv, short_options, long_options, NULL) ) != -1 ) { switch ( next_option ) { case 'h': /* -h or --help */ print_usage(stdout, 0); case 'v': /* -v or --version */ printf("%s (Ver 1.0)\n",program_name); printf("Copyright (C) 2012 Cypress Semiconductors Inc. / ATR-LABS\n"); exit(0); case 'o': /* -o or --output */ out_filename = optarg; fp = fopen(out_filename,"a"); if ( !fp ) print_usage(stdout, 1); break; case 'V': /* -V or --vendor */ vendor_provided = 1; vid = strtoul(optarg,NULL,16); break; case 'P': /* -P or --Product */ product_provided = 1; pid = strtoul(optarg,NULL,16); break; case '?': /* Invalid option */ print_usage(stdout, 1); default : /* Something else, unexpected */ abort(); } } validate_inputs(); if ( (vendor_provided) && (product_provided) ) user_input = 'y'; else user_input = 'n'; if ( user_input == 'y' ) { r = cyusb_open(vid, pid); if ( r < 0 ) { printf("Error opening library\n"); return -1; } else if ( r == 0 ) { printf("No device found\n"); return 0; } } else { r = cyusb_open(); if ( r < 0 ) { printf("Error opening library\n"); return -1; } else if ( r == 0 ) { printf("No device found\n"); return 0; } } r = cyusb_get_device_descriptor(cyusb_gethandle(0), &desc); if ( r ) { printf("error getting device descriptor\n"); return -2; } fprintf(fp,"bLength = %d\n", desc.bLength); fprintf(fp,"bDescriptorType = %d\n", desc.bDescriptorType); fprintf(fp,"bcdUSB = 0x%04x\n", desc.bcdUSB); fprintf(fp,"bDeviceClass = 0x%02x\n", desc.bDeviceClass); fprintf(fp,"bDeviceSubClass = 0x%02x\n", desc.bDeviceSubClass); fprintf(fp,"bDeviceProtocol = 0x%02x\n", desc.bDeviceProtocol); fprintf(fp,"bMaxPacketSize = %d\n", desc.bMaxPacketSize0); fprintf(fp,"idVendor = 0x%04x\n", desc.idVendor); fprintf(fp,"idProduct = 0x%04x\n", desc.idProduct); fprintf(fp,"bcdDevice = 0x%04x\n", desc.bcdDevice); fprintf(fp,"iManufacturer = %d\n", desc.iManufacturer); fprintf(fp,"iProduct = %d\n", desc.iProduct); fprintf(fp,"iSerialNumber = %d\n", desc.iSerialNumber); fprintf(fp,"bNumConfigurations = %d\n", desc.bNumConfigurations); cyusb_close(); return 0; }
ModelObjectsTemp ModelObject::get_inputs() const { IMP_OBJECT_LOG; validate_inputs(); return do_get_inputs(); }
/** * Logistic regression stochastic average gradient trainer * * @param w(p, 1) weights * @param Xt(p, n) real feature matrix * @param y(n, 1) {-1, 1} target matrix * @param lambda scalar regularization parameters * @param Li scalar constant step size * @param iVals(max_iter, 1) sequence of examples to choose * @param d(p, 1) initial approximation of average gradient * @param g(n, 1) previous derivatives of loss * @param covered(n, 1) whether the example has been visited * @param stepSizeType scalar default is 1 to use 1/L, set to 2 to * use 2/(L + n*myu) * @return optimal weights (p, 1) */ SEXP C_sag(SEXP wInit, SEXP Xt, SEXP y, SEXP lambdas, SEXP alpha, // SAG Constant Step size SEXP stepSizeType, // SAG Linesearch SEXP LiInit, // SAG Linesearch and Adaptive SEXP LmaxInit, // SAG Adaptive SEXP increasing, // SAG Adaptive SEXP dInit, SEXP gInit, SEXP coveredInit, SEXP tol, SEXP maxiter, SEXP family, SEXP fit_alg, SEXP ex_model_params, SEXP sparse) { /*===============\ | Error Checking | \===============*/ validate_inputs(wInit, Xt, y, dInit, gInit, coveredInit, sparse); /* Initializing protection counter */ int nprot = 0; /* Duplicating objects to be modified */ SEXP w = PROTECT(duplicate(wInit)); nprot++; SEXP d = PROTECT(duplicate(dInit)); nprot++; SEXP g = PROTECT(duplicate(gInit)); nprot++; SEXP covered = PROTECT(duplicate(coveredInit)); nprot++; SEXP Li = PROTECT(duplicate(LiInit)); nprot++; SEXP Lmax = PROTECT(duplicate(LmaxInit)); nprot++; /*======\ | Input | \======*/ /* Initializing dataset */ Dataset train_set = make_Dataset(Xt, y, covered, Lmax, Li, increasing, fit_alg, sparse); /* Initializing Trainer */ GlmTrainer trainer = make_GlmTrainer(R_NilValue, alpha, d, g, maxiter, stepSizeType, tol, fit_alg, R_NilValue, R_NilValue); /* Initializing Model */ GlmModel model = make_GlmModel(w, family, ex_model_params); /*============================\ | Stochastic Average Gradient | \============================*/ /* Initializing lambda/weights Matrix*/ SEXP lambda_w = PROTECT(allocMatrix(REALSXP, train_set.nVars, LENGTH(lambdas))); nprot++; Memzero(REAL(lambda_w), LENGTH(lambdas) * train_set.nVars); /* Training */ sag_warm(&trainer, &model, &train_set, REAL(lambdas), LENGTH(lambdas), REAL(lambda_w)); /* Cleanup */ cleanup(&trainer, &model, &train_set); /*=======\ | Return | \=======*/ SEXP convergence_code = PROTECT(allocVector(INTSXP, 1)); nprot++; *INTEGER(convergence_code) = trainer.convergence_code; SEXP iter_count = PROTECT(allocVector(INTSXP, 1)); nprot++; *INTEGER(iter_count) = trainer.iter_count; /* Assigning variables to SEXP list */ SEXP results = PROTECT(allocVector(VECSXP, 8)); nprot++; INC_APPLY(SEXP, SET_VECTOR_ELT, results, lambda_w, d, g, covered, Li, Lmax, convergence_code, iter_count); // in utils.h /* Creating SEXP for list names */ SEXP results_names = PROTECT(allocVector(STRSXP, 8)); nprot++; INC_APPLY_SUB(char *, SET_STRING_ELT, mkChar, results_names, "lambda_w", "d", "g", "covered", "Li", "Lmax", "convergence_code", "iter_count"); setAttrib(results, R_NamesSymbol, results_names); /* ------------------------------------------------------------------------ */ UNPROTECT(nprot); return results; }
static int nistctr_init(const struct ccdrbg_nistctr_custom *custom, struct ccdrbg_nistctr_state *drbg, char *keys, const void* entropy, unsigned long entropyLength, const void* nonce, unsigned long nonceLength, const void* ps, unsigned long psLength ) { int err; uint32_t count; char *buf; drbg->ecb = custom->ecb; drbg->keylen = custom->keylen; buf=keys; unsigned long bs=drbg->ecb->block_size; drbg->encryptedIV = (uint8_t *)buf; buf+=((drbg->keylen+bs*2-1)/bs)*bs; drbg->V = (uint32_t *)buf; buf+=bs; //CCADRBG_OUTLEN(drbg); drbg->nullInput = (uint32_t *)buf; buf+=drbg->keylen+bs; //CCADRBG_SEEDLEN(drbg); drbg->bcc.S = (uint32_t *)buf; buf+=bs; //CCADRBG_OUTLEN(drbg); drbg->key = (ccecb_ctx *)buf; buf+=drbg->ecb->size; drbg->df_key = (ccecb_ctx *)buf; // First initialize the struct drbg->strictFIPS = custom->strictFIPS; drbg->use_df = custom->use_df; #if NONFIPSINC128 if (strictFIPS) drbg->inc128 = increment_bigend_128; else drbg->inc128 = increment_bigend_128_NOFIPS; #endif for (count = 0; count < CCADRBG_SEEDLEN_INTS(drbg); count++) drbg->nullInput[count] = 0; // Reseed counter is set in [6] below. // V is set in [4] and [5] // Initialize the derivation function // //nonce is not checked, caller needs to make sure nonce is right as per NIST 800-90A section 8.6.7 int rc=validate_inputs(drbg, entropyLength, 0, psLength); if(rc!=CCDRBG_STATUS_OK){ done((struct ccdrbg_state *)drbg); return rc; } uint8_t K[CCADRBG_KEYLEN(drbg)]; uint32_t seed_material[CCADRBG_SEEDLEN_INTS(drbg)]; if(drbg->use_df) { uint32_t length[3]; const char *input_string[3]; df_initialize(drbg); /* [1] seed_material = entropy || nonce || ps */ input_string[0] = entropy; /* typecast: guaranteed to fit by above checks */ length[0] = (uint32_t)entropyLength; input_string[1] = nonce; /* typecast: guaranteed to fit by above checks */ length[1] = (uint32_t)nonceLength; count = 2; if (ps && psLength) { input_string[count] = ps; /* typecast: guaranteed to fit by above checks */ length[count] = (uint32_t) psLength; ++count; } /* [2] seed_material = Block_Cipher_df(seed_material, seedlen) */ err = df(drbg, input_string, length, count, (uint8_t *)seed_material, sizeof(seed_material)); if (err) { cc_clear(sizeof(seed_material),seed_material); done((struct ccdrbg_state *)drbg); return err; } } else { cc_clear(sizeof(seed_material),seed_material); cc_assert(psLength==0 || psLength==sizeof(seed_material)); //pslength is validated above CC_MEMCPY(seed_material, ps, psLength); cc_xor(CCADRBG_SEEDLEN(drbg), seed_material, seed_material, entropy); } /* [3] Key = 0^keylen */ cc_clear(sizeof(K), K); drbg->ecb->init(drbg->ecb, drbg->key, sizeof(K), K); /* [4] V = 0^outlen */ cc_clear(CCADRBG_OUTLEN(drbg),drbg->V); /* [5] (Key, V) = Update(seed_material, Key, V) */ if (drbg_update(drbg, seed_material)) { cc_clear(sizeof(seed_material),seed_material); done((struct ccdrbg_state *)drbg); return CCDRBG_STATUS_PARAM_ERROR; } cc_clear(sizeof(seed_material),seed_material); /* [6] reseed_counter = 1 */ drbg->reseed_counter = 1; return CCDRBG_STATUS_OK; }