int get_code(unsigned* pycbuf, struct code_obj* pobj, int cur, int size) { int func_idx = 0; int op_arg = 0; int num_obj = 1; int dealt = 0; int len = length(pycbuf, cur-5); int end = cur + len; while (cur < end-1 && !(cur >= size)) { if(!(callable(cur, pycbuf))) { field_add(&(pobj->code), pycbuf[cur]); if (have_arg(pycbuf[cur])) { op_arg = get_op_arg(pycbuf, cur); field_add(&(pobj->code), op_arg); cur += 3; } else { cur += 1; } } else { //printf("****:dealing function\n"); dealt = find_next_callable(pycbuf, cur, num_obj); struct code_obj* pnew_obj = malloc(sizeof(*pnew_obj)); func_idx = get_fields(pycbuf, pnew_obj, dealt, size); /* get fields of the entire code object */ objects[func_idx]->val.pobj = pnew_obj; objects[func_idx]->type = TYPE_CODE; num_obj++; cur += 9; } } field_add(&(pobj->code), pycbuf[cur]); return cur+1; }
int get_consts(unsigned* pycbuf, struct code_obj* pobj, int cur, int size) { int num_co = length(pycbuf, cur); int i = 0; cur += 5; while(i < num_co) { if (pycbuf[cur] == TYPE_INTEGER) { field_add(&(pobj->consts), compute_const(pycbuf, cur)); cur += 5; } else if (pycbuf[cur] == TYPE_NONE){ field_add(&(pobj->consts), 0); cur += 1; } else if (pycbuf[cur] == TYPE_CODE) { field_add(&(pobj->consts), 0); cur = find_end_of_code(pycbuf, cur); } i++; } return cur; }
void DatabaseIO::create_group(EntityType type, const std::string & /*type_name*/, const std::vector<std::string> &group_spec, const SideSet * /*set_type*/) { // Not generalized yet... This only works for T == SideSet if (type != SIDESET) { return; } int64_t entity_count = 0; int64_t df_count = 0; // Create the new set... auto new_set = new SideSet(this, group_spec[0]); get_region()->add(new_set); // Find the member SideSets... for (size_t i = 1; i < group_spec.size(); i++) { SideSet *set = get_region()->get_sideset(group_spec[i]); if (set != nullptr) { SideBlockContainer side_blocks = set->get_side_blocks(); for (auto &sbold : side_blocks) { size_t side_count = sbold->get_property("entity_count").get_int(); auto sbnew = new SideBlock(this, sbold->name(), sbold->topology()->name(), sbold->parent_element_topology()->name(), side_count); int64_t id = sbold->get_property("id").get_int(); sbnew->property_add(Property("set_offset", entity_count)); sbnew->property_add(Property("set_df_offset", df_count)); sbnew->property_add(Property("id", id)); new_set->add(sbnew); size_t old_df_count = sbold->get_property("distribution_factor_count").get_int(); if (old_df_count > 0) { std::string storage = "Real["; storage += Utils::to_string(sbnew->topology()->number_nodes()); storage += "]"; sbnew->field_add( Field("distribution_factors", Field::REAL, storage, Field::MESH, side_count)); } entity_count += side_count; df_count += old_df_count; } } else { IOSS_WARNING << "WARNING: While creating the grouped surface '" << group_spec[0] << "', the surface '" << group_spec[i] << "' does not exist. " << "This surface will skipped and not added to the group.\n\n"; } } }
// if you want a full multiplication, then make res.order = l.order + r.order // but if you just care about a lower order, e.g. mul mod x^i, then you can select // fewer coefficients void polynomial_mul(field_t field, polynomial_t l, polynomial_t r, polynomial_t res) { // perform an element-wise multiplication of two polynomials memset(res.coeff, 0, sizeof(field_element_t) * (res.order + 1)); for (unsigned int i = 0; i <= l.order; i++) { if (i > res.order) { continue; } unsigned int j_limit = (r.order > res.order - i) ? res.order - i : r.order; for (unsigned int j = 0; j <= j_limit; j++) { // e.g. alpha^5*x * alpha^37*x^2 --> alpha^42*x^3 res.coeff[i + j] = field_add(field, res.coeff[i + j], field_mul(field, l.coeff[i], r.coeff[j])); } } }
int stmtsort_init(struct Stmt *sort) { int s = 0; struct Field *fld_in = 0; struct Field *fld_out = 0; if (! sort->out) { sort->out = (struct StmtMeta *) calloc(1, sizeof(struct StmtMeta)); if (! sort->out) return E_MEM; } if (sort->out->filename) { /* check if file output is exist */ s = file_raw_is_exist(sort->out->filename); if (s) { str_raw_copy(sort->out->filename, &_vos.e_sparm0); return E_FILE_EXIST; } } if (! sort->out->alias) { s = str_raw_copy(sort->in->alias, &sort->out->alias); if (s) return s; } sort->out->flag = sort->in->flag; fld_in = sort->in->fields; while (fld_in) { fld_out = (struct Field *) calloc(1, sizeof(struct Field)); if (! fld_out) return E_MEM; fld_out->idx = fld_in->idx; fld_out->flag = fld_in->flag; fld_out->type = fld_in->type; if (fld_in->next) fld_out->sep = '|'; fld_out->name = fld_in->name; fld_out->date_format = fld_in->date_format; field_add(&sort->out->fields, fld_out); fld_in = fld_in->next; } return 0; }
field_element_t polynomial_eval_lut(field_t field, polynomial_t poly, const field_logarithm_t *val_exp) { // evaluate the polynomial poly at a particular element val // in this case, all of the logarithms of the successive powers of val have been precalculated // this removes the extra work we'd have to do to calculate val_exponentiated each time // if this function is to be called on the same val multiple times if (val_exp[0] == 0) { return poly.coeff[0]; } field_element_t res = 0; for (unsigned int i = 0; i <= poly.order; i++) { if (poly.coeff[i] != 0) { // multiply-accumulate by the next coeff times the next power of val res = field_add(field, res, field_mul_log_element(field, field.log[poly.coeff[i]], val_exp[i])); } } return res; }
void polynomial_mod(field_t field, polynomial_t dividend, polynomial_t divisor, polynomial_t mod) { // find the polynomial remainder of dividend mod divisor // do long division and return just the remainder (written to mod) if (mod.order < dividend.order) { // mod.order must be >= dividend.order (scratch space needed) // this is an error -- catch it in debug? return; } // initialize remainder as dividend memcpy(mod.coeff, dividend.coeff, sizeof(field_element_t) * (dividend.order + 1)); // XXX make sure divisor[divisor_order] is nonzero field_logarithm_t divisor_leading = field.log[divisor.coeff[divisor.order]]; // long division steps along one order at a time, starting at the highest order for (unsigned int i = dividend.order; i > 0; i--) { // look at the leading coefficient of dividend and divisor // if leading coefficient of dividend / leading coefficient of divisor is q // then the next row of subtraction will be q * divisor // if order of q < 0 then what we have is the remainder and we are done if (i < divisor.order) { break; } if (mod.coeff[i] == 0) { continue; } unsigned int q_order = i - divisor.order; field_logarithm_t q_coeff = field_div_log(field, field.log[mod.coeff[i]], divisor_leading); // now that we've chosen q, multiply the divisor by q and subtract from // our remainder. subtracting in GF(2^8) is XOR, just like addition for (unsigned int j = 0; j <= divisor.order; j++) { if (divisor.coeff[j] == 0) { continue; } // all of the multiplication is shifted up by q_order places mod.coeff[j + q_order] = field_add(field, mod.coeff[j + q_order], field_mul_log_element(field, field.log[divisor.coeff[j]], q_coeff)); } } }
field_element_t polynomial_eval(field_t field, polynomial_t poly, field_element_t val) { // evaluate the polynomial poly at a particular element val if (val == 0) { return poly.coeff[0]; } field_element_t res = 0; // we're going to start at 0th order and multiply by val each time field_logarithm_t val_exponentiated = field.log[1]; field_logarithm_t val_log = field.log[val]; for (unsigned int i = 0; i <= poly.order; i++) { if (poly.coeff[i] != 0) { // multiply-accumulate by the next coeff times the next power of val res = field_add(field, res, field_mul_log_element(field, field.log[poly.coeff[i]], val_exponentiated)); } // now advance to the next power val_exponentiated = field_mul_log(field, val_exponentiated, val_log); } return res; }
field_element_t polynomial_eval_log_lut(field_t field, polynomial_t poly_log, const field_logarithm_t *val_exp) { // evaluate the log_polynomial poly at a particular element val // like polynomial_eval_lut, the logarithms of the successive powers of val have been // precomputed if (val_exp[0] == 0) { if (poly_log.coeff[0] == 0) { // special case for the non-existant log case return 0; } return field.exp[poly_log.coeff[0]]; } field_element_t res = 0; for (unsigned int i = 0; i <= poly_log.order; i++) { // using 0 as a sentinel value in log -- log(0) is really -inf if (poly_log.coeff[i] != 0) { // multiply-accumulate by the next coeff times the next power of val res = field_add(field, res, field_mul_log_element(field, poly_log.coeff[i], val_exp[i])); } } return res; }
static int log_failure_mkfields(pool *p) { pr_table_t *fields; fields = pr_table_alloc(p, 0); if (pr_table_ctl(fields, PR_TABLE_CTL_SET_KEY_CMP, (void *) field_id_cmp) < 0) { int xerrno = errno; pr_log_pri(PR_LOG_INFO, "error setting key comparison callback for " "field ID/names: %s", strerror(errno)); pr_table_free(fields); errno = xerrno; return -1; } if (pr_table_ctl(fields, PR_TABLE_CTL_SET_KEY_HASH, (void *) field_id_hash) < 0) { int xerrno = errno; pr_log_pri(PR_LOG_INFO, "error setting key hash callback for " "field ID/names: %s", strerror(errno)); pr_table_free(fields); errno = xerrno; return -1; } /* Now populate the table with the ID/name values. The key is the * LogFormat "meta" ID, and the value is the corresponding name string, * for use e.g. as JSON object member names. */ field_add(p, fields, LOGFMT_META_BYTES_SENT, "bytes_sent", LOG_FAILURE_FIELD_TYPE_NUMBER); field_add(p, fields, LOGFMT_META_FILENAME, "file", LOG_FAILURE_FIELD_TYPE_STRING); field_add(p, fields, LOGFMT_META_ENV_VAR, "ENV:", LOG_FAILURE_FIELD_TYPE_STRING); field_add(p, fields, LOGFMT_META_REMOTE_HOST, "remote_dns", LOG_FAILURE_FIELD_TYPE_STRING); field_add(p, fields, LOGFMT_META_REMOTE_IP, "remote_ip", LOG_FAILURE_FIELD_TYPE_STRING); #if defined(LOGFMT_META_REMOTE_PORT) field_add(p, fields, LOGFMT_META_REMOTE_PORT, "remote_port", LOG_FAILURE_FIELD_TYPE_NUMBER); #endif /* LOGFMT_META_REMOTE_PORT */ field_add(p, fields, LOGFMT_META_IDENT_USER, "identd_user", LOG_FAILURE_FIELD_TYPE_STRING); field_add(p, fields, LOGFMT_META_PID, "pid", LOG_FAILURE_FIELD_TYPE_NUMBER); field_add(p, fields, LOGFMT_META_TIME, "local_time", LOG_FAILURE_FIELD_TYPE_STRING); field_add(p, fields, LOGFMT_META_SECONDS, "transfer_secs", LOG_FAILURE_FIELD_TYPE_NUMBER); field_add(p, fields, LOGFMT_META_COMMAND, "raw_command", LOG_FAILURE_FIELD_TYPE_STRING); field_add(p, fields, LOGFMT_META_LOCAL_NAME, "server_name", LOG_FAILURE_FIELD_TYPE_STRING); field_add(p, fields, LOGFMT_META_LOCAL_PORT, "local_port", LOG_FAILURE_FIELD_TYPE_NUMBER); field_add(p, fields, LOGFMT_META_LOCAL_IP, "local_ip", LOG_FAILURE_FIELD_TYPE_STRING); field_add(p, fields, LOGFMT_META_LOCAL_FQDN, "server_dns", LOG_FAILURE_FIELD_TYPE_STRING); field_add(p, fields, LOGFMT_META_USER, "user", LOG_FAILURE_FIELD_TYPE_STRING); field_add(p, fields, LOGFMT_META_ORIGINAL_USER, "original_user", LOG_FAILURE_FIELD_TYPE_STRING); field_add(p, fields, LOGFMT_META_RESPONSE_CODE, "response_code", LOG_FAILURE_FIELD_TYPE_NUMBER); #if defined(LOGFMT_META_RESPONSE_MS) field_add(p, fields, LOGFMT_META_RESPONSE_MS, "response_ms", LOG_FAILURE_FIELD_TYPE_NUMBER); #endif /* LOGFMT_META_RESPONSE_MS */ field_add(p, fields, LOGFMT_META_CLASS, "connection_class", LOG_FAILURE_FIELD_TYPE_STRING); field_add(p, fields, LOGFMT_META_ANON_PASS, "anon_password", LOG_FAILURE_FIELD_TYPE_STRING); field_add(p, fields, LOGFMT_META_METHOD, "command", LOG_FAILURE_FIELD_TYPE_STRING); field_add(p, fields, LOGFMT_META_XFER_PATH, "transfer_path", LOG_FAILURE_FIELD_TYPE_STRING); field_add(p, fields, LOGFMT_META_DIR_NAME, "dir_name", LOG_FAILURE_FIELD_TYPE_STRING); field_add(p, fields, LOGFMT_META_DIR_PATH, "dir_path", LOG_FAILURE_FIELD_TYPE_STRING); field_add(p, fields, LOGFMT_META_CMD_PARAMS, "command_params", LOG_FAILURE_FIELD_TYPE_STRING); field_add(p, fields, LOGFMT_META_RESPONSE_STR, "response_msg", LOG_FAILURE_FIELD_TYPE_STRING); field_add(p, fields, LOGFMT_META_PROTOCOL, "protocol", LOG_FAILURE_FIELD_TYPE_STRING); field_add(p, fields, LOGFMT_META_VERSION, "server_version", LOG_FAILURE_FIELD_TYPE_STRING); field_add(p, fields, LOGFMT_META_RENAME_FROM, "rename_from", LOG_FAILURE_FIELD_TYPE_STRING); field_add(p, fields, LOGFMT_META_FILE_MODIFIED, "file_modified", LOG_FAILURE_FIELD_TYPE_BOOLEAN); field_add(p, fields, LOGFMT_META_UID, "uid", LOG_FAILURE_FIELD_TYPE_NUMBER); field_add(p, fields, LOGFMT_META_GID, "gid", LOG_FAILURE_FIELD_TYPE_NUMBER); field_add(p, fields, LOGFMT_META_RAW_BYTES_IN, "session_bytes_rcvd", LOG_FAILURE_FIELD_TYPE_NUMBER); field_add(p, fields, LOGFMT_META_RAW_BYTES_OUT, "session_bytes_sent", LOG_FAILURE_FIELD_TYPE_NUMBER); field_add(p, fields, LOGFMT_META_EOS_REASON, "session_end_reason", LOG_FAILURE_FIELD_TYPE_STRING); field_add(p, fields, LOGFMT_META_VHOST_IP, "server_ip", LOG_FAILURE_FIELD_TYPE_STRING); field_add(p, fields, LOGFMT_META_NOTE_VAR, "NOTE:", LOG_FAILURE_FIELD_TYPE_STRING); #if defined(LOGFMT_META_XFER_MS) field_add(p, fields, LOGFMT_META_XFER_MS, "transfer_ms", LOG_FAILURE_FIELD_TYPE_NUMBER); #endif /* LOGFMT_META_XFER_MS */ field_add(p, fields, LOGFMT_META_XFER_STATUS, "transfer_status", LOG_FAILURE_FIELD_TYPE_STRING); field_add(p, fields, LOGFMT_META_XFER_FAILURE, "transfer_failure", LOG_FAILURE_FIELD_TYPE_STRING); field_add(p, fields, LOGFMT_META_MICROSECS, "microsecs", LOG_FAILURE_FIELD_TYPE_NUMBER); field_add(p, fields, LOGFMT_META_MILLISECS, "millisecs", LOG_FAILURE_FIELD_TYPE_NUMBER); field_add(p, fields, LOGFMT_META_ISO8601, "timestamp", LOG_FAILURE_FIELD_TYPE_STRING); field_add(p, fields, LOGFMT_META_GROUP, "group", LOG_FAILURE_FIELD_TYPE_STRING); field_add(p, fields, LOG_FAILURE_META_CONNECT, "connecting", LOG_FAILURE_FIELD_TYPE_BOOLEAN); field_add(p, fields, LOG_FAILURE_META_DISCONNECT, "disconnecting", LOG_FAILURE_FIELD_TYPE_BOOLEAN); log_failure_fields = fields; return 0; }
static int goldilocks_shared_secret_core ( uint8_t shared[GOLDI_SHARED_SECRET_BYTES], const struct goldilocks_private_key_t *my_privkey, const struct goldilocks_public_key_t *your_pubkey, const struct goldilocks_precomputed_public_key_t *pre ) { /* This function doesn't actually need anything in goldilocks_global, * so it doesn't check init. */ assert(GOLDI_SHARED_SECRET_BYTES == SHA512_OUTPUT_BYTES); word_t sk[GOLDI_FIELD_WORDS]; struct field_t pk; mask_t succ = field_deserialize(&pk,your_pubkey->opaque), msucc = -1; #ifdef EXPERIMENT_ECDH_STIR_IN_PUBKEYS struct field_t sum, prod; msucc &= field_deserialize(&sum,&my_privkey->opaque[GOLDI_FIELD_BYTES]); field_mul(&prod,&pk,&sum); field_add(&sum,&pk,&sum); #endif msucc &= barrett_deserialize(sk,my_privkey->opaque,&curve_prime_order); #if GOLDI_IMPLEMENT_PRECOMPUTED_KEYS if (pre) { struct tw_extensible_t tw; succ &= scalarmul_fixed_base(&tw, sk, GOLDI_SCALAR_BITS, &pre->table); untwist_and_double_and_serialize(&pk, &tw); } else { succ &= montgomery_ladder(&pk,&pk,sk,GOLDI_SCALAR_BITS,1); } #else (void)pre; succ &= montgomery_ladder(&pk,&pk,sk,GOLDI_SCALAR_BITS,1); #endif field_serialize(shared,&pk); /* obliterate records of our failure by adjusting with obliteration key */ struct sha512_ctx_t ctx; sha512_init(&ctx); #ifdef EXPERIMENT_ECDH_OBLITERATE_CT uint8_t oblit[GOLDI_DIVERSIFY_BYTES + GOLDI_SYMKEY_BYTES]; unsigned i; for (i=0; i<GOLDI_DIVERSIFY_BYTES; i++) { oblit[i] = "noshared"[i] & ~(succ&msucc); } for (i=0; i<GOLDI_SYMKEY_BYTES; i++) { oblit[GOLDI_DIVERSIFY_BYTES+i] = my_privkey->opaque[2*GOLDI_FIELD_BYTES+i] & ~(succ&msucc); } sha512_update(&ctx, oblit, sizeof(oblit)); #endif #ifdef EXPERIMENT_ECDH_STIR_IN_PUBKEYS /* stir in the sum and product of the pubkeys. */ uint8_t a_pk[GOLDI_FIELD_BYTES]; field_serialize(a_pk, &sum); sha512_update(&ctx, a_pk, GOLDI_FIELD_BYTES); field_serialize(a_pk, &prod); sha512_update(&ctx, a_pk, GOLDI_FIELD_BYTES); #endif /* stir in the shared key and finish */ sha512_update(&ctx, shared, GOLDI_FIELD_BYTES); sha512_final(&ctx, shared); return (GOLDI_ECORRUPT & ~msucc) | (GOLDI_EINVAL & msucc &~ succ) | (GOLDI_EOK & msucc & succ); }