code: def sqrt(self, val, flag):
          
          if val.iszero():
              return val
          sw = self.p % 8
          if sw == 3 or sw == 7:
              res = val ** ((self.p + 1) / 4)
          elif sw == 5:
              x = val ** ((self.p + 1) / 4)
              if x == 1:
                  res = val ** ((self.p + 3) / 8)
              else:
                  res = (4 * val) ** ((self.p - 5) / 8) * 2 * val
          else:
              raise Exception("modsqrt non supported for (p%8)==1")
  
          if res.value % 2 == flag:
              return res
          else:
              return -res
docs: calculate the square root modulus p