jwz

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2023 License: Apache-2.0, MIT Imports: 17 Imported by: 5

README

jwz

Golang implementation of json web zeroknowledge

Notes on prover optimization for x86_64 hardware and witness calculator dependencies

See readme in iden3/go-rapidsnark/prover and iden3/go-rapidsnark/witness

Contributing

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as below, without any additional terms or conditions.

License

© 2023 0kims Association

This project is licensed under either of

at your option.

Documentation

Overview

Package jwz contains implementation of JSON WEB ZERO-Knowledge specification.

Index

Constants

View Source
const (
	// Groth16 alg
	Groth16 string = "groth16"
)

Variables

View Source
var AuthGroth16Alg = ProvingMethodAlg{Groth16, string(circuits.AuthCircuitID)}

AuthGroth16Alg its first auth v1 alg (groth16 vs auth v1 circuit)

View Source
var AuthV2Groth16Alg = ProvingMethodAlg{Groth16, string(circuits.AuthV2CircuitID)}

AuthV2Groth16Alg its auth v2 alg (groth16 vs auth v2 circuit)

Functions

func Hash

func Hash(message []byte) (*big.Int, error)

Hash returns poseidon hash of big.Int that was created from sha256 hash of the message bytes if such big.Int is not in the Field, DivMod result is returned.

func RegisterProvingMethod

func RegisterProvingMethod(alg ProvingMethodAlg, f func() ProvingMethod)

RegisterProvingMethod registers the "alg" name and a factory function for proving method. This is typically done during init() in the method's implementation

Types

type HeaderKey

type HeaderKey string

HeaderKey represents type for jwz headers keys

const (

	// HeaderType is 'typ' header, so we can set specific typ
	HeaderType HeaderKey = "typ" // we allow to set typ of token

)

type ProofInputsPreparerHandlerFunc

type ProofInputsPreparerHandlerFunc func(hash []byte, circuitID circuits.CircuitID) ([]byte, error)

ProofInputsPreparerHandlerFunc prepares inputs using hash message and circuit id

func (ProofInputsPreparerHandlerFunc) Prepare

func (f ProofInputsPreparerHandlerFunc) Prepare(hash []byte, circuitID circuits.CircuitID) ([]byte, error)

Prepare function is responsible to call provided handler for inputs preparation

type ProvingMethod

type ProvingMethod interface {
	Verify(messageHash []byte, proof *types.ZKProof, verificationKey []byte) error // Returns nil if proof is valid
	Prove(inputs []byte, provingKey []byte, wasm []byte) (*types.ZKProof, error)   // Returns proof or error
	Alg() string                                                                   // Returns the alg identifier for this method (example: 'AUTH-GROTH-16')
	CircuitID() string
}

ProvingMethod can be used add new methods for signing or verifying tokens.

func GetProvingMethod

func GetProvingMethod(alg ProvingMethodAlg) (method ProvingMethod)

GetProvingMethod retrieves a proving method from an "alg" string

type ProvingMethodAlg added in v1.0.0

type ProvingMethodAlg struct {
	Alg       string
	CircuitID string
}

ProvingMethodAlg defines proofs family and specific circuit

func GetAlgorithms

func GetAlgorithms() (algs []ProvingMethodAlg)

GetAlgorithms returns a list of registered "alg" names

func NewProvingMethodAlg added in v1.0.0

func NewProvingMethodAlg(alg, circuitID string) ProvingMethodAlg

NewProvingMethodAlg creates a new ProvingMethodAlg

type ProvingMethodGroth16Auth

type ProvingMethodGroth16Auth struct {
	ProvingMethodAlg
}

ProvingMethodGroth16Auth defines proofs family and specific circuit

var (
	ProvingMethodGroth16AuthInstance *ProvingMethodGroth16Auth
)

ProvingMethodGroth16AuthInstance instance for Groth16 proving method with an auth circuit

func (*ProvingMethodGroth16Auth) Alg

Alg returns current zk alg

func (*ProvingMethodGroth16Auth) CircuitID

func (m *ProvingMethodGroth16Auth) CircuitID() string

