int sc_main(int argc, char* argv[]) { sc_signal<bool> Din; sc_signal< sc_uint<4> > r; sc_clock TestClk("TestClock", 10, SC_NS,0,SC_NS); stim Stim1("Stimulus"); Stim1.Din(Din); sr SR1("SR1"); SR1.din(Din); SR1.clk(TestClk); SR1.r(r); mon Monitor1("Monitor"); Monitor1.Din(Din); Monitor1.r(r); Monitor1.Clk(TestClk); sc_start(); // run forever return 0; }
/* Process 3OP Integer instructions */ bool eval_3OP_Int(struct lilith* vm, struct Instruction* c) { #ifdef DEBUG char Name[20] = "ILLEGAL_3OP"; #endif switch(c->raw_XOP) { case 0x000: /* ADD */ { #ifdef DEBUG strncpy(Name, "ADD", 19); #elif TRACE record_trace("ADD"); #endif ADD(vm, c); break; } case 0x001: /* ADDU */ { #ifdef DEBUG strncpy(Name, "ADDU", 19); #elif TRACE record_trace("ADDU"); #endif ADDU(vm, c); break; } case 0x002: /* SUB */ { #ifdef DEBUG strncpy(Name, "SUB", 19); #elif TRACE record_trace("SUB"); #endif SUB(vm, c); break; } case 0x003: /* SUBU */ { #ifdef DEBUG strncpy(Name, "SUBU", 19); #elif TRACE record_trace("SUBU"); #endif SUBU(vm, c); break; } case 0x004: /* CMP */ { #ifdef DEBUG strncpy(Name, "CMP", 19); #elif TRACE record_trace("CMP"); #endif CMP(vm, c); break; } case 0x005: /* CMPU */ { #ifdef DEBUG strncpy(Name, "CMPU", 19); #elif TRACE record_trace("CMPU"); #endif CMPU(vm, c); break; } case 0x006: /* MUL */ { #ifdef DEBUG strncpy(Name, "MUL", 19); #elif TRACE record_trace("MUL"); #endif MUL(vm, c); break; } case 0x007: /* MULH */ { #ifdef DEBUG strncpy(Name, "MULH", 19); #elif TRACE record_trace("MULH"); #endif MULH(vm, c); break; } case 0x008: /* MULU */ { #ifdef DEBUG strncpy(Name, "MULU", 19); #elif TRACE record_trace("MULU"); #endif MULU(vm, c); break; } case 0x009: /* MULUH */ { #ifdef DEBUG strncpy(Name, "MULUH", 19); #elif TRACE record_trace("MULUH"); #endif MULUH(vm, c); break; } case 0x00A: /* DIV */ { #ifdef DEBUG strncpy(Name, "DIV", 19); #elif TRACE record_trace("DIV"); #endif DIV(vm, c); break; } case 0x00B: /* MOD */ { #ifdef DEBUG strncpy(Name, "MOD", 19); #elif TRACE record_trace("MOD"); #endif MOD(vm, c); break; } case 0x00C: /* DIVU */ { #ifdef DEBUG strncpy(Name, "DIVU", 19); #elif TRACE record_trace("DIVU"); #endif DIVU(vm, c); break; } case 0x00D: /* MODU */ { #ifdef DEBUG strncpy(Name, "MODU", 19); #elif TRACE record_trace("MODU"); #endif MODU(vm, c); break; } case 0x010: /* MAX */ { #ifdef DEBUG strncpy(Name, "MAX", 19); #elif TRACE record_trace("MAX"); #endif MAX(vm, c); break; } case 0x011: /* MAXU */ { #ifdef DEBUG strncpy(Name, "MAXU", 19); #elif TRACE record_trace("MAXU"); #endif MAXU(vm, c); break; } case 0x012: /* MIN */ { #ifdef DEBUG strncpy(Name, "MIN", 19); #elif TRACE record_trace("MIN"); #endif MIN(vm, c); break; } case 0x013: /* MINU */ { #ifdef DEBUG strncpy(Name, "MINU", 19); #elif TRACE record_trace("MINU"); #endif MINU(vm, c); break; } case 0x014: /* PACK */ case 0x015: /* UNPACK */ case 0x016: /* PACK8.CO */ case 0x017: /* PACK8U.CO */ case 0x018: /* PACK16.CO */ case 0x019: /* PACK16U.CO */ case 0x01A: /* PACK32.CO */ case 0x01B: /* PACK32U.CO */ { illegal_instruction(vm, c); break; } case 0x020: /* AND */ { #ifdef DEBUG strncpy(Name, "AND", 19); #elif TRACE record_trace("AND"); #endif AND(vm, c); break; } case 0x021: /* OR */ { #ifdef DEBUG strncpy(Name, "OR", 19); #elif TRACE record_trace("OR"); #endif OR(vm, c); break; } case 0x022: /* XOR */ { #ifdef DEBUG strncpy(Name, "XOR", 19); #elif TRACE record_trace("XOR"); #endif XOR(vm, c); break; } case 0x023: /* NAND */ { #ifdef DEBUG strncpy(Name, "NAND", 19); #elif TRACE record_trace("NAND"); #endif NAND(vm, c); break; } case 0x024: /* NOR */ { #ifdef DEBUG strncpy(Name, "NOR", 19); #elif TRACE record_trace("NOR"); #endif NOR(vm, c); break; } case 0x025: /* XNOR */ { #ifdef DEBUG strncpy(Name, "XNOR", 19); #elif TRACE record_trace("XNOR"); #endif XNOR(vm, c); break; } case 0x026: /* MPQ */ { #ifdef DEBUG strncpy(Name, "MPQ", 19); #elif TRACE record_trace("MPQ"); #endif MPQ(vm, c); break; } case 0x027: /* LPQ */ { #ifdef DEBUG strncpy(Name, "LPQ", 19); #elif TRACE record_trace("LPQ"); #endif LPQ(vm, c); break; } case 0x028: /* CPQ */ { #ifdef DEBUG strncpy(Name, "CPQ", 19); #elif TRACE record_trace("CPQ"); #endif CPQ(vm, c); break; } case 0x029: /* BPQ */ { #ifdef DEBUG strncpy(Name, "BPQ", 19); #elif TRACE record_trace("BPQ"); #endif BPQ(vm, c); break; } case 0x030: /* SAL */ { #ifdef DEBUG strncpy(Name, "SAL", 19); #elif TRACE record_trace("SAL"); #endif SAL(vm, c); break; } case 0x031: /* SAR */ { #ifdef DEBUG strncpy(Name, "SAR", 19); #elif TRACE record_trace("SAR"); #endif SAR(vm, c); break; } case 0x032: /* SL0 */ { #ifdef DEBUG strncpy(Name, "SL0", 19); #elif TRACE record_trace("SL0"); #endif SL0(vm, c); break; } case 0x033: /* SR0 */ { #ifdef DEBUG strncpy(Name, "SR0", 19); #elif TRACE record_trace("SR0"); #endif SR0(vm, c); break; } case 0x034: /* SL1 */ { #ifdef DEBUG strncpy(Name, "SL1", 19); #elif TRACE record_trace("SL1"); #endif SL1(vm, c); break; } case 0x035: /* SR1 */ { #ifdef DEBUG strncpy(Name, "SR1", 19); #elif TRACE record_trace("SR1"); #endif SR1(vm, c); break; } case 0x036: /* ROL */ { #ifdef DEBUG strncpy(Name, "ROL", 19); #elif TRACE record_trace("ROL"); #endif ROL(vm, c); break; } case 0x037: /* ROR */ { #ifdef DEBUG strncpy(Name, "ROR", 19); #elif TRACE record_trace("ROR"); #endif ROR(vm, c); break; } case 0x038: /* LOADX */ { #ifdef DEBUG strncpy(Name, "LOADX", 19); #elif TRACE record_trace("LOADX"); #endif LOADX(vm, c); break; } case 0x039: /* LOADX8 */ { #ifdef DEBUG strncpy(Name, "LOADX8", 19); #elif TRACE record_trace("LOADX8"); #endif LOADX8(vm, c); break; } case 0x03A: /* LOADXU8 */ { #ifdef DEBUG strncpy(Name, "LOADXU8", 19); #elif TRACE record_trace("LOADXU8"); #endif LOADXU8(vm, c); break; } case 0x03B: /* LOADX16 */ { #ifdef DEBUG strncpy(Name, "LOADX16", 19); #elif TRACE record_trace("LOADX16"); #endif LOADX16(vm, c); break; } case 0x03C: /* LOADXU16 */ { #ifdef DEBUG strncpy(Name, "LOADXU16", 19); #elif TRACE record_trace("LOADXU16"); #endif LOADXU16(vm, c); break; } case 0x03D: /* LOADX32 */ { #ifdef DEBUG strncpy(Name, "LOADX32", 19); #elif TRACE record_trace("LOADX32"); #endif LOADX32(vm, c); break; } case 0x03E: /* LOADXU32 */ { #ifdef DEBUG strncpy(Name, "LOADXU32", 19); #elif TRACE record_trace("LOADXU32"); #endif LOADXU32(vm, c); break; } case 0x048: /* STOREX */ { #ifdef DEBUG strncpy(Name, "STOREX", 19); #elif TRACE record_trace("STOREX"); #endif STOREX(vm, c); break; } case 0x049: /* STOREX8 */ { #ifdef DEBUG strncpy(Name, "STOREX8", 19); #elif TRACE record_trace("STOREX8"); #endif STOREX8(vm, c); break; } case 0x04A: /* STOREX16 */ { #ifdef DEBUG strncpy(Name, "STOREX16", 19); #elif TRACE record_trace("STOREX16"); #endif STOREX16(vm, c); break; } case 0x04B: /* STOREX32 */ { #ifdef DEBUG strncpy(Name, "STOREX32", 19); #elif TRACE record_trace("STOREX32"); #endif STOREX32(vm, c); break; } case 0x050: /* CMPJUMP.G */ { #ifdef DEBUG strncpy(Name, "CMPJUMP.G", 19); #elif TRACE record_trace("CMPJUMP.G"); #endif CMPJUMP_G(vm, c); break; } case 0x051: /* CMPJUMP.GE */ { #ifdef DEBUG strncpy(Name, "CMPJUMP.GE", 19); #elif TRACE record_trace("CMPJUMP.GE"); #endif CMPJUMP_GE(vm, c); break; } case 0x052: /* CMPJUMP.E */ { #ifdef DEBUG strncpy(Name, "CMPJUMP.E", 19); #elif TRACE record_trace("CMPJUMP.E"); #endif CMPJUMP_E(vm, c); break; } case 0x053: /* CMPJUMP.NE */ { #ifdef DEBUG strncpy(Name, "CMPJUMP.NE", 19); #elif TRACE record_trace("CMPJUMP.NE"); #endif CMPJUMP_NE(vm, c); break; } case 0x054: /* CMPJUMP.LE */ { #ifdef DEBUG strncpy(Name, "CMPJUMP.LE", 19); #elif TRACE record_trace("CMPJUMP.LE"); #endif CMPJUMP_LE(vm, c); break; } case 0x055: /* CMPJUMP.L */ { #ifdef DEBUG strncpy(Name, "CMPJUMP.L", 19); #elif TRACE record_trace("CMPJUMP.L"); #endif CMPJUMP_L(vm, c); break; } case 0x060: /* CMPJUMPU.G */ { #ifdef DEBUG strncpy(Name, "CMPJUMPU.G", 19); #elif TRACE record_trace("CMPJUMPU.G"); #endif CMPJUMPU_G(vm, c); break; } case 0x061: /* CMPJUMPU.GE */ { #ifdef DEBUG strncpy(Name, "CMPJUMPU.GE", 19); #elif TRACE record_trace("CMPJUMPU.GE"); #endif CMPJUMPU_GE(vm, c); break; } case 0x064: /* CMPJUMPU.LE */ { #ifdef DEBUG strncpy(Name, "CMPJUMPU.LE", 19); #elif TRACE record_trace("CMPJUMPU.LE"); #endif CMPJUMPU_LE(vm, c); break; } case 0x065: /* CMPJUMPU.L */ { #ifdef DEBUG strncpy(Name, "CMPJUMPU.L", 19); #elif TRACE record_trace("CMPJUMPU.L"); #endif CMPJUMPU_L(vm, c); break; } default: { illegal_instruction(vm, c); break; } } #ifdef DEBUG fprintf(stdout, "# %s reg%u reg%u reg%u\n", Name, c->reg0, c->reg1, c->reg2); #endif return false; }
void encrypt_64blocks(u256 x[32], u256 rk[40][16]) { int i, j; u256 rc, tmp[8]; rc = _mm256_set_epi64x(0x000000FF000000FFull, 0x000000FF000000FFull, 0x000000FF000000FFull, 0x000000FF000000FFull); for(i = 0; i < 40; i++){ //SubBytes for(j = 0; j < 4; j++) { tmp[7] = XOR(x[2 + 8*j], NOR(XOR(x[3 + 8*j], NOR(x[0 + 8*j],x[1 + 8*j])),XOR(x[7 + 8*j], NOR(x[4 + 8*j],x[5 + 8*j])))); tmp[6] = XOR(x[3 + 8*j], NOR(x[0 + 8*j],x[1 + 8*j])); tmp[5] = XOR(x[7 + 8*j], NOR(x[4 + 8*j],x[5 + 8*j])); tmp[4] = XOR(x[4 + 8*j], NOR(XOR(x[2 + 8*j], NOR(XOR(x[3 + 8*j], NOR(x[0 + 8*j],x[1 + 8*j])),XOR(x[7 + 8*j], NOR(x[4 + 8*j],x[5 + 8*j])))),XOR(x[3 + 8*j], NOR(x[0 + 8*j],x[1 + 8*j])))); tmp[3] = XOR(x[6 + 8*j], NOR(XOR(x[7 + 8*j], NOR(x[4 + 8*j],x[5 + 8*j])),x[4 + 8*j])); tmp[2] = XOR(x[1 + 8*j], NOR(x[5 + 8*j],x[6 + 8*j])); tmp[1] = XOR(x[0 + 8*j], NOR(XOR(x[1 + 8*j], NOR(x[5 + 8*j],x[6 + 8*j])),XOR(x[2 + 8*j], NOR(XOR(x[3 + 8*j], NOR(x[0 + 8*j],x[1 + 8*j])),XOR(x[7 + 8*j], NOR(x[4 + 8*j],x[5 + 8*j])))))); tmp[0] = XOR(x[5 + 8*j], NOR(XOR(x[6 + 8*j], NOR(XOR(x[7 + 8*j], NOR(x[4 + 8*j],x[5 + 8*j])),x[4 + 8*j])),XOR(x[0 + 8*j], NOR(XOR(x[1 + 8*j], NOR(x[5 + 8*j],x[6 + 8*j])),XOR(x[2 + 8*j], NOR(XOR(x[3 + 8*j], NOR(x[0 + 8*j],x[1 + 8*j])),XOR(x[7 + 8*j], NOR(x[4 + 8*j],x[5 + 8*j])))))))); x[0 + 8*j] = tmp[7]; x[1 + 8*j] = tmp[6]; x[2 + 8*j] = tmp[5]; x[3 + 8*j] = tmp[4]; x[4 + 8*j] = tmp[3]; x[5 + 8*j] = tmp[2]; x[6 + 8*j] = tmp[1]; x[7 + 8*j] = tmp[0]; } //AddConstant //This only adds c2. The other constants are added with the key x[22] = XOR(x[22], rc); //AddKey x[0] = XOR(x[0], rk[i][0]); x[1] = XOR(x[1], rk[i][1]); x[2] = XOR(x[2], rk[i][2]); x[3] = XOR(x[3], rk[i][3]); x[4] = XOR(x[4], rk[i][4]); x[5] = XOR(x[5], rk[i][5]); x[6] = XOR(x[6], rk[i][6]); x[7] = XOR(x[7], rk[i][7]); x[8] = XOR(x[8], rk[i][8]); x[9] = XOR(x[9], rk[i][9]); x[10] = XOR(x[10], rk[i][10]); x[11] = XOR(x[11], rk[i][11]); x[12] = XOR(x[12], rk[i][12]); x[13] = XOR(x[13], rk[i][13]); x[14] = XOR(x[14], rk[i][14]); x[15] = XOR(x[15], rk[i][15]); //ShiftRows x[8] = SR1(x[8]); x[16] = SR2(x[16]); x[24] = SR3(x[24]); x[9] = SR1(x[9]); x[17] = SR2(x[17]); x[25] = SR3(x[25]); x[10] = SR1(x[10]); x[18] = SR2(x[18]); x[26] = SR3(x[26]); x[11] = SR1(x[11]); x[19] = SR2(x[19]); x[27] = SR3(x[27]); x[12] = SR1(x[12]); x[20] = SR2(x[20]); x[28] = SR3(x[28]); x[13] = SR1(x[13]); x[21] = SR2(x[21]); x[29] = SR3(x[29]); x[14] = SR1(x[14]); x[22] = SR2(x[22]); x[30] = SR3(x[30]); x[15] = SR1(x[15]); x[23] = SR2(x[23]); x[31] = SR3(x[31]); //MixColumns tmp[0] = x[24]; tmp[1] = x[25]; tmp[2] = x[26]; tmp[3] = x[27]; tmp[4] = x[28]; tmp[5] = x[29]; tmp[6] = x[30]; tmp[7] = x[31]; x[24] = XOR(x[16], x[0]); x[28] = XOR(x[20], x[4]); x[25] = XOR(x[17], x[1]); x[29] = XOR(x[21], x[5]); x[26] = XOR(x[18], x[2]); x[30] = XOR(x[22], x[6]); x[27] = XOR(x[19], x[3]); x[31] = XOR(x[23], x[7]); x[16] = XOR(x[8], x[16]); x[20] = XOR(x[12], x[20]); x[17] = XOR(x[9], x[17]); x[21] = XOR(x[13], x[21]); x[18] = XOR(x[10], x[18]); x[22] = XOR(x[14], x[22]); x[19] = XOR(x[11], x[19]); x[23] = XOR(x[15], x[23]); x[8] = x[0]; x[12] = x[4]; x[9] = x[1]; x[13] = x[5]; x[10] = x[2]; x[14] = x[6]; x[11] = x[3]; x[15] = x[7]; x[0] = XOR(tmp[0], x[24]); x[4] = XOR(tmp[4], x[28]); x[1] = XOR(tmp[1], x[25]); x[5] = XOR(tmp[5], x[29]); x[2] = XOR(tmp[2], x[26]); x[6] = XOR(tmp[6], x[30]); x[3] = XOR(tmp[3], x[27]); x[7] = XOR(tmp[7], x[31]); } }