Skip to content

Commit

Permalink
fix anonymous maxCensusSize check
Browse files Browse the repository at this point in the history
Signed-off-by: p4u <[email protected]>
  • Loading branch information
p4u committed Sep 7, 2023
1 parent fb3fe13 commit 0cd530e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
8 changes: 5 additions & 3 deletions crypto/zk/circuit/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,12 @@ func (conf *ZkCircuitConfig) KeySize() int {
// or not. If it is not precalculated, it will calculate and initialise it. In
// any case, the value is returned as big.Int.
func (conf *ZkCircuitConfig) MaxCensusSize() *big.Int {
circuitMaxCensusSize := new(big.Int).Exp(big.NewInt(2), big.NewInt(int64(conf.Levels)), nil)
if conf.maxCensusSize == nil {
conf.maxCensusSize = circuitMaxCensusSize
if conf.maxCensusSize != nil {
return conf.maxCensusSize
}

circuitMaxCensusSize := new(big.Int).Exp(big.NewInt(2), big.NewInt(int64(conf.Levels)), nil)
conf.maxCensusSize = circuitMaxCensusSize
return circuitMaxCensusSize
}

Expand Down
20 changes: 9 additions & 11 deletions vochain/transaction/election_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,24 @@ func (t *TransactionHandler) NewProcessTxCheck(vtx *vochaintx.Tx) (*models.Proce
}

// check MaxCensusSize is properly set and within the allowed range
if tx.Process.GetMaxCensusSize() == 0 {
txMaxCensusSize := tx.Process.GetMaxCensusSize()
if txMaxCensusSize == 0 {
return nil, ethereum.Address{}, fmt.Errorf("maxCensusSize is zero")
}
maxProcessSize, err := t.state.MaxProcessSize()
if err != nil {
return nil, ethereum.Address{}, fmt.Errorf("cannot get maxProcessSize: %w", err)
}
if maxProcessSize > 0 && tx.Process.GetMaxCensusSize() > maxProcessSize {
if maxProcessSize > 0 && txMaxCensusSize > maxProcessSize {
return nil, ethereum.Address{},
fmt.Errorf("maxCensusSize is greater than the maximum allowed (%d)", maxProcessSize)
}
// check that the census size is not bigger than the circuit levels
if tx.Process.EnvelopeType.Anonymous && !t.ZkCircuit.Config.SupportsCensusSize(txMaxCensusSize) {
return nil, ethereum.Address{}, fmt.Errorf("maxCensusSize for anonymous envelope "+
"cannot be bigger than the number of levels of the circuit (max:%d provided:%d)",
t.ZkCircuit.Config.MaxCensusSize().Int64(), txMaxCensusSize)
}

// check signature
addr, acc, err := t.state.AccountFromSignature(vtx.SignedBody, vtx.Signature)
Expand Down Expand Up @@ -121,15 +128,6 @@ func (t *TransactionHandler) NewProcessTxCheck(vtx *vochaintx.Tx) (*models.Proce
}
tx.Process.ProcessId = pid.Marshal()

// if pre-register is enabled, check that the census size is not bigger than the circuit levels
if tx.Process.Mode.PreRegister && tx.Process.EnvelopeType.Anonymous {
if !t.ZkCircuit.Config.SupportsCensusSize(tx.Process.GetMaxCensusSize()) {
return nil, ethereum.Address{}, fmt.Errorf("maxCensusSize for anonymous envelope "+
"cannot be bigger than the number of levels of the circuit (%d)",
t.ZkCircuit.Config.Levels)
}
}

// TODO: Enable support for PreRegiser without Anonymous. Figure out
// all the required changes to support a process with a rolling census
// that is not Anonymous.
Expand Down

0 comments on commit 0cd530e

Please sign in to comment.