"""simple docstring"""



from manim import *






class 				snake_case__     (   snake_case_   ):


 def 		a__ (				self		):
        __a     				=  Rectangle(height=0.5  ,						width=0.5		)
        __a     				=  Rectangle(height=0.46  ,						width=0.46		).set_stroke(width=0		)

        __a     				=  Rectangle(height=0.25  ,						width=0.25		)

        __a     				=  [mem.copy() for i in range(6		)]
        __a     				=  [mem.copy() for i in range(6		)]
        __a     				=  VGroup(*lowerCamelCase		).arrange(lowerCamelCase  ,						buff=0		)
        __a     				=  VGroup(*lowerCamelCase		).arrange(lowerCamelCase  ,						buff=0		)
        __a     				=  VGroup(lowerCamelCase  ,						lowerCamelCase		).arrange(lowerCamelCase  ,						buff=0		)
        __a     				=  Text("CPU"  ,						font_size=24		)
        __a     				=  Group(lowerCamelCase  ,						lowerCamelCase		).arrange(lowerCamelCase  ,						buff=0.5  ,						aligned_edge=lowerCamelCase		)
        cpu.move_to([-2.5, -0.5, 0]		)
        self.add(lowerCamelCase		)

        __a     				=  [mem.copy() for i in range(4		)]
        __a     				=  VGroup(*lowerCamelCase		).arrange(lowerCamelCase  ,						buff=0		)
        __a     				=  Text("GPU"  ,						font_size=24		)
        __a     				=  Group(lowerCamelCase  ,						lowerCamelCase		).arrange(lowerCamelCase  ,						buff=0.5  ,						aligned_edge=lowerCamelCase		)
        gpu.move_to([-1, -1, 0]		)
        self.add(lowerCamelCase		)

        __a     				=  [mem.copy() for i in range(6		)]
        __a     				=  VGroup(*lowerCamelCase		).arrange(lowerCamelCase  ,						buff=0		)

        __a     				=  Text("Model"  ,						font_size=24		)
        __a     				=  Group(lowerCamelCase  ,						lowerCamelCase		).arrange(lowerCamelCase  ,						buff=0.5  ,						aligned_edge=lowerCamelCase		)
        model.move_to([3, -1.0, 0]		)
        self.add(lowerCamelCase		)

        __a     				=  []
        __a     				=  []

        for i, rect in enumerate(lowerCamelCase		):
               __a     				=  fill.copy().set_fill(lowerCamelCase  ,						opacity=0.8		)
               target.move_to(lowerCamelCase		)
               model_arr.append(lowerCamelCase		)

               __a     				=  Rectangle(height=0.46  ,						width=0.46		).set_stroke(width=0.0		).set_fill(lowerCamelCase  ,						opacity=0.8		)
               cpu_target.move_to(cpu_left_col_base[i]		)
               model_cpu_arr.append(lowerCamelCase		)

        self.add(*lowerCamelCase  ,						*lowerCamelCase		)

        __a     				=  [meta_mem.copy() for i in range(6		)]
        __a     				=  [meta_mem.copy() for i in range(6		)]
        __a     				=  VGroup(*lowerCamelCase		).arrange(lowerCamelCase  ,						buff=0		)
        __a     				=  VGroup(*lowerCamelCase		).arrange(lowerCamelCase  ,						buff=0		)
        __a     				=  VGroup(lowerCamelCase  ,						lowerCamelCase		).arrange(lowerCamelCase  ,						buff=0		)
        __a     				=  Text("Disk"  ,						font_size=24		)
        __a     				=  Group(lowerCamelCase  ,						lowerCamelCase		).arrange(lowerCamelCase  ,						buff=0.5  ,						aligned_edge=lowerCamelCase		)
        disk.move_to([-4, -1.25, 0]		)
        self.add(lowerCamelCase  ,						lowerCamelCase		)

        __a     				=  Square(side_length=2.2		)
        key.move_to([-5, 2, 0]		)

        __a     				=  MarkupText(
            F"<b>Key:</b>\n\n<span fgcolor='{YELLOW}'>●</span> Empty Model"  ,						font_size=18  ,						)

        key_text.move_to([-5, 2.4, 0]		)

        self.add(lowerCamelCase  ,						lowerCamelCase		)

        __a     				=  MarkupText(
            F"<span fgcolor='{BLUE}'>●</span> Checkpoint"  ,						font_size=18  ,						)

        blue_text.next_to(lowerCamelCase  ,						DOWN * 2.4  ,						aligned_edge=key_text.get_left()		)
        self.add(lowerCamelCase		)

        __a     				=  MarkupText(
            F"Now watch as an input is passed through the model\nand how the memory is utilized and handled."  ,						font_size=24  ,						)
        step_a.move_to([2, 2, 0]		)

        self.play(Write(lowerCamelCase		)		)

        __a     				=  Square(0.3		)
        input.set_fill(lowerCamelCase  ,						opacity=1.0		)
        input.set_stroke(width=0.0		)
        input.next_to(model_base[0]  ,						lowerCamelCase  ,						buff=0.5		)

        self.play(Write(lowerCamelCase		)		)

        input.generate_target()
        input.target.next_to(model_arr[0]  ,						direction=lowerCamelCase  ,						buff=0.02		)
        self.play(MoveToTarget(lowerCamelCase		)		)

        self.play(FadeOut(lowerCamelCase		)		)

        __a     				=  Arrow(start=lowerCamelCase  ,						end=lowerCamelCase  ,						color=lowerCamelCase  ,						buff=0.5		)
        a.next_to(model_arr[0].get_left()  ,						lowerCamelCase  ,						buff=0.2		)

        model_cpu_arr[0].generate_target()
        model_cpu_arr[0].target.move_to(gpu_rect[0]		)

        __a     				=  MarkupText(
            F"As the input reaches a layer, the hook triggers\nand weights are moved from the CPU\nto the GPU and back."  ,						font_size=24  ,						)
        step_a.move_to([2, 2, 0]		)

        self.play(Write(lowerCamelCase  ,						run_time=3		)		)

        __a     				=  {"run_time": 1, "fade_in": True, "fade_out": True, "buff": 0.02}

        self.play(
            Write(lowerCamelCase		)  ,						Circumscribe(model_arr[0]  ,						color=lowerCamelCase  ,						**lowerCamelCase		)  ,						Circumscribe(model_cpu_arr[0]  ,						color=lowerCamelCase  ,						**lowerCamelCase		)  ,						Circumscribe(gpu_rect[0]  ,						color=lowerCamelCase  ,						**lowerCamelCase		)  ,						)
        self.play(MoveToTarget(model_cpu_arr[0]		)		)

        __a     				=  a.copy()
        for i in range(6		):
               a_c.next_to(model_arr[i].get_right() + 0.02  ,						lowerCamelCase  ,						buff=0.2		)

               input.generate_target()
               input.target.move_to(model_arr[i].get_right() + 0.02		)

               __a     				=  AnimationGroup(
                   FadeOut(lowerCamelCase  ,						run_time=0.5		)  ,						MoveToTarget(lowerCamelCase  ,						run_time=0.5		)  ,						FadeIn(lowerCamelCase  ,						run_time=0.5		)  ,						lag_ratio=0.2		)

               self.play(lowerCamelCase		)

               model_cpu_arr[i].generate_target()
               model_cpu_arr[i].target.move_to(cpu_left_col_base[i]		)

               if i < 5:
                      model_cpu_arr[i + 1].generate_target()
                      model_cpu_arr[i + 1].target.move_to(gpu_rect[0]		)
                      if i >= 1:
                             __a     				=  0.7

                      self.play(
                          Circumscribe(model_arr[i]  ,						**lowerCamelCase		)  ,						Circumscribe(cpu_left_col_base[i]  ,						**lowerCamelCase		)  ,						Circumscribe(cpu_left_col_base[i + 1]  ,						color=lowerCamelCase  ,						**lowerCamelCase		)  ,						Circumscribe(gpu_rect[0]  ,						color=lowerCamelCase  ,						**lowerCamelCase		)  ,						Circumscribe(model_arr[i + 1]  ,						color=lowerCamelCase  ,						**lowerCamelCase		)  ,						)
                      if i < 1:
                             self.play(
                                 MoveToTarget(model_cpu_arr[i]		)  ,						MoveToTarget(model_cpu_arr[i + 1]		)  ,						)
                      else:
                             self.play(
                                 MoveToTarget(model_cpu_arr[i]  ,						run_time=0.7		)  ,						MoveToTarget(model_cpu_arr[i + 1]  ,						run_time=0.7		)  ,						)
               else:
                      model_cpu_arr[i].generate_target()
                      model_cpu_arr[i].target.move_to(cpu_left_col_base[-1]		)
                      input.generate_target()
                      input.target.next_to(model_arr[-1].get_right()  ,						RIGHT + 0.02  ,						buff=0.2		)

                      self.play(
                          Circumscribe(model_arr[-1]  ,						color=lowerCamelCase  ,						**lowerCamelCase		)  ,						Circumscribe(cpu_left_col_base[-1]  ,						color=lowerCamelCase  ,						**lowerCamelCase		)  ,						Circumscribe(gpu_rect[0]  ,						color=lowerCamelCase  ,						**lowerCamelCase		)  ,						)

                      self.play(MoveToTarget(model_cpu_arr[i]		)		)

               __a     				=  a_c
               __a     				=  a_c.copy()

        input.generate_target()
        input.target.next_to(model_base[-1]  ,						RIGHT + 0.02  ,						buff=0.5		)
        self.play(
            FadeOut(lowerCamelCase		)  ,						FadeOut(lowerCamelCase  ,						run_time=0.5		)  ,						)

        __a     				=  MarkupText(F"Inference on a model too large for GPU memory\nis successfully completed."  ,						font_size=24		)
        step_a.move_to([2, 2, 0]		)

        self.play(Write(lowerCamelCase  ,						run_time=3		)  ,						MoveToTarget(lowerCamelCase		)		)

        self.wait()

