// File: jBlinden[P]private-codis/protocol.cpp
// Function: CreateOTCorrelation(SenderOTCorrelation&, ReceiverOTCorrelation&, unsigned char, unsigned char)
void CreateOTCorrelation(SenderOTCorrelation &sc,
                         ReceiverOTCorrelation &rc, uint8_t bitsPerFSMROT, uint8_t bitsPerFSMSOT)
{
  if ((sc.N != rc.N) ||
      (sc.K != rc.K))
  {
    throw "sender/receiver correlation type mismatch";
  }

  uint8_t K = rc.K;
  uint32_t N = rc.N;

  AESContext senderKey;
  senderKey.FillBuffer((uint8_t *)sc.blindingFactors, N * K * sizeof(uint8_t), 0);

  for (int i = 0; i < N * K; i++){
    sc.blindingFactors[i] &= (uint8_t)((1 << bitsPerFSMSOT) - 1);
  }
  
  AESContext receiverKey;
  receiverKey.FillBuffer((uint8_t *)rc.choices, N, 0);

  sc.msgKey = senderKey;
  rc.choiceKey = receiverKey;

  for (uint32_t i = 0; i < N; i++)
  {
    rc.choices[i] %= K;
    rc.blindingFactors[i] = sc.blindingFactors[i * K + rc.choices[i]] & ((uint8_t) (1 << bitsPerFSMROT) - 1);
  }
}

// Assembly:
subq $0x268, %rsp            # imm = 0x268
movb %cl, %al
movb %dl, %cl
movq %rdi, 0x260(%rsp)
movq %rsi, 0x258(%rsp)
movb %cl, 0x257(%rsp)
movb %al, 0x256(%rsp)
movq 0x260(%rsp), %rax
movl 0x4(%rax), %eax
movq 0x258(%rsp), %rcx
cmpl 0x4(%rcx), %eax
jne 0x602b
movq 0x260(%rsp), %rax
movzbl (%rax), %eax
movq 0x258(%rsp), %rcx
movzbl (%rcx), %ecx
cmpl %ecx, %eax
je 0x6052
movl $0x8, %edi
callq 0x3150
movq %rax, %rdi
leaq 0x420a(%rip), %rax      # 0xa249
movq %rax, (%rdi)
movq 0x6f7f(%rip), %rsi      # 0xcfc8
xorl %eax, %eax
movl %eax, %edx
callq 0x3440
movq 0x258(%rsp), %rax
movb (%rax), %al
movb %al, 0x255(%rsp)
movq 0x258(%rsp), %rax
movl 0x4(%rax), %eax
movl %eax, 0x250(%rsp)
leaq 0x140(%rsp), %rdi
movq %rdi, 0x10(%rsp)
callq 0x8880
movq 0x10(%rsp), %rdi
movq 0x260(%rsp), %rax
movq 0x120(%rax), %rsi
movl 0x250(%rsp), %edx
movzbl 0x255(%rsp), %eax
imull %eax, %edx
xorl %eax, %eax
movl %eax, %ecx
callq 0x9d50
jmp 0x60b8
movl $0x0, 0x130(%rsp)
movl 0x130(%rsp), %eax
movl 0x250(%rsp), %ecx
movzbl 0x255(%rsp), %edx
imull %edx, %ecx
cmpl %ecx, %eax
jae 0x6141
movzbl 0x256(%rsp), %ecx
movl $0x1, %eax
shll %cl, %eax
subl $0x1, %eax
movzbl %al, %esi
movq 0x260(%rsp), %rax
movq 0x120(%rax), %rax
movslq 0x130(%rsp), %rcx
movzbl (%rax,%rcx), %edx
andl %esi, %edx
movb %dl, (%rax,%rcx)
movl 0x130(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x130(%rsp)
jmp 0x60c3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x138(%rsp)
movl %eax, 0x134(%rsp)
jmp 0x62b1
leaq 0x20(%rsp), %rdi
callq 0x8880
jmp 0x614d
movq 0x258(%rsp), %rax
movq 0x120(%rax), %rsi
movl 0x250(%rsp), %edx
xorl %eax, %eax
movl %eax, %ecx
leaq 0x20(%rsp), %rdi
callq 0x9d50
jmp 0x6173
movq 0x260(%rsp), %rdi
addq $0x10, %rdi
leaq 0x140(%rsp), %rsi
movl $0x110, %edx            # imm = 0x110
callq 0x3200
movq 0x258(%rsp), %rdi
addq $0x10, %rdi
leaq 0x20(%rsp), %rsi
movl $0x110, %edx            # imm = 0x110
callq 0x3200
movl $0x0, 0x1c(%rsp)
movl 0x1c(%rsp), %eax
cmpl 0x250(%rsp), %eax
jae 0x6292
movzbl 0x255(%rsp), %esi
movq 0x258(%rsp), %rax
movq 0x120(%rax), %rax
movq %rax, 0x8(%rsp)
movl 0x1c(%rsp), %ecx
movzbl (%rax,%rcx), %eax
cltd
idivl %esi
movq 0x8(%rsp), %rax
movb %dl, (%rax,%rcx)
movq 0x260(%rsp), %rax
movq 0x120(%rax), %rax
movl 0x1c(%rsp), %ecx
movzbl 0x255(%rsp), %edx
imull %edx, %ecx
movq 0x258(%rsp), %rdx
movq 0x120(%rdx), %rdx
movl 0x1c(%rsp), %esi
movzbl (%rdx,%rsi), %edx
addl %edx, %ecx
movl %ecx, %ecx
movzbl (%rax,%rcx), %eax
movzbl 0x257(%rsp), %ecx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
movzbl %cl, %ecx
subl $0x1, %ecx
andl %ecx, %eax
movb %al, %dl
movq 0x258(%rsp), %rax
movq 0x128(%rax), %rax
movl 0x1c(%rsp), %ecx
movb %dl, (%rax,%rcx)
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0x61b4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x138(%rsp)
movl %eax, 0x134(%rsp)
leaq 0x20(%rsp), %rdi
callq 0x98b0
jmp 0x62b1
leaq 0x20(%rsp), %rdi
callq 0x98b0
leaq 0x140(%rsp), %rdi
callq 0x98b0
addq $0x268, %rsp            # imm = 0x268
retq
leaq 0x140(%rsp), %rdi
callq 0x98b0
movq 0x138(%rsp), %rdi
callq 0x3460
nopl (%rax,%rax)