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

arbo: add blake3 HashFunction #1396

Merged
merged 2 commits into from
Oct 18, 2024
Merged
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
4 changes: 2 additions & 2 deletions censustree/censustree.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,12 @@ func (t *Tree) Hash(data []byte) ([]byte, error) {

// BytesToBigInt unmarshals a slice of bytes into a bigInt following the censusTree encoding rules
func (*Tree) BytesToBigInt(data []byte) *big.Int {
return arbo.BytesToBigInt(data)
return arbo.BytesLEToBigInt(data)
}

// BigIntToBytes marshals a bigInt following the censusTree encoding rules
func (t *Tree) BigIntToBytes(b *big.Int) []byte {
return arbo.BigIntToBytes(t.hashLen, b)
return arbo.BigIntToBytesLE(t.hashLen, b)
}

// FromRoot returns a new read-only Tree for the given root, that uses the same
Expand Down
2 changes: 1 addition & 1 deletion cmd/tools/censusdump/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func main() {
}

if err := censusRef.Tree().IterateLeaves(func(key, value []byte) bool {
balance := arbo.BytesToBigInt(value)
balance := arbo.BytesLEToBigInt(value)
census.Data = append(census.Data, struct {
Address common.Address `json:"address"`
Balance string `json:"balance"`
Expand Down
4 changes: 2 additions & 2 deletions crypto/ethereum/vocdoni_sik.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ func (k *SignKeys) AccountSIK(secret []byte) ([]byte, error) {
return nil, fmt.Errorf("error signing sik payload: %w", err)
}
seed := []*big.Int{
arbo.BytesToBigInt(k.Address().Bytes()),
arbo.BytesLEToBigInt(k.Address().Bytes()),
util.BigToFF(new(big.Int).SetBytes(secret)),
util.BigToFF(new(big.Int).SetBytes(sign)),
}
hash, err := poseidon.Hash(seed)
if err != nil {
return nil, err
}
return arbo.BigIntToBytes(arbo.HashFunctionPoseidon.Len(), hash), nil
return arbo.BigIntToBytesLE(arbo.HashFunctionPoseidon.Len(), hash), nil
}

// AccountSIKnullifier method composes the nullifier of the current SignKeys
Expand Down
6 changes: 3 additions & 3 deletions crypto/zk/circuit/inputs.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ func GenerateCircuitInput(p CircuitInputsParameters) (*CircuitInputs, error) {
Nullifier: new(big.Int).SetBytes(nullifier).String(),
AvailableWeight: p.AvailableWeight.String(),
VoteHash: util.BytesToArboSplitStr(p.VotePackage),
SIKRoot: arbo.BytesToBigInt(p.SIKRoot).String(),
CensusRoot: arbo.BytesToBigInt(p.CensusRoot).String(),
SIKRoot: arbo.BytesLEToBigInt(p.SIKRoot).String(),
CensusRoot: arbo.BytesLEToBigInt(p.CensusRoot).String(),

Address: arbo.BytesToBigInt(p.Account.Address().Bytes()).String(),
Address: arbo.BytesLEToBigInt(p.Account.Address().Bytes()).String(),
Password: ffPassword.String(),
Signature: util.BigToFF(new(big.Int).SetBytes(signature)).String(),

Expand Down
2 changes: 1 addition & 1 deletion crypto/zk/circuit/inputs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func TestGenerateCircuitInput(t *testing.T) {
SIKRoot: "7714269703880573582519379213888374390024853519732158909852028066903886590497",
CensusRoot: "7714269703880573582519379213888374390024853519732158909852028066903886590497",

Address: arbo.BytesToBigInt(acc.Address().Bytes()).String(),
Address: arbo.BytesLEToBigInt(acc.Address().Bytes()).String(),
Password: "0",
Signature: util.BigToFF(new(big.Int).SetBytes(signature)).String(),

Expand Down
4 changes: 2 additions & 2 deletions crypto/zk/prover/pubsignals.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (p *Proof) CensusRoot() ([]byte, error) {
if err != nil {
return nil, err
}
return arbo.BigIntToBytes(arbo.HashFunctionPoseidon.Len(), bi), nil
return arbo.BigIntToBytesLE(arbo.HashFunctionPoseidon.Len(), bi), nil
}

// VoteWeight returns the VoteWeight included into the current proof.
Expand Down Expand Up @@ -108,5 +108,5 @@ func (p *Proof) SIKRoot() ([]byte, error) {
if err != nil {
return nil, err
}
return arbo.BigIntToBytes(arbo.HashFunctionPoseidon.Len(), bi), nil
return arbo.BigIntToBytesLE(arbo.HashFunctionPoseidon.Len(), bi), nil
}
16 changes: 8 additions & 8 deletions crypto/zk/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,20 +93,20 @@ func ProverProofToProtobufZKProof(p *prover.Proof, electionId, sikRoot,
func zkProofPublicInputs(electionId, sikRoot, censusRoot, nullifier types.HexBytes, voteWeight *big.Int) []string {
pubInputs := []string{}
// 0. electionId[0]
pubInputs = append(pubInputs, arbo.BytesToBigInt(electionId[:16]).String())
pubInputs = append(pubInputs, arbo.BytesLEToBigInt(electionId[:16]).String())
// 1. electionId[1]
pubInputs = append(pubInputs, arbo.BytesToBigInt(electionId[16:]).String())
pubInputs = append(pubInputs, arbo.BytesLEToBigInt(electionId[16:]).String())
// 2. nullifier
pubInputs = append(pubInputs, arbo.BytesToBigInt(nullifier).String())
pubInputs = append(pubInputs, arbo.BytesLEToBigInt(nullifier).String())
voteHash := sha256.Sum256(voteWeight.Bytes())
// 3. voteHash[0]
pubInputs = append(pubInputs, arbo.BytesToBigInt(voteHash[:16]).String())
pubInputs = append(pubInputs, arbo.BytesLEToBigInt(voteHash[:16]).String())
// 4. voteHash[1]
pubInputs = append(pubInputs, arbo.BytesToBigInt(voteHash[16:]).String())
pubInputs = append(pubInputs, arbo.BytesLEToBigInt(voteHash[16:]).String())
// 5. sikRoot
pubInputs = append(pubInputs, arbo.BytesToBigInt(sikRoot).String())
pubInputs = append(pubInputs, arbo.BytesLEToBigInt(sikRoot).String())
// 6. censusRoot
pubInputs = append(pubInputs, arbo.BytesToBigInt(censusRoot).String())
pubInputs = append(pubInputs, arbo.BytesLEToBigInt(censusRoot).String())
// 7. availableWeight
pubInputs = append(pubInputs, voteWeight.String())

Expand All @@ -133,7 +133,7 @@ func ProofToCircomSiblings(proof []byte) ([]string, error) {
siblings := make([]string, censustree.DefaultMaxLevels+1)
for i := 0; i < len(siblings); i++ {
if i < len(rawSiblings) {
siblings[i] = arbo.BytesToBigInt(rawSiblings[i]).String()
siblings[i] = arbo.BytesLEToBigInt(rawSiblings[i]).String()
} else {
siblings[i] = "0"
}
Expand Down
2 changes: 1 addition & 1 deletion test/testcommon/testutil/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (r *Random) RandomInZKField() []byte {
panic(err)
}
b[31] &= 0b00111111
if iden3cryptoutils.CheckBigIntInField(arbo.BytesToBigInt(b)) {
if iden3cryptoutils.CheckBigIntInField(arbo.BytesLEToBigInt(b)) {
return b
}
}
Expand Down
102 changes: 51 additions & 51 deletions tree/arbo/addbatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ func testInit(c *qt.C, n int) (*Tree, *Tree) {
// add the initial leafs to fill a bit the trees before calling the
// AddBatch method
for i := 0; i < n; i++ {
k := BigIntToBytes(bLen, big.NewInt(int64(i)))
v := BigIntToBytes(bLen, big.NewInt(int64(i*2)))
k := BigIntToBytesLE(bLen, big.NewInt(int64(i)))
v := BigIntToBytesLE(bLen, big.NewInt(int64(i*2)))
if err := tree1.Add(k, v); err != nil {
c.Fatal(err)
}
Expand All @@ -80,8 +80,8 @@ func TestAddBatchTreeEmpty(t *testing.T) {
bLen := 32
var keys, values [][]byte
for i := 0; i < nLeafs; i++ {
k := BigIntToBytes(bLen, big.NewInt(int64(i)))
v := BigIntToBytes(bLen, big.NewInt(int64(i*2)))
k := BigIntToBytesLE(bLen, big.NewInt(int64(i)))
v := BigIntToBytesLE(bLen, big.NewInt(int64(i*2)))
keys = append(keys, k)
values = append(values, v)
}
Expand Down Expand Up @@ -128,8 +128,8 @@ func TestAddBatchTreeEmptyNotPowerOf2(t *testing.T) {

bLen := 32
for i := 0; i < nLeafs; i++ {
k := BigIntToBytes(bLen, big.NewInt(int64(i)))
v := BigIntToBytes(bLen, big.NewInt(int64(i*2)))
k := BigIntToBytesLE(bLen, big.NewInt(int64(i)))
v := BigIntToBytesLE(bLen, big.NewInt(int64(i*2)))
if err := tree.Add(k, v); err != nil {
t.Fatal(err)
}
Expand All @@ -144,8 +144,8 @@ func TestAddBatchTreeEmptyNotPowerOf2(t *testing.T) {

var keys, values [][]byte
for i := 0; i < nLeafs; i++ {
k := BigIntToBytes(bLen, big.NewInt(int64(i)))
v := BigIntToBytes(bLen, big.NewInt(int64(i*2)))
k := BigIntToBytesLE(bLen, big.NewInt(int64(i)))
v := BigIntToBytesLE(bLen, big.NewInt(int64(i*2)))
keys = append(keys, k)
values = append(values, v)
}
Expand Down Expand Up @@ -277,17 +277,17 @@ func TestAddBatchTestVector2(t *testing.T) {
bLen := tree1.HashFunction().Len()
var keys, values [][]byte
// 1
keys = append(keys, BigIntToBytes(bLen, big.NewInt(int64(1))))
values = append(values, BigIntToBytes(bLen, big.NewInt(int64(1))))
keys = append(keys, BigIntToBytesLE(bLen, big.NewInt(int64(1))))
values = append(values, BigIntToBytesLE(bLen, big.NewInt(int64(1))))
// 2
keys = append(keys, BigIntToBytes(bLen, big.NewInt(int64(2))))
values = append(values, BigIntToBytes(bLen, big.NewInt(int64(2))))
keys = append(keys, BigIntToBytesLE(bLen, big.NewInt(int64(2))))
values = append(values, BigIntToBytesLE(bLen, big.NewInt(int64(2))))
// 3
keys = append(keys, BigIntToBytes(bLen, big.NewInt(int64(3))))
values = append(values, BigIntToBytes(bLen, big.NewInt(int64(3))))
keys = append(keys, BigIntToBytesLE(bLen, big.NewInt(int64(3))))
values = append(values, BigIntToBytesLE(bLen, big.NewInt(int64(3))))
// 5
keys = append(keys, BigIntToBytes(bLen, big.NewInt(int64(5))))
values = append(values, BigIntToBytes(bLen, big.NewInt(int64(5))))
keys = append(keys, BigIntToBytesLE(bLen, big.NewInt(int64(5))))
values = append(values, BigIntToBytesLE(bLen, big.NewInt(int64(5))))

for i := 0; i < len(keys); i++ {
if err := tree1.Add(keys[i], values[i]); err != nil {
Expand Down Expand Up @@ -324,17 +324,17 @@ func TestAddBatchTestVector3(t *testing.T) {
bLen := tree1.HashFunction().Len()
var keys, values [][]byte
// 0
keys = append(keys, BigIntToBytes(bLen, big.NewInt(int64(0))))
values = append(values, BigIntToBytes(bLen, big.NewInt(int64(0))))
keys = append(keys, BigIntToBytesLE(bLen, big.NewInt(int64(0))))
values = append(values, BigIntToBytesLE(bLen, big.NewInt(int64(0))))
// 3
keys = append(keys, BigIntToBytes(bLen, big.NewInt(int64(3))))
values = append(values, BigIntToBytes(bLen, big.NewInt(int64(3))))
keys = append(keys, BigIntToBytesLE(bLen, big.NewInt(int64(3))))
values = append(values, BigIntToBytesLE(bLen, big.NewInt(int64(3))))
// 7
keys = append(keys, BigIntToBytes(bLen, big.NewInt(int64(7))))
values = append(values, BigIntToBytes(bLen, big.NewInt(int64(7))))
keys = append(keys, BigIntToBytesLE(bLen, big.NewInt(int64(7))))
values = append(values, BigIntToBytesLE(bLen, big.NewInt(int64(7))))
// 135
keys = append(keys, BigIntToBytes(bLen, big.NewInt(int64(135))))
values = append(values, BigIntToBytes(bLen, big.NewInt(int64(135))))
keys = append(keys, BigIntToBytesLE(bLen, big.NewInt(int64(135))))
values = append(values, BigIntToBytesLE(bLen, big.NewInt(int64(135))))

for i := 0; i < len(keys); i++ {
if err := tree1.Add(keys[i], values[i]); err != nil {
Expand Down Expand Up @@ -403,8 +403,8 @@ func TestAddBatchTreeNotEmptyFewLeafs(t *testing.T) {
bLen := tree1.HashFunction().Len()
start := time.Now()
for i := initialNLeafs; i < nLeafs; i++ {
k := BigIntToBytes(bLen, big.NewInt(int64(i)))
v := BigIntToBytes(bLen, big.NewInt(int64(i*2)))
k := BigIntToBytesLE(bLen, big.NewInt(int64(i)))
v := BigIntToBytesLE(bLen, big.NewInt(int64(i*2)))
if err := tree1.Add(k, v); err != nil {
t.Fatal(err)
}
Expand All @@ -414,8 +414,8 @@ func TestAddBatchTreeNotEmptyFewLeafs(t *testing.T) {
// prepare the key-values to be added
var keys, values [][]byte
for i := initialNLeafs; i < nLeafs; i++ {
k := BigIntToBytes(bLen, big.NewInt(int64(i)))
v := BigIntToBytes(bLen, big.NewInt(int64(i*2)))
k := BigIntToBytesLE(bLen, big.NewInt(int64(i)))
v := BigIntToBytesLE(bLen, big.NewInt(int64(i*2)))
keys = append(keys, k)
values = append(values, v)
}
Expand Down Expand Up @@ -446,8 +446,8 @@ func TestAddBatchTreeNotEmptyEnoughLeafs(t *testing.T) {
bLen := tree1.HashFunction().Len()
start := time.Now()
for i := initialNLeafs; i < nLeafs; i++ {
k := BigIntToBytes(bLen, big.NewInt(int64(i)))
v := BigIntToBytes(bLen, big.NewInt(int64(i*2)))
k := BigIntToBytesLE(bLen, big.NewInt(int64(i)))
v := BigIntToBytesLE(bLen, big.NewInt(int64(i*2)))
if err := tree1.Add(k, v); err != nil {
t.Fatal(err)
}
Expand All @@ -457,8 +457,8 @@ func TestAddBatchTreeNotEmptyEnoughLeafs(t *testing.T) {
// prepare the key-values to be added
var keys, values [][]byte
for i := initialNLeafs; i < nLeafs; i++ {
k := BigIntToBytes(bLen, big.NewInt(int64(i)))
v := BigIntToBytes(bLen, big.NewInt(int64(i*2)))
k := BigIntToBytesLE(bLen, big.NewInt(int64(i)))
v := BigIntToBytesLE(bLen, big.NewInt(int64(i*2)))
keys = append(keys, k)
values = append(values, v)
}
Expand Down Expand Up @@ -488,15 +488,15 @@ func TestAddBatchTreeEmptyRepeatedLeafs(t *testing.T) {
// prepare the key-values to be added
var keys, values [][]byte
for i := 0; i < nLeafs; i++ {
k := BigIntToBytes(bLen, big.NewInt(int64(i)))
v := BigIntToBytes(bLen, big.NewInt(int64(i*2)))
k := BigIntToBytesLE(bLen, big.NewInt(int64(i)))
v := BigIntToBytesLE(bLen, big.NewInt(int64(i*2)))
keys = append(keys, k)
values = append(values, v)
}
// add repeated key-values
for i := 0; i < nRepeatedKeys; i++ {
k := BigIntToBytes(bLen, big.NewInt(int64(i)))
v := BigIntToBytes(bLen, big.NewInt(int64(i*2)))
k := BigIntToBytesLE(bLen, big.NewInt(int64(i)))
v := BigIntToBytesLE(bLen, big.NewInt(int64(i*2)))
keys = append(keys, k)
values = append(values, v)
}
Expand Down Expand Up @@ -527,8 +527,8 @@ func TestAddBatchTreeNotEmptyFewLeafsRepeatedLeafs(t *testing.T) {
// prepare the key-values to be added
var keys, values [][]byte
for i := 0; i < nLeafs; i++ {
k := BigIntToBytes(bLen, big.NewInt(int64(i)))
v := BigIntToBytes(bLen, big.NewInt(int64(i*2)))
k := BigIntToBytesLE(bLen, big.NewInt(int64(i)))
v := BigIntToBytesLE(bLen, big.NewInt(int64(i*2)))
keys = append(keys, k)
values = append(values, v)
}
Expand All @@ -554,8 +554,8 @@ func TestSplitInBuckets(t *testing.T) {
nLeafs := 16
kvs := make([]kv, nLeafs)
for i := 0; i < nLeafs; i++ {
k := BigIntToBytes(bLen, big.NewInt(int64(i)))
v := BigIntToBytes(bLen, big.NewInt(int64(i*2)))
k := BigIntToBytesLE(bLen, big.NewInt(int64(i)))
v := BigIntToBytesLE(bLen, big.NewInt(int64(i*2)))
keyPath := make([]byte, 32)
copy(keyPath, k)
kvs[i].pos = i
Expand Down Expand Up @@ -660,8 +660,8 @@ func TestAddBatchTreeNotEmpty(t *testing.T) {
bLen := tree1.HashFunction().Len()
start := time.Now()
for i := initialNLeafs; i < nLeafs; i++ {
k := BigIntToBytes(bLen, big.NewInt(int64(i)))
v := BigIntToBytes(bLen, big.NewInt(int64(i*2)))
k := BigIntToBytesLE(bLen, big.NewInt(int64(i)))
v := BigIntToBytesLE(bLen, big.NewInt(int64(i*2)))
if err := tree1.Add(k, v); err != nil {
t.Fatal(err)
}
Expand All @@ -671,8 +671,8 @@ func TestAddBatchTreeNotEmpty(t *testing.T) {
// prepare the key-values to be added
var keys, values [][]byte
for i := initialNLeafs; i < nLeafs; i++ {
k := BigIntToBytes(bLen, big.NewInt(int64(i)))
v := BigIntToBytes(bLen, big.NewInt(int64(i*2)))
k := BigIntToBytesLE(bLen, big.NewInt(int64(i)))
v := BigIntToBytesLE(bLen, big.NewInt(int64(i*2)))
keys = append(keys, k)
values = append(values, v)
}
Expand Down Expand Up @@ -702,8 +702,8 @@ func TestAddBatchNotEmptyUnbalanced(t *testing.T) {

start := time.Now()
for i := initialNLeafs; i < nLeafs; i++ {
k := BigIntToBytes(bLen, big.NewInt(int64(i)))
v := BigIntToBytes(bLen, big.NewInt(int64(i*2)))
k := BigIntToBytesLE(bLen, big.NewInt(int64(i)))
v := BigIntToBytesLE(bLen, big.NewInt(int64(i*2)))
if err := tree1.Add(k, v); err != nil {
t.Fatal(err)
}
Expand All @@ -722,8 +722,8 @@ func TestAddBatchNotEmptyUnbalanced(t *testing.T) {
// add the initial leafs to fill a bit the tree before calling the
// AddBatch method
for i := 0; i < initialNLeafs; i++ {
k := BigIntToBytes(bLen, big.NewInt(int64(i)))
v := BigIntToBytes(bLen, big.NewInt(int64(i*2)))
k := BigIntToBytesLE(bLen, big.NewInt(int64(i)))
v := BigIntToBytesLE(bLen, big.NewInt(int64(i*2)))
// use only the keys of one bucket, store the not used ones for
// later
if i%4 != 0 {
Expand All @@ -737,8 +737,8 @@ func TestAddBatchNotEmptyUnbalanced(t *testing.T) {
}

for i := initialNLeafs; i < nLeafs; i++ {
k := BigIntToBytes(bLen, big.NewInt(int64(i)))
v := BigIntToBytes(bLen, big.NewInt(int64(i*2)))
k := BigIntToBytesLE(bLen, big.NewInt(int64(i)))
v := BigIntToBytesLE(bLen, big.NewInt(int64(i*2)))
keys = append(keys, k)
values = append(values, v)
}
Expand Down Expand Up @@ -950,7 +950,7 @@ func TestAddKeysWithEmptyValues(t *testing.T) {
bLen := 32
var keys, values [][]byte
for i := 0; i < nLeafs; i++ {
k := BigIntToBytes(bLen, big.NewInt(int64(i)))
k := BigIntToBytesLE(bLen, big.NewInt(int64(i)))
v := []byte{}
keys = append(keys, k)
values = append(values, v)
Expand Down
Loading
Loading