<s> import ctypes . util <EOL> import os <EOL> from . bignum import bignum_type_for_library <EOL> class BignumContext ( ctypes . Structure ) : <EOL> pass <EOL> def set_api ( library , api_info ) : <EOL> for f_name , argtypes , restype in api_info : <EOL> f = getattr ( library , f_name ) <EOL> f . argtypes = argtypes <EOL> f . restype = restype <EOL> def load_library ( ) : <EOL> if os . getenv ( "<STR_LIT>" ) != "<STR_LIT>" : <EOL> return None <EOL> library_path = ctypes . util . find_library ( "<STR_LIT>" ) <EOL> if library_path is None : <EOL> return None <EOL> library = ctypes . CDLL ( library_path ) <EOL> library . BignumType = bignum_type_for_library ( library ) <EOL> BN_P = ctypes . POINTER ( library . BignumType ) <EOL> BN_CTX = ctypes . POINTER ( BignumContext ) <EOL> BIGNUM_API = [ <EOL> ( "<STR_LIT>" , [ BN_P ] , None ) , <EOL> ( "<STR_LIT>" , [ ] , BN_P ) , <EOL> ( "<STR_LIT>" , [ BN_P , ctypes . c_ulong ] , ctypes . c_int ) , <EOL> ( "<STR_LIT>" , [ BN_P ] , None ) , <EOL> ( "<STR_LIT>" , [ ctypes . c_char_p , ctypes . c_int , BN_P ] , BN_P ) , <EOL> ( "<STR_LIT>" , [ BN_P , BN_P , BN_P , BN_CTX ] , BN_P ) , <EOL> ( "<STR_LIT>" , [ ] , BN_CTX ) , <EOL> ( "<STR_LIT>" , [ BN_CTX ] , None ) , <EOL> ( "<STR_LIT>" , [ ctypes . c_char_p , ctypes . c_int , BN_P ] , BN_P ) , <EOL> ] <EOL> ECC_API = [ <EOL> ( "<STR_LIT>" , [ ctypes . c_int ] , ctypes . c_void_p ) , <EOL> ( "<STR_LIT>" , [ ctypes . c_void_p ] , ctypes . c_void_p ) , <EOL> ( "<STR_LIT>" , [ ctypes . c_void_p ] , None ) , <EOL> ( "<STR_LIT>" , <EOL> [ ctypes . c_void_p , ctypes . c_void_p , BN_P , BN_P , BN_CTX ] , ctypes . c_int ) , <EOL> ( "<STR_LIT>" , <EOL> [ ctypes . c_void_p , ctypes . c_void_p , BN_P , BN_P , BN_CTX ] , ctypes . c_int ) , <EOL> ( "<STR_LIT>" , <EOL> [ ctypes . c_void_p , ctypes . c_void_p , BN_P , ctypes . c_void_p , BN_P , BN_CTX ] , ctypes . c_int ) , <EOL> ] <EOL> set_api ( library , BIGNUM_API ) <EOL> set_api ( library , ECC_API ) <EOL> return library <EOL> def make_fast_mul_f ( library ) : <EOL> NID_secp256k1_GROUP = library . EC_GROUP_new_by_curve_name ( <NUM_LIT> ) <EOL> def fast_mul ( point , N ) : <EOL> bn_x = library . BignumType ( point . x ( ) ) <EOL> bn_y = library . BignumType ( point . y ( ) ) <EOL> bn_n = library . BignumType ( N ) <EOL> ctx = library . BN_CTX_new ( ) <EOL> ec_result = library . EC_POINT_new ( NID_secp256k1_GROUP ) <EOL> ec_point = library . EC_POINT_new ( NID_secp256k1_GROUP ) <EOL> library . EC_POINT_set_affine_coordinates_GFp ( NID_secp256k1_GROUP , ec_point , bn_x , bn_y , ctx ) <EOL> library . EC_POINT_mul ( NID_secp256k1_GROUP , ec_result , None , ec_point , bn_n , ctx ) <EOL> library . EC_POINT_get_affine_coordinates_GFp ( NID_secp256k1_GROUP , ec_result , bn_x , bn_y , ctx ) <EOL> library . EC_POINT_free ( ec_point ) <EOL> library . EC_POINT_free ( ec_result ) <EOL> library . BN_CTX_free ( ctx ) <EOL> return type ( point ) ( point . curve ( ) , bn_x . to_int ( ) , bn_y . to_int ( ) ) <EOL> return fast_mul <EOL> def make_inverse_mod_f ( library ) : <EOL> def inverse_mod ( a , n ) : <EOL> ctx = library . BN_CTX_new ( ) <EOL> a1 = library . BignumType ( a ) <EOL> library . BN_mod_inverse ( a1 , a1 , library . BignumType ( n ) , ctx ) <EOL> library . BN_CTX_free ( ctx ) <EOL> return a1 . to_int ( ) <EOL> return inverse_mod <EOL> try : <EOL> NATIVE_LIBRARY = load_library ( ) <EOL> except : <EOL> NATIVE_LIBRARY = None <EOL> if NATIVE_LIBRARY : <EOL> NATIVE_LIBRARY . fast_mul = make_fast_mul_f ( NATIVE_LIBRARY ) <EOL> NATIVE_LIBRARY . inverse_mod = make_inverse_mod_f ( NATIVE_LIBRARY ) </s>
