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

adding serialisation and deserialisation for u256 #600

Open
wants to merge 5 commits into
base: main
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions utils/Felt.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,44 @@ func feltToString(f *felt.Felt) (string, error) {
}
return string(b), nil
}

// U256ToFelt converts big int an array of Felt objects to represent U256 datat type
//
// Parameters:
// - num: big.Int
// Returns:
// - []*felt.Felt: the array of felt.Felt objects
// - error: an error, if any
func U256ToFelt(num *big.Int) ([]*felt.Felt, error) {
bytes := num.Bytes()
if len(bytes) > 32 {
return nil, fmt.Errorf("not a valid U256")
}

all := make([]byte, 32)
copy(all[32-len(bytes):], bytes[:])

least := new(felt.Felt).SetBytes(all[16:])
significant := new(felt.Felt).SetBytes(all[0:16])
return []*felt.Felt{least, significant}, nil
}

// FeltArrToU256 array of Felt objects that represents U256 data type to big.Int
//
// Parameters:
// - arr: []*felt.Felt
// Returns:
// - *big.Int: big.Int representation of U256
// - error: an error, if any
func FeltArrToU256(arr []*felt.Felt) (*big.Int, error) {
if len(arr) != 2 {
return nil, fmt.Errorf("not a valid felt array for U256 conversion")
}

significant := arr[1].Bytes()
least := arr[0].Bytes()
res := make([]byte, 32)
copy(res[0:16], significant[16:])
copy(res[16:], least[16:])
return new(big.Int).SetBytes(res), nil
}
65 changes: 65 additions & 0 deletions utils/Felt_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package utils

import (
"math/big"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -95,3 +96,67 @@ func TestByteArrFeltToString(t *testing.T) {
require.Equal(t, tc.out, res, "invalid conversion: output does not match")
}
}

func TestU256ToFelt(t *testing.T) {
b := new(big.Int).Exp(big.NewInt(2), big.NewInt(128), nil)
b1 := new(big.Int).Exp(big.NewInt(2), big.NewInt(129), nil)
b2 := b1.Add(b1, b).Add(b1, big.NewInt(20))

var tests = []struct {
in *big.Int
out []string
}{
{
in: big.NewInt(2),
out: []string{"0x2", "0x0"},
},
{
in: new(big.Int).Exp(big.NewInt(2), big.NewInt(128), nil),
out: []string{"0x0", "0x1"},
},
{
in: b2,
out: []string{"0x14", "0x3"},
},
}

for _, tc := range tests {
res, err := U256ToFelt(tc.in)
require.NoError(t, err, "error returned from U256ToFelt")
expected, err := HexArrToFelt(tc.out)
require.NoError(t, err, "error returned from HexArrToFelt")
require.Equal(t, expected, res, "invalid conversion: output does not match")
}
}

func TestFeltArrToU256(t *testing.T) {
b := new(big.Int).Exp(big.NewInt(2), big.NewInt(128), nil)
b1 := new(big.Int).Exp(big.NewInt(2), big.NewInt(129), nil)
b2 := b1.Add(b1, b).Add(b1, big.NewInt(20))

var tests = []struct {
in []string
out *big.Int
}{
{
in: []string{"0x2", "0x0"},
out: big.NewInt(2),
},
{
in: []string{"0x0", "0x1"},
out: new(big.Int).Exp(big.NewInt(2), big.NewInt(128), nil),
},
{
in: []string{"0x14", "0x3"},
out: b2,
},
}

for _, tc := range tests {
in, err := HexArrToFelt(tc.in)
require.NoError(t, err, "error returned from HexArrToFelt")
res, err := FeltArrToU256(in)
require.NoError(t, err, "error returned from U256ToFelt")
require.Equal(t, tc.out, res, "invalid conversion: output does not match")
}
}