Skip to content

Commit

Permalink
Return errors instead of panics where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
OBrezhniev committed Sep 20, 2023
1 parent 12366f1 commit 48636e9
Show file tree
Hide file tree
Showing 12 changed files with 140 additions and 132 deletions.
10 changes: 5 additions & 5 deletions babyjub/babyjub.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@ var B8 *Point

// init initializes global numbers and the subgroup base.
func init() {
A = utils.NewIntFromString("168700")
D = utils.NewIntFromString("168696")
A, _ = utils.NewIntFromString("168700")
D, _ = utils.NewIntFromString("168696")
Aff = ff.NewElement().SetBigInt(A)
Dff = ff.NewElement().SetBigInt(D)

Order = utils.NewIntFromString(
Order, _ = utils.NewIntFromString(
"21888242871839275222246405745257275088614511777268538073601725287587578984328")
SubOrder = new(big.Int).Rsh(Order, 3) //nolint:gomnd

B8 = NewPoint()
B8.X = utils.NewIntFromString(
B8.X, _ = utils.NewIntFromString(
"5299619240641551281634865583518297030282874472190772894086521144482721001553")
B8.Y = utils.NewIntFromString(
B8.Y, _ = utils.NewIntFromString(
"16950150798460657717958625567821834550301663161624707787222815936182638968203")
}

Expand Down
78 changes: 39 additions & 39 deletions babyjub/babyjub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ func TestAdd1(t *testing.T) {
}

func TestAdd2(t *testing.T) {
aX := utils.NewIntFromString(
aX, _ := utils.NewIntFromString(
"17777552123799933955779906779655732241715742912184938656739573121738514868268")
aY := utils.NewIntFromString(
aY, _ := utils.NewIntFromString(
"2626589144620713026669568689430873010625803728049924121243784502389097019475")
a := &Point{X: aX, Y: aY}

bX := utils.NewIntFromString(
bX, _ := utils.NewIntFromString(
"17777552123799933955779906779655732241715742912184938656739573121738514868268")
bY := utils.NewIntFromString(
bY, _ := utils.NewIntFromString(
"2626589144620713026669568689430873010625803728049924121243784502389097019475")
b := &Point{X: bX, Y: bY}

Expand All @@ -57,15 +57,15 @@ func TestAdd2(t *testing.T) {
}

func TestAdd3(t *testing.T) {
aX := utils.NewIntFromString(
aX, _ := utils.NewIntFromString(
"17777552123799933955779906779655732241715742912184938656739573121738514868268")
aY := utils.NewIntFromString(
aY, _ := utils.NewIntFromString(
"2626589144620713026669568689430873010625803728049924121243784502389097019475")
a := &Point{X: aX, Y: aY}

bX := utils.NewIntFromString(
bX, _ := utils.NewIntFromString(
"16540640123574156134436876038791482806971768689494387082833631921987005038935")
bY := utils.NewIntFromString(
bY, _ := utils.NewIntFromString(
"20819045374670962167435360035096875258406992893633759881276124905556507972311")
b := &Point{X: bX, Y: bY}

Expand All @@ -80,15 +80,15 @@ func TestAdd3(t *testing.T) {
}

func TestAdd4(t *testing.T) {
aX := utils.NewIntFromString(
aX, _ := utils.NewIntFromString(
"0")
aY := utils.NewIntFromString(
aY, _ := utils.NewIntFromString(
"1")
a := &Point{X: aX, Y: aY}

bX := utils.NewIntFromString(
bX, _ := utils.NewIntFromString(
"16540640123574156134436876038791482806971768689494387082833631921987005038935")
bY := utils.NewIntFromString(
bY, _ := utils.NewIntFromString(
"20819045374670962167435360035096875258406992893633759881276124905556507972311")
b := &Point{X: bX, Y: bY}

Expand All @@ -113,12 +113,12 @@ func TestInCurve2(t *testing.T) {
}

func TestMul0(t *testing.T) {
x := utils.NewIntFromString(
x, _ := utils.NewIntFromString(
"17777552123799933955779906779655732241715742912184938656739573121738514868268")
y := utils.NewIntFromString(
y, _ := utils.NewIntFromString(
"2626589144620713026669568689430873010625803728049924121243784502389097019475")
p := &Point{X: x, Y: y}
s := utils.NewIntFromString("3")
s, _ := utils.NewIntFromString("3")

r2 := NewPoint().Projective().Add(p.Projective(), p.Projective()).Affine()
r2 = NewPoint().Projective().Add(r2.Projective(), p.Projective()).Affine()
Expand All @@ -135,12 +135,12 @@ func TestMul0(t *testing.T) {
}

func TestMul1(t *testing.T) {
x := utils.NewIntFromString(
x, _ := utils.NewIntFromString(
"17777552123799933955779906779655732241715742912184938656739573121738514868268")
y := utils.NewIntFromString(
y, _ := utils.NewIntFromString(
"2626589144620713026669568689430873010625803728049924121243784502389097019475")
p := &Point{X: x, Y: y}
s := utils.NewIntFromString(
s, _ := utils.NewIntFromString(
"14035240266687799601661095864649209771790948434046947201833777492504781204499")
r := NewPoint().Mul(s, p)
assert.Equal(t,
Expand All @@ -152,12 +152,12 @@ func TestMul1(t *testing.T) {
}

func TestMul2(t *testing.T) {
x := utils.NewIntFromString(
x, _ := utils.NewIntFromString(
"6890855772600357754907169075114257697580319025794532037257385534741338397365")
y := utils.NewIntFromString(
y, _ := utils.NewIntFromString(
"4338620300185947561074059802482547481416142213883829469920100239455078257889")
p := &Point{X: x, Y: y}
s := utils.NewIntFromString(
s, _ := utils.NewIntFromString(
"20819045374670962167435360035096875258406992893633759881276124905556507972311")
r := NewPoint().Mul(s, p)
assert.Equal(t,
Expand All @@ -169,45 +169,45 @@ func TestMul2(t *testing.T) {
}

func TestInCurve3(t *testing.T) {
x := utils.NewIntFromString(
x, _ := utils.NewIntFromString(
"17777552123799933955779906779655732241715742912184938656739573121738514868268")
y := utils.NewIntFromString(
y, _ := utils.NewIntFromString(
"2626589144620713026669568689430873010625803728049924121243784502389097019475")
p := &Point{X: x, Y: y}
assert.Equal(t, true, p.InCurve())
}

func TestInCurve4(t *testing.T) {
x := utils.NewIntFromString(
x, _ := utils.NewIntFromString(
"6890855772600357754907169075114257697580319025794532037257385534741338397365")
y := utils.NewIntFromString(
y, _ := utils.NewIntFromString(
"4338620300185947561074059802482547481416142213883829469920100239455078257889")
p := &Point{X: x, Y: y}
assert.Equal(t, true, p.InCurve())
}

func TestInSubGroup1(t *testing.T) {
x := utils.NewIntFromString(
x, _ := utils.NewIntFromString(
"17777552123799933955779906779655732241715742912184938656739573121738514868268")
y := utils.NewIntFromString(
y, _ := utils.NewIntFromString(
"2626589144620713026669568689430873010625803728049924121243784502389097019475")
p := &Point{X: x, Y: y}
assert.Equal(t, true, p.InSubGroup())
}

func TestInSubGroup2(t *testing.T) {
x := utils.NewIntFromString(
x, _ := utils.NewIntFromString(
"6890855772600357754907169075114257697580319025794532037257385534741338397365")
y := utils.NewIntFromString(
y, _ := utils.NewIntFromString(
"4338620300185947561074059802482547481416142213883829469920100239455078257889")
p := &Point{X: x, Y: y}
assert.Equal(t, true, p.InSubGroup())
}

func TestPointFromSignAndy(t *testing.T) {
x := utils.NewIntFromString(
x, _ := utils.NewIntFromString(
"17777552123799933955779906779655732241715742912184938656739573121738514868268")
y := utils.NewIntFromString(
y, _ := utils.NewIntFromString(
"2626589144620713026669568689430873010625803728049924121243784502389097019475")
p := &Point{X: x, Y: y}

Expand All @@ -219,9 +219,9 @@ func TestPointFromSignAndy(t *testing.T) {
}

func TestPackAndUnpackSignY(t *testing.T) {
x := utils.NewIntFromString(
x, _ := utils.NewIntFromString(
"17777552123799933955779906779655732241715742912184938656739573121738514868268")
y := utils.NewIntFromString(
y, _ := utils.NewIntFromString(
"2626589144620713026669568689430873010625803728049924121243784502389097019475")
p := &Point{X: x, Y: y}
pComp := p.Compress()
Expand All @@ -239,9 +239,9 @@ func TestPackAndUnpackSignY(t *testing.T) {
}

func TestCompressDecompress1(t *testing.T) {
x := utils.NewIntFromString(
x, _ := utils.NewIntFromString(
"17777552123799933955779906779655732241715742912184938656739573121738514868268")
y := utils.NewIntFromString(
y, _ := utils.NewIntFromString(
"2626589144620713026669568689430873010625803728049924121243784502389097019475")
p := &Point{X: x, Y: y}

Expand All @@ -257,9 +257,9 @@ func TestCompressDecompress1(t *testing.T) {
}

func TestCompressDecompress2(t *testing.T) {
x := utils.NewIntFromString(
x, _ := utils.NewIntFromString(
"6890855772600357754907169075114257697580319025794532037257385534741338397365")
y := utils.NewIntFromString(
y, _ := utils.NewIntFromString(
"4338620300185947561074059802482547481416142213883829469920100239455078257889")
p := &Point{X: x, Y: y}

Expand Down Expand Up @@ -299,9 +299,9 @@ func BenchmarkBabyjub(b *testing.B) {

var points [n]*Point
var pointsProj [n]*PointProjective
baseX := utils.NewIntFromString(
baseX, _ := utils.NewIntFromString(
"17777552123799933955779906779655732241715742912184938656739573121738514868268")
baseY := utils.NewIntFromString(
baseY, _ := utils.NewIntFromString(
"2626589144620713026669568689430873010625803728049924121243784502389097019475")
base := &Point{X: baseX, Y: baseY}
for i := 0; i < n; i++ {
Expand Down
9 changes: 6 additions & 3 deletions babyjub/babyjub_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,12 @@ func NewBjjWrappedKey(privKey *PrivateKey) *BjjWrappedPrivateKey {
}

// RandomBjjWrappedKey creates a new BjjWrappedPrivateKey with a random private key.
func RandomBjjWrappedKey() *BjjWrappedPrivateKey {
privKey := NewRandPrivKey()
return NewBjjWrappedKey(&privKey)
func RandomBjjWrappedKey() (*BjjWrappedPrivateKey, error) {
privKey, err := NewRandPrivKey()
if err != nil {
return nil, err
}
return NewBjjWrappedKey(&privKey), nil
}

// Public returns the public key of the private key.
Expand Down
20 changes: 12 additions & 8 deletions babyjub/babyjub_wrapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestBjjWrappedPrivateKeyInterfaceImpl(t *testing.T) {
}

func TestBjjWrappedPrivateKey(t *testing.T) {
pk := RandomBjjWrappedKey()
pk, _ := RandomBjjWrappedKey()

hasher, err := poseidon.New(16)
require.NoError(t, err)
Expand All @@ -43,13 +43,14 @@ func TestBjjWrappedPrivateKey(t *testing.T) {
require.NoError(t, err)

digestBI := big.NewInt(0).SetBytes(digest)
pub.pubKey.VerifyPoseidon(digestBI, decomrpessSig)
err = pub.pubKey.VerifyPoseidon(digestBI, decomrpessSig)
require.NoError(t, err)
}

func TestBjjWrappedPrivateKeyEqual(t *testing.T) {
x1 := RandomBjjWrappedKey()
x1, _ := RandomBjjWrappedKey()
require.True(t, x1.Equal(x1))
x2 := RandomBjjWrappedKey()
x2, _ := RandomBjjWrappedKey()
require.False(t, x1.Equal(x2))
}

Expand All @@ -58,8 +59,11 @@ func TestBjjWrappedPublicKeyInterfaceImpl(t *testing.T) {
}

func TestBjjWrappedPublicKeyEqual(t *testing.T) {
x1 := RandomBjjWrappedKey().Public().(*BjjWrappedPublicKey)
require.True(t, x1.Equal(x1))
x2 := RandomBjjWrappedKey().Public()
require.False(t, x1.Equal(x2))
x1, _ := RandomBjjWrappedKey()
x1pub := x1.Public().(*BjjWrappedPublicKey)
require.True(t, x1pub.Equal(x1pub))
require.True(t, x1pub.Equal(x1.Public()))
x2, _ := RandomBjjWrappedKey()
x2pub := x2.Public()
require.False(t, x1pub.Equal(x2pub))
}
6 changes: 3 additions & 3 deletions babyjub/eddsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ type PrivateKey [32]byte

// NewRandPrivKey generates a new random private key (using cryptographically
// secure randomness).
func NewRandPrivKey() PrivateKey {
func NewRandPrivKey() (PrivateKey, error) {
var k PrivateKey
_, err := rand.Read(k[:])
if err != nil {
panic(err)
return PrivateKey{}, err
}
return k
return k, nil
}

// Scalar converts a private key into the scalar value s following the EdDSA
Expand Down
Loading

0 comments on commit 48636e9

Please sign in to comment.