Skip to content

Commit

Permalink
Merge pull request #249 from spacemeshos/bump-post-rs-v0.6.0
Browse files Browse the repository at this point in the history
Bump post-rs to v0.6.1
  • Loading branch information
fasmat authored Nov 24, 2023
2 parents 35d0654 + b4b08ae commit fd3547c
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 112 deletions.
6 changes: 0 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,6 @@ jobs:
- name: Add OpenCL support - Ubuntu
if: ${{ matrix.os == 'ubuntu-latest' }}
run: sudo apt-get update -q && sudo apt-get install -qy ocl-icd-opencl-dev libpocl2
- name: Override SDKROOT for macOS
if: ${{ contains(matrix.os, 'macos') && runner.arch == 'arm64' }}
run: echo "SDKROOT=/Library/Developer/CommandLineTools/SDKs/MacOSX12.3.sdk" >> $GITHUB_ENV
- name: disable Windows Defender - Windows
if: ${{ matrix.os == 'windows-latest' }}
run: |
Expand Down Expand Up @@ -119,9 +116,6 @@ jobs:
- name: Add OpenCL support - Ubuntu
if: ${{ matrix.os == 'ubuntu-latest' }}
run: sudo apt-get update -q && sudo apt-get install -qy ocl-icd-opencl-dev libpocl2
- name: Override SDKROOT for macOS
if: ${{ contains(matrix.os, 'macos') && runner.arch == 'arm64' }}
run: echo "SDKROOT=/Library/Developer/CommandLineTools/SDKs/MacOSX12.3.sdk" >> $GITHUB_ENV
- name: disable Windows Defender - Windows
if: ${{ matrix.os == 'windows-latest' }}
run: |
Expand Down
2 changes: 1 addition & 1 deletion Makefile.Inc
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ else
endif
endif

