// File: permutationlock[P]aes_math/lib/aes.cpp
// Function: encrypt(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, int)
string encrypt(string plaintext, string key, int rounds){
    // Set up Galois Field
    GaloisPolynomial::globalSetModulus(rijndael_Mod);
    
    // Convert strings to unsigned characters
    vector<unsigned char> vplaintext(plaintext.begin(), plaintext.end());
    vector<unsigned char> vkey(key.begin(), key.end());
    
    // Expand key
    vector<QSMatrix<GaloisPolynomial>> keyMatrices = expandKey(vkey, rounds);
    
    // Convert plaintext to matrix representation
    vector<GaloisPolynomial> sVec;
    sVec.reserve(16);
    for(int i=0; i<16; i++){
        sVec.push_back(extractPoly(vplaintext));
    }
    QSMatrix<GaloisPolynomial> state(4, 4, sVec);
    
    // Perform rounds on plaintext
    addRoundKey(state, keyMatrices[0]);
    for(int i=1; i<rounds; i++){
        subBytes(state);
        shiftRows(state);
        mixColumns(state);
        addRoundKey(state, keyMatrices[i]);
    }
    subBytes(state);
    shiftRows(state);
    addRoundKey(state, keyMatrices[rounds]);
    
    // Convert back to string
    vector<unsigned char> vciphertext;
    for(int i=0; i<4; i++){
        for(int j=0; j<4; j++){
            vciphertext.push_back(polyToChar(state(i,j)));
        }
    }
    string ciphertext(vciphertext.begin(), vciphertext.end());
    
    return ciphertext;
}

