Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] bchec: Constant time field normalization #78

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions bchec/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,14 @@ func BenchmarkFieldNormalize(b *testing.B) {
f.Normalize()
}
}

// BenchmarkFieldNormalize2 requires the evaluation of more involved logic in
// field.Normalize(). We should ensure the runtime is the same as in
// BenchmarkFieldNormalize
func BenchmarkFieldNormalize2(b *testing.B) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: BenchmarkFieldNormalizeWithCarry seems like a better name.

Also, I'm not sure this provides much benefit since it doesn't automatically compare the timing against regular BenchmarkFieldNormalize.

f := new(fieldVal)
f.n = [10]uint32{0x148f6, 0x3ffffc0, 0x3ffffff, 0x3ffffff, 0x3ffffff, 0x3ffffff, 0x3ffffff, 0x3ffffff, 0x3ffffff, 0x000007}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might as well just construct the fieldVal directly: f := &fieldVal{ [10]uint32{...} }

for i := 0; i < b.N; i++ {
f.Normalize()
}
}
41 changes: 41 additions & 0 deletions bchec/constant_time.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) 2017 The btcsuite developers
// Copyright (c) 2017 Brent Perreault
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

package bchec

// constant_time.go provides constant time implementations of useful
// mathematical operations. In addition, these functions return integers,
// using 0 or 1 to represent false or true respectively, which is useful
// for writing logic in terms of bitwise operators

// References
// These functions are based on the sample implementation in
// golang.org/src/crypto/subtle/constant_time.go
// Here we have refactored these functions for uint32 arithmetic and
// to avoid extra shifts and casts

// Note - these use the sign bit of int32 internally. For that reason all
// of the inputs need to be less than 2^31 to avoid overflowing int32.
// These are intended for use internal to btcec.

// lessThanUint32 returns 1 if x < y and 0 otherwise.
// It works by checking the most significant bit, and then testing the
// rest of the bits by casting to int32
func lessThanUint32(x, y uint32) uint32 {
diff := int32(x) - int32(y)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of the casts to/from int32 are useless. uint32s overflow/underflow in the exact same way.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great point!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will try to update my branch in btcd accordingly

return uint32((diff >> 31) & 1)
}

// isZeroUint32 returns 1 if x == y and 0 otherwise.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's "y"?

Copy link

@bmperrea bmperrea Nov 18, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm mistaken comment should be like // isZeroUint32 returns 1 if x == 0 and 0 otherwise.

Therefore y is zero (kidding).

func isZeroUint32(x uint32) uint32 {
x32 := int32(x)
return uint32((((x32 - 1) ^ x32) >> 31) & 1)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, the cast to int32 seems unnecessary and IMO technically "worse" since it assumes two's complement representation.

OK yes two's complement is basically universal nowadays. :-) But isn't uint32 specified to be a modulo arithmetic type in go? If so then you don't need to bother about sign bits, this is just good clean mod-2^32 arithmetic. Also btw the final &1 seems to be unneeded in each case.

(also, these are neat bit tricks, I like :-) This trick works because there are only two values for which, upon subtracting 1, the high bit is flipped: 0x80000000 and 0x00000000. Since you assume everything must be less than the first value, then it's good.)

}

// notZeroUint32 returns 1 if x != y and 0 otherwise.
func notZeroUint32(x uint32) uint32 {
x32 := int32(x)
return uint32((((-x32) | x32) >> 31) & 1)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(this trick works because there is only one value which has a high bit of 0, both before and after negation: 0x00000000.)

}
74 changes: 74 additions & 0 deletions bchec/constant_time_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright (c) 2017 The btcsuite developers
// Copyright (c) 2017 Brent Perreault
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

package bchec

import "testing"

func TestLessThanUint32(t *testing.T) {
tests := []struct {
x uint32
y uint32
a uint32
}{
{0, 1, 1},
{2, 2, 0},
{1 << 30, 1 << 30, 0},
{17, 1 << 30, 1},
{1 << 30, 0, 0},
}

t.Logf("Running %d tests", len(tests))
for i, test := range tests {
answer := lessThanUint32(test.x, test.y)
if test.a != answer {
t.Errorf("lessThanUint32 #%d wrong result\ngot: %v\n"+
"want: %v", i, answer, test.a)
continue
}
}
}

func TestIsZeroUint32(t *testing.T) {
tests := []struct {
x uint32
a uint32
}{
{1, 0},
{0, 1},
{1 << 30, 0},
}

t.Logf("Running %d tests", len(tests))
for i, test := range tests {
answer := isZeroUint32(test.x)
if test.a != answer {
t.Errorf("isZeroUint32 #%d wrong result\ngot: %v\n"+
"want: %v", i, answer, test.a)
continue
}
}
}