POSTRS_SETUP_REV = 0.5.1
POSTRS_SETUP_REV = 0.6.1
POSTRS_SETUP_ZIP = libpost-$(platform)-v$(POSTRS_SETUP_REV).zip
POSTRS_SETUP_URL_ZIP ?= https://github.com/spacemeshos/post-rs/releases/download/v$(POSTRS_SETUP_REV)/$(POSTRS_SETUP_ZIP)
ifeq ($(platform), windows)
Expand Down
2 changes: 1 addition & 1 deletion cmd/postcli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ func cmdVerifyPos(opts config.InitOpts, fraction float64, logger *zap.Logger) {
log.Println("cli: key.bin is valid")
log.Println("cli: verifying POS data")

params := postrs.TranslateScryptParams(opts.Scrypt.N, opts.Scrypt.R, opts.Scrypt.P)
params := postrs.NewScryptParams(opts.Scrypt.N, opts.Scrypt.R, opts.Scrypt.P)
verifyOpts := []postrs.VerifyPosOptionsFunc{
postrs.WithFraction(fraction),
postrs.FromFile(uint32(opts.FromFileIdx)),
Expand Down
12 changes: 1 addition & 11 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,7 @@ func DefaultVerifyingPowFlags() PowFlags {
return RecommendedPowFlags()
}

type Config struct {
MinNumUnits uint32
MaxNumUnits uint32
LabelsPerUnit uint64

K1 uint32 // K1 specifies the difficulty for a label to be a candidate for a proof.
K2 uint32 // K2 is the number of labels below the required difficulty required for a proof.
K3 uint32 // K3 is the size of the subset of proof indices that is validated.

PowDifficulty [32]byte
}
type Config postrs.Config

// MainnetConfig returns the default config for mainnet.
func MainnetConfig() Config {
Expand Down
2 changes: 1 addition & 1 deletion initialization/initialization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ func TestStop(t *testing.T) {

cfg, opts := getTestConfig(t)
opts.Scrypt.N = 64 // higher difficulty for a chance at stopping before finished
opts.NumUnits = 10
opts.NumUnits = 20
opts.ComputeBatchSize = 1 << 10

init, err := NewInitializer(
Expand Down
4 changes: 2 additions & 2 deletions initialization/pos_verification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ func TestVerifyPos(t *testing.T) {
err = init.Initialize(context.Background())
require.NoError(t, err)

scryptParams := postrs.TranslateScryptParams(opts.Scrypt.N, opts.Scrypt.R, opts.Scrypt.P)
scryptParams := postrs.NewScryptParams(opts.Scrypt.N, opts.Scrypt.R, opts.Scrypt.P)

t.Run("valid", func(t *testing.T) {
err := postrs.VerifyPos(opts.DataDir, scryptParams, postrs.WithFraction(100.0))
require.NoError(t, err)
})
t.Run("invalid N", func(t *testing.T) {
wrongScrypt := postrs.TranslateScryptParams(4, opts.Scrypt.R, opts.Scrypt.P)
wrongScrypt := postrs.NewScryptParams(4, opts.Scrypt.R, opts.Scrypt.P)
err := postrs.VerifyPos(opts.DataDir, wrongScrypt, postrs.WithFraction(100.0))
require.ErrorIs(t, err, postrs.ErrInvalidPos)
})
Expand Down
58 changes: 34 additions & 24 deletions internal/postrs/proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"encoding/hex"
"errors"
"fmt"
"math"
"sync"
"unsafe"

Expand All @@ -20,6 +19,26 @@ import (

type ScryptParams = C.ScryptParams

type Config struct {
MinNumUnits uint32
MaxNumUnits uint32
LabelsPerUnit uint64

K1 uint32 // K1 specifies the difficulty for a label to be a candidate for a proof.
K2 uint32 // K2 is the number of labels below the required difficulty required for a proof.
K3 uint32 // K3 is the size of the subset of proof indices that is validated.

PowDifficulty [32]byte
}

func NewScryptParams(n, r, p uint) ScryptParams {
return ScryptParams{
n: C.size_t(n),
r: C.size_t(r),
p: C.size_t(p),
}
}

type HexEncoded []byte

func (h HexEncoded) String() string {
Expand All @@ -29,20 +48,6 @@ func (h HexEncoded) String() string {
// ErrVerifierClosed is returned when calling a method on an already closed Scrypt instance.
var ErrVerifierClosed = errors.New("verifier has been closed")

// Translate scrypt parameters expressed as N,R,P to Nfactor, Rfactor and Pfactor
// that are understood by scrypt-jane.
// Relation:
// N = 1 << (nfactor + 1)
// r = 1 << rfactor
// p = 1 << pfactor
func TranslateScryptParams(n, r, p uint) ScryptParams {
return ScryptParams{
nfactor: C.uint8_t(math.Log2(float64(n))) - 1,
rfactor: C.uint8_t(math.Log2(float64(r))),
pfactor: C.uint8_t(math.Log2(float64(p))),
}
}

func GenerateProof(dataDir string, challenge []byte, logger *zap.Logger, nonces, threads uint, K1, K2 uint32, powDifficulty [32]byte, powFlags PowFlags) (*shared.Proof, error) {
if logger != nil {
setLogCallback(logger)
Expand All @@ -54,7 +59,7 @@ func GenerateProof(dataDir string, challenge []byte, logger *zap.Logger, nonces,
challengePtr := C.CBytes(challenge)
defer C.free(challengePtr)

config := C.Config{
config := C.ProofConfig{
k1: C.uint32_t(K1),
k2: C.uint32_t(K2),
}
Expand Down Expand Up @@ -147,7 +152,7 @@ func (v *Verifier) Close() error {
return nil
}

func (v *Verifier) VerifyProof(proof *shared.Proof, metadata *shared.ProofMetadata, logger *zap.Logger, k1, k2, k3 uint32, powDifficulty [32]byte, scryptParams ScryptParams) error {
func (v *Verifier) VerifyProof(proof *shared.Proof, metadata *shared.ProofMetadata, logger *zap.Logger, cfg Config, scryptParams ScryptParams) error {
if logger != nil {
setLogCallback(logger)
}
Expand All @@ -171,15 +176,20 @@ func (v *Verifier) VerifyProof(proof *shared.Proof, metadata *shared.ProofMetada
return errors.New("proof indices are empty")
}

config := C.Config{
k1: C.uint32_t(k1),
k2: C.uint32_t(k2),
k3: C.uint32_t(k3),
scrypt: scryptParams,
config := C.ProofConfig{
k1: C.uint32_t(cfg.K1),
k2: C.uint32_t(cfg.K2),
k3: C.uint32_t(cfg.K3),
}
for i, b := range powDifficulty {
for i, b := range cfg.PowDifficulty {
config.pow_difficulty[i] = C.uchar(b)
}
initConfig := C.InitConfig{
labels_per_unit: C.uint64_t(cfg.LabelsPerUnit),
min_num_units: C.uint32_t(cfg.MinNumUnits),
max_num_units: C.uint32_t(cfg.MaxNumUnits),
scrypt: scryptParams,
}

cProof := C.Proof{
nonce: C.uint32_t(proof.Nonce),
Expand All @@ -196,7 +206,6 @@ func (v *Verifier) VerifyProof(proof *shared.Proof, metadata *shared.ProofMetada
commitment_atx_id: *(*[32]C.uchar)(unsafe.Pointer(&metadata.CommitmentAtxId[0])),
challenge: *(*[32]C.uchar)(unsafe.Pointer(&metadata.Challenge[0])),
num_units: C.uint32_t(metadata.NumUnits),
labels_per_unit: C.uint64_t(metadata.LabelsPerUnit),
}

v.mu.RLock()
Expand All @@ -210,6 +219,7 @@ func (v *Verifier) VerifyProof(proof *shared.Proof, metadata *shared.ProofMetada
cProof,
&cMetadata,
config,
initConfig,
)

switch result {
Expand Down
19 changes: 0 additions & 19 deletions internal/postrs/proof_test.go

This file was deleted.

23 changes: 5 additions & 18 deletions verifying/verifying.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,7 @@ func VerifyVRFNonce(nonce *uint64, m *shared.VRFNonceMetadata, opts ...OptionFun
return fmt.Errorf("invalid `commitmentAtxId` length; expected: 32, given: %v", len(m.CommitmentAtxId))
}

options := defaultOpts()
for _, opt := range opts {
if err := opt(options); err != nil {
return err
}
}

options := applyOpts(opts...)
numLabels := uint64(m.NumUnits) * uint64(m.LabelsPerUnit)
difficulty := shared.PowDifficulty(numLabels)

Expand Down Expand Up @@ -68,10 +62,7 @@ type ProofVerifier struct {
// NewProofVerifier creates a new proof verifier.
// The verifier must be closed after use with Close().
func NewProofVerifier(opts ...OptionFunc) (*ProofVerifier, error) {
options, err := applyOpts(opts...)
if err != nil {
return nil, err
}
options := applyOpts(opts...)
inner, err := postrs.NewVerifier(options.powFlags)
if err != nil {
return nil, err
Expand All @@ -83,18 +74,14 @@ func NewProofVerifier(opts ...OptionFunc) (*ProofVerifier, error) {
// Verify ensures the validity of a proof in respect to its metadata.
// It returns nil if the proof is valid or an error describing the failure, otherwise.
func (v *ProofVerifier) Verify(p *shared.Proof, m *shared.ProofMetadata, cfg config.Config, logger *zap.Logger, opts ...OptionFunc) error {
options, err := applyOpts(opts...)
if err != nil {
return err
}
if len(m.NodeId) != 32 {
return fmt.Errorf("invalid `nodeId` length; expected: 32, given: %v", len(m.NodeId))
}
if len(m.CommitmentAtxId) != 32 {
return fmt.Errorf("invalid `commitmentAtxId` length; expected: 32, given: %v", len(m.CommitmentAtxId))
}

scryptParams := postrs.TranslateScryptParams(options.labelScrypt.N, options.labelScrypt.R, options.labelScrypt.P)

return v.VerifyProof(p, m, logger, cfg.K1, cfg.K2, cfg.K3, cfg.PowDifficulty, scryptParams)
options := applyOpts(opts...)
scryptParams := postrs.NewScryptParams(options.labelScrypt.N, options.labelScrypt.R, options.labelScrypt.P)
return v.VerifyProof(p, m, logger, postrs.Config(cfg), scryptParams)
}
36 changes: 7 additions & 29 deletions verifying/verifying_options.go
Original file line number Diff line number Diff line change
@@ -1,58 +1,36 @@
package verifying

import (
"errors"

"github.com/spacemeshos/post/config"
)

type option struct {
powFlags config.PowFlags
// scrypt parameters for labels initialization
labelScrypt config.ScryptParams

powCreatorId []byte
}

func defaultOpts() *option {
return &option{
func applyOpts(options ...OptionFunc) *option {
opts := &option{
powFlags: config.DefaultVerifyingPowFlags(),
labelScrypt: config.DefaultLabelParams(),
}
}

func applyOpts(options ...OptionFunc) (*option, error) {
opts := defaultOpts()
for _, opt := range options {
if err := opt(opts); err != nil {
return nil, err
}
opt(opts)
}
return opts, nil
return opts
}

type OptionFunc func(*option) error
type OptionFunc func(*option)

func WithLabelScryptParams(params config.ScryptParams) OptionFunc {
return func(o *option) error {
return func(o *option) {
o.labelScrypt = params
return nil
}
}

func WithPowFlags(flags config.PowFlags) OptionFunc {
return func(o *option) error {
return func(o *option) {
o.powFlags = flags
return nil
}
}

func WithPowCreator(id []byte) OptionFunc {
return func(o *option) error {
if len(id) != 32 {
return errors.New("pow creator id must be 32 bytes")
}
o.powCreatorId = id
return nil
}
}

0 comments on commit fd3547c

Please sign in to comment.