types

package
v0.0.0-...-5757dfe Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 15, 2022 License: AGPL-3.0 Imports: 16 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// MaxLevels indicates the maximum number of levels in the Census
	// MerkleTree
	MaxLevels int = 64
	// MaxNLeafs indicates the maximum number of leaves in the Census
	// MerkleTree
	MaxNLeafs uint64 = uint64(math.MaxUint64)
	// MaxKeyLen indicates the maximum key (index) length in the Census
	// MerkleTree
	MaxKeyLen int = int(math.Ceil(float64(MaxLevels) / float64(8))) //nolint:gomnd
	// EmptyRoot is a byte array of 0s, with the length of the hash
	// function output length used in the Census MerkleTree
	EmptyRoot = make([]byte, arbo.HashFunctionPoseidon.Len())
)

Functions

func BytesToIndexAndWeight

func BytesToIndexAndWeight(b []byte) (uint64, *big.Int, error)

BytesToIndexAndWeight returns the index and weight from the given byte array

func HashPubKBytes

func HashPubKBytes(pubK *babyjub.PublicKey, weight *big.Int) ([]byte, error)

HashPubKBytes returns the bytes representation of the Poseidon hash of the given PublicKey together with its weight, that will be used as a leaf value in the MerkleTree. If no weight is provided (eg. nil), a weight of 1 is assigned.

func HashVote

func HashVote(chainID, processID uint64, vote []byte) (*big.Int, error)

HashVote computes the vote hash following the circuit approach

func HexToPublicKey

func HexToPublicKey(h string) (*babyjub.PublicKey, error)

HexToPublicKey converts the given hex representation of a babyjub.PublicKeyComp, and returns the babyjub.PublicKey

func IndexAndWeightToBytes

func IndexAndWeightToBytes(index uint64, weight *big.Int) []byte

IndexAndWeightToBytes returns a byte array containing the given index and weight, encoded as: [ 8 | 32 ] [ index | weight ]

func Uint64ToIndex

func Uint64ToIndex(u uint64) []byte

Uint64ToIndex returns the bytes representation of the given uint64 that will be used as a leaf index in the MerkleTree

Types

type ByteArray

type ByteArray []byte

ByteArray is a type alias over []byte to implement custom json marshalers in hex

func (ByteArray) MarshalJSON

func (b ByteArray) MarshalJSON() ([]byte, error)

MarshalJSON implements the Marshaler interface for ByteArray types

func (*ByteArray) UnmarshalJSON

func (b *ByteArray) UnmarshalJSON(j []byte) error

UnmarshalJSON implements the Unmarshaler interface for ByteArray types

type CensusProof

type CensusProof struct {
	Index       uint64             `json:"index"`
	PublicKey   *babyjub.PublicKey `json:"publicKey"`
	Weight      *big.Int           `json:"weight"`
	MerkleProof ByteArray          `json:"merkleProof"`
}

CensusProof contains the proof of a PublicKey in the Census Tree

type Process

type Process struct {
	// ID is determined by the SmartContract, is unique for each Process
	ID uint64
	// ProofID determines the ID assigned by the proof-server, is used to
	// retrieve the proof
	ProofID uint64
	// CensusRoot indicates the CensusRoot of the Process, the same
	// CensusRoot can be reused by different Processes
	CensusRoot []byte
	// CensusSize determines the number of public keys placed in the
	// CensusTree leaves under the CensusRoot
	CensusSize uint64
	// EthBlockNum indicates at which Ethereum block number the process has
	// been created
	EthBlockNum uint64
	// ResPubStartBlock (Results Publishing Start Block) indicates the
	// EthBlockNum where the process ends, in which the results can be
	// published
	ResPubStartBlock uint64
	// ResPubWindow (Results Publishing Window) indicates the window of
	// time (blocks), which starts at the ResPubStartBlock and ends at
	// ResPubStartBlock+ResPubWindow.  During this window of time, the
	// results + zkProofs can be sent to the SmartContract.
	ResPubWindow uint64
	// MinParticipation sets a threshold of minimum number of votes over
	// the total users in the census (% over CensusSize)
	MinParticipation uint8
	// MinPositiveVotes sets a threshold of minimum votes supporting the
	// proposal, over all the processed votes (% over nVotes)
	MinPositiveVotes uint8
	// Type of process, where 0: multisig, 1: referendum
	Type uint8
	// InsertedDatetime contains the datetime of when the process was
	// inserted in the db
	InsertedDatetime time.Time
	// Status determines the current status of the process
	Status ProcessStatus
}

