|
@ -11,8 +11,10 @@ import ( |
|
|
ethCrypto "github.com/ethereum/go-ethereum/crypto" |
|
|
ethCrypto "github.com/ethereum/go-ethereum/crypto" |
|
|
"github.com/iden3/go-iden3-crypto/babyjub" |
|
|
"github.com/iden3/go-iden3-crypto/babyjub" |
|
|
cryptoConstants "github.com/iden3/go-iden3-crypto/constants" |
|
|
cryptoConstants "github.com/iden3/go-iden3-crypto/constants" |
|
|
|
|
|
"github.com/iden3/go-iden3-crypto/poseidon" |
|
|
cryptoUtils "github.com/iden3/go-iden3-crypto/utils" |
|
|
cryptoUtils "github.com/iden3/go-iden3-crypto/utils" |
|
|
"github.com/stretchr/testify/assert" |
|
|
"github.com/stretchr/testify/assert" |
|
|
|
|
|
"github.com/stretchr/testify/require" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
func TestIdxParser(t *testing.T) { |
|
|
func TestIdxParser(t *testing.T) { |
|
@ -85,7 +87,7 @@ func TestAccount(t *testing.T) { |
|
|
} |
|
|
} |
|
|
b, err := account.Bytes() |
|
|
b, err := account.Bytes() |
|
|
assert.Nil(t, err) |
|
|
assert.Nil(t, err) |
|
|
assert.Equal(t, byte(1), b[10]) |
|
|
|
|
|
|
|
|
assert.Equal(t, byte(1), b[22]) |
|
|
a1, err := AccountFromBytes(b) |
|
|
a1, err := AccountFromBytes(b) |
|
|
assert.Nil(t, err) |
|
|
assert.Nil(t, err) |
|
|
assert.Equal(t, account, a1) |
|
|
assert.Equal(t, account, a1) |
|
@ -99,7 +101,7 @@ func TestAccount(t *testing.T) { |
|
|
|
|
|
|
|
|
assert.Equal(t, "1000", e[1].String()) |
|
|
assert.Equal(t, "1000", e[1].String()) |
|
|
assert.Equal(t, pk.Y.String(), e[2].String()) |
|
|
assert.Equal(t, pk.Y.String(), e[2].String()) |
|
|
assert.Equal(t, new(big.Int).SetBytes(SwapEndianness(account.EthAddr.Bytes())).String(), e[3].String()) |
|
|
|
|
|
|
|
|
assert.Equal(t, new(big.Int).SetBytes(account.EthAddr.Bytes()).String(), e[3].String()) |
|
|
|
|
|
|
|
|
a2, err := AccountFromBigInts(e) |
|
|
a2, err := AccountFromBigInts(e) |
|
|
assert.Nil(t, err) |
|
|
assert.Nil(t, err) |
|
@ -108,7 +110,7 @@ func TestAccount(t *testing.T) { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func TestAccountLoop(t *testing.T) { |
|
|
func TestAccountLoop(t *testing.T) { |
|
|
// check that for different Address there is no problem
|
|
|
|
|
|
|
|
|
// check that for different deterministic BabyJubJub keys & random Address there is no problem
|
|
|
for i := 0; i < 256; i++ { |
|
|
for i := 0; i < 256; i++ { |
|
|
var sk babyjub.PrivateKey |
|
|
var sk babyjub.PrivateKey |
|
|
_, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001")) |
|
|
_, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001")) |
|
@ -145,6 +147,51 @@ func TestAccountLoop(t *testing.T) { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func TestAccountLoopRandom(t *testing.T) { |
|
|
|
|
|
// check that for different random Address & BabyJubJub keys there is
|
|
|
|
|
|
// no problem
|
|
|
|
|
|
for i := 0; i < 256; i++ { |
|
|
|
|
|
sk := babyjub.NewRandPrivKey() |
|
|
|
|
|
pk := sk.Public() |
|
|
|
|
|
|
|
|
|
|
|
key, err := ethCrypto.GenerateKey() |
|
|
|
|
|
assert.Nil(t, err) |
|
|
|
|
|
address := ethCrypto.PubkeyToAddress(key.PublicKey) |
|
|
|
|
|
|
|
|
|
|
|
account := &Account{ |
|
|
|
|
|
TokenID: TokenID(i), |
|
|
|
|
|
Nonce: Nonce(i), |
|
|
|
|
|
Balance: big.NewInt(1000), |
|
|
|
|
|
PublicKey: pk, |
|
|
|
|
|
EthAddr: address, |
|
|
|
|
|
} |
|
|
|
|
|
b, err := account.Bytes() |
|
|
|
|
|
assert.Nil(t, err) |
|
|
|
|
|
a1, err := AccountFromBytes(b) |
|
|
|
|
|
assert.Nil(t, err) |
|
|
|
|
|
assert.Equal(t, account, a1) |
|
|
|
|
|
|
|
|
|
|
|
e, err := account.BigInts() |
|
|
|
|
|
assert.Nil(t, err) |
|
|
|
|
|
assert.True(t, cryptoUtils.CheckBigIntInField(e[0])) |
|
|
|
|
|
assert.True(t, cryptoUtils.CheckBigIntInField(e[1])) |
|
|
|
|
|
assert.True(t, cryptoUtils.CheckBigIntInField(e[2])) |
|
|
|
|
|
assert.True(t, cryptoUtils.CheckBigIntInField(e[3])) |
|
|
|
|
|
|
|
|
|
|
|
a2, err := AccountFromBigInts(e) |
|
|
|
|
|
assert.Nil(t, err) |
|
|
|
|
|
assert.Equal(t, account, a2) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func bigFromStr(h string, u int) *big.Int { |
|
|
|
|
|
b, ok := new(big.Int).SetString(h, u) |
|
|
|
|
|
if !ok { |
|
|
|
|
|
panic("bigFromStr err") |
|
|
|
|
|
} |
|
|
|
|
|
return b |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
func TestAccountHashValue(t *testing.T) { |
|
|
func TestAccountHashValue(t *testing.T) { |
|
|
var sk babyjub.PrivateKey |
|
|
var sk babyjub.PrivateKey |
|
|
_, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001")) |
|
|
_, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001")) |
|
@ -158,10 +205,77 @@ func TestAccountHashValue(t *testing.T) { |
|
|
PublicKey: pk, |
|
|
PublicKey: pk, |
|
|
EthAddr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"), |
|
|
EthAddr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"), |
|
|
} |
|
|
} |
|
|
|
|
|
v, err := account.HashValue() |
|
|
|
|
|
assert.Nil(t, err) |
|
|
|
|
|
assert.Equal(t, "16297758255249203915951182296472515138555043617458222397753168518282206850764", v.String()) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func TestAccountHashValueTestVectors(t *testing.T) { |
|
|
|
|
|
// values from js test vectors
|
|
|
|
|
|
ay := new(big.Int).Sub(new(big.Int).Exp(big.NewInt(2), big.NewInt(253), nil), big.NewInt(1)) |
|
|
|
|
|
assert.Equal(t, "1fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", (hex.EncodeToString(ay.Bytes()))) |
|
|
|
|
|
bjj, err := babyjub.PointFromSignAndY(true, ay) |
|
|
|
|
|
require.Nil(t, err) |
|
|
|
|
|
|
|
|
|
|
|
account := &Account{ |
|
|
|
|
|
Idx: 1, |
|
|
|
|
|
TokenID: 0xFFFFFFFF, |
|
|
|
|
|
PublicKey: (*babyjub.PublicKey)(bjj), |
|
|
|
|
|
EthAddr: ethCommon.HexToAddress("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"), |
|
|
|
|
|
Nonce: Nonce(0xFFFFFFFFFF), |
|
|
|
|
|
Balance: bigFromStr("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 16), |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
e, err := account.BigInts() |
|
|
|
|
|
assert.Nil(t, err) |
|
|
|
|
|
assert.Equal(t, "9444732965739290427391", e[0].String()) |
|
|
|
|
|
assert.Equal(t, "6277101735386680763835789423207666416102355444464034512895", e[1].String()) |
|
|
|
|
|
assert.Equal(t, "14474011154664524427946373126085988481658748083205070504932198000989141204991", e[2].String()) |
|
|
|
|
|
assert.Equal(t, "1461501637330902918203684832716283019655932542975", e[3].String()) |
|
|
|
|
|
|
|
|
|
|
|
h, err := poseidon.Hash(e[:]) |
|
|
|
|
|
assert.Nil(t, err) |
|
|
|
|
|
assert.Equal(t, "4550823210217540218403400309533329186487982452461145263910122718498735057257", h.String()) |
|
|
|
|
|
|
|
|
v, err := account.HashValue() |
|
|
v, err := account.HashValue() |
|
|
assert.Nil(t, err) |
|
|
assert.Nil(t, err) |
|
|
assert.Equal(t, "9478468711598093334066833736294178928569163287501434518121324135729106649559", v.String()) |
|
|
|
|
|
|
|
|
assert.Equal(t, "4550823210217540218403400309533329186487982452461145263910122718498735057257", v.String()) |
|
|
|
|
|
|
|
|
|
|
|
// second account
|
|
|
|
|
|
ay = big.NewInt(0) |
|
|
|
|
|
bjj, err = babyjub.PointFromSignAndY(false, ay) |
|
|
|
|
|
require.Nil(t, err) |
|
|
|
|
|
account = &Account{ |
|
|
|
|
|
TokenID: 0, |
|
|
|
|
|
PublicKey: (*babyjub.PublicKey)(bjj), |
|
|
|
|
|
EthAddr: ethCommon.HexToAddress("0x00"), |
|
|
|
|
|
Nonce: Nonce(0), |
|
|
|
|
|
Balance: big.NewInt(0), |
|
|
|
|
|
} |
|
|
|
|
|
v, err = account.HashValue() |
|
|
|
|
|
assert.Nil(t, err) |
|
|
|
|
|
assert.Equal(t, "7750253361301235345986002241352365187241910378619330147114280396816709365657", v.String()) |
|
|
|
|
|
|
|
|
|
|
|
// third account
|
|
|
|
|
|
ay = bigFromStr("21b0a1688b37f77b1d1d5539ec3b826db5ac78b2513f574a04c50a7d4f8246d7", 16) |
|
|
|
|
|
bjj, err = babyjub.PointFromSignAndY(false, ay) |
|
|
|
|
|
require.Nil(t, err) |
|
|
|
|
|
account = &Account{ |
|
|
|
|
|
TokenID: 3, |
|
|
|
|
|
PublicKey: (*babyjub.PublicKey)(bjj), |
|
|
|
|
|
EthAddr: ethCommon.HexToAddress("0xA3C88ac39A76789437AED31B9608da72e1bbfBF9"), |
|
|
|
|
|
Nonce: Nonce(129), |
|
|
|
|
|
Balance: bigFromStr("42000000000000000000", 10), |
|
|
|
|
|
} |
|
|
|
|
|
e, err = account.BigInts() |
|
|
|
|
|
assert.Nil(t, err) |
|
|
|
|
|
assert.Equal(t, "554050781187", e[0].String()) |
|
|
|
|
|
assert.Equal(t, "42000000000000000000", e[1].String()) |
|
|
|
|
|
assert.Equal(t, "15238403086306505038849621710779816852318505119327426213168494964113886299863", e[2].String()) |
|
|
|
|
|
assert.Equal(t, "935037732739828347587684875151694054123613453305", e[3].String()) |
|
|
|
|
|
v, err = account.HashValue() |
|
|
|
|
|
assert.Nil(t, err) |
|
|
|
|
|
assert.Equal(t, "10565754214047872850889045989683221123564392137456000481397520902594455245517", v.String()) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func TestAccountErrNotInFF(t *testing.T) { |
|
|
func TestAccountErrNotInFF(t *testing.T) { |
|
@ -244,7 +358,7 @@ func TestAccountErrNumOverflowBalance(t *testing.T) { |
|
|
_, err = AccountFromBytes(b) |
|
|
_, err = AccountFromBytes(b) |
|
|
assert.Nil(t, err) |
|
|
assert.Nil(t, err) |
|
|
|
|
|
|
|
|
b[56] = 1 |
|
|
|
|
|
|
|
|
b[39] = 1 |
|
|
_, err = AccountFromBytes(b) |
|
|
_, err = AccountFromBytes(b) |
|
|
assert.NotNil(t, err) |
|
|
assert.NotNil(t, err) |
|
|
assert.Equal(t, fmt.Errorf("%s Balance", ErrNumOverflow), err) |
|
|
assert.Equal(t, fmt.Errorf("%s Balance", ErrNumOverflow), err) |
|
|