2018-07-08 17:05:46 +00:00
from typing import Optional , Type
2018-02-09 05:40:02 +00:00
from warnings import warn
2018-06-28 20:30:56 +00:00
from umbral . curve import Curve , SECP256K1
2018-07-06 22:44:33 +00:00
from umbral . params import UmbralParameters
2018-02-08 00:18:50 +00:00
2018-02-08 04:09:47 +00:00
class _CONFIG :
2018-02-08 00:18:50 +00:00
__curve = None
2018-02-08 04:09:47 +00:00
__params = None
2018-07-07 05:08:52 +00:00
__CURVE_TO_USE_IF_NO_DEFAULT_IS_SET_BY_USER = SECP256K1
2018-02-10 04:30:25 +00:00
__WARNING_IF_NO_DEFAULT_SET = " No default curve has been set. Using SECP256K1. A slight performance penalty has been incurred for only this call. Set a default curve with umbral.config.set_default_curve(). "
2018-02-08 04:09:47 +00:00
2018-02-08 19:30:24 +00:00
class UmbralConfigurationError ( RuntimeError ) :
""" Raised when somebody does something dumb re: configuration. """
2018-02-09 05:40:02 +00:00
@classmethod
def __set_curve_by_default ( cls ) :
warn ( cls . __WARNING_IF_NO_DEFAULT_SET , RuntimeWarning )
cls . set_curve ( cls . __CURVE_TO_USE_IF_NO_DEFAULT_IS_SET_BY_USER )
2018-02-08 04:09:47 +00:00
@classmethod
2018-07-06 22:44:33 +00:00
def params ( cls ) - > UmbralParameters :
2018-02-08 04:09:47 +00:00
if not cls . __params :
2018-02-09 05:40:02 +00:00
cls . __set_curve_by_default ( )
return cls . __params
2018-02-08 00:18:50 +00:00
@classmethod
2018-07-08 17:05:46 +00:00
def curve ( cls ) - > Type [ Curve ] :
2018-02-08 00:18:50 +00:00
if not cls . __curve :
2018-02-09 05:40:02 +00:00
cls . __set_curve_by_default ( )
return cls . __curve
2018-02-08 00:18:50 +00:00
@classmethod
2018-07-06 22:44:33 +00:00
def set_curve ( cls , curve : Optional [ Curve ] = None ) - > None :
2018-02-08 00:18:50 +00:00
if cls . __curve :
2018-02-09 05:40:02 +00:00
raise cls . UmbralConfigurationError (
" You can only set the default curve once. Do it once and then leave it alone. " )
2018-02-08 00:18:50 +00:00
else :
2018-02-08 04:09:47 +00:00
from umbral . params import UmbralParameters
2018-02-10 04:57:17 +00:00
if curve is None :
curve = _CONFIG . __CURVE_TO_USE_IF_NO_DEFAULT_IS_SET_BY_USER
2018-02-08 00:18:50 +00:00
cls . __curve = curve
2018-02-08 04:09:47 +00:00
cls . __params = UmbralParameters ( curve )
2018-02-08 00:18:50 +00:00
2018-07-06 22:44:33 +00:00
def set_default_curve ( curve : Optional [ Curve ] = None ) - > None :
2018-02-10 04:57:17 +00:00
return _CONFIG . set_curve ( curve )
2018-02-08 04:09:47 +00:00
2018-02-08 00:18:50 +00:00
2018-07-08 17:05:46 +00:00
def default_curve ( ) - > Type [ Curve ] :
2018-02-08 04:09:47 +00:00
return _CONFIG . curve ( )
2018-07-06 22:44:33 +00:00
def default_params ( ) - > UmbralParameters :
2018-02-09 05:40:02 +00:00
return _CONFIG . params ( )