Skip to content

Commit 8f12f56

Browse files
author
Mike Lodder
committed
fix endian issue
1 parent 9f973a8 commit 8f12f56

3 files changed

Lines changed: 41 additions & 19 deletions

File tree

go/pkg/oberon/proof.go

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"fmt"
66
"github.com/coinbase/kryptology/pkg/core/curves"
7+
"github.com/coinbase/kryptology/pkg/core/curves/native"
78
"github.com/coinbase/kryptology/pkg/core/curves/native/bls12381"
89
"io"
910
)
@@ -159,8 +160,10 @@ func (p Proof) MarshalBinary() ([]byte, error) {
159160
copy(tmp[:48], p.Proof.ToAffineCompressed())
160161
copy(tmp[48:96], p.UTick.ToAffineCompressed())
161162
copy(tmp[96:192], p.Commitment.ToAffineCompressed())
162-
copy(tmp[192:224], p.Challenge.Bytes())
163-
copy(tmp[224:], p.Schnorr.Bytes())
163+
t := p.Challenge.Value.Bytes()
164+
copy(tmp[192:224], t[:])
165+
t = p.Schnorr.Value.Bytes()
166+
copy(tmp[224:], t[:])
164167
return tmp[:], nil
165168
}
166169

@@ -181,11 +184,16 @@ func (p *Proof) UnmarshalBinary(in []byte) error {
181184
if err != nil {
182185
return err
183186
}
184-
challenge, err := curve.NewScalar().SetBytes(in[192:224])
187+
var t [native.FieldBytes]byte
188+
copy(t[:], in[192:224])
189+
challenge, _ := curve.NewScalar().(*curves.ScalarBls12381)
190+
_, err = challenge.Value.SetBytes(&t)
185191
if err != nil {
186192
return err
187193
}
188-
schnorr, err := curve.NewScalar().SetBytes(in[224:])
194+
copy(t[:], in[224:])
195+
schnorr, _ := curve.NewScalar().(*curves.ScalarBls12381)
196+
_, err = schnorr.Value.SetBytes(&t)
189197
if err != nil {
190198
return err
191199
}
@@ -202,20 +210,22 @@ func (p *Proof) UnmarshalBinary(in []byte) error {
202210
p.Proof = Proof
203211
p.UTick = UTick
204212
p.Commitment = Commitment
205-
p.Challenge, _ = challenge.(*curves.ScalarBls12381)
206-
p.Schnorr, _ = schnorr.(*curves.ScalarBls12381)
213+
p.Challenge = challenge
214+
p.Schnorr = schnorr
207215
return nil
208216
}
209217
return fmt.Errorf("invalid proof")
210218
}
211219

212220
func (p Proof) MarshalText() ([]byte, error) {
221+
c := p.Challenge.Value.Bytes()
222+
s := p.Schnorr.Value.Bytes()
213223
tmp := map[string][]byte{
214224
"proof": p.Proof.ToAffineCompressed(),
215225
"u_tick": p.UTick.ToAffineCompressed(),
216226
"commitment": p.Commitment.ToAffineCompressed(),
217-
"challenge": p.Challenge.Bytes(),
218-
"schnorr": p.Schnorr.Bytes(),
227+
"challenge": c[:],
228+
"schnorr": s[:],
219229
}
220230
return json.Marshal(&tmp)
221231
}
@@ -260,18 +270,28 @@ func (p *Proof) UnmarshalText(in []byte) error {
260270
return fmt.Errorf("missing expected mak key 'commitment'")
261271
}
262272
if challengeBytes, ok := tmp["challenge"]; ok {
263-
chal, err := curve.NewScalar().SetBytes(challengeBytes)
273+
if len(challengeBytes) != native.FieldBytes {
274+
return fmt.Errorf("invalid byte sequence for 'challenge'")
275+
}
276+
var t [native.FieldBytes]byte
277+
copy(t[:], challengeBytes)
278+
challenge, _ = curve.NewScalar().(*curves.ScalarBls12381)
279+
_, err = challenge.Value.SetBytes(&t)
264280
if err != nil {
265281
return err
266282
}
267-
challenge, _ = chal.(*curves.ScalarBls12381)
268283
}
269284
if schnorrBytes, ok := tmp["schnorr"]; ok {
270-
sch, err := curve.NewScalar().SetBytes(schnorrBytes)
285+
if len(schnorrBytes) != native.FieldBytes {
286+
return fmt.Errorf("invalid byte sequence for 'schnorr'")
287+
}
288+
var t [native.FieldBytes]byte
289+
copy(t[:], schnorrBytes)
290+
schnorr, _ = curve.NewScalar().(*curves.ScalarBls12381)
291+
_, err = schnorr.Value.SetBytes(&t)
271292
if err != nil {
272293
return err
273294
}
274-
schnorr, _ = sch.(*curves.ScalarBls12381)
275295
}
276296