// Assembly:
subq $0x1c8, %rsp            # imm = 0x1C8
movq %rdx, 0x30(%rsp)
movq %rsi, 0x48(%rsp)
movq %rdi, 0x38(%rsp)
movq %rdi, %rax
movq %rax, 0x40(%rsp)
movq %rdi, 0x1c0(%rsp)
movq %rsi, 0x1b8(%rsp)
movq %rdx, 0x1b0(%rsp)
movl %ecx, 0x1ac(%rsp)
leaq 0x12c94(%rip), %rdi     # 0x1e1d8
callq 0x6700
movq 0x48(%rsp), %rdi
callq 0x2170
movq 0x48(%rsp), %rdi
movq %rax, 0x188(%rsp)
callq 0x20d0
movq %rax, 0x180(%rsp)
leaq 0x17f(%rsp), %rdi
movq %rdi, 0x50(%rsp)
callq 0xce50
movq 0x50(%rsp), %rcx
movq 0x188(%rsp), %rsi
movq 0x180(%rsp), %rdx
leaq 0x190(%rsp), %rdi
callq 0xce60
jmp 0xb5a3
leaq 0x17f(%rsp), %rdi
callq 0xcef0
movq 0x30(%rsp), %rdi
callq 0x2170
movq 0x30(%rsp), %rdi
movq %rax, 0x148(%rsp)
callq 0x20d0
movq %rax, 0x140(%rsp)
leaq 0x13f(%rsp), %rdi
movq %rdi, 0x28(%rsp)
callq 0xce50
movq 0x28(%rsp), %rcx
movq 0x148(%rsp), %rsi
movq 0x140(%rsp), %rdx
leaq 0x150(%rsp), %rdi
callq 0xce60
jmp 0xb60a
leaq 0x13f(%rsp), %rdi
callq 0xcef0
leaq 0x108(%rsp), %rdi
leaq 0x150(%rsp), %rsi
callq 0xcf00
jmp 0xb62e
movl 0x1ac(%rsp), %edx
leaq 0x120(%rsp), %rdi
leaq 0x108(%rsp), %rsi
callq 0xb010
jmp 0xb64c
leaq 0x108(%rsp), %rdi
callq 0xd000
leaq 0xf0(%rsp), %rdi
movq %rdi, 0x20(%rsp)
callq 0xc420
movq 0x20(%rsp), %rdi
movl $0x10, %esi
callq 0xcac0
jmp 0xb67c
movl $0x0, 0xec(%rsp)
cmpl $0x10, 0xec(%rsp)
jge 0xb7ad
leaq 0xc8(%rsp), %rdi
leaq 0x190(%rsp), %rsi
callq 0xaca0
jmp 0xb6ac
leaq 0xf0(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
callq 0xcc00
jmp 0xb6c3
leaq 0xc8(%rsp), %rdi
callq 0x6f00
movl 0xec(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xec(%rsp)
jmp 0xb687
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x170(%rsp)
movl %eax, 0x16c(%rsp)
leaq 0x17f(%rsp), %rdi
callq 0xcef0
jmp 0xbac5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x170(%rsp)
movl %eax, 0x16c(%rsp)
leaq 0x13f(%rsp), %rdi
callq 0xcef0
jmp 0xbab8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x170(%rsp)
movl %eax, 0x16c(%rsp)
jmp 0xbaab
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x170(%rsp)
movl %eax, 0x16c(%rsp)
leaq 0x108(%rsp), %rdi
callq 0xd000
jmp 0xbaab
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x170(%rsp)
movl %eax, 0x16c(%rsp)
jmp 0xba91
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x170(%rsp)
movl %eax, 0x16c(%rsp)
leaq 0xc8(%rsp), %rdi
callq 0x6f00
jmp 0xba91
leaq 0xa0(%rsp), %rdi
movl $0x4, %edx
leaq 0xf0(%rsp), %rcx
movl %edx, %esi
callq 0xc160
jmp 0xb7cb
xorl %eax, %eax
movl %eax, %esi
leaq 0x120(%rsp), %rdi
callq 0xd060
movq %rax, %rsi
leaq 0xa0(%rsp), %rdi
callq 0xac60
jmp 0xb7ee
movl $0x1, 0x9c(%rsp)
movl 0x9c(%rsp), %eax
cmpl 0x1ac(%rsp), %eax
jge 0xb88f
leaq 0xa0(%rsp), %rdi
callq 0xa6e0
jmp 0xb81c
leaq 0xa0(%rsp), %rdi
callq 0xa840
jmp 0xb82b
leaq 0xa0(%rsp), %rdi
callq 0xab80
jmp 0xb83a
movslq 0x9c(%rsp), %rsi
leaq 0x120(%rsp), %rdi
callq 0xd060
movq %rax, %rsi
leaq 0xa0(%rsp), %rdi
callq 0xac60
jmp 0xb861
jmp 0xb863
movl 0x9c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x9c(%rsp)
jmp 0xb7f9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x170(%rsp)
movl %eax, 0x16c(%rsp)
jmp 0xba84
leaq 0xa0(%rsp), %rdi
callq 0xa6e0
jmp 0xb89e
leaq 0xa0(%rsp), %rdi
callq 0xa840
jmp 0xb8ad
movslq 0x1ac(%rsp), %rsi
leaq 0x120(%rsp), %rdi
callq 0xd060
movq %rax, %rsi
leaq 0xa0(%rsp), %rdi
callq 0xac60
jmp 0xb8d4
leaq 0x80(%rsp), %rdi
callq 0xd080
movl $0x0, 0x7c(%rsp)
cmpl $0x4, 0x7c(%rsp)
jge 0xb987
movl $0x0, 0x78(%rsp)
cmpl $0x4, 0x78(%rsp)
jge 0xb975
leaq 0xa0(%rsp), %rdi
leaq 0x7c(%rsp), %rsi
leaq 0x78(%rsp), %rdx
callq 0xc3e0
movq %rax, 0x18(%rsp)
jmp 0xb921
movq 0x18(%rsp), %rdi
callq 0xac90
movb %al, 0x17(%rsp)
jmp 0xb931
movb 0x17(%rsp), %al
movb %al, 0x77(%rsp)
leaq 0x80(%rsp), %rdi
leaq 0x77(%rsp), %rsi
callq 0xd090
jmp 0xb94d
jmp 0xb94f
movl 0x78(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x78(%rsp)
jmp 0xb8fc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x170(%rsp)
movl %eax, 0x16c(%rsp)
jmp 0xba77
jmp 0xb977
movl 0x7c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x7c(%rsp)
jmp 0xb8e9
movb $0x0, 0x76(%rsp)
leaq 0x80(%rsp), %rdi
movq %rdi, (%rsp)
callq 0xca20
movq (%rsp), %rdi
movq %rax, 0x68(%rsp)
callq 0xd0c0
movq %rax, 0x60(%rsp)
leaq 0x5f(%rsp), %rdi
movq %rdi, 0x8(%rsp)
callq 0x22c0
movq 0x38(%rsp), %rdi
movq 0x8(%rsp), %rcx
movq 0x68(%rsp), %rsi
movq 0x60(%rsp), %rdx
callq 0xd0f0
jmp 0xb9da
leaq 0x5f(%rsp), %rdi
callq 0x21f0
movb $0x1, 0x76(%rsp)
testb $0x1, 0x76(%rsp)
jne 0xba1c
jmp 0xba12
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x170(%rsp)
movl %eax, 0x16c(%rsp)
leaq 0x5f(%rsp), %rdi
callq 0x21f0
jmp 0xba77
movq 0x38(%rsp), %rdi
callq 0x20e0
leaq 0x80(%rsp), %rdi
callq 0xd000
leaq 0xa0(%rsp), %rdi
callq 0xc360
leaq 0xf0(%rsp), %rdi
callq 0xc2f0
leaq 0x120(%rsp), %rdi
callq 0xcdf0
leaq 0x150(%rsp), %rdi
callq 0xd000
leaq 0x190(%rsp), %rdi
callq 0xd000
movq 0x40(%rsp), %rax
addq $0x1c8, %rsp            # imm = 0x1C8
retq
leaq 0x80(%rsp), %rdi
callq 0xd000
leaq 0xa0(%rsp), %rdi
callq 0xc360
leaq 0xf0(%rsp), %rdi
callq 0xc2f0
leaq 0x120(%rsp), %rdi
callq 0xcdf0
leaq 0x150(%rsp), %rdi
callq 0xd000
leaq 0x190(%rsp), %rdi
callq 0xd000
movq 0x170(%rsp), %rdi
callq 0x22b0
nopw %cs:(%rax,%rax)