@@ -646,6 +646,37 @@ def test_getShares(set_init_bits):
646646 assert True
647647
648648
649+ def test_random_dithering ():
650+ random_list = []
651+
652+ def dithering (random_string ):
653+ random_list .append (hex (int (random_string )))
654+
655+ # Check dithering disabled
656+ secrets .init ()
657+ secrets .getShares (1234 , 6 , 3 )
658+ assert len (set (random_list )) == 0
659+
660+ # Enable dithering
661+ settings .update_defaults (dithering = lambda string : dithering (string ))
662+ secrets .getShares (1234 , 6 , 3 )
663+ # print(random_list)
664+ assert len (set (random_list )) > 1
665+
666+ # Check testRandom values are the same
667+ random_list = []
668+ secrets .init (8 , "testRandom" )
669+ settings .update_defaults (dithering = lambda string : dithering (string ))
670+ secrets .getShares (1234 , 6 , 3 )
671+ assert len (set (random_list )) == 1
672+
673+ # Check dithering disabled
674+ random_list = []
675+ secrets .init ()
676+ secrets .getShares (1234 , 6 , 3 )
677+ assert len (set (random_list )) == 0
678+
679+
649680@pytest .fixture (
650681 params = [
651682 None ,
@@ -673,15 +704,145 @@ def full_range_of_bits(request):
673704 return request .param
674705
675706
707+ # Select a number of random shares
676708def pieces (shares , number = 3 ):
677709 random .shuffle (shares ) # Randomize
678710 return shares [- number :]
679711
680712
681713def test_py_share (full_range_of_bits ):
714+ # Generate shares with python
682715 secrets .init (full_range_of_bits )
683- secret = secrets .random (64 )
716+ secret = secrets .random (128 )
717+
718+ # A few cold runs to cover non-zero in rng
719+ secrets .share (secret , 7 , 6 )
720+ secrets .share (secret , 7 , 6 )
721+ secrets .share (secret , 7 , 6 )
722+
684723 shares = secrets .share (secret , 6 , 3 )
685724
725+ # Combine shares using the JavaScript
686726 result = node .combine (pieces (shares , 3 ))
687727 assert result == secret
728+
729+
730+ def test_py_share_error_string ():
731+ match = "Secret must be a hex string."
732+ with pytest .raises (ValueError , match = match ):
733+ secrets .init ()
734+ secrets .share (12345 , 6 , 3 )
735+ # Check if any warnings were raised
736+ assert len (caught_warnings ) == 1
737+ assert issubclass (caught_warnings [0 ].category , Warning )
738+ assert match in str (caught_warnings [0 ].message )
739+
740+
741+ def test_py_share_error_shares ():
742+ match = "Number of shares must be an integer"
743+ with pytest .raises (ValueError , match = match ):
744+ secrets .init ()
745+ secret = secrets .random (32 )
746+ secrets .share (secret , 1 , 1 )
747+ # Check if any warnings were raised
748+ assert len (caught_warnings ) == 1
749+ assert issubclass (caught_warnings [0 ].category , Warning )
750+ assert match in str (caught_warnings [0 ].message )
751+ with pytest .raises (ValueError , match = match ):
752+ secrets .init ()
753+ secret = secrets .random (32 )
754+ secrets .share (secret , "hello" , 3 )
755+ # Check if any warnings were raised
756+ assert len (caught_warnings ) == 1
757+ assert issubclass (caught_warnings [0 ].category , Warning )
758+ assert match in str (caught_warnings [0 ].message )
759+
760+
761+ def test_py_share_error_high_shares ():
762+ match = "Number of shares must be"
763+ with pytest .raises (ValueError , match = match ):
764+ secrets .init ()
765+ secret = secrets .random (32 )
766+ secrets .share (secret , 800 , 3 )
767+ # Check if any warnings were raised
768+ assert len (caught_warnings ) == 1
769+ assert issubclass (caught_warnings [0 ].category , Warning )
770+ assert match in str (caught_warnings [0 ].message )
771+
772+
773+ def test_py_share_error_threshold ():
774+ match = "Threshold number of shares must be an integer >= 2."
775+ with pytest .raises (ValueError , match = match ):
776+ secrets .init ()
777+ secret = secrets .random (32 )
778+ secrets .share (secret , 6 , 1 )
779+ # Check if any warnings were raised
780+ assert len (caught_warnings ) == 1
781+ assert issubclass (caught_warnings [0 ].category , Warning )
782+ assert match in str (caught_warnings [0 ].message )
783+ with pytest .raises (ValueError , match = match ):
784+ secrets .init ()
785+ secret = secrets .random (32 )
786+ secrets .share (secret , 6 , "two" )
787+ # Check if any warnings were raised
788+ assert len (caught_warnings ) == 1
789+ assert issubclass (caught_warnings [0 ].category , Warning )
790+ assert match in str (caught_warnings [0 ].message )
791+
792+
793+ def test_py_share_error_high_threshold ():
794+ match = "Threshold number of shares must be"
795+ with pytest .raises (ValueError , match = match ):
796+ secrets .init ()
797+ secret = secrets .random (32 )
798+ secrets .share (secret , 6 , 800 )
799+ # Check if any warnings were raised
800+ assert len (caught_warnings ) == 1
801+ assert issubclass (caught_warnings [0 ].category , Warning )
802+ assert match in str (caught_warnings [0 ].message )
803+ with pytest .raises (ValueError , match = match ):
804+ secrets .init ()
805+ secret = secrets .random (32 )
806+ secrets .share (secret , 6 , 7 )
807+ # Check if any warnings were raised
808+ assert len (caught_warnings ) == 1
809+ assert issubclass (caught_warnings [0 ].category , Warning )
810+ assert match in str (caught_warnings [0 ].message )
811+
812+
813+ def test_py_share_error_padding ():
814+ match = "Zero-pad length must be an integer between 0 and 1024 inclusive."
815+ with pytest .raises (ValueError , match = match ):
816+ secrets .init ()
817+ secret = secrets .random (32 )
818+ secrets .share (secret , 6 , 3 , "hello" )
819+ # Check if any warnings were raised
820+ assert len (caught_warnings ) == 1
821+ assert issubclass (caught_warnings [0 ].category , Warning )
822+ assert match in str (caught_warnings [0 ].message )
823+ with pytest .raises (ValueError , match = match ):
824+ secrets .init ()
825+ secret = secrets .random (32 )
826+ secrets .share (secret , 6 , 3 , - 1 )
827+ # Check if any warnings were raised
828+ assert len (caught_warnings ) == 1
829+ assert issubclass (caught_warnings [0 ].category , Warning )
830+ assert match in str (caught_warnings [0 ].message )
831+ with pytest .raises (ValueError , match = match ):
832+ secrets .init ()
833+ secret = secrets .random (32 )
834+ secrets .share (secret , 6 , 3 , 1025 )
835+ # Check if any warnings were raised
836+ assert len (caught_warnings ) == 1
837+ assert issubclass (caught_warnings [0 ].category , Warning )
838+ assert match in str (caught_warnings [0 ].message )
839+
840+
841+ def test_py_random_error ():
842+ match = "Number of bits must be an Integer between 1 and 65536."
843+ with pytest .raises (ValueError , match = match ):
844+ secret = secrets .random (65538 )
845+ # Check if any warnings were raised
846+ assert len (caught_warnings ) == 1
847+ assert issubclass (caught_warnings [0 ].category , Warning )
848+ assert match in str (caught_warnings [0 ].message )
0 commit comments