-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
260 lines (236 loc) · 8.3 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
"""Reverse from all the information stored in a pokemon to a seed"""
import numpy as np
import numba
from numba_pokemon_prngs.util import rotate_left_u64
from numba_pokemon_prngs.xorshift import Xoroshiro128PlusRejection
from numba_pokemon_prngs.data.personal import PERSONAL_INFO_LA, PersonalInfo8LA
import pla_reverse.pla_reverse as pla_reverse
from pa8 import PA8
SHINY_LOCKED = False
POKEMON: PA8 = np.fromfile("pokemon.pa8", dtype=PA8.dtype).view(np.recarray)[0]
PERSONAL_INFO: PersonalInfo8LA = PERSONAL_INFO_LA[POKEMON.species]
if POKEMON.form:
PERSONAL_INFO: PersonalInfo8LA = PERSONAL_INFO_LA[
PERSONAL_INFO.form_stats_index + POKEMON.form - 1
]
POKEMON_IVS = (
POKEMON.iv32 & 0x1F,
(POKEMON.iv32 >> 5) & 0x1F,
(POKEMON.iv32 >> 10) & 0x1F,
(POKEMON.iv32 >> 20) & 0x1F,
(POKEMON.iv32 >> 25) & 0x1F,
(POKEMON.iv32 >> 15) & 0x1F,
)
MAX_GUARANTEED_IVS = POKEMON_IVS.count(31)
IS_SHINY = (
(POKEMON.pid & 0xFFFF) ^ (POKEMON.pid >> 16) ^ POKEMON.tid ^ POKEMON.sid
) < 16
IS_ALPHA = POKEMON._16 & 32
LEGAL_SHINY_ROLLS = (
range(1, 33)
if IS_SHINY
else (1, 2, 4, 5, 6, 13, 14, 16, 17, 19, 26, 27, 29, 30, 32)
)
def xoroshiro128plus_next(
seed0: np.uint64, seed1: np.uint64
) -> tuple[np.uint64, np.uint64]:
seed1 ^= seed0
return np.uint64(rotate_left_u64(seed0, 24)) ^ seed1 ^ (
seed1 << np.uint64(16)
), np.uint64(rotate_left_u64(seed1, 37))
def ec_pid_matrix(shiny_rolls: int) -> np.ndarray:
"""Build fixed_seed -> ec/pid matrix"""
mat = np.zeros((64, 64), np.uint8)
for bit in range(64):
seed0, seed1 = np.uint64(1 << bit), np.uint64(0)
if bit < 32:
mat[bit, bit] = 1 # ec
seed0, seed1 = xoroshiro128plus_next(seed0, seed1) # ec
seed0, seed1 = xoroshiro128plus_next(seed0, seed1) # tidsid
for _ in range(shiny_rolls - 1):
seed0, seed1 = xoroshiro128plus_next(seed0, seed1) # pid
for pid_bit in range(16):
mat[bit, 32 + pid_bit] = (seed0 >> np.uint64(pid_bit)) & np.uint64(1)
mat[bit, 32 + 16 + pid_bit] = (seed1 >> np.uint64(pid_bit)) & np.uint64(1)
return mat
def ec_pid_const(shiny_rolls: int) -> np.array:
"""Compute the xoroshiro constant's impact on ec/pid"""
vec = np.zeros(64, np.uint8)
seed0, seed1 = np.uint64(0), np.uint64(0x82A2B175229D6A5B)
seed0, seed1 = xoroshiro128plus_next(seed0, seed1) # ec
seed0, seed1 = xoroshiro128plus_next(seed0, seed1) # tidsid
for _ in range(shiny_rolls - 1):
seed0, seed1 = xoroshiro128plus_next(seed0, seed1) # pid
for bit in range(16):
vec[32 + bit] = (seed0 >> np.uint64(bit)) & np.uint64(1)
vec[32 + 16 + bit] = (seed1 >> np.uint64(bit)) & np.uint64(1)
return vec
@numba.njit
def find_test_seeds_ec_pid(
encryption_constant: np.uint32,
pid: np.uint32,
seed_mat: np.ndarray,
nullspace: np.ndarray,
xoro_const: np.uint64,
):
ec_s0 = (encryption_constant - 0x229D6A5B) & 0xFFFFFFFF
pid_low = pid & 0xFFFF
seeds = np.empty(0x10000 * len(nullspace), np.uint64)
seed_i = 0
for pid_s1 in range(0x10000):
pid_s0 = (pid_low - pid_s1) & 0xFFFF
ec_pid_vec = (
np.uint64(ec_s0) | (np.uint64(pid_s0) << 32) | (np.uint64(pid_s1) << 48)
) ^ xoro_const
base_seed = np.uint64(0)
i = 0
while ec_pid_vec:
if ec_pid_vec & 1:
base_seed ^= seed_mat[i]
ec_pid_vec >>= 1
i += 1
for nullspace_vector in nullspace:
seeds[seed_i] = base_seed ^ nullspace_vector
seed_i += 1
return seeds
def find_fixed_seeds_ec_pid(
encryption_constant: np.uint32,
pid: np.uint32,
tsv: np.uint16,
shiny_rolls: np.uint8,
shiny_locked: np.bool8,
):
nullspace = np.array(
tuple(
pla_reverse.matrix.vec_to_int(row)
for row in pla_reverse.matrix.nullspace(ec_pid_matrix(shiny_rolls))
),
np.uint64,
)
seed_mat = np.array(
tuple(
pla_reverse.matrix.vec_to_int(row)
for row in pla_reverse.matrix.generalized_inverse(
ec_pid_matrix(shiny_rolls)
)
),
np.uint64,
)
xoro_const = np.uint64(pla_reverse.matrix.vec_to_int(ec_pid_const(shiny_rolls)))
test_seeds = find_test_seeds_ec_pid(
encryption_constant, pid, seed_mat, nullspace, xoro_const
)
return test_seeds[
np.where(verify_all_seeds(test_seeds, pid, tsv, shiny_rolls, shiny_locked))[0]
]
@numba.njit
def generate_fix_init_spec(
fixed_seed: np.uint64,
gender_ratio: np.uint8,
shiny_rolls: np.uint8,
shiny_locked: np.bool8,
guaranteed_ivs: np.uint8,
is_alpha: np.uint8,
real_sidtid: np.uint32,
):
rng = Xoroshiro128PlusRejection(
np.uint64(fixed_seed), np.uint64(0x82A2B175229D6A5B)
)
encryption_constant = np.uint32(rng.next_rand(0xFFFFFFFF))
sidtid = np.uint32(rng.next_rand(0xFFFFFFFF))
for _ in range(1 if shiny_locked else shiny_rolls):
pid = np.uint32(rng.next_rand(0xFFFFFFFF))
temp = pid ^ sidtid
xor = (temp & 0xFFFF) ^ (temp >> 16)
shiny = xor < 16
if shiny:
break
temp = pid ^ real_sidtid
real_shiny = ((temp & 0xFFFF) ^ (temp >> 16)) < 16
if shiny_locked and real_shiny:
pid ^= 0x10000000
if (not shiny_locked) and (not real_shiny) and shiny:
pid = (pid & 0xFFFF) | (
(
(pid & 0xFFFF)
^ (real_sidtid & 0xFFFF)
^ (real_sidtid >> 16)
^ (1 if xor > 0 else 0)
)
<< 16
)
ivs = np.zeros(6, np.uint8)
for _ in range(guaranteed_ivs):
index = rng.next_rand(6)
while ivs[index] != 0:
index = rng.next_rand(6)
ivs[index] = 31
for i in range(6):
if ivs[i] == 0:
ivs[i] = rng.next_rand(32)
ability = rng.next_rand(2)
gender = 0 if gender_ratio == 0 else 1 if gender_ratio == 254 else 2
if 1 <= gender_ratio <= 253:
gender = int((rng.next_rand(253) + 1) < gender_ratio)
nature = rng.next_rand(25)
if is_alpha:
height = weight = 255
else:
height = rng.next_rand(0x81) + rng.next_rand(0x80)
weight = rng.next_rand(0x81) + rng.next_rand(0x80)
return shiny, encryption_constant, pid, ivs, ability, gender, nature, height, weight
@numba.njit
def verify_all_seeds(seeds, pokemon_pid, tsv, shiny_rolls, shiny_locked):
bools = np.zeros(seeds.shape, np.bool8)
for i, seed in enumerate(seeds):
seed = seeds[i]
rng = Xoroshiro128PlusRejection(np.uint64(seed), np.uint64(0x82A2B175229D6A5B))
rng.next_rand(0xFFFFFFFF)
sidtid = np.uint32(rng.next_rand(0xFFFFFFFF))
for _ in range(shiny_rolls):
pid = np.uint32(rng.next_rand(0xFFFFFFFF))
temp = pid ^ sidtid
xor = (temp & 0xFFFF) ^ (temp >> 16)
shiny = xor < 16
if shiny:
break
real_shiny = (np.uint16(pid & 0xFFFF) ^ np.uint16(pid >> 16) ^ tsv) < 16
if shiny_locked and real_shiny:
pid ^= 0x10000000
if (not shiny_locked) and (not real_shiny) and shiny:
pid = (pid & 0xFFFF) | (
((pid & 0xFFFF) ^ tsv ^ (1 if xor > 0 else 0)) << 16
)
bools[i] = pid == pokemon_pid
return bools
for shiny_rolls in LEGAL_SHINY_ROLLS:
seeds = find_fixed_seeds_ec_pid(
POKEMON.encryption_constant,
POKEMON.pid,
POKEMON.tid ^ POKEMON.sid,
shiny_rolls,
SHINY_LOCKED,
)
for seed in seeds:
for guaranteed_ivs in range(MAX_GUARANTEED_IVS + 1):
(
shiny,
encryption_constant,
pid,
ivs,
ability,
gender,
nature,
height,
weight,
) = generate_fix_init_spec(
seed,
PERSONAL_INFO.gender_ratio,
shiny_rolls,
SHINY_LOCKED,
guaranteed_ivs,
IS_ALPHA,
POKEMON.tid | (POKEMON.sid << np.uint32(16)),
)
if all(iv0 == iv1 for iv0, iv1 in zip(POKEMON_IVS, ivs)):
print(f"Matches {guaranteed_ivs=} | {shiny_rolls=} | {seed=:016X}")