Skip to content

Commit 59bb895

Browse files
committed
Update shcrypto
1 parent a436a7b commit 59bb895

File tree

7 files changed

+29
-50
lines changed

7 files changed

+29
-50
lines changed

rolling-shutter/app/messages.go

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

66
"github.com/ethereum/go-ethereum/common"
7-
"github.com/ethereum/go-ethereum/crypto/bls12381"
87
"github.com/pkg/errors"
8+
blst "github.com/supranational/blst/bindings/go"
99

1010
"github.com/shutter-network/shutter/shlib/shcrypto"
1111

@@ -54,14 +54,14 @@ 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()
5857
gammas := shcrypto.Gammas{}
5958
for _, g := range msg.Gammas {
60-
p, err := g2.FromBytes(g)
61-
if err != nil {
62-
return nil, err
59+
p := new(blst.P2Affine)
60+
p = p.Uncompress(g)
61+
if p == nil {
62+
return nil, errors.Errorf("invalid gamma value %x", g)
6363
}
64-
if !g2.IsOnCurve(p) {
64+
if !p.InG2() {
6565
return nil, errors.Errorf("invalid gamma value %x", g)
6666
}
6767
gammas = append(gammas, p)

rolling-shutter/cmd/cryptocmd/jsontests.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
"os"
1111

1212
"github.com/ethereum/go-ethereum/common/hexutil"
13-
"github.com/ethereum/go-ethereum/crypto/bls12381"
1413
"github.com/spf13/cobra"
14+
blst "github.com/supranational/blst/bindings/go"
1515

1616
"github.com/shutter-network/shutter/shlib/shcrypto"
1717

@@ -452,14 +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-
g2 := bls12381.NewG2()
456-
var c1 *bls12381.PointG2
455+
c1 := new(blst.P2)
457456
var err error
458457

459458
for i := 1; i <= 10000; i++ {
460-
c1 = et.Expected.C1
461-
g2.Add(c1, c1, c1)
462-
et.Expected.C1 = c1
459+
c1.FromAffine(et.Expected.C1)
460+
c1.AddAssign(c1)
461+
et.Expected.C1 = c1.ToAffine()
463462
sigma := et.Expected.Sigma(decryptionKey)
464463
decryptedBlocks := shcrypto.DecryptBlocks(et.Expected.C3, sigma)
465464
_, err = shcrypto.UnpadMessage(decryptedBlocks)

rolling-shutter/go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ require (
3333
github.com/rs/zerolog v1.28.0
3434
github.com/shutter-network/gnosh-contracts v0.2.0
3535
github.com/shutter-network/shop-contracts v0.0.0-20240407151512-08ef5d8355b6
36-
github.com/shutter-network/shutter/shlib v0.1.18
36+
github.com/shutter-network/shutter/shlib v0.1.19
3737
github.com/shutter-network/txtypes v0.1.0
3838
github.com/spf13/afero v1.8.2
3939
github.com/spf13/cobra v1.6.1
4040
github.com/spf13/pflag v1.0.5
4141
github.com/spf13/viper v1.13.0
4242
github.com/stretchr/testify v1.9.0
43-
github.com/supranational/blst v0.3.11
43+
github.com/supranational/blst v0.3.12
4444
github.com/tendermint/go-amino v0.16.0
4545
github.com/tendermint/tendermint v0.37.0-rc2
4646
go.opentelemetry.io/otel v1.27.0

rolling-shutter/go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -839,8 +839,8 @@ github.com/shutter-network/gnosh-contracts v0.2.0 h1:qH3gAhlh5VZzvJcbi044lxFWQ+M
839839
github.com/shutter-network/gnosh-contracts v0.2.0/go.mod h1:QB0d64ybbVFKMrLjrc1tldri87KNjTmKQjhk9jaso2E=
840840
github.com/shutter-network/shop-contracts v0.0.0-20240407151512-08ef5d8355b6 h1:m6Ti1/IH+GBTtGqyAX3xbh+ruUKvC+m+/uzYDUa+JDQ=
841841
github.com/shutter-network/shop-contracts v0.0.0-20240407151512-08ef5d8355b6/go.mod h1:LEWXLRruvxq9fe2oKtJI3xfzbauhfWTjOczHN61RU+4=
842-
github.com/shutter-network/shutter/shlib v0.1.18 h1:ei1EWEavnlkwbX51aGKtgt7NydY0IPNV35J525vAfeo=
843-
github.com/shutter-network/shutter/shlib v0.1.18/go.mod h1:RlYNZjx+pfKAi0arH+jfdlxG4kQ75UFzDfVjgCVYaUw=
842+
github.com/shutter-network/shutter/shlib v0.1.19 h1:Fm/PnxAapl5UK4JuJyF2wBh3dYB5ESztnNFcbmzNNOI=
843+
github.com/shutter-network/shutter/shlib v0.1.19/go.mod h1:miY10OJ0FKjLZTwvmG0AJNijsjsNDAk6l1tROTj8uzU=
844844
github.com/shutter-network/txtypes v0.1.0 h1:QqdiiiB9AiBCSJ/ke6z1ZoDGfu2+1Lgpz5vHzVN4FKc=
845845
github.com/shutter-network/txtypes v0.1.0/go.mod h1:gaLQWfEFH+3q5CmM3kkSgS+NspclEs8SCnj8QAozsaI=
846846
github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q=
@@ -899,8 +899,8 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
899899
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
900900
github.com/subosito/gotenv v1.4.2 h1:X1TuBLAMDFbaTAChgCBLu3DU3UPyELpnF2jjJ2cz/S8=
901901
github.com/subosito/gotenv v1.4.2/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNGqEflhK0=
902-
github.com/supranational/blst v0.3.11 h1:LyU6FolezeWAhvQk0k6O/d49jqgO52MSDDfYgbeoEm4=
903-
github.com/supranational/blst v0.3.11/go.mod h1:jZJtfjgudtNl4en1tzwPIV3KjUnQUvG3/j+w+fVonLw=
902+
github.com/supranational/blst v0.3.12 h1:Vfas2U2CFHhniv2QkUm2OVa1+pGTdqtpqm9NnhUUbZ8=
903+
github.com/supranational/blst v0.3.12/go.mod h1:jZJtfjgudtNl4en1tzwPIV3KjUnQUvG3/j+w+fVonLw=
904904
github.com/syndtr/goleveldb v1.0.1-0.20220614013038-64ee5596c38a h1:1ur3QoCqvE5fl+nylMaIr9PVV1w343YRDtsy+Rwu7XI=
905905
github.com/syndtr/goleveldb v1.0.1-0.20220614013038-64ee5596c38a/go.mod h1:RRCYJbIwD5jmqPI9XoAFR0OcDxqUctll6zUj/+B4S48=
906906
github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA=

rolling-shutter/keyper/shutterevents/marshal.go

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ 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-
"github.com/ethereum/go-ethereum/crypto/bls12381"
1413
"github.com/ethereum/go-ethereum/crypto/ecies"
1514
"github.com/pkg/errors"
1615

@@ -96,35 +95,20 @@ func decodePubkey(val string) (*ecdsa.PublicKey, error) {
9695
}
9796

9897
func encodeGammas(gammas *shcrypto.Gammas) string {
99-
g2 := bls12381.NewG2()
100-
var encoded []string
101-
if gammas != nil {
102-
for _, g := range *gammas {
103-
encoded = append(encoded, hex.EncodeToString(g2.ToBytes(g)))
104-
}
105-
}
106-
return strings.Join(encoded, ",")
98+
gammasBytes := gammas.Marshal()
99+
return hex.EncodeToString(gammasBytes)
107100
}
108101

109102
func decodeGammas(eventValue string) (shcrypto.Gammas, error) {
110-
g2 := bls12381.NewG2()
111-
parts := strings.Split(eventValue, ",")
112-
var res shcrypto.Gammas
113-
for _, p := range parts {
114-
marshaledG2, err := hex.DecodeString(p)
115-
if err != nil {
116-
return shcrypto.Gammas{}, err
117-
}
118-
g, err := g2.FromBytes(marshaledG2)
119-
if err != nil {
120-
return shcrypto.Gammas{}, err
121-
}
122-
if !g2.IsOnCurve(g) {
123-
return shcrypto.Gammas{}, errors.Errorf("invalid gamma value %x", p)
124-
}
125-
res = append(res, g)
103+
gammasBytes, err := hex.DecodeString(eventValue)
104+
if err != nil {
105+
return shcrypto.Gammas{}, errors.Wrapf(err, "failed to decode gammas")
126106
}
127-
return res, nil
107+
gammas := shcrypto.Gammas{}
108+
if err := gammas.Unmarshal(gammasBytes); err != nil {
109+
return shcrypto.Gammas{}, errors.Wrap(err, "failed to unmarshal gammas")
110+
}
111+
return gammas, nil
128112
}
129113

130114
func encodeAddress(a common.Address) string {

rolling-shutter/shmsg/messages.go

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

66
"github.com/ethereum/go-ethereum/common"
77
"github.com/ethereum/go-ethereum/crypto"
8-
"github.com/ethereum/go-ethereum/crypto/bls12381"
98
"github.com/ethereum/go-ethereum/crypto/ecies"
109

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

8180
// NewPolyCommitment creates a new poly commitment message containing gamma values.
8281
func NewPolyCommitment(eon uint64, gammas *shcrypto.Gammas) *Message {
83-
g2 := bls12381.NewG2()
8482
gammaBytes := [][]byte{}
8583
for _, gamma := range *gammas {
86-
gammaBytes = append(gammaBytes, g2.ToBytes(gamma))
84+
gammaBytes = append(gammaBytes, gamma.Compress())
8785
}
8886

8987
return &Message{

rolling-shutter/shmsg/messages_test.go

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

88
"github.com/ethereum/go-ethereum/common"
9-
"github.com/ethereum/go-ethereum/crypto/bls12381"
109
"gotest.tools/v3/assert"
1110

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

2625
assert.Equal(t, eon, msg.Eon)
2726
assert.Equal(t, int(threshold)+1, len(msg.Gammas))
28-
g2 := bls12381.NewG2()
2927
for i := 0; i < int(threshold)+1; i++ {
3028
gammaBytes := msg.Gammas[i]
31-
assert.DeepEqual(t, gammaBytes, g2.ToBytes((*gammas)[i]))
29+
assert.DeepEqual(t, gammaBytes, (*gammas)[i].Compress())
3230
}
3331
}
3432

0 commit comments

Comments
 (0)