"""simple docstring"""



import os
import random
import sys

from . import cryptomath_module as cryptomath
from . import rabin_miller

SCREAMING_SNAKE_CASE__:List[str]			=	3



def 					_lowerCamelCase(					a							):
       print("Generating primitive root of p"							)
       while True:
              __a     				=  random.randrange(3 ,  a							)
              if pow(a ,  2 ,  a							) == 1:
                     continue
              if pow(a ,  a ,  a							) == 1:
                     continue
              return g



def 					_lowerCamelCase(					a							):
       print("Generating prime p..."							)
       __a     				=  rabin_miller.generate_large_prime(a							)  # select large prime number.
       __a     				=  primitive_root(a							)  # one primitive root on modulo p.
       __a     				=  random.randrange(3 ,  a							)  # private_key -> have to be greater than 2 for safety.
       __a     				=  cryptomath.find_mod_inverse(pow(a ,  a ,  a							) ,  a							)

       __a     				=  (key_size, e_a, e_a, p)
       __a     				=  (key_size, d)

       return public_key, private_key



def 					_lowerCamelCase(					a ,  a							):
       if os.path.exists(F"{name}_pubkey.txt"							) or os.path.exists(F"{name}_privkey.txt"							):
              print("\nWARNING:"							)
              print(
                  F"\"{name}_pubkey.txt\" or \"{name}_privkey.txt\" already exists. \n"
                  "Use a different name or delete these files and re-run this program."							)
              sys.exit()

       __a				, __a     				=  generate_key(a							)
       print(F"\nWriting public key to file {name}_pubkey.txt..."							)
       with open(F"{name}_pubkey.txt" ,  "w"							) as fo:
              fo.write(F"{public_key[0]},{public_key[1]},{public_key[2]},{public_key[3]}"							)

       print(F"Writing private key to file {name}_privkey.txt..."							)
       with open(F"{name}_privkey.txt" ,  "w"							) as fo:
              fo.write(F"{private_key[0]},{private_key[1]}"							)



def 					_lowerCamelCase(					):
       print("Making key files..."							)
       make_key_files("elgamal" ,  2_0_4_8							)
       print("Key files generation successful"							)


if __name__ == "__main__":
   main()