277297
goodProof := isValidPointG1(proof)

go/pkg/oberon/secret_key.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,11 @@ func (s *SecretKey) Sign(id []byte) (*Token, error) {
8787
}
8888

8989
func (s SecretKey) MarshalBinary() ([]byte, error) {
90+
w, x, y := s.W.Value.Bytes(), s.X.Value.Bytes(), s.Y.Value.Bytes()
9091
var tmp [96]byte
91-
copy(tmp[:32], s.W.Bytes())
92-
copy(tmp[32:64], s.X.Bytes())
93-
copy(tmp[64:96], s.Y.Bytes())
92+
copy(tmp[:32], w[:])
93+
copy(tmp[32:64], x[:])
94+
copy(tmp[64:96], y[:])
9495
return tmp[:], nil
9596
}
9697

@@ -128,10 +129,11 @@ func (s *SecretKey) UnmarshalBinary(in []byte) error {
128129
}
129130

130131
func (s SecretKey) MarshalText() ([]byte, error) {
132+
w, x, y := s.W.Value.Bytes(), s.X.Value.Bytes(), s.Y.Value.Bytes()
131133
tmp := map[string][]byte{
132-
"w": s.W.Bytes(),
133-
"x": s.X.Bytes(),
134-
"y": s.Y.Bytes(),
134+
"w": w[:],
135+
"x": x[:],
136+
"y": y[:],
135137
}
136138
return json.Marshal(&tmp)
137139
}

go/pkg/oberon/secret_key_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,6 @@ func TestHashSecretKey(t *testing.T) {
5959
require.NoError(t, err)
6060
out, err := sk.MarshalBinary()
6161
require.NoError(t, err)
62-
expBytes := []byte{6, 129, 143, 106, 175, 152, 144, 81, 239, 218, 36, 33, 224, 247, 166, 82, 231, 107, 54, 34, 63, 0, 42, 216, 230, 25, 194, 104, 39, 52, 134, 132, 86, 100, 67, 124, 53, 144, 206, 125, 91, 24, 39, 39, 207, 81, 87, 32, 72, 141, 111, 179, 210, 81, 177, 208, 135, 247, 119, 255, 97, 119, 192, 122, 37, 67, 118, 118, 48, 31, 164, 95, 215, 216, 235, 243, 165, 81, 54, 235, 56, 136, 107, 174, 131, 82, 7, 0, 211, 145, 6, 83, 14, 135, 124, 107}
62+
expBytes := []byte{132, 134, 52, 39, 104, 194, 25, 230, 216, 42, 0, 63, 34, 54, 107, 231, 82, 166, 247, 224, 33, 36, 218, 239, 81, 144, 152, 175, 106, 143, 129, 6, 122, 192, 119, 97, 255, 119, 247, 135, 208, 177, 81, 210, 179, 111, 141, 72, 32, 87, 81, 207, 39, 39, 24, 91, 125, 206, 144, 53, 124, 67, 100, 86, 107, 124, 135, 14, 83, 6, 145, 211, 0, 7, 82, 131, 174, 107, 136, 56, 235, 54, 81, 165, 243, 235, 216, 215, 95, 164, 31, 48, 118, 118, 67, 37}
6363
require.Equal(t, 0, bytes.Compare(out, expBytes))
6464
}

0 commit comments

Comments
 (0)