func TestNotZeroUint32(t *testing.T) {
tests := []struct {
x uint32
a uint32
}{
{1, 1},
{0, 0},
{1 << 30, 1},
}

t.Logf("Running %d tests", len(tests))
for i, test := range tests {
answer := notZeroUint32(test.x)
if test.a != answer {
t.Errorf("notZeroUint32 #%d wrong result\ngot: %v\n"+
"want: %v", i, answer, test.a)
continue
}
}
}
31 changes: 5 additions & 26 deletions bchec/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,32 +305,11 @@ func (f *fieldVal) Normalize() *fieldVal {
// following determines if either or these conditions are true and does
// the final reduction in constant time.
//
// Note that the if/else statements here intentionally do the bitwise
// operators even when it won't change the value to ensure constant time
// between the branches. Also note that 'm' will be zero when neither
// of the aforementioned conditions are true and the value will not be
// changed when 'm' is zero.
m = 1
if t9 == fieldMSBMask {
m &= 1
} else {
m &= 0
}
if t2&t3&t4&t5&t6&t7&t8 == fieldBaseMask {
m &= 1
} else {
m &= 0
}
if ((t0+977)>>fieldBase + t1 + 64) > fieldBaseMask {
m &= 1
} else {
m &= 0
}
if t9>>fieldMSBBits != 0 {
m |= 1
} else {
m |= 0
}
// Note that 'm' will be zero when neither of the aforementioned conditions
// are true and the value will not be changed when 'm' is zero.
m = isZeroUint32((t9 - fieldMSBMask) | (t2&t3&t4&t5&t6&t7&t8 - fieldBaseMask))
m &= lessThanUint32(fieldBaseMask, (t0+977)>>fieldBase+t1+64)
m |= notZeroUint32(t9 >> fieldMSBBits)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reference, this seems to get done more simply in libsecp which also has an implementation of the same 10x26 field limbing: https://github.com/bitcoin-core/secp256k1/blob/0c774d89e61808d883b8f2df74462421491d59bb/src/field_10x26_impl.h#L66-L67

I have to admit I don't understand this deeply enough to know how either works.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(for what it's worth, this PR is at least clearly identical to the code being modified, so there is nothing to worry about in terms of changed behaviour)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK I see now this is identical with libsecp... the two different meanings of 'm' was confusing me.

The libsecp expression is still simpler, for example it takes advantage of the fact that t9 >> fieldMSBBits is surely either 0 or 1 at this point. You could do the same by making the final line to be m |= t9 >> fieldMSBBits.

And libsecp uses equality comparisons along with implicit bool -> int cast (i.e., in the assembly it calls cmp which just does a subtraction anyway but only uses the result to set flags, then sete casts the flag to an int). Unfortunately it looks like in go you might not have a simple bool->int cast so this is about as good as it gets?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the lack of bool->int cast is an unfortunate choice in golang when you want constant time math. This is as good as it gets and follows the standard patterns in crypto/subtle.

It would be great to incorporate simplifications and improvements based on the libsecp implementation, modulo lack of above casts and different language standards for constants-as-variables.

t0 = t0 + m*977
t1 = (t0 >> fieldBase) + t1 + (m << 6)
t0 = t0 & fieldBaseMask
Expand Down
10 changes: 10 additions & 0 deletions bchec/field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,16 @@ func TestNormalize(t *testing.T) {
[10]uint32{0x03fffc30, 0x03ffffc0, 0x03ffffff, 0x03ffffff, 0x03ffffff, 0x03ffffff, 0x03ffffff, 0x03ffffff, 0x07ffffff, 0x003fffff},
[10]uint32{0x00000001, 0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000001},
},
// A more difficult branch for the Normalize logic
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better comment:

// Number less than P that satisfies two of the three overflow
// conditions for a field value of magnitude of 1 with value greater
// than P in all but the last word.

{
[10]uint32{0x148f6, 0x3ffffc0, 0x3ffffff, 0x3ffffff, 0x3ffffff, 0x3ffffff, 0x3ffffff, 0x3ffffff, 0x3ffffff, 0x000007},
[10]uint32{0x148f6, 0x3ffffc0, 0x3ffffff, 0x3ffffff, 0x3ffffff, 0x3ffffff, 0x3ffffff, 0x3ffffff, 0x3ffffff, 0x000007},
},
// A test that would fail without the check t2&t3&t4&t5&t6&t7&t8 == fieldBaseMask before normalization
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better comment:

// Number less than P that satisfies two of the three overflow
// conditions for a field value of magnitude of 1 with value greater
// than P in all but the second to last word.

{
[10]uint32{0x03fffc30, 0x03ffffff, 0x03ffffff, 0x03ffffff, 0x03ffffff, 0x03ffffff, 0x03ffffff, 0x03ffffff, 0x03fffff0, 0x003fffff},
[10]uint32{0x03fffc30, 0x03ffffff, 0x03ffffff, 0x03ffffff, 0x03ffffff, 0x03ffffff, 0x03ffffff, 0x03ffffff, 0x03fffff0, 0x003fffff},
},
}

t.Logf("Running %d tests", len(tests))
Expand Down