@@ -327,6 +327,75 @@ func TestBatchNormalize(t *testing.T) {
327327 })
328328}
329329
330+ func TestBytesUncompressSerializeDeserialize (t * testing.T ) {
331+ t .Parallel ()
332+
333+ var point Element
334+ point .Add (& Generator , & Generator )
335+ point .Double (& Generator )
336+
337+ bytesUncompressed := point .BytesUncompressedTrusted ()
338+
339+ var point2 Element
340+
341+ // Trying to deserialize the from an untrusted source would mean that the Y coordinate would be checked from
342+ // the EC formula. This would reject the point since BytesUncompressedTrusted() doesn't consider the Y coordinate sign.
343+ if err := point2 .SetBytesUncompressed (bytesUncompressed [:], false ); err == nil {
344+ t .Fatalf ("the point must be rejected since the serialized bytes didn't consider the Y coordinate sign" )
345+ }
346+
347+ // Deserializing with the trusted flag, must succeed since it's simply deserializing the x and y coordinate directly
348+ // without subgroup or lexicographic checks.
349+ if err := point2 .SetBytesUncompressed (bytesUncompressed [:], true ); err != nil {
350+ t .Fatalf ("could not deserialize point: %s" , err )
351+ }
352+ if ! point .Equal (& point2 ) {
353+ t .Fatalf ("deserialized point does not match original point" )
354+ }
355+ }
356+
357+ func TestSetUncompressedFail (t * testing.T ) {
358+ t .Parallel ()
359+ one := fp .One ()
360+
361+ t .Run ("X not in curve" , func (t * testing.T ) {
362+ startX := one
363+ // Find in startX a point that isn't in the curve
364+ for {
365+ point := bandersnatch .GetPointFromX (& startX , true )
366+ if point == nil {
367+ break
368+ }
369+ startX .Add (& startX , & one )
370+ continue
371+ }
372+ var serializedPoint [UncompressedSize ]byte
373+ xBytes := startX .Bytes ()
374+ yBytes := Generator .inner .Y .Bytes () // Use some valid-ish Y, but this shouldn't matter much.
375+ copy (serializedPoint [:], xBytes [:])
376+ copy (serializedPoint [CompressedSize :], yBytes [:])
377+
378+ var point2 Element
379+ if err := point2 .SetBytesUncompressed (serializedPoint [:], false ); err == nil {
380+ t .Fatalf ("the point must be rejected" )
381+ }
382+ })
383+
384+ t .Run ("wrong Y" , func (t * testing.T ) {
385+ gen := Generator
386+ // Despite X would lead to a point in the curve,
387+ // we modify Y+1 to check the provided (serialized) Y
388+ // coordinate isn't trusted blindly.
389+ gen .inner .Y .Add (& gen .inner .Y , & one )
390+
391+ pointBytes := gen .BytesUncompressedTrusted ()
392+ var point2 Element
393+ if err := point2 .SetBytesUncompressed (pointBytes [:], false ); err == nil {
394+ t .Fatalf ("the point must be rejected" )
395+ }
396+ })
397+ }
398+
330399func FuzzDeserializationCompressed (f * testing.F ) {
331400 f .Fuzz (func (t * testing.T , serializedpoint []byte ) {
332401 var point Element
0 commit comments