def sign(self, hash, low_s=True):
    if (not isinstance(hash, bytes)):
        raise TypeError(('Hash must be bytes instance; got %r' % hash.__class__))
    if (len(hash) != 32):
        raise ValueError('Hash must be exactly 32 bytes long')
    sig_size0 = ctypes.c_uint32()
    sig_size0.value = ssl.AMBKSA_size(self.k)
    mb_sig = ctypes.create_string_buffer(sig_size0.value)
    result = ssl.AMBKSA_sign(0, hash, len(hash), mb_sig, ctypes.byref(sig_size0), self.k)
    assert (1 == result)
    assert (mb_sig.raw[0] == 48)
    assert (mb_sig.raw[1] == (sig_size0.value - 2))
    total_size = mb_sig.raw[1]
    assert (mb_sig.raw[2] == 2)
    r_size = mb_sig.raw[3]
    assert (mb_sig.raw[(4 + r_size)] == 2)
    s_size = mb_sig.raw[(5 + r_size)]
    s_value = int.from_bytes(mb_sig.raw[(6 + r_size):((6 + r_size) + s_size)], byteorder='big')
    if ((not low_s) or (s_value <= SECP256K1_ORDER_HALF)):
        return mb_sig.raw[:sig_size0.value]
    else:
        low_s_value = (SECP256K1_ORDER - s_value)
        low_s_bytes = low_s_value.to_bytes(33, byteorder='big')
        while ((len(low_s_bytes) > 1) and (low_s_bytes[0] == 0) and (low_s_bytes[1] < 128)):
            low_s_bytes = low_s_bytes[1:]
        new_s_size = len(low_s_bytes)
        new_total_size_byte = ((total_size + new_s_size) - s_size).to_bytes(1, byteorder='big')
        new_s_size_byte = new_s_size.to_bytes(1, byteorder='big')
        return ((((b'0' + new_total_size_byte) + mb_sig.raw[2:(5 + r_size)]) + new_s_size_byte) + low_s_bytes)
def sign(self, hash, low_s=True):
    if (not isinstance(hash, bytes)):
        raise TypeError(('Hash must be bytes instance; got %r' % hash.__class__))
    if (len(hash) != 32):
        raise ValueError('Hash must be exactly 32 bytes long')
    sig_size0 = ctypes.c_uint32()
    sig_size0.value = ssl.AMBKSA_size(self.k)
    mb_sig = ctypes.create_string_buffer(sig_size0.value)
    result = ssl.AMBKSA_sign(0, hash, len(hash), mb_sig, ctypes.byref(sig_size0), self.k)
    assert (1 == result)
    assert (mb_sig.raw[0] == 48)
    assert (mb_sig.raw[1] == (sig_size0.value - 2))
    total_size = mb_sig.raw[1]
    assert (mb_sig.raw[2] == 2)
    r_size = mb_sig.raw[3]
    assert (mb_sig.raw[(4 + r_size)] == 2)
    s_size = mb_sig.raw[(5 + r_size)]
    s_value = int.from_bytes(mb_sig.raw[(6 + r_size):((6 + r_size) + s_size)], byteorder='big')
    if ((not low_s) or (s_value <= SECP256K1_ORDER_HALF)):
        return mb_sig.raw[:sig_size0.value]
    else:
        low_s_value = (SECP256K1_ORDER - s_value)
        low_s_bytes = low_s_value.to_bytes(33, byteorder='big')
        while ((len(low_s_bytes) > 1) and (low_s_bytes[0] == 0) and (low_s_bytes[1] < 128)):
            low_s_bytes = low_s_bytes[1:]
        new_s_size = len(low_s_bytes)
        new_total_size_byte = ((total_size + new_s_size) - s_size).to_bytes(1, byteorder='big')
        new_s_size_byte = new_s_size.to_bytes(1, byteorder='big')
        return ((((b'0' + new_total_size_byte) + mb_sig.raw[2:(5 + r_size)]) + new_s_size_byte) + low_s_bytes)