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

Var time fuzzer #14

Open
wants to merge 7 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions core/vm/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ func runBn256Add(input []byte) ([]byte, error) {
}
res := new(bn256.G1)
res.Add(x, y)
return res.Marshal(), nil
return res.MarshalVariableTime(), nil
}

// bn256Add implements a native elliptic curve point addition conforming to
Expand Down Expand Up @@ -376,7 +376,7 @@ func runBn256ScalarMul(input []byte) ([]byte, error) {
}
res := new(bn256.G1)
res.ScalarMult(p, new(big.Int).SetBytes(getData(input, 64, 32)))
return res.Marshal(), nil
return res.MarshalVariableTime(), nil
}

// bn256ScalarMulIstanbul implements a native elliptic curve scalar
Expand Down
26 changes: 26 additions & 0 deletions crypto/bn256/cloudflare/bn256.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,32 @@ func (e *G1) Marshal() []byte {
return ret
}

// Marshal converts e to a byte slice.
// Uses variable time algorithms for inversion
// for transformation to affine coordinates
func (e *G1) MarshalVariableTime() []byte {
// Each value is a 256-bit number.
const numBytes = 256 / 8

if e.p == nil {
e.p = &curvePoint{}
}

e.p.MakeAffineVariableTime()
ret := make([]byte, numBytes*2)
if e.p.IsInfinity() {
return ret
}
temp := &gfP{}

montDecode(temp, &e.p.x)
temp.Marshal(ret)
montDecode(temp, &e.p.y)
temp.Marshal(ret[numBytes:])

return ret
}

// Unmarshal sets e to the result of converting the output of Marshal back into
// a group element and then returns e.
func (e *G1) Unmarshal(m []byte) ([]byte, error) {
Expand Down
65 changes: 65 additions & 0 deletions crypto/bn256/cloudflare/bn256_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,71 @@ func TestTripartiteDiffieHellman(t *testing.T) {
}
}

func TestBinaryEAA(t *testing.T) {
for i := 0; i < 10000; i++ {
_, Ga, err := RandomG1(rand.Reader)
if err != nil {
t.Fatal(err)
}
tmpLittleFermat := &gfP{}
tmpLittleFermat.Invert(&Ga.p.x)

tmpBinaryEAA := &gfP{}
tmpBinaryEAA.InvertVariableTime(&Ga.p.x)

tmpBinaryEAASelfSet := &gfP{}
tmpBinaryEAASelfSet.Set(&Ga.p.x)
tmpBinaryEAASelfSet.InvertVariableTime(tmpBinaryEAASelfSet)

if *tmpLittleFermat != *tmpBinaryEAA {
t.Fatalf("results of different inversion do not agree")
}

if *tmpLittleFermat != *tmpBinaryEAASelfSet {
t.Fatalf("self-assigned inversion is invalid")
}
}
}

func BenchmarkLittleFermatInversion(b *testing.B) {
el := gfP{0x0, 0x97816a916871ca8d, 0xb85045b68181585d, 0x30644e72e131a029}

b.ResetTimer()

tmp := &gfP{}
for i := 0; i < b.N; i++ {
tmp.Invert(&el)
}
}

func BenchmarkBinaryEEAInversion(b *testing.B) {
el := gfP{0x0, 0x97816a916871ca8d, 0xb85045b68181585d, 0x30644e72e131a029}

b.ResetTimer()

tmp := &gfP{}
for i := 0; i < b.N; i++ {
tmp.InvertVariableTime(&el)
}
}

func BenchmarkG1AddAndMakeAffine(b *testing.B) {
_, Ga, err := RandomG1(rand.Reader)
if err != nil {
b.Fatal(err)
}
_, Gb, err := RandomG1(rand.Reader)
if err != nil {
b.Fatal(err)
}
b.ResetTimer()

for i := 0; i < b.N; i++ {
e := new(G1).Add(Ga, Gb)
e.p.MakeAffine()
}
}

