//
// BN_mod_exp_mont_word from 0xa82a10 to 0xa82e78 (1128 bytes)
//
// 2 xrefs:
// > BN_mod_exp @ 0xa82f44
// > ??> @ 0x837dc
//
// This function is hookable on this platform (idp: arm, 32 bit: True, 64 bit: False!
//
// Using BromaIDA 8.3.0 @ https://github.com/Stazzical/BromaIDA
// Using bindings at commit 1c6ec395 at Thu Nov 26 21:29:22 2026 from https://github.com/geode-sdk/bindings
// Generated at Fri, 27 Nov 2026 12:08:50 +0000
//
int __fastcall BN_mod_exp_mont_word(int *a1, unsigned int a2, _DWORD *a3, int a4, int ***a5, _BYTE *a6)
{
int v9; // r6
int **v10; // r4
int **v11; // r5
int **v12; // r0
bool v13; // zf
int v14; // r4
int v15; // r6
_BOOL4 v16; // r4
unsigned int v17; // r8
int v18; // r10
int v19; // r0
int **v21; // r3
int v23; // r1
int **v24; // r3
_DWORD *v25; // r0
_BYTE *v26; // [sp+Ch] [bp-18h]
_DWORD *v28; // [sp+14h] [bp-10h]
int **v29; // [sp+18h] [bp-Ch]
int *v30; // [sp+1Ch] [bp-8h]
if ( BN_get_flags((int)a3, 4) )
{
ERR_put_error(3, 117, 66, (int)"crypto/bn/bn_exp.c", 1125);
return 0;
}
if ( !BN_is_odd(a4) )
{
ERR_put_error(3, 117, 102, (int)"crypto/bn/bn_exp.c", 1133);
return 0;
}
if ( *(_DWORD *)(a4 + 4) == 1 )
a2 %= **(_DWORD **)a4;
v9 = BN_num_bits(a3);
if ( v9 )
{
if ( a2 )
{
BN_CTX_start((int)a5);
v10 = BN_CTX_get(a5);
v11 = BN_CTX_get(a5);
v12 = BN_CTX_get(a5);
v13 = v10 == nullptr;
if ( v10 )
v13 = v11 == nullptr;
v14 = v13;
if ( !v12 )
v14 |= 1u;
v29 = v12;
if ( v14 )
{
v16 = 0;
v26 = nullptr;
goto LABEL_51;
}
if ( a6 )
{
v26 = a6;
}
else
{
v25 = BN_MONT_CTX_new();
v26 = v25;
if ( !v25 || !BN_MONT_CTX_set((int)v25, (int **)a4, (int)a5) )
{
v16 = 0;
goto LABEL_64;
}
}
v15 = v9 - 2;
if ( v15 < 0 )
{
if ( a2 != 1 )
goto LABEL_46;
goto LABEL_69;
}
v28 = a3;
v30 = (int *)a4;
v16 = 1;
v17 = a2;
v18 = (int)v29;
while ( v17 == v17 * v17 / v17 )
{
v17 *= v17;
if ( !v16 )
goto LABEL_23;
v16 = 1;
LABEL_25:
if ( BN_is_bit_set(v28, v15) )
{
if ( v17 == a2 * v17 / a2 )
{
v17 *= a2;
}
else if ( v16 )
{
v19 = BN_set_word(v11, v17);
if ( !v19 )
goto LABEL_60;
v19 = BN_to_montgomery((int)v11, (int)v11, (int)v26, (int)a5);
if ( !v19 )
goto LABEL_60;
v17 = a2;
v16 = 0;
}
else
{
if ( !BN_mul_word(v11, v17) || !BN_div(nullptr, v18, (int *)v11, v30, a5) )
goto LABEL_51;
v24 = v11;
v17 = a2;
v11 = (int **)v18;
v18 = (int)v24;
}
}
if ( v15-- == 0 )
{
v23 = (int)v11;
a2 = v17;
if ( v17 != 1 )
{
if ( v16 )
{
LABEL_46:
v19 = BN_set_word(v11, a2);
if ( v19 )
{
v19 = BN_to_montgomery((int)v11, (int)v11, (int)v26, (int)a5);
if ( v19 )
{
v23 = (int)v11;
goto LABEL_50;
}
}
LABEL_60:
v16 = v19;
LABEL_51:
if ( a6 )
{
LABEL_52:
BN_CTX_end(a5);
return v16;
}
LABEL_64:
BN_MONT_CTX_free(v26);
goto LABEL_52;
}
if ( !BN_mul_word(v11, v17) || !BN_div(nullptr, v18, (int *)v11, v30, a5) )
goto LABEL_51;
v23 = v18;
LABEL_50:
v16 = BN_from_montgomery(a1, v23, v26, (int)a5) != 0;
goto LABEL_51;
}
if ( !v16 )
goto LABEL_50;
LABEL_69:
v16 = BN_set_word((int **)a1, 1) != 0;
goto LABEL_51;
}
}
if ( v16 )
{
v19 = BN_set_word(v11, v17);
if ( !v19 )
goto LABEL_60;
v19 = BN_to_montgomery((int)v11, (int)v11, (int)v26, (int)a5);
if ( !v19 )
goto LABEL_60;
}
else
{
if ( !BN_mul_word(v11, v17) || !BN_div(nullptr, v18, (int *)v11, v30, a5) )
goto LABEL_51;
v21 = v11;
v11 = (int **)v18;
v18 = (int)v21;
}
v17 = 1;
LABEL_23:
v19 = BN_mod_mul_montgomery((int *)v11, v11, v11, v26, (int)a5);
if ( !v19 )
goto LABEL_60;
v16 = 0;
goto LABEL_25;
}
LABEL_39:
BN_set_word((int **)a1, 0);
return 1;
}
if ( BN_is_one(a4) )
goto LABEL_39;
return BN_set_word((int **)a1, 1);
}