package srp

import (
	
	

	
)

// computeXV computes following numbers
//
// `x = PH2(password, salt1, salt2)`
// `v = pow(g, x) mod p`
//
// TDLib uses terms `clientSalt` for `salt1` and `serverSalt` for `salt2`.
func ( SRP) (, ,  []byte, ,  *big.Int) (,  *big.Int) {
	// `x = PH2(password, salt1, salt2)`
	 = new(big.Int).SetBytes(.secondary(, , ))
	// `v = pow(g, x) mod p`
	 = new(big.Int).Exp(, , )
	return , 
}

// NewHash computes new user password hash using parameters from server.
//
// See https://core.telegram.org/api/srp#setting-a-new-2fa-password.
//
// TDLib implementation:
// See https://github.com/tdlib/td/blob/fa8feefed70d64271945e9d5fd010b957d93c8cd/td/telegram/PasswordManager.cpp#L57.
//
// TDesktop implementation:
// See https://github.com/telegramdesktop/tdesktop/blob/v3.4.8/Telegram/SourceFiles/core/core_cloud_password.cpp#L68.
func ( SRP) ( []byte,  Input) (,  []byte,  error) {
	// Generate a new new_password_hash using the KDF algorithm specified in the new_settings,
	// just append 32 sufficiently random bytes to the salt1, first. Proceed as for checking passwords with SRP,
	// just stop at the generation of the v parameter, and use it as new_password_hash:
	 := new(big.Int).SetBytes(.P)
	if  := checkInput(.G, );  != nil {
		return nil, nil, errors.Wrap(, "validate algo")
	}

	// Make a copy.
	 := append([]byte(nil), .Salt1...)
	 = append(, make([]byte, 32)...)
	// ... append 32 sufficiently random bytes to the salt1 ...
	if ,  := io.ReadFull(.random, [len()-32:]);  != nil {
		return nil, nil, 
	}

	,  := .computeXV(, , .Salt2, big.NewInt(int64(.G)), )
	// As usual in big endian form, padded to 2048 bits.
	,  := .pad256FromBig()
	return [:], , nil
}