@@ -2,6 +2,8 @@ package group
22
33import (
44 "crypto"
5+ "crypto/ecdh"
6+ "crypto/ecdsa"
57 "crypto/elliptic"
68 _ "crypto/sha256"
79 _ "crypto/sha512"
@@ -10,21 +12,23 @@ import (
1012 "io"
1113 "math/big"
1214
13- "github.com/cloudflare/circl/ecc/p384"
15+ optP384 "github.com/cloudflare/circl/ecc/p384"
1416 "github.com/cloudflare/circl/expander"
1517)
1618
1719var (
1820 // P256 is the group generated by P-256 elliptic curve.
19- P256 Group = wG {elliptic .P256 ()}
21+ P256 Group = wG {ellC : elliptic . P256 , ecdhC : ecdh . P256 , c : elliptic .P256 ()}
2022 // P384 is the group generated by P-384 elliptic curve.
21- P384 Group = wG {p384 .P384 ()}
23+ P384 Group = wG {ellC : elliptic . P384 , ecdhC : ecdh . P384 , c : optP384 .P384 ()}
2224 // P521 is the group generated by P-521 elliptic curve.
23- P521 Group = wG {elliptic .P521 ()}
25+ P521 Group = wG {ellC : elliptic . P521 , ecdhC : ecdh . P521 , c : elliptic .P521 ()}
2426)
2527
2628type wG struct {
27- c elliptic.Curve
29+ c elliptic.Curve
30+ ellC func () elliptic.Curve
31+ ecdhC func () ecdh.Curve
2832}
2933
3034func (g wG ) String () string { return g .c .Params ().Name }
@@ -226,9 +230,15 @@ func (e *wElt) MarshalBinary() ([]byte, error) {
226230 if e .IsIdentity () {
227231 return []byte {0x0 }, nil
228232 }
233+
229234 e .x .Mod (e .x , e .c .Params ().P )
230235 e .y .Mod (e .y , e .c .Params ().P )
231- return elliptic .Marshal (e .wG .c , e .x , e .y ), nil
236+ pk , err := (& ecdsa.PublicKey {Curve : e .wG .ellC (), X : e .x , Y : e .y }).ECDH ()
237+ if err != nil {
238+ return nil , err
239+ }
240+
241+ return pk .Bytes (), nil
232242}
233243
234244func (e * wElt ) MarshalBinaryCompress () ([]byte , error ) {
@@ -254,11 +264,13 @@ func (e *wElt) UnmarshalBinary(b []byte) error {
254264 }
255265 e .x , e .y = x , y
256266 case l == 1 + 2 * byteLen && b [0 ] == 0x04 : // uncompressed
257- x , y := elliptic . Unmarshal ( e .wG .c , b )
258- if x = = nil {
267+ _ , err := e .wG .ecdhC (). NewPublicKey ( b )
268+ if err ! = nil {
259269 return ErrUnmarshal
260270 }
261- e .x , e .y = x , y
271+
272+ e .x .SetBytes (b [1 : 1 + byteLen ])
273+ e .y .SetBytes (b [1 + byteLen : 1 + 2 * byteLen ])
262274 default :
263275 return ErrUnmarshal
264276 }
0 commit comments