Process represents a voting process

type ProcessStatus

type ProcessStatus int

ProcessStatus type is used to define the status of the Process

var (

	// ProcessStatusOn indicates that the process is accepting vote (Voting
	// phase)
	ProcessStatusOn ProcessStatus = 0
	// ProcessStatusFrozen indicates that the process is in
	// ResultsPublishingPhase and is no longer accepting new votes, the
	// proof can now be generated
	ProcessStatusFrozen ProcessStatus = 1
	// ProcessStatusProofGenerating indicates that the process is no longer
	// accepting new votes and the zkProof is being generated
	ProcessStatusProofGenerating ProcessStatus = 2
	// ProcessStatusProofGenerated indicates that the process is finished,
	// and the zkProof is already generated
	ProcessStatusProofGenerated ProcessStatus = 3
)

type Proof

type Proof struct {
	A        [3]*big.Int    `json:"pi_a"`
	B        [3][2]*big.Int `json:"pi_b"`
	C        [3]*big.Int    `json:"pi_c"`
	Protocol string         `json:"protocol"`
}

Proof represents a Groth16 zkSNARK proof

type ProofInDB

type ProofInDB struct {
	ProofID            uint64
	Proof              []byte
	PublicInputs       []byte
	InsertedDatetime   time.Time
	ProofAddedDatetime time.Time
	ProcessID          uint64
}

ProofInDB contains the proof data from an entry in the db

type VotePackage

type VotePackage struct {
	Signature   babyjub.SignatureComp `json:"signature"`
	CensusProof CensusProof           `json:"censusProof"`
	Vote        ByteArray             `json:"vote"`
}

VotePackage represents the vote sent by the User

func (*VotePackage) Verify

func (vp *VotePackage) Verify(chainID, processID uint64, root []byte) error

Verify checks the signature and merkleproof of the VotePackage

type ZKCircuitMeta

type ZKCircuitMeta struct {
	NMaxVotes int
	NLevels   int
}

ZKCircuitMeta contains metadata related to the circuit configuration

type ZKInputs

type ZKInputs struct {
	Meta ZKCircuitMeta `json:"-"`

	// public inputs
	ChainID      *big.Int `json:"chainID"`
	ProcessID    *big.Int `json:"processID"`
	CensusRoot   *big.Int `json:"censusRoot"`
	ReceiptsRoot *big.Int `json:"receiptsRoot"`
	NVotes       *big.Int `json:"nVotes"`
	Result       *big.Int `json:"result"`
	WithReceipts *big.Int `json:"withReceipts"`

	/////
	// private inputs
	Vote []*big.Int `json:"vote"`
	// user's key related
	Index  []*big.Int `json:"index"`
	PkX    []*big.Int `json:"pkX"`
	PkY    []*big.Int `json:"pkY"`
	Weight []*big.Int `json:"weight"`
	// signatures
	S   []*big.Int `json:"s"`
	R8x []*big.Int `json:"r8x"`
	R8y []*big.Int `json:"r8y"`
	// census proofs
	Siblings         [][]*big.Int `json:"siblings"`
	ReceiptsSiblings [][]*big.Int `json:"receiptsSiblings"`
}

ZKInputs contains the inputs used to generate the zkProof

func NewZKInputs

func NewZKInputs(nMaxVotes, nLevels int) *ZKInputs

NewZKInputs returns an initialized ZKInputs struct

func (*ZKInputs) ComputeReceipts

func (z *ZKInputs) ComputeReceipts(processID uint64, receiptsKeys, receiptsValues [][]byte) error

ComputeReceipts builds a temp MerkleTree with all the given index & publicKeys (receiptsKeys & receiptsValues), to then compute the siblings of each recipt, adding the siblings & root of the receipts tree to ZKInputs.ReceiptsRoot & ZKInputs.ReceiptsSiblings.

func (ZKInputs) MarshalJSON

func (z ZKInputs) MarshalJSON() ([]byte, error)

MarshalJSON implements the json marshaler for ZKInputs

func (*ZKInputs) MerkleProofToZKInputsFormat

func (z *ZKInputs) MerkleProofToZKInputsFormat(p []byte) ([]*big.Int, error)

MerkleProofToZKInputsFormat prepares the given MerkleProof into the ZKInputs.Siblings format for the circuit

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL