From 0cd530e4dba40331eeac135035934a651ab250c3 Mon Sep 17 00:00:00 2001 From: p4u Date: Fri, 8 Sep 2023 00:07:42 +0200 Subject: [PATCH] fix anonymous maxCensusSize check Signed-off-by: p4u --- crypto/zk/circuit/config.go | 8 +++++--- vochain/transaction/election_tx.go | 20 +++++++++----------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/crypto/zk/circuit/config.go b/crypto/zk/circuit/config.go index 026d8c81d..d589e7cea 100644 --- a/crypto/zk/circuit/config.go +++ b/crypto/zk/circuit/config.go @@ -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 } diff --git a/vochain/transaction/election_tx.go b/vochain/transaction/election_tx.go index 0204182a2..7f85f30e6 100644 --- a/vochain/transaction/election_tx.go +++ b/vochain/transaction/election_tx.go @@ -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) @@ -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.