Repository: NethermindEth/juno
Pull request #531 (2023-01-30 10:21:54)

Explicitly marshal Felt to CBOR
## Description

Since `val` is private, we should explicitly marshal that.

## Changes:

- add MarshalCBOR, UnmarshalCBOR to Felt type

## Types of changes

- New feature (non-breaking change which adds functionality)

## Testing

**Requires testing**: Yes

**Did you write tests??**: Yes


codecov[bot]: # [Codecov](https://codecov.io/gh/NethermindEth/juno/pull/531?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth) Report
Base: **70.49**% // Head: **70.80**% // Increases project coverage by **`+0.30%`** :tada:
> Coverage data is based on head [(`5850927`)](https://codecov.io/gh/NethermindEth/juno/pull/531?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth) compared to base [(`8e46794`)](https://codecov.io/gh/NethermindEth/juno/commit/8e467943d00e806ae0f5f97a38200fd478528984?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth).
> Patch coverage: 100.00% of modified lines in pull request are covered.

> :exclamation: Current head 5850927 differs from pull request most recent head 8d18177. Consider uploading reports for the commit 8d18177 to get more accurate results

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff             @@
##           develop     #531      +/-   ##
===========================================
+ Coverage    70.49%   70.80%   +0.30%     
===========================================
  Files           20       20              
  Lines         1366     1370       +4     
===========================================
+ Hits           963      970       +7     
+ Misses         316      313       -3     
  Partials        87       87              
```


| [Impacted Files](https://codecov.io/gh/NethermindEth/juno/pull/531?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth) | Coverage Δ | |
|---|---|---|
| [core/felt/felt.go](https://codecov.io/gh/NethermindEth/juno/pull/531?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth#diff-Y29yZS9mZWx0L2ZlbHQuZ28=) | `28.94% <100.00%> (+8.11%)` | :arrow_up: |

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth)

</details>

[:umbrella: View full report at Codecov](https://codecov.io/gh/NethermindEth/juno/pull/531?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth).   
:loudspeaker: Do you have feedback about the report comment? [Let us know in this issue](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth).

omerfirmak: @IronGauntlets , apparently there was a v2 of CBOR package and it does not need that `EncOptions` when marshaling. Updated to V2.

Patch:
diff --git a/core/felt/felt.go b/core/felt/felt.go
index 5a29cc98..52b1927e 100644
--- a/core/felt/felt.go
+++ b/core/felt/felt.go
@@ -5,6 +5,8 @@ import (
 	"math/big"
 	"sync"
 
+	"github.com/fxamacker/cbor/v2"
+
 	"github.com/consensys/gnark-crypto/ecc/stark-curve/fp"
 )
 
@@ -165,3 +167,13 @@ func (z *Felt) Add(x, y *Felt) *Felt {
 func (z *Felt) Halve() {
 	z.val.Halve()
 }
+
+// MarshalCBOR lets Felt be encoded in CBOR format with private `val`
+func (z *Felt) MarshalCBOR() ([]byte, error) {
+	return cbor.Marshal(z.val)
+}
+
+// UnmarshalCBOR lets Felt be decoded from CBOR format with private `val`
+func (z *Felt) UnmarshalCBOR(data []byte) error {
+	return cbor.Unmarshal(data, &z.val)
+}
diff --git a/go.mod b/go.mod
index b4111294..db706d46 100644
--- a/go.mod
+++ b/go.mod
@@ -7,6 +7,7 @@ require (
 	github.com/consensys/gnark-crypto v0.8.1-0.20221209144717-f585adf5f61e
 	github.com/dgraph-io/badger/v3 v3.2103.4
 	github.com/ethereum/go-ethereum v1.10.26
+	github.com/fxamacker/cbor/v2 v2.4.0
 	github.com/spf13/cobra v1.5.0
 	github.com/spf13/viper v1.12.0
 	github.com/stretchr/testify v1.8.0
@@ -44,6 +45,7 @@ require (
 	github.com/spf13/jwalterweatherman v1.1.0 // indirect
 	github.com/spf13/pflag v1.0.5 // indirect
 	github.com/subosito/gotenv v1.3.0 // indirect
+	github.com/x448/float16 v0.8.4 // indirect
 	go.opencensus.io v0.23.0 // indirect
 	golang.org/x/net v0.0.0-20220607020251-c690dde0001d // indirect
 	golang.org/x/sys v0.2.0 // indirect
diff --git a/go.sum b/go.sum
index 154a1c2d..f2da45ef 100644
--- a/go.sum
+++ b/go.sum
@@ -87,6 +87,8 @@ github.com/frankban/quicktest v1.14.3 h1:FJKSZTDHjyhriyC81FLQ0LY93eSai0ZyR/ZIkd3
 github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
 github.com/fsnotify/fsnotify v1.5.4 h1:jRbGcIw6P2Meqdwuo0H1p6JVLbL5DHKAKlYndzMwVZI=
 github.com/fsnotify/fsnotify v1.5.4/go.mod h1:OVB6XrOHzAwXMpEM7uPOzcehqUV2UqJxmVXmkdnm1bU=
+github.com/fxamacker/cbor/v2 v2.4.0 h1:ri0ArlOR+5XunOP8CRUowT0pSJOwhW098ZCUyskZD88=
+github.com/fxamacker/cbor/v2 v2.4.0/go.mod h1:TA1xS00nchWmaBnEIxPSE5oHLuJBAVvqrtAnWBwBCVo=
 github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
 github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
 github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
@@ -241,6 +243,8 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
 github.com/subosito/gotenv v1.3.0 h1:mjC+YW8QpAdXibNi+vNWgzmgBH4+5l5dCXv8cNysBLI=
 github.com/subosito/gotenv v1.3.0/go.mod h1:YzJjq/33h7nrwdY+iHMhEOEEbW0ovIz0tB6t6PwAXzs=
 github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
+github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
+github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
 github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
 github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
 github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=


Test patch:
diff --git a/core/felt/felt_test.go b/core/felt/felt_test.go
index 34946344..f3573d6a 100644
--- a/core/felt/felt_test.go
+++ b/core/felt/felt_test.go
@@ -3,6 +3,8 @@ package felt
 import (
 	"testing"
 
+	"github.com/fxamacker/cbor/v2"
+
 	"github.com/stretchr/testify/assert"
 )
 
@@ -14,3 +16,16 @@ func TestUnmarshalJson(t *testing.T) {
 	assert.NoError(t, without.UnmarshalJSON([]byte("4437ab")))
 	assert.Equal(t, true, without.Equal(&with))
 }
+
+func TestFeltCbor(t *testing.T) {
+	var val Felt
+	_, err := val.SetRandom()
+	assert.NoError(t, err)
+
+	bytes, err := cbor.Marshal(val)
+	assert.NoError(t, err)
+
+	var unmarshaledFelt Felt
+	assert.NoError(t, cbor.Unmarshal(bytes, &unmarshaledFelt))
+	assert.Equal(t, val, unmarshaledFelt)
+}