@@ -728,3 +728,69 @@ var testData2s = []testData2{
728728
729729
730730}
731+
732+ func Test_ElgamalEncrypt (t * testing.T ) {
733+ c := elliptic .P256 ()
734+
735+ priv , err := GenerateKey (rand .Reader , c )
736+ if err != nil {
737+ t .Fatal (err )
738+ }
739+
740+ pub := & priv .PublicKey
741+
742+ data := []byte ("test-data test-data test-data test-data test-data" )
743+
744+ C1x , C1y , C2 , err := ElgamalEncrypt (rand .Reader , pub , data )
745+ if err != nil {
746+ t .Fatal (err )
747+ }
748+
749+ p , _ := ElgamalDecrypt (priv , C1x , C1y , C2 )
750+ if ! bytes .Equal (data , p ) {
751+ t .Errorf ("ElgamalDecrypt fail, got %x, want %x" , p , data )
752+ }
753+
754+ }
755+
756+ func Test_ElgamalEncrypt_Check (t * testing.T ) {
757+ c := elliptic .P256 ()
758+
759+ x := fromHex ("67e87b98e8a4383098fedb448c1f3d278bebba50525dec57c0576c605bfffdda" )
760+ Y := fromHex ("045ec79d828a77af85aa9c8ef3ba5afd7674376d6b134d14b10bb4afb7fd0952b0e894f696b38096ff547bbd0a0f1d14a195f2fc9ce951901fe2925560f29d98c0" )
761+
762+ priv , err := NewPrivateKey (c , x )
763+ if err != nil {
764+ t .Fatal (err )
765+ }
766+
767+ pub , err := NewPublicKey (c , Y )
768+ if err != nil {
769+ t .Fatal (err )
770+ }
771+
772+ data := []byte ("Hello" )
773+
774+ C1x , C1y , C2 , err := ElgamalEncrypt (rand .Reader , pub , data )
775+ if err != nil {
776+ t .Fatal (err )
777+ }
778+
779+ // C1Bytes := elliptic.Marshal(pub.Curve, C1x, C1y)
780+ // C1=04af7035184190ce72b1ee000ec8f18927a664c23358ce4d41ff757283a5846bb58c19c551753ea0af151c31c1a3698606af565c122a387dbe67d7fa5deba2393f
781+ // t.Errorf("C1Bytes: %x", C2)
782+
783+ p , _ := ElgamalDecrypt (priv , C1x , C1y , C2 )
784+ if ! bytes .Equal (data , p ) {
785+ t .Errorf ("ElgamalDecrypt fail, got %x, want %x" , p , data )
786+ }
787+
788+ C1 := fromHex ("04af7035184190ce72b1ee000ec8f18927a664c23358ce4d41ff757283a5846bb58c19c551753ea0af151c31c1a3698606af565c122a387dbe67d7fa5deba2393f" )
789+ C22 , _ := new (big.Int ).SetString ("64998866770800537035816591092081487793369751526287129670052291837083837454710935744289325621649282383337514803150244272041445838052262073601284819093853352" , 10 )
790+
791+ C1x2 , C1y2 := elliptic .Unmarshal (priv .Curve , C1 )
792+ p2 , _ := ElgamalDecrypt (priv , C1x2 , C1y2 , C22 )
793+ if ! bytes .Equal (data , p2 ) {
794+ t .Errorf ("Test_ElgamalEncrypt_Check fail, got %x, want %x" , p2 , data )
795+ }
796+ }
0 commit comments