elgamal

package
v0.0.0-...-4cd30bd Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2025 License: AGPL-3.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const BigIntsPerCiphertext = 4

BigIntsPerCiphertext is 4 since each Ciphertext has C1.X, C1.Y, C2.X and C2.Y coords

View Source
const (
	SerializedBallotSize = circuits.FieldsPerBallot * sizeCiphertext
)

sizes in bytes needed to serialize a Ballot

Variables

This section is empty.

Functions

func BabyStepGiantStepECC

func BabyStepGiantStepECC(M, G ecc.Point, maxMessage uint64) (*big.Int, error)

BabyStepGiantStepECC solves M = x*G for x in [0, maxMessage] using the baby-step giant-step algorithm over elliptic curves.

func CheckK

func CheckK(c1 ecc.Point, k *big.Int) bool

CheckK checks if a given k was used to produce the ciphertext (c1, c2) under the given publicKey. It returns true if c1 == k * G, false otherwise. This does not require decrypting the message or computing the discrete log.

func Decrypt

func Decrypt(publicKey ecc.Point, privateKey *big.Int, c1, c2 ecc.Point, maxMessage uint64) (M ecc.Point, message *big.Int, err error)

Decrypt decrypts the given ciphertext (c1, c2) using the private key. It returns the point M = c2 - d*c1 and the discrete log message scalar. If no solution is found, returns an error.

func Encrypt

func Encrypt(publicKey ecc.Point, msg *big.Int) (ecc.Point, ecc.Point, *big.Int, error)

Encrypt function encrypts a message using the public key provided as elliptic curve point. It generates a random k and returns the two points that represent the encrypted message and the random k used to encrypt it. It returns an error if any.

func EncryptWithK

func EncryptWithK(pubKey ecc.Point, msg, k *big.Int) (ecc.Point, ecc.Point, error)

EncryptWithK function encrypts a message using the public key provided as elliptic curve point and the random k value provided. It returns the two points that represent the encrypted message and error if any.

TODO: remove error return, since it can never error

func GenerateKey

func GenerateKey(curve ecc.Point) (publicKey ecc.Point, privateKey *big.Int, err error)

GenerateKey generates a new public/private ElGamal encryption key pair.

func RandK

func RandK() (*big.Int, error)

RandK function generates a random k value for encryption.

Types

type Ballot

type Ballot struct {
	CurveType   string                                `json:"curveType"`
	Ciphertexts [circuits.FieldsPerBallot]*Ciphertext `json:"ciphertexts"`
}

func NewBallot

func NewBallot(curve ecc.Point) *Ballot

NewBallot creates a new Ballot for the given curve.

func (*Ballot) Add

func (z *Ballot) Add(x, y *Ballot) *Ballot

Add adds two Ballots and stores the result in the receiver, which is also returned.

func (*Ballot) BigInts

func (z *Ballot) BigInts() []*big.Int

BigInts returns a slice with 8*4 BigInts, namely the coords of each Ciphertext C1.X, C1.Y, C2.X, C2.Y as little-endian, in reduced twisted edwards form.

func (*Ballot) Deserialize

func (z *Ballot) Deserialize(data []byte) error

Deserialize reconstructs a Ballot from a slice of bytes. The input must be of len N*4*32 bytes (otherwise it returns an error), representing each Ciphertext C1.X, C1.Y, C2.X, C2.Y as little-endian, in reduced twisted edwards form.

func (*Ballot) Encrypt

func (z *Ballot) Encrypt(message [circuits.FieldsPerBallot]*big.Int, publicKey ecc.Point, k *big.Int) (*Ballot, error)

Encrypt encrypts a message using the public key provided as elliptic curve point. The randomness k can be provided or nil to generate a new one.

func (*Ballot) MarshalCBOR

func (z *Ballot) MarshalCBOR() ([]byte, error)

MarshalCBOR serializes the Ballot to CBOR.

func (*Ballot) MarshalJSON

func (z *Ballot) MarshalJSON() ([]byte, error)

MarshalJSON serializes the Ballot to JSON.

func (*Ballot) Serialize

func (z *Ballot) Serialize() []byte

Serialize returns a slice of len N*4*32 bytes, representing each Ciphertext C1.X, C1.Y, C2.X, C2.Y as little-endian, in reduced twisted edwards form.

func (*Ballot) String

func (z *Ballot) String() string

String returns a string representation of the Ballot.

func (*Ballot) ToGnark

func (z *Ballot) ToGnark() *circuits.Ballot

ToGnark returns z as the struct used by gnark, with the points in reduced twisted edwards format

func (*Ballot) ToGnarkEmulatedBN254

func (z *Ballot) ToGnarkEmulatedBN254() *circuits.EmulatedBallot[sw_bn254.ScalarField]

ToGnarkEmulatedBN254 returns z as the struct used by gnark, with the points in reduced twisted edwards format but as emulated.Element[sw_bn254.ScalarField] instead of frontend.Variable

func (*Ballot) UnmarshalCBOR

func (z *Ballot) UnmarshalCBOR(buf []byte) error

UnmarshalCBOR deserializes the Ballot from CBOR.

func (*Ballot) UnmarshalJSON

func (z *Ballot) UnmarshalJSON(data []byte) error

UnmarshalJSON deserializes the Ballot from JSON.

type Ciphertext

type Ciphertext struct {
	C1 ecc.Point `json:"c1"`
	C2 ecc.Point `json:"c2"`
}

Ciphertext represents an ElGamal encrypted message with homomorphic properties. It is a wrapper for convenience of the elGamal ciphersystem that encapsulates the two points of a ciphertext.

func NewCiphertext

func NewCiphertext(curve ecc.Point) *Ciphertext

NewCiphertext creates a new Ciphertext on the same curve as the given Point. The Point must be one on of the supported curves by crypto/ecc/curves package, can be easily created with curves.New(type)

func (*Ciphertext) Add

func (z *Ciphertext) Add(x, y *Ciphertext) *Ciphertext

Add adds two Ciphertext and stores the result in z, which is also returned.

func (*Ciphertext) Deserialize

func (z *Ciphertext) Deserialize(data []byte) error

Deserialize reconstructs an Ciphertext from a slice of bytes. The input must be of len 4*32 bytes (otherwise it returns an error), representing the C1.X, C1.Y, C2.X, C2.Y as little-endian, in reduced twisted edwards form.

func (*Ciphertext) Encrypt

func (z *Ciphertext) Encrypt(message *big.Int, publicKey ecc.Point, k *big.Int) (*Ciphertext, error)

Encrypt encrypts a message using the public key provided as elliptic curve point. The randomness k can be provided or nil to generate a new one.

func (*Ciphertext) MarshalCBOR

func (z *Ciphertext) MarshalCBOR() ([]byte, error)

MarshalCBOR serializes the Ciphertext to CBOR.

func (*Ciphertext) MarshalJSON

func (z *Ciphertext) MarshalJSON() ([]byte, error)

MarshalJSON serializes the Ciphertext to JSON.

func (*Ciphertext) Serialize

func (z *Ciphertext) Serialize() []byte

Serialize returns a slice of len 4*32 bytes, representing the C1.X, C1.Y, C2.X, C2.Y as little-endian, in reduced twisted edwards form.

func (*Ciphertext) String

func (z *Ciphertext) String() string

String returns a string representation of the Ciphertext.

func (*Ciphertext) ToGnark

func (z *Ciphertext) ToGnark() *gelgamal.Ciphertext

ToGnark returns z as the struct used by gnark, with the points in reduced twisted edwards format

func (*Ciphertext) UnmarshalCBOR

func (z *Ciphertext) UnmarshalCBOR(buf []byte) error

UnmarshalCBOR deserializes the Ciphertext from CBOR.

func (*Ciphertext) UnmarshalJSON

func (z *Ciphertext) UnmarshalJSON(data []byte) error

UnmarshalJSON deserializes the Ciphertext from JSON.

Directories

Path Synopsis
dkg

Jump to

Keyboard shortcuts

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