internal/radix51: rename AppendBytes to Bytes

This commit is contained in:
Filippo Valsorda 2019-03-30 19:10:39 -04:00 committed by George Tankersley
parent c9d2135504
commit 1e528602b8
2 changed files with 11 additions and 11 deletions

View File

@ -239,8 +239,8 @@ func (v *FieldElement) FromBytes(x []byte) *FieldElement {
return v return v
} }
// AppendBytes appends a 32 bytes little-endian encoding of v to b. // Bytes appends a 32 bytes little-endian encoding of v to b.
func (v *FieldElement) AppendBytes(b []byte) []byte { func (v *FieldElement) Bytes(b []byte) []byte {
t := new(FieldElement).Reduce(v) t := new(FieldElement).Reduce(v)
res, out := sliceForAppend(b, 32) res, out := sliceForAppend(b, 32)
@ -300,7 +300,7 @@ func (v *FieldElement) FromBig(n *big.Int) *FieldElement {
// ToBig returns v as a big.Int. // ToBig returns v as a big.Int.
func (v *FieldElement) ToBig() *big.Int { func (v *FieldElement) ToBig() *big.Int {
buf := v.AppendBytes(nil) buf := v.Bytes(nil)
words := make([]big.Word, 32*8/bits.UintSize) words := make([]big.Word, 32*8/bits.UintSize)
for n := range words { for n := range words {
@ -319,8 +319,8 @@ func (v *FieldElement) ToBig() *big.Int {
// Equal returns 1 if v and u are equal, and 0 otherwise. // Equal returns 1 if v and u are equal, and 0 otherwise.
func (v *FieldElement) Equal(u *FieldElement) int { func (v *FieldElement) Equal(u *FieldElement) int {
var sa, sv [32]byte var sa, sv [32]byte
u.AppendBytes(sa[:0]) u.Bytes(sa[:0])
v.AppendBytes(sv[:0]) v.Bytes(sv[:0])
return subtle.ConstantTimeCompare(sa[:], sv[:]) return subtle.ConstantTimeCompare(sa[:], sv[:])
} }
@ -346,7 +346,7 @@ func (v *FieldElement) CondNeg(u *FieldElement, cond int) *FieldElement {
// IsNegative returns 1 if v is negative, and 0 otherwise. // IsNegative returns 1 if v is negative, and 0 otherwise.
func (v *FieldElement) IsNegative() int { func (v *FieldElement) IsNegative() int {
var b [32]byte var b [32]byte
v.AppendBytes(b[:0]) v.Bytes(b[:0])
return int(b[0] & 1) return int(b[0] & 1)
} }

View File

@ -122,7 +122,7 @@ func BenchmarkWideMultCall(t *testing.B) {
func TestFromBytesRoundTrip(t *testing.T) { func TestFromBytesRoundTrip(t *testing.T) {
f1 := func(in, out [32]byte, fe FieldElement) bool { f1 := func(in, out [32]byte, fe FieldElement) bool {
fe.FromBytes(in[:]) fe.FromBytes(in[:])
fe.AppendBytes(out[:0]) fe.Bytes(out[:0])
// Mask the most significant bit as it's ignored by FromBytes. (Now // Mask the most significant bit as it's ignored by FromBytes. (Now
// instead of earlier so we check the masking in FromBytes is working.) // instead of earlier so we check the masking in FromBytes is working.)
@ -141,10 +141,10 @@ func TestFromBytesRoundTrip(t *testing.T) {
} }
f2 := func(fe, r FieldElement, out [32]byte) bool { f2 := func(fe, r FieldElement, out [32]byte) bool {
fe.AppendBytes(out[:0]) fe.Bytes(out[:0])
r.FromBytes(out[:]) r.FromBytes(out[:])
// Intentionally not using Equal not to go through AppendBytes again. // Intentionally not using Equal not to go through Bytes again.
// Calling Reduce because both Generate and FromBytes can produce // Calling Reduce because both Generate and FromBytes can produce
// non-canonical representations. // non-canonical representations.
fe.Reduce(&fe) fe.Reduce(&fe)
@ -175,7 +175,7 @@ func TestBytesBigEquivalence(t *testing.T) {
return false return false
} }
fe.AppendBytes(out[:0]) fe.Bytes(out[:0])
buf := make([]byte, 32) // pad with zeroes buf := make([]byte, 32) // pad with zeroes
copy(buf, swapEndianness(fe1.ToBig().Bytes())) copy(buf, swapEndianness(fe1.ToBig().Bytes()))
@ -189,7 +189,7 @@ func TestBytesBigEquivalence(t *testing.T) {
func TestFromBytesRoundTripEdgeCases(t *testing.T) { func TestFromBytesRoundTripEdgeCases(t *testing.T) {
// TODO: values close to 0, close to 2^255-19, between 2^255-19 and 2^255-1, // TODO: values close to 0, close to 2^255-19, between 2^255-19 and 2^255-1,
// and between 2^255 and 2^256-1. Test both the documented FromBytes // and between 2^255 and 2^256-1. Test both the documented FromBytes
// behavior, and that AppendBytes reduces them. // behavior, and that Bytes reduces them.
} }
// Tests self-consistency between FeMul and FeSquare. // Tests self-consistency between FeMul and FeSquare.