Skip to content

Commit e922e6a

Browse files
committed
fix bign
1 parent a20f36b commit e922e6a

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

pubkey/bign/bign.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,3 +531,46 @@ func randFieldElement(rand io.Reader, c elliptic.Curve) (k *big.Int, err error)
531531
}
532532
}
533533

534+
// Encrypt with Elgamal
535+
func ElgamalEncrypt(random io.Reader, pub *PublicKey, data []byte) (C1x, C1y *big.Int, C2 *big.Int, err error) {
536+
x := new(big.Int).SetBytes(data)
537+
538+
curve := pub.Curve
539+
n := curve.Params().N
540+
541+
r, err := rand.Int(random, n)
542+
if err != nil {
543+
err = errors.New("go-cryptobin/bign: invalid rand k")
544+
return
545+
}
546+
547+
rYx, rYy := curve.ScalarMult(pub.X, pub.Y, r.Bytes())
548+
rGx, rGy := curve.ScalarBaseMult(r.Bytes())
549+
550+
rYBytes := elliptic.Marshal(curve, rYx, rYy)
551+
552+
rYval := new(big.Int).SetBytes(rYBytes)
553+
C2 = new(big.Int).Add(rYval, x)
554+
555+
C1x, C1y = new(big.Int).Set(rGx), new(big.Int).Set(rGy)
556+
557+
return
558+
}
559+
560+
// Decrypt with Elgamal
561+
func ElgamalDecrypt(priv *PrivateKey, C1x, C1y *big.Int, C2 *big.Int) (plain []byte, err error) {
562+
curve := priv.Curve
563+
564+
xCx, xCy := curve.ScalarMult(C1x, C1y, priv.D.Bytes())
565+
566+
xCBytes := elliptic.Marshal(curve, xCx, xCy)
567+
568+
xCval := new(big.Int).SetBytes(xCBytes)
569+
570+
p := new(big.Int).Set(C2)
571+
p.Sub(p, xCval)
572+
573+
plain = p.Bytes()
574+
575+
return
576+
}

pubkey/bign/bign_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)