CircuitID returns name of circuit

func (*ProvingMethodGroth16Auth) Prove

func (m *ProvingMethodGroth16Auth) Prove(inputs, provingKey, wasm []byte) (*types.ZKProof, error)

Prove generates proof using auth circuit and Groth16 alg, checks that proven message hash is set as a part of circuit specific inputs

func (*ProvingMethodGroth16Auth) Verify

func (m *ProvingMethodGroth16Auth) Verify(messageHash []byte, proof *types.ZKProof, verificationKey []byte) error

Verify performs Groth16 proof verification and checks equality of message hash and proven challenge public signals

type ProvingMethodGroth16AuthV2 added in v1.0.0

type ProvingMethodGroth16AuthV2 struct {
	ProvingMethodAlg
}

ProvingMethodGroth16AuthV2 instance for Groth16 proving method with an authV2 circuit

var (
	ProvingMethodGroth16AuthV2Instance *ProvingMethodGroth16AuthV2
)

ProvingMethodGroth16AuthInstance instance for Groth16 proving method with an authV2 circuit

func (*ProvingMethodGroth16AuthV2) Alg added in v1.0.0

Alg returns current zk alg

func (*ProvingMethodGroth16AuthV2) CircuitID added in v1.0.0

func (m *ProvingMethodGroth16AuthV2) CircuitID() string

CircuitID returns name of circuit

func (*ProvingMethodGroth16AuthV2) Prove added in v1.0.0

func (m *ProvingMethodGroth16AuthV2) Prove(inputs, provingKey, wasm []byte) (*types.ZKProof, error)

Prove generates proof using authV2 circuit and Groth16 alg, checks that proven message hash is set as a part of circuit specific inputs

func (*ProvingMethodGroth16AuthV2) Verify added in v1.0.0

func (m *ProvingMethodGroth16AuthV2) Verify(messageHash []byte, proof *types.ZKProof, verificationKey []byte) error

Verify performs Groth16 proof verification and checks equality of message hash and proven challenge public signals

type Token

type Token struct {
	ZkProof *types.ZKProof // The third segment of the token.  Populated when you Parse a token

	Alg       string // fields that are part of headers
	CircuitID string // id of circuit that will be used for proving

	Method ProvingMethod // proving method to create a zkp
	// contains filtered or unexported fields
}

Token represents a JWZ Token.

func NewWithPayload

func NewWithPayload(prover ProvingMethod, payload []byte, inputsPreparer ProofInputsPreparerHandlerFunc) (*Token, error)

NewWithPayload creates a new Token with the specified proving method and payload.

func Parse

func Parse(token string) (*Token, error)

Parse parses a jwz message in compact or full serialization format.

func (*Token) CompactSerialize

func (token *Token) CompactSerialize() (string, error)

CompactSerialize returns token serialized in three parts: base64 encoded headers, payload and proof.

func (*Token) FullSerialize

func (token *Token) FullSerialize() (string, error)

FullSerialize returns marshaled presentation of raw token as json string.

func (*Token) GetHeader added in v1.0.0

func (token *Token) GetHeader() map[HeaderKey]interface{}

GetHeader returns header

func (*Token) GetMessageHash

func (token *Token) GetMessageHash() ([]byte, error)

GetMessageHash returns bytes of jwz message hash.

func (*Token) GetPayload

func (token *Token) GetPayload() []byte

GetPayload returns message payload

func (*Token) ParsePubSignals

func (token *Token) ParsePubSignals(out circuits.PubSignalsUnmarshaller) error

ParsePubSignals unmarshalls proof public signals to provided structure.

func (*Token) Prove

func (token *Token) Prove(provingKey, wasm []byte) (string, error)

Prove creates and returns a complete, proved JWZ. The token is proven using the Proving Method specified in the token.

func (*Token) Verify

func (token *Token) Verify(verificationKey []byte) (bool, error)

Verify perform zero knowledge verification.

func (*Token) WithHeader

func (token *Token) WithHeader(key HeaderKey, value interface{}) error

WithHeader allows to set or redefine default headers

Jump to

Keyboard shortcuts

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