def test_bin_reverse(): assert p8(0x12) == b"\x12" assert p8(0x34) == b"\x34" assert p8(0xff) == b"\xff" assert p16(0x5678) == b"\x78\x56" assert p16(0xffff) == b"\xff\xff" assert p32(0x87654321) == b"\x21\x43\x65\x87" assert p32(0xffffffff) == b"\xff\xff\xff\xff" assert p64(0x8877665544332211) == b"\x11\x22\x33\x44\x55\x66\x77\x88" assert p64(0xffffffffffffffff) == b"\xff\xff\xff\xff\xff\xff\xff\xff"
def test_unsigned(): assert UInt8(255) + 10 + 256 == 9 n = UInt8(-1) n -= 1 assert n == 254 assert UInt16(0xF0F0) << 8 == 0xF000 assert UInt16(0x4000) << 1 == 0x8000 assert UInt64(0xFFFFFFFF) << 32 == 0xFFFFFFFF00000000 assert UInt64(0xFFFFFFFF) << 48 == 0xFFFF000000000000 assert UInt64(0xFFFFFFFF) >> 32 == 0 assert UInt32(0xFFFFFFFF) << 32 == 0 assert UInt8(-1) == 255 assert UInt16(-1) == 65535 assert UInt16(-1) * -1 == 1 assert UInt16(-4) / 2 == UInt16(-4) >> 1 assert UInt32(-65535) > UInt32(65535) assert UInt32(0x4444FFFF) << 16 | 0x8080 == 0xFFFF8080 assert UInt8(UInt32(0xF0F0F0F0)) == 0xF0 assert 255 + UInt8(1) == 256 assert UInt8(255) + 1 == 0 assert -UInt8(-1) == 1 assert -UInt8(1) == UInt8(-1) assert ~UInt8(0xFF) == 0 assert abs(UInt8(-1)) == UInt8(-1) assert p64(0x12345678) == b"\x78\x56\x34\x12\x00\x00\x00\x00" assert p32(0x12345678) == b"\x78\x56\x34\x12" assert p8(0x12345678) == b"\x78" assert p16(-1) == b"\xFF\xFF" assert p16(-32768) == b"\x00\x80" assert u32(b"\x78\x56\x34\x12") == 0x12345678 assert u64(b"\x78\x56\x34\x12") is None
def test_rabbit(): key1 = b"".join( p8(ch) for ch in ( 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, )) key2 = b"".join( p8(ch) for ch in ( 0xAC, 0xC3, 0x51, 0xDC, 0xF1, 0x62, 0xFC, 0x3B, 0xFE, 0x36, 0x3D, 0x2E, 0x29, 0x13, 0x28, 0x91, )) key3 = b"".join( p8(ch) for ch in ( 0x43, 0x00, 0x9B, 0xC0, 0x01, 0xAB, 0xE9, 0xE9, 0x33, 0xC7, 0xE0, 0x87, 0x15, 0x74, 0x95, 0x83, )) iv1 = b"".join( p8(ch) for ch in (0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)) iv2 = b"".join( p8(ch) for ch in ( 0x59, 0x7E, 0x26, 0xC1, 0x75, 0xF5, 0x73, 0xC3, )) iv3 = b"".join( p8(ch) for ch in ( 0x27, 0x17, 0xF4, 0xD2, 0x1A, 0x56, 0xEB, 0xA6, )) out1 = b"".join( p8(ch) for ch in ( 0x02, 0xF7, 0x4A, 0x1C, 0x26, 0x45, 0x6B, 0xF5, 0xEC, 0xD6, 0xA5, 0x36, 0xF0, 0x54, 0x57, 0xB1, 0xA7, 0x8A, 0xC6, 0x89, 0x47, 0x6C, 0x69, 0x7B, 0x39, 0x0C, 0x9C, 0xC5, 0x15, 0xD8, 0xE8, 0x88, 0x96, 0xD6, 0x73, 0x16, 0x88, 0xD1, 0x68, 0xDA, 0x51, 0xD4, 0x0C, 0x70, 0xC3, 0xA1, 0x16, 0xF4, )) out2 = b"".join( p8(ch) for ch in ( 0x9C, 0x51, 0xE2, 0x87, 0x84, 0xC3, 0x7F, 0xE9, 0xA1, 0x27, 0xF6, 0x3E, 0xC8, 0xF3, 0x2D, 0x3D, 0x19, 0xFC, 0x54, 0x85, 0xAA, 0x53, 0xBF, 0x96, 0x88, 0x5B, 0x40, 0xF4, 0x61, 0xCD, 0x76, 0xF5, 0x5E, 0x4C, 0x4D, 0x20, 0x20, 0x3B, 0xE5, 0x8A, 0x50, 0x43, 0xDB, 0xFB, 0x73, 0x74, 0x54, 0xE5, )) out3 = b"".join( p8(ch) for ch in ( 0x9B, 0x60, 0xD0, 0x02, 0xFD, 0x5C, 0xEB, 0x32, 0xAC, 0xCD, 0x41, 0xA0, 0xCD, 0x0D, 0xB1, 0x0C, 0xAD, 0x3E, 0xFF, 0x4C, 0x11, 0x92, 0x70, 0x7B, 0x5A, 0x01, 0x17, 0x0F, 0xCA, 0x9F, 0xFC, 0x95, 0x28, 0x74, 0x94, 0x3A, 0xAD, 0x47, 0x41, 0x92, 0x3F, 0x7F, 0xFC, 0x8B, 0xDE, 0xE5, 0x49, 0x96, )) out4 = b"".join( p8(ch) for ch in ( 0xED, 0xB7, 0x05, 0x67, 0x37, 0x5D, 0xCD, 0x7C, 0xD8, 0x95, 0x54, 0xF8, 0x5E, 0x27, 0xA7, 0xC6, 0x8D, 0x4A, 0xDC, 0x70, 0x32, 0x29, 0x8F, 0x7B, 0xD4, 0xEF, 0xF5, 0x04, 0xAC, 0xA6, 0x29, 0x5F, 0x66, 0x8F, 0xBF, 0x47, 0x8A, 0xDB, 0x2B, 0xE5, 0x1E, 0x6C, 0xDE, 0x29, 0x2B, 0x82, 0xDE, 0x2A, )) out5 = b"".join( p8(ch) for ch in ( 0x6D, 0x7D, 0x01, 0x22, 0x92, 0xCC, 0xDC, 0xE0, 0xE2, 0x12, 0x00, 0x58, 0xB9, 0x4E, 0xCD, 0x1F, 0x2E, 0x6F, 0x93, 0xED, 0xFF, 0x99, 0x24, 0x7B, 0x01, 0x25, 0x21, 0xD1, 0x10, 0x4E, 0x5F, 0xA7, 0xA7, 0x9B, 0x02, 0x12, 0xD0, 0xBD, 0x56, 0x23, 0x39, 0x38, 0xE7, 0x93, 0xC3, 0x12, 0xC1, 0xEB, )) out6 = b"".join( p8(ch) for ch in ( 0x4D, 0x10, 0x51, 0xA1, 0x23, 0xAF, 0xB6, 0x70, 0xBF, 0x8D, 0x85, 0x05, 0xC8, 0xD8, 0x5A, 0x44, 0x03, 0x5B, 0xC3, 0xAC, 0xC6, 0x67, 0xAE, 0xAE, 0x5B, 0x2C, 0xF4, 0x47, 0x79, 0xF2, 0xC8, 0x96, 0xCB, 0x51, 0x15, 0xF0, 0x34, 0xF0, 0x3D, 0x31, 0x17, 0x1C, 0xA7, 0x5F, 0x89, 0xFC, 0xCB, 0x9F, )) assert rabbit(key1, None, b"\x00" * 48) == out1 assert rabbit(key2, None, b"\x00" * 48) == out2 assert rabbit(key3, None, b"\x00" * 48) == out3 assert rabbit(key1, iv1, b"\x00" * 48) == out4 assert rabbit(key1, iv2, b"\x00" * 48) == out5 assert rabbit(key1, iv3, b"\x00" * 48) == out6