/* * KASUMI Encryption */ void KASUMI::encrypt_n(const byte in[], byte out[], size_t blocks) const { for(size_t i = 0; i != blocks; ++i) { u16bit B0 = load_be<u16bit>(in, 0); u16bit B1 = load_be<u16bit>(in, 1); u16bit B2 = load_be<u16bit>(in, 2); u16bit B3 = load_be<u16bit>(in, 3); for(size_t j = 0; j != 8; j += 2) { const u16bit* K = &m_EK[8*j]; u16bit R = B1 ^ (rotate_left(B0, 1) & K[0]); u16bit L = B0 ^ (rotate_left(R, 1) | K[1]); L = FI(L ^ K[ 2], K[ 3]) ^ R; R = FI(R ^ K[ 4], K[ 5]) ^ L; L = FI(L ^ K[ 6], K[ 7]) ^ R; R = B2 ^= R; L = B3 ^= L; R = FI(R ^ K[10], K[11]) ^ L; L = FI(L ^ K[12], K[13]) ^ R; R = FI(R ^ K[14], K[15]) ^ L; R ^= (rotate_left(L, 1) & K[8]); L ^= (rotate_left(R, 1) | K[9]); B0 ^= L; B1 ^= R; } store_be(out, B0, B1, B2, B3); in += BLOCK_SIZE; out += BLOCK_SIZE; } }
/* * 右平衡旋转处理 * 新插入一个结点后,右子树高度大于左子树高度 */ void right_balance(struct avl **tree) { struct avl *right, *rl; right = (*tree)->rchild; switch (right->bf) { case RH: /* 右子树的BF为-1,与根结点的BF值-2符号相同,进行左旋操作 */ /* 即:平衡二叉树某一节点的右孩子的右子树上插入一个新的结点 */ (*tree)->bf = right->bf = EH; /* BF值设为0 */ rotate_left(tree); break; case LH: /* 右子树的BF为1,与根结点的BF值-2符号相反,进行双旋转处理 */ rl = right->lchild; /* rl指向right的左子树 */ switch (rl->bf) { /* 修改tree和它的右孩子的BF */ case LH: (*tree)->bf = EH; right->bf = RH; break; case EH: /* 这个应该不会出现吧 */ (*tree)->bf = right->bf = EH; break; case RH: (*tree)->bf = LH; right->bf = EH; break; } rl->bf = EH; /* 先对tree的右子树进行右旋处理 */ rotate_right(&(*tree)->rchild); /* 最后是对tree进行左旋处理 */ rotate_left(tree); break; } }
static void equilibrate(SgBTreeNode *n, SgBTreeNode **root) { SgBTreeNode *p, *f; SgSize lml, rml; SgSize r; for (;; n = p) { p = n->parent; f = NULL; lml = n->left->mlevel; rml = n->right->mlevel; if (lml < rml) { n->mlevel = rml + 1; if ((r = rml - lml) == 1) { if (rml > 1 && p->left == n) f = rotate_left(n); } else { f = rotate_left(n); if (r > 2) equilibrate(n, &f); } } else if (lml > rml) { n->mlevel = lml + 1; if ((r = lml - rml) == 1) { if (lml > 1 && p->right == n) f = rotate_right(n); } else { f = rotate_right(n); if (r > 2) equilibrate(n, &f); } } else n->mlevel = (lml > rml ? lml : rml) + 1; if (n == *root) { if (f) *root = f; break; } } }
/* balance tree after insertion of N */ static void balance_tree(ScmTreeCore *tc, Node *n) { Node *p = n->parent; if (!p) { BALANCE_CASE("1"); n->color = BLACK; return; } /* root */ if (BLACKP(p)) { BALANCE_CASE("2"); return; } /* nothing to do */ /* Here we're sure we have grandparent. */ Node *g = p->parent; SCM_ASSERT(g != NULL); Node *u = (g->left == p)? g->right : g->left; if (REDP(u)) { p->color = u->color = BLACK; g->color = RED; BALANCE_CASE("3"); balance_tree(tc, g); return; } if (n == p->right && p == g->left) { rotate_left(tc, p); n = n->left; BALANCE_CASE("4a"); } else if (n == p->left && p == g->right) { rotate_right(tc, p); n = n->right; BALANCE_CASE("4b"); } p = n->parent; g = p->parent; p->color = BLACK; g->color = RED; if (n == p->left && p == g->left) { rotate_right(tc, g); BALANCE_CASE("5a"); } else { rotate_left(tc, g); BALANCE_CASE("5b"); } }
void insert_case4(rb_node* n, rb_tree* tree) { rb_node* g = grandparent(n); if ((n == n->parent->right) && (n->parent == g->left)) { rotate_left(n->parent, tree); n = n->left; } else if ((n == n->parent->left) && (n->parent == g->right)) { rotate_right(n->parent, tree); n = n->right; } insert_case5(n, tree); }
/* * Noekeon Key Schedule */ void Noekeon::key_schedule(const byte key[], size_t) { u32bit A0 = load_be<u32bit>(key, 0); u32bit A1 = load_be<u32bit>(key, 1); u32bit A2 = load_be<u32bit>(key, 2); u32bit A3 = load_be<u32bit>(key, 3); for(size_t i = 0; i != 16; ++i) { A0 ^= RC[i]; theta(A0, A1, A2, A3); A1 = rotate_left(A1, 1); A2 = rotate_left(A2, 5); A3 = rotate_left(A3, 2); gamma(A0, A1, A2, A3); A1 = rotate_right(A1, 1); A2 = rotate_right(A2, 5); A3 = rotate_right(A3, 2); } A0 ^= RC[16]; DK.resize(4); DK[0] = A0; DK[1] = A1; DK[2] = A2; DK[3] = A3; theta(A0, A1, A2, A3); EK.resize(4); EK[0] = A0; EK[1] = A1; EK[2] = A2; EK[3] = A3; }
void delete_case2(GtkWidget *darea, rbtree t, node n) { if (node_color(sibling(n)) == RED) { n->parent->color = RED; sibling(n)->color = BLACK; if (n == n->parent->left) rotate_left(darea, t, n->parent); else rotate_right(darea, t, n->parent); } delete_case3(darea, t, n); }
int encode( int enc_ch, int enc_shift ) { int i; if(enc_shift > 0 ) { for(i=0;i<enc_shift;i++) { enc_ch = rotate_right(enc_ch); } } else if (enc_shift < 0) { for(i=0;i>enc_shift;i--) { enc_ch = rotate_left(enc_ch); } } return enc_ch; }
void rb_tree_insert_fixup (RBTREE *T , NODE *x) { // this function is to fixup the rb-tree after calling rb_tree_insert while (x->parent->color == RED) { if (x->parent == x->parent->parent->left) { NODE *y = x->parent->parent->right; if (y->color == RED) { y->color = BLACK; x->parent->color = BLACK; x->parent->parent->color = RED; x = x->parent->parent; }else if (y->color == BLACK) { if (x == x->parent->right) { x = x->parent; rotate_left (T , x); } x->parent->color = BLACK; x->parent->parent->color = RED; rotate_right (T , x->parent->parent); } }else { NODE *y = x->parent->parent->left; if (y->color == RED) { y->color = BLACK; x->parent->color = BLACK; x->parent->parent->color = RED; x = x->parent->parent; }else if (y->color == BLACK) { if (x == x->parent->left) { x = x->parent; rotate_right (T , x); } x->parent->color = BLACK; x->parent->parent->color = RED; rotate_left (T , x->parent->parent); } } } T->root->color = BLACK; }
void RB_insert_fixup(RB_tree *tree, RB_node *node){ node->color = RED; RB_node *last; while(node != tree->root && node->parent->color == RED){ if(is_left(node->parent)){ last = node->parent->parent->right; if(last->color == RED){//1st CASE node->parent->color = BLACK; last->color = BLACK; node->parent->parent->color = RED; node = node->parent->parent; }else{ if(node == node->parent->right){//2nd CASE --> 3rd node = node->parent; rotate_left(tree, node); } node->parent->color = BLACK;//3rd CASE node->parent->parent->color = RED; rotate_right(tree, node->parent->parent); } }else{ last = node->parent->parent->left; if(last->color == RED){//1st CASE node->parent->color = BLACK; last->color = BLACK; node->parent->parent->color = RED; node = node->parent->parent; }else{ if(node == node->parent->left){//2nd CASE --> 3rd node = node->parent; rotate_right(tree, node); } node->parent->color = BLACK;//3rd CASE node->parent->parent->color = RED; rotate_left(tree, node->parent->parent); } } } }
void permutate(char *s, int start, int end) { if (start < end) { for (int i = start; i <= end; ++i) { rotate_left(s, start, i); permutate(s, start + 1, end); rotate_right(s, start, i); } } else { if (comma) printf(","); printf("%s", s); comma = 1; } }
/* * 自底向上调整 */ rbtree_node fixup(rbtree_node node){ /* 情况1:强制左倾 */ if( is_red(node->right) && !is_red(node->left) ){ node = rotate_left(node); } /* 情况2:调整平衡 */ if( is_red(node->left) && is_red(node->left->left)) node = rotate_right(node); /* 情况3:分解4-node */ if( is_red(node->left) && is_red(node->right)) flip_colors(node); return node; }
// нажатие определенной клавиши void Scene3D::keyPressEvent(QKeyEvent* pe) { switch (pe->key()) { case Qt::Key_Plus: scale_plus(); // приблизить сцену break; case Qt::Key_Equal: scale_plus(); // приблизить сцену break; case Qt::Key_Minus: scale_minus(); // удалиться от сцены break; case Qt::Key_Up: rotate_up(); // повернуть сцену вверх break; case Qt::Key_Down: rotate_down(); // повернуть сцену вниз break; case Qt::Key_Left: rotate_left(); // повернуть сцену влево break; case Qt::Key_Right: rotate_right(); // повернуть сцену вправо break; case Qt::Key_Z: translate_down(); // транслировать сцену вниз break; case Qt::Key_X: translate_up(); // транслировать сцену вверх break; case Qt::Key_Space: // клавиша пробела defaultScene(); // возвращение значений по умолчанию break; case Qt::Key_Escape: // клавиша "эскейп" this->close(); // завершает приложение break; } updateGL(); // обновление изображения }
void Scene3D::keyPressEvent(QKeyEvent* pe) { switch (pe->key()) { case Qt::Key_Plus: scale_plus(); break; case Qt::Key_Equal: scale_plus(); break; case Qt::Key_Minus: scale_minus(); break; case Qt::Key_Up: rotate_up(); break; case Qt::Key_Down: rotate_down(); break; case Qt::Key_Left: rotate_left(); break; case Qt::Key_Right: rotate_right(); break; case Qt::Key_Z: translate_down(); break; case Qt::Key_X: translate_up(); break; case Qt::Key_Space: defaultScene(); break; case Qt::Key_Escape: this->close(); break; } updateGL(); }
void balanced_pst::insert_fixup(node* z) { while (z->p->color == RED) { if (z->p == z->p->p->left) { node* y = z->p->p->right; if (y->color == RED) { z->p->color = BLACK; y->color = BLACK; z->p->p->color = RED; z = z->p->p; } else { if (z == z->p->right) { z = z->p; rotate_left(z); } z->p->color = BLACK; z->p->p->color = RED; rotate_right(z->p->p); } } else { node* y = z->p->p->left; if (y->color == RED) { z->p->color = BLACK; y->color = BLACK; z->p->p->color = RED; z = z->p->p; } else { if (z == z->p->left) { z = z->p; rotate_right(z); } z->p->color = BLACK; z->p->p->color = RED; rotate_left(z->p->p); } } } root->color = BLACK; }
/* * RC2 Encryption */ void RC2::encrypt_n(const byte in[], byte out[], size_t blocks) const { for(size_t i = 0; i != blocks; ++i) { u16bit R0 = load_le<u16bit>(in, 0); u16bit R1 = load_le<u16bit>(in, 1); u16bit R2 = load_le<u16bit>(in, 2); u16bit R3 = load_le<u16bit>(in, 3); for(size_t j = 0; j != 16; ++j) { R0 += (R1 & ~R3) + (R2 & R3) + K[4*j]; R0 = rotate_left(R0, 1); R1 += (R2 & ~R0) + (R3 & R0) + K[4*j + 1]; R1 = rotate_left(R1, 2); R2 += (R3 & ~R1) + (R0 & R1) + K[4*j + 2]; R2 = rotate_left(R2, 3); R3 += (R0 & ~R2) + (R1 & R2) + K[4*j + 3]; R3 = rotate_left(R3, 5); if(j == 4 || j == 10) { R0 += K[R3 % 64]; R1 += K[R0 % 64]; R2 += K[R1 % 64]; R3 += K[R2 % 64]; } } store_le(out, R0, R1, R2, R3); in += BLOCK_SIZE; out += BLOCK_SIZE; } }
/* * RC6 Key Schedule */ void RC6::key_schedule(const byte key[], size_t length) { const size_t WORD_KEYLENGTH = (((length - 1) / 4) + 1); const size_t MIX_ROUNDS = 3 * std::max(WORD_KEYLENGTH, S.size()); S[0] = 0xB7E15163; for(size_t i = 1; i != S.size(); ++i) S[i] = S[i-1] + 0x9E3779B9; SecureVector<u32bit> K(8); for(s32bit i = length-1; i >= 0; --i) K[i/4] = (K[i/4] << 8) + key[i]; u32bit A = 0, B = 0; for(size_t i = 0; i != MIX_ROUNDS; ++i) { A = rotate_left(S[i % S.size()] + A + B, 3); B = rotate_left(K[i % WORD_KEYLENGTH] + A + B, (A + B) % 32); S[i % S.size()] = A; K[i % WORD_KEYLENGTH] = B; } }
/** * Rebalance the dictionary tree. * This recomputes the tree depth wayy too often, but so what.. this * only wastes cpu time during the initial dictinary read. */ static Dict_node *rebalance(Dict_node *root) { int bal = tree_balance(root); if (2 == bal) { bal = tree_balance(root->right); if (-1 == bal) { root->right = rotate_right (root->right); } return rotate_left(root); } else if (-2 == bal) { bal = tree_balance(root->left); if (1 == bal) { root->left = rotate_left (root->left); } return rotate_right(root); } return root; }
void insert_case4(GtkWidget *darea, rbtree t, node n) { if (n == n->parent->right && n->parent == grandparent(n)->left) { rotate_left(darea, t, n->parent); n = n->left; } else if (n == n->parent->left && n->parent == grandparent(n)->right) { rotate_right(darea, t, n->parent); n = n->right; } insert_case5(darea, t, n); }
void delete_case6(rb_node* n, rb_tree* tree) { rb_node* s = sibling(n); s->color = n->parent->color; n->parent->color = BLACK; if (n == n->parent->left) { s->right->color = BLACK; rotate_left(n->parent, tree); } else { s->left->color = BLACK; rotate_right(n->parent, tree); } }
inline void II ( uint32& a, uint32 b, uint32 c, uint32 d, uint32 x, uint32 s, uint32 ac ) { a += I(b, c, d) + x + ac; a = rotate_left(a, s); a += b; }
link_t partition_recursive(link_t root, int k) { int cur = root->left->count; if (k < cur) { root->left = partition_recursive(root->left, k); root = rotate_right(root); } else if (k > cur) { root->right = partition_recursive(root->right, k - cur - 1); root = rotate_left(root); } return root; }
void balance_tree(node* n) { if (n!=NULL) { if(n->right_height - n->left_height >1) { if(n->right->right_height - n->right->left_height <0) rotate_right(n->right); rotate_left(n); balance_tree(n->parent); } else if(n->right_height - n->left_height <-1) { if(n->left->right_height - n->left->left_height >0) rotate_left(n->left); rotate_right(n); balance_tree(n->parent); } else { balance_tree(n->parent); } } }
void delete_case6(rbtree t, node n) { sibling(n)->color = node_color(n->parent); n->parent->color = BLACK; if (n == n->parent->left) { assert (node_color(sibling(n)->right) == RED); sibling(n)->right->color = BLACK; rotate_left(t, n->parent); } else { assert (node_color(sibling(n)->left) == RED); sibling(n)->left->color = BLACK; rotate_right(t, n->parent); } }
void delete_case6(GtkWidget *darea, rbtree t, node n) { sibling(n)->color = node_color(n->parent); n->parent->color = BLACK; if (n == n->parent->left) { sibling(n)->right->color = BLACK; rotate_left(darea, t, n->parent); } else { sibling(n)->left->color = BLACK; rotate_right(darea, t, n->parent); } }
static void update_balance(struct Node** root, struct Node* node) { if (node != NULL) { int height_diff = get_height_diff(node); if (height_diff > 1) { bool needs_double_rotation = node->height > 1 && get_height_diff(node->left) < 0; if (needs_double_rotation) { rotate_left(root, node->left); rotate_right(root, node); } else { rotate_right(root, node); } } else if (height_diff < -1) { bool needs_double_rotation = node->height > 1 && get_height_diff(node->right) > 0; if (needs_double_rotation) { rotate_right(root, node->right); rotate_left(root, node); } else { rotate_left(root, node); } } update_balance(root, node->parent); } }
int main() { int n,i,r; printf("Enter number and index:"); scanf("%d",&n); scanf("%d",&i); printf("\n Given number"); display(n); printf("\t Decimal Equivalant: %d",n); printf("\n Bit %d set to 1:",i); r=set_1(n,i); display(r); printf("\t Decimal Equivalant: %d",r); printf("\n Bit %d set to 0:",i); r=set_0(n,i); display(r); printf("\t Decimal Equivalant: %d",r); printf("\n Toggled Bit %d:",i); r=toggle(n,i); display(r); printf("\t Decimal Equivalant: %d",r); printf("\n Toggled Except Bit %d:",i); r=toggle_except(n,i); display(r); printf("\t Decimal Equivalant: %d",r); printf("\n Right Rotate:"); r=rotate_right(n); display(r); printf("\t Decimal Equivalant: %d",r); printf("\n Left Rotate:"); r=rotate_left(n); display(r); printf("\t Decimal Equivalant: %d",r); printf("\n Swap Nibble: "); r=swap(n); display(r); printf("\t Decimal Equivalant: %d",r); return 0; }
/** * AES Key Schedule */ void AES::key_schedule(const byte key[], u32bit length) { static const u32bit RC[10] = { 0x01000000, 0x02000000, 0x04000000, 0x08000000, 0x10000000, 0x20000000, 0x40000000, 0x80000000, 0x1B000000, 0x36000000 }; ROUNDS = (length / 4) + 6; SecureBuffer<u32bit, 64> XEK, XDK; const u32bit X = length / 4; for(u32bit j = 0; j != X; ++j) XEK[j] = load_be<u32bit>(key, j); for(u32bit j = X; j < 4*(ROUNDS+1); j += X) { XEK[j] = XEK[j-X] ^ S(rotate_left(XEK[j-1], 8)) ^ RC[(j-X)/X]; for(u32bit k = 1; k != X; ++k) { if(X == 8 && k == 4) XEK[j+k] = XEK[j+k-X] ^ S(XEK[j+k-1]); else XEK[j+k] = XEK[j+k-X] ^ XEK[j+k-1]; } } for(u32bit j = 0; j != 4*(ROUNDS+1); j += 4) { XDK[j ] = XEK[4*ROUNDS-j ]; XDK[j+1] = XEK[4*ROUNDS-j+1]; XDK[j+2] = XEK[4*ROUNDS-j+2]; XDK[j+3] = XEK[4*ROUNDS-j+3]; } for(u32bit j = 4; j != length + 24; ++j) XDK[j] = TD[SE[get_byte(0, XDK[j])] + 0] ^ TD[SE[get_byte(1, XDK[j])] + 256] ^ TD[SE[get_byte(2, XDK[j])] + 512] ^ TD[SE[get_byte(3, XDK[j])] + 768]; for(u32bit j = 0; j != 4; ++j) { store_be(XEK[j+4*ROUNDS], ME + 4*j); store_be(XEK[j], MD + 4*j); } EK.copy(XEK, length + 24); DK.copy(XDK, length + 24); }
/*! * Compute the check sum value according to the algorithm found in the header file * \param data the array on which perform the computation * \param index_min first index in the array to take into account * \param index_max last index (EXCLUSIVE) in the array to take into account */ uchar compute_checksum (uchar *data, int index_min, int index_max) { uchar checksum; int index; checksum = 0; for (index = index_min; index < index_max; index++) { checksum ^= data[index]; checksum = rotate_left (checksum); } return checksum; }
/** Compute the hash value of an object and if it makes sense to store it in * the objects status_flags, do so. The method inherited from class basic * computes a hash value based on the type and hash values of possible * members. For this reason it is well suited for container classes but * atomic classes should override this implementation because otherwise they * would all end up with the same hashvalue. */ unsigned basic::calchash() const { unsigned v = make_hash_seed(typeid(*this)); for (size_t i=0; i<nops(); i++) { v = rotate_left(v); v ^= this->op(i).gethash(); } // store calculated hash value only if object is already evaluated if (flags & status_flags::evaluated) { setflag(status_flags::hash_calculated); hashvalue = v; } return v; }