Skip to content

Commit 4e536e5

Browse files
committed
Update shcrypto
1 parent 7812fcb commit 4e536e5

File tree

8 files changed

+29
-18
lines changed

8 files changed

+29
-18
lines changed

rolling-shutter/app/messages.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"math/big"
55

66
"github.com/ethereum/go-ethereum/common"
7-
bn256 "github.com/ethereum/go-ethereum/crypto/bn256/cloudflare"
7+
"github.com/ethereum/go-ethereum/crypto/bls12381"
88
"github.com/pkg/errors"
99

1010
"github.com/shutter-network/shutter/shlib/shcrypto"
@@ -54,14 +54,17 @@ func ParsePolyEvalMsg(msg *shmsg.PolyEval, sender common.Address) (*PolyEval, er
5454

5555
// ParsePolyCommitmentMsg converts a shmsg.PolyCommitmentMsg to an app.PolyCommitmentMsg.
5656
func ParsePolyCommitmentMsg(msg *shmsg.PolyCommitment, sender common.Address) (*PolyCommitment, error) {
57+
g2 := bls12381.NewG2()
5758
gammas := shcrypto.Gammas{}
5859
for _, g := range msg.Gammas {
59-
g2 := new(bn256.G2)
60-
_, err := g2.Unmarshal(g)
60+
p, err := g2.FromBytes(g)
6161
if err != nil {
6262
return nil, err
6363
}
64-
gammas = append(gammas, g2)
64+
if !g2.IsOnCurve(p) {
65+
return nil, errors.Errorf("invalid gamma value %x", g)
66+
}
67+
gammas = append(gammas, p)
6568
}
6669
return &PolyCommitment{
6770
Sender: sender,

rolling-shutter/cmd/cryptocmd/jsontests.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"os"
1111

1212
"github.com/ethereum/go-ethereum/common/hexutil"
13-
bn256 "github.com/ethereum/go-ethereum/crypto/bn256/cloudflare"
13+
"github.com/ethereum/go-ethereum/crypto/bls12381"
1414
"github.com/spf13/cobra"
1515

1616
"github.com/shutter-network/shutter/shlib/shcrypto"
@@ -452,12 +452,13 @@ func createEncryptionTest(keygen *testkeygen.KeyGenerator, message []byte) (*enc
452452
// tamperEncryptedMessage changes the C1 value of EncryptedMessage, which allows to test for malleability issues.
453453
func tamperEncryptedMessage(keygen *testkeygen.KeyGenerator, et encryptionTest) encryptionTest {
454454
decryptionKey := keygen.EpochSecretKey(et.EpochID)
455-
var c1 *bn256.G2
455+
g2 := bls12381.NewG2()
456+
var c1 *bls12381.PointG2
456457
var err error
457458

458459
for i := 1; i <= 10000; i++ {
459460
c1 = et.Expected.C1
460-
c1.Add(c1, c1)
461+
g2.Add(c1, c1, c1)
461462
et.Expected.C1 = c1
462463
sigma := et.Expected.Sigma(decryptionKey)
463464
decryptedBlocks := shcrypto.DecryptBlocks(et.Expected.C3, sigma)

rolling-shutter/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ require (
3131
github.com/rs/zerolog v1.28.0
3232
github.com/shutter-network/gnosh-contracts v0.2.0
3333
github.com/shutter-network/shop-contracts v0.0.0-20240407151512-08ef5d8355b6
34-
github.com/shutter-network/shutter/shlib v0.1.13
34+
github.com/shutter-network/shutter/shlib v0.1.18
3535
github.com/shutter-network/txtypes v0.1.0
3636
github.com/spf13/afero v1.8.2
3737
github.com/spf13/cobra v1.6.1

rolling-shutter/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -835,8 +835,8 @@ github.com/shutter-network/gnosh-contracts v0.2.0 h1:qH3gAhlh5VZzvJcbi044lxFWQ+M
835835
github.com/shutter-network/gnosh-contracts v0.2.0/go.mod h1:QB0d64ybbVFKMrLjrc1tldri87KNjTmKQjhk9jaso2E=
836836
github.com/shutter-network/shop-contracts v0.0.0-20240407151512-08ef5d8355b6 h1:m6Ti1/IH+GBTtGqyAX3xbh+ruUKvC+m+/uzYDUa+JDQ=
837837
github.com/shutter-network/shop-contracts v0.0.0-20240407151512-08ef5d8355b6/go.mod h1:LEWXLRruvxq9fe2oKtJI3xfzbauhfWTjOczHN61RU+4=
838-
github.com/shutter-network/shutter/shlib v0.1.13 h1:9YloDJBdhFAKm2GMg4gBNeaJ+Mw9Qzeh5Kz9A2ayp1E=
839-
github.com/shutter-network/shutter/shlib v0.1.13/go.mod h1:RlYNZjx+pfKAi0arH+jfdlxG4kQ75UFzDfVjgCVYaUw=
838+
github.com/shutter-network/shutter/shlib v0.1.18 h1:ei1EWEavnlkwbX51aGKtgt7NydY0IPNV35J525vAfeo=
839+
github.com/shutter-network/shutter/shlib v0.1.18/go.mod h1:RlYNZjx+pfKAi0arH+jfdlxG4kQ75UFzDfVjgCVYaUw=
840840
github.com/shutter-network/txtypes v0.1.0 h1:QqdiiiB9AiBCSJ/ke6z1ZoDGfu2+1Lgpz5vHzVN4FKc=
841841
github.com/shutter-network/txtypes v0.1.0/go.mod h1:gaLQWfEFH+3q5CmM3kkSgS+NspclEs8SCnj8QAozsaI=
842842
github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q=

rolling-shutter/keyper/shutterevents/marshal.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"github.com/ethereum/go-ethereum/common"
1111
"github.com/ethereum/go-ethereum/common/hexutil"
1212
ethcrypto "github.com/ethereum/go-ethereum/crypto"
13-
bn256 "github.com/ethereum/go-ethereum/crypto/bn256/cloudflare"
13+
"github.com/ethereum/go-ethereum/crypto/bls12381"
1414
"github.com/ethereum/go-ethereum/crypto/ecies"
1515
"github.com/pkg/errors"
1616

@@ -96,28 +96,32 @@ func decodePubkey(val string) (*ecdsa.PublicKey, error) {
9696
}
9797

9898
func encodeGammas(gammas *shcrypto.Gammas) string {
99+
g2 := bls12381.NewG2()
99100
var encoded []string
100101
if gammas != nil {
101102
for _, g := range *gammas {
102-
encoded = append(encoded, hex.EncodeToString(g.Marshal()))
103+
encoded = append(encoded, hex.EncodeToString(g2.ToBytes(g)))
103104
}
104105
}
105106
return strings.Join(encoded, ",")
106107
}
107108

108109
func decodeGammas(eventValue string) (shcrypto.Gammas, error) {
110+
g2 := bls12381.NewG2()
109111
parts := strings.Split(eventValue, ",")
110112
var res shcrypto.Gammas
111113
for _, p := range parts {
112114
marshaledG2, err := hex.DecodeString(p)
113115
if err != nil {
114116
return shcrypto.Gammas{}, err
115117
}
116-
g := new(bn256.G2)
117-
_, err = g.Unmarshal(marshaledG2)
118+
g, err := g2.FromBytes(marshaledG2)
118119
if err != nil {
119120
return shcrypto.Gammas{}, err
120121
}
122+
if !g2.IsOnCurve(g) {
123+
return shcrypto.Gammas{}, errors.Errorf("invalid gamma value %x", p)
124+
}
121125
res = append(res, g)
122126
}
123127
return res, nil

rolling-shutter/mocknode/mocknode.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"sync"
99
"time"
1010

11-
bn256 "github.com/ethereum/go-ethereum/crypto/bn256/cloudflare"
1211
"github.com/pkg/errors"
1312
"github.com/rs/zerolog/log"
1413
txtypes "github.com/shutter-network/txtypes/types"
@@ -107,7 +106,7 @@ func (m *MockNode) handleEonPublicKey(
107106
if err := m.eonPublicKey.Unmarshal(key.PublicKey); err != nil {
108107
log.Info().Err(err).Msg("failed to unmarshal eon public key")
109108
}
110-
log.Info().Str("eon-public-key", (*bn256.G2)(m.eonPublicKey).String()).
109+
log.Info().Hex("eon-public-key", m.eonPublicKey.Marshal()).
111110
Msg("updated eon public key from messages to %s")
112111
return make([]p2pmsg.Message, 0), nil
113112
}

rolling-shutter/shmsg/messages.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/ethereum/go-ethereum/common"
77
"github.com/ethereum/go-ethereum/crypto"
8+
"github.com/ethereum/go-ethereum/crypto/bls12381"
89
"github.com/ethereum/go-ethereum/crypto/ecies"
910

1011
shcrypto "github.com/shutter-network/shutter/shlib/shcrypto"
@@ -79,9 +80,10 @@ func NewAccusation(eon uint64, accused []common.Address) *Message {
7980

8081
// NewPolyCommitment creates a new poly commitment message containing gamma values.
8182
func NewPolyCommitment(eon uint64, gammas *shcrypto.Gammas) *Message {
83+
g2 := bls12381.NewG2()
8284
gammaBytes := [][]byte{}
8385
for _, gamma := range *gammas {
84-
gammaBytes = append(gammaBytes, gamma.Marshal())
86+
gammaBytes = append(gammaBytes, g2.ToBytes(gamma))
8587
}
8688

8789
return &Message{

rolling-shutter/shmsg/messages_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"testing"
77

88
"github.com/ethereum/go-ethereum/common"
9+
"github.com/ethereum/go-ethereum/crypto/bls12381"
910
"gotest.tools/v3/assert"
1011

1112
shcrypto "github.com/shutter-network/shutter/shlib/shcrypto"
@@ -24,9 +25,10 @@ func TestNewPolyCommitmentMsg(t *testing.T) {
2425

2526
assert.Equal(t, eon, msg.Eon)
2627
assert.Equal(t, int(threshold)+1, len(msg.Gammas))
28+
g2 := bls12381.NewG2()
2729
for i := 0; i < int(threshold)+1; i++ {
2830
gammaBytes := msg.Gammas[i]
29-
assert.DeepEqual(t, gammaBytes, (*gammas)[i].Marshal())
31+
assert.DeepEqual(t, gammaBytes, g2.ToBytes((*gammas)[i]))
3032
}
3133
}
3234

0 commit comments

Comments
 (0)