func BenchmarkG1(b *testing.B) {
x, _ := rand.Int(rand.Reader, Order)
b.ResetTimer()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
;���8���w������
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ty710542pe 437764856785504449281190514550177096728800832068CallSlice-466353402crypto/ra blobocked for 70 ecordrandnds waiting to read er for 60 econds waiting to read random data from the kernelnot a pointerrandom data fromthe kerWelnota pointed:
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
\vflec��B710542735760100185871124267578125o� py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type crypto/ra blobocked for 60 econds waiting to read random data from the kernelnot a pointer:
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�X�)c>P
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2155010672361yt/rrrn aig3319bood t763726738rbc@�u�dittm778375564928114"051455017709672880083202068677396868739640837315@616991234crypto/ra blobockrandom d0 ecordrandnds waiting to read random dataBfroo readdrandom data fro����������������������������������m the kernelno
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�����������89��wQ�ʭ���
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ty710542pe 43776485743678550444492811490514550177096728800832068687396408373151616991234crypto/ra blobocryp/otrand: blocked for 60 seconds waiting to read random data from the kernelcked for 70 ecordrandnds waiting to read random data from the kernelnota pointer:
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ty1051149051455017709672880832068687396408373151616991234crypto/ra blobocked for 70 ecordrandnds waiting to read random42pe 43776485743678550444492811490514550177096728800832068687396408373151616991234crypto/ra blobocked for 70 ecordrandnds waiting to read random data from the kernelnota point
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
82699ob0n a389114896408373151616991234crypto/fro��m data fro�� to read random42pe 43776485743678550444492811490514550177096728800832068687396408373151616991234crypto/fro��m data fro���������������������������������������������������������������������������������m the k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ty051149051955017709672880083550177096728800832068687396408973151616991234crypto/ra blobocked for 700Û@�o ucordrandnds waiting to read random42pe 43776485743678550444492811490514550177096728800832068687396408373151616991234crypto/ra blobocked for 70 ecordrandnds waiting2068687396408373151616991234crypto/ra blobocked for 700Û@�o ucordrandnds waiting to read random42pe 4377648574367855044449281149051455017709672880083206868739640837315@616991234crypto/ra blobocked for 70 ecordrandnds waiting to read random data from the kernelnota point
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
549010770628800832068687396408373151616991234crypto/ra blobocked for 70 ecordrandnds waiting to read random42pe 43776485743678550444492811490514550177096728800832068687396408373151616991234crypto/ra blobocked for 70 ecordrandnds waiting to read random data from the kernelnota po
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type not a pointer:
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a11pror44444168003432t��f����550444492�81149051455017709672880amd64p3287396408373151616991234crypto/fro��m data fro�����������������������������������������������������������������������������������������������������������������������������������������������������������
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ty10511490514550177096728800832068687396408373151616991234crypto/ra blobocked for 70 ecordrandnds waiting to read random42pe 43776485743678550444492811490514550177096728800832068687396408373151616991234crypto/ra blobocked for 70 ecordrandnds waiting to read random data from the kernelnota point
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ty710542pe 43776485743678550444492811490514550177096728800832068687396408373151616991234crypto/ra blobocked for 70 ecordrandnds waiting to read random data from the kernelnota pointer:
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ty051149051955017709672880083550177096728800832068687396408973151616991234crypto/ra blobocked for 700Û@�o ucordrandndaiting to read random42pe 43776485743678550444492811490514550177096728800832068687396408373151616991234crypto/ra blobocked for 70 ecordrandnds waiting2068687396408373151616991234crypto/ra blobocked for 700Û@�o ucordrandnds waiting to read random42pe 437764857436785455017709672880083206868991234crypto/ra blobocked for 70 ecordrandnds waiting to read random data from the kernelnota point
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0350177096728800832068687396408973151616991234crypto/ra blobocked for 700Û@�o ucordrandndaiting to read random42pe 43776485743678550444492811490514550177096728800832068687396408373151616991234crypto/fro��m data fro���������������������������������������������m the kerne
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3cry��tr��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
012abcdefghijklmnopqrst%#uvwxyzA<CDEFGHIJKLMNOPQRSTUVWXYZ��ٛ
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ty710542pe 4377648574367854928114950177096728800832068687396315161699134crypto/rked for 70 ecordrandnds waiting to read randomom the kernelnota pointer
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0123456789abcdef
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a11pror44444168003432t��f����550444492�811490514550177096728800832068687396408373151616991234crypto/fro��m data fro��������������������������"��������������������������������������������������������������������������������������������������������������������������������
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0123496789afghijklmnopqrst%#uvwxyzA<CDEFGHIJKLMNOPQRSTUVWXYZ��43776485743678550444492811490514550177096728800832068687396408373151616991234��ٛ
1 change: 1 addition & 0 deletions crypto/bn256/cloudflare/corpus/adsf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
123456432
1 change: 1 addition & 0 deletions crypto/bn256/cloudflare/corpus/asdaf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
12412312
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
r�ad go472155010672361yt/rrrn aig3319bood t768726738rbc@�u�dittm7783755449281149051455017709672880083202068687396868739640837315@616991234crypto/ra blobockrandom d0 ecordrandnds waiting to read random data froo read random data fro��������������������������m the kernelno
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0601112o00�@uwt3645068965bcerdn879914lbr�ad go472155010672361yt/rrrn aig3319bood t768726738rbc@�u�dittm7783755449281149051455017709672880083202068687396868739640837315@616991234crypto/ra blobocked for 70 ecordrandnds waiting to read random data fro���������m the kernelnot
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ty710542pe 3776485743678550444492811490514550177096728800832068687396408373151616991234crypto/ra blobocryp/otrand: b read random d the kerfor 70 ecordrandnds waiting to read random data from the kernelnota pointer:
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0123456789abcdefghijklmnopqrst%#uvwxyzA<CDEFGHIJKLMNOPQRSTUVWXYZ��ٛ
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
;��8���wQ�ʭ������ 
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
le71054275760100185871124267578125o� p
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type crypto/ra blolocked for 60 seconds waiting to read random data from the kernelnot a pointer:
Binary file not shown.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
missing verb: % at end of format string
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
t195501770709672880083206868739640897315161291234crypto/a blobocked for 700Û@�o ucordrandnds waiti37765743678550444492811490514550177096728800832068687396408373151616991234crypto/ra blobocked for 70 ecordrandnds waiting206868739640837315161699123800832068687396408973151616991234crypto/ra blobocked for 700Û@�o ucordrandnds waiting to read random42pe 43776485743678550444492811490514550177096728800832068687396408373151616991234crypto/ra blobocked for 70 ecordrandnds waiting2068687396408373151616991234crypto/ra blobocked for 700Û@�o ucordrandnds waiting to read random42pe 4377648574367855044449281149051455017709672880083206868739640837315@616991234cryp4crypto/ra blobocked for 700Û@�o ucordrandnds waiting to read random42pe 4377648574367855044449281149051455017709672880083206868739640837315@616991234crypto/ra blobocked for 70 ecordrandnds waiting to read random data from the kernelnot
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
002�uwt3645068965bcerdn879914lbr�ad go472155010672361yt/rrrn aig3319bood t768726738rbc@�u�dittm7783755449281149051455017709672880083202068687396868739640837315@616991234crypto/ra blobockrandom d0 ecordrandnds waiting to read random data fro�����������������m the kernelnot
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ty710542pe 43776485743678550444492149051455017709672880485743678550444492811490514550177096728800832068687396408373151616991234crypto/ra blobocryp/otrand: blocked for 60 seconds waiting to read random data from the kernelcked for 70 ecordrandnds waiting to 0832068687396408373151616991234crypto/ra blobocryp/otrand: blocked for 60 seconds waiting to read random data from the kernelcked for 70 ecordrandnds waiting to read random data from the kernelnota pointer:
1 change: 1 addition & 0 deletions crypto/bn256/cloudflare/corpus/zero
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
1 change: 1 addition & 0 deletions crypto/bn256/cloudflare/corpus/zeroz
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Empty file.
24 changes: 24 additions & 0 deletions crypto/bn256/cloudflare/curve.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,30 @@ func (c *curvePoint) Mul(a *curvePoint, scalar *big.Int) {
c.Set(sum)
}

func (c *curvePoint) MakeAffineVariableTime() {
if c.z == *newGFp(1) {
return
} else if c.z == *newGFp(0) {
c.x = gfP{0}
c.y = *newGFp(1)
c.t = gfP{0}
return
}

zInv := &gfP{}
zInv.InvertVariableTime(&c.z)

t, zInv2 := &gfP{}, &gfP{}
gfpMul(t, &c.y, zInv)
gfpMul(zInv2, zInv, zInv)

gfpMul(&c.x, &c.x, zInv2)
gfpMul(&c.y, t, zInv2)

c.z = *newGFp(1)
c.t = *newGFp(1)
}

func (c *curvePoint) MakeAffine() {
if c.z == *newGFp(1) {
return
Expand Down
98 changes: 98 additions & 0 deletions crypto/bn256/cloudflare/fuzz.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package bn256

import (
"fmt"

fuzz "github.com/google/gofuzz"
)

func Fuzz(data []byte) int {
var gfpTest gfP
f := fuzz.NewFromGoFuzz(data)
f.Fuzz(&gfpTest)
zInvVar := &gfP{}
zInvVar.InvertVariableTime(&gfpTest)
zInv := &gfP{}
zInv.Invert(&gfpTest)
if !gfpEq(zInv, zInvVar) {
panic(fmt.Sprintf("invalid invert: %v %v", zInv, zInvVar))
}
return 0
}

func Fuzz2(data []byte) int {
var gfpTest gfP2
f := fuzz.NewFromGoFuzz(data)
f.Fuzz(&gfpTest)
zInvVar := &gfP2{}
zInvVar.InvertVariableTime(&gfpTest)
zInv := &gfP2{}
zInv.Invert(&gfpTest)
if !gfp2Eq(zInv, zInvVar) {
panic(fmt.Sprintf("invalid invert: %v %v", zInv, zInvVar))
}
return 0
}

func Fuzz3(data []byte) int {
var gfpTest gfP6
f := fuzz.NewFromGoFuzz(data)
f.Fuzz(&gfpTest)
zInvVar := &gfP6{}
zInvVar.InvertVariableTime(&gfpTest)
zInv := &gfP6{}
zInv.Invert(&gfpTest)
if !gfp6Eq(zInv, zInvVar) {
panic(fmt.Sprintf("invalid invert: %v %v", zInv, zInvVar))
}
return 0
}

func Fuzz4(data []byte) int {
var gfpTest gfP12
f := fuzz.NewFromGoFuzz(data)
f.Fuzz(&gfpTest)
zInvVar := &gfP12{}
zInvVar.InvertVariableTime(&gfpTest)
zInv := &gfP12{}
zInv.Invert(&gfpTest)
if !gfp12Eq(zInv, zInvVar) {
panic(fmt.Sprintf("invalid invert: %v %v", zInv, zInvVar))
}
return 0
}

func gfp12Eq(a, b *gfP12) bool {
if gfp6Eq(&a.x, &b.x) &&
gfp6Eq(&a.y, &b.y) {
return true
}
return false
}

func gfp6Eq(a, b *gfP6) bool {
if gfp2Eq(&a.x, &b.x) &&
gfp2Eq(&a.y, &b.y) &&
gfp2Eq(&a.z, &b.z) {
return true
}
return false
}

func gfp2Eq(a, b *gfP2) bool {
if gfpEq(&a.x, &b.x) &&
gfpEq(&a.y, &b.y) {
return true
}
return false
}

func gfpEq(a, b *gfP) bool {
if a[0] == b[0] &&
a[1] == b[1] &&
a[2] == b[2] &&
a[3] == b[3] {
return true
}
return false
}