Skip to content

Commit 6723388

Browse files
crypto/bn256/gnark: align marshaling behavior (#32065)
Aligns the marshaling behavior of gnark to google and cloudflare Co-authored-by: kevaundray <[email protected]>
1 parent 35dd61a commit 6723388

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

crypto/bn256/gnark/g2.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type G2 struct {
2121
// Unmarshal deserializes `buf` into `g`
2222
//
2323
// The input is expected to be in the EVM format:
24-
// 128 bytes: [32-byte x.0][32-byte x.1][32-byte y.0][32-byte y.1]
24+
// 128 bytes: [32-byte x.1][32-byte x.0][32-byte y.1][32-byte y.0]
2525
// where each value is a big-endian integer.
2626
//
2727
// This method also checks whether the point is on the
@@ -39,16 +39,16 @@ func (g *G2) Unmarshal(buf []byte) (int, error) {
3939
g.inner.Y.A1.SetZero()
4040
return 128, nil
4141
}
42-
if err := g.inner.X.A0.SetBytesCanonical(buf[0:32]); err != nil {
42+
if err := g.inner.X.A1.SetBytesCanonical(buf[0:32]); err != nil {
4343
return 0, err
4444
}
45-
if err := g.inner.X.A1.SetBytesCanonical(buf[32:64]); err != nil {
45+
if err := g.inner.X.A0.SetBytesCanonical(buf[32:64]); err != nil {
4646
return 0, err
4747
}
48-
if err := g.inner.Y.A0.SetBytesCanonical(buf[64:96]); err != nil {
48+
if err := g.inner.Y.A1.SetBytesCanonical(buf[64:96]); err != nil {
4949
return 0, err
5050
}
51-
if err := g.inner.Y.A1.SetBytesCanonical(buf[96:128]); err != nil {
51+
if err := g.inner.Y.A0.SetBytesCanonical(buf[96:128]); err != nil {
5252
return 0, err
5353
}
5454

@@ -64,22 +64,22 @@ func (g *G2) Unmarshal(buf []byte) (int, error) {
6464
// Marshal serializes the point into a byte slice.
6565
//
6666
// The output is in EVM format: 128 bytes total.
67-
// [32-byte x.0][32-byte x.1][32-byte y.0][32-byte y.1]
67+
// [32-byte x.1][32-byte x.0][32-byte y.1][32-byte y.0]
6868
// where each value is a big-endian integer.
6969
func (g *G2) Marshal() []byte {
7070
output := make([]byte, 128)
7171

72-
xA0Bytes := g.inner.X.A0.Bytes()
73-
copy(output[:32], xA0Bytes[:])
74-
7572
xA1Bytes := g.inner.X.A1.Bytes()
76-
copy(output[32:64], xA1Bytes[:])
73+
copy(output[:32], xA1Bytes[:])
7774

78-
yA0Bytes := g.inner.Y.A0.Bytes()
79-
copy(output[64:96], yA0Bytes[:])
75+
xA0Bytes := g.inner.X.A0.Bytes()
76+
copy(output[32:64], xA0Bytes[:])
8077

8178
yA1Bytes := g.inner.Y.A1.Bytes()
82-
copy(output[96:128], yA1Bytes[:])
79+
copy(output[64:96], yA1Bytes[:])
80+
81+
yA0Bytes := g.inner.Y.A0.Bytes()
82+
copy(output[96:128], yA0Bytes[:])
8383

8484
return output
8585
}

0 commit comments

Comments
 (0)