-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsortition.go
70 lines (57 loc) · 2.09 KB
/
sortition.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright (C) 2019-2023 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand. If not, see <https://www.gnu.org/licenses/>.
package sortition
// #cgo CFLAGS: -O3
// #cgo CXXFLAGS: -std=c++11 -Wno-deprecated
// #include <stdint.h>
// #include <stdlib.h>
// #include "sortition.h"
import "C"
import (
"crypto/sha512"
"fmt"
"math/big"
"strings"
)
// DigestSize is the number of bytes in the preferred hash Digest used here.
const DigestSize = sha512.Size256
// Digest represents a 32-byte value holding the 256-bit Hash digest.
type Digest [DigestSize]byte
const precision = uint(8 * (DigestSize + 1))
var maxFloat *big.Float
// Select runs the sortition function and returns the number of time the key was selected
func Select(money uint64, totalMoney uint64, expectedSize float64, vrfOutput Digest) uint64 {
binomialN := float64(money)
binomialP := expectedSize / float64(totalMoney)
t := &big.Int{}
t.SetBytes(vrfOutput[:])
h := big.Float{}
h.SetPrec(precision)
h.SetInt(t)
ratio := big.Float{}
cratio, _ := ratio.Quo(&h, maxFloat).Float64()
return uint64(C.sortition_binomial_cdf_walk(C.double(binomialN), C.double(binomialP), C.double(cratio), C.uint64_t(money)))
}
func init() {
var b int
var err error
maxFloatString := fmt.Sprintf("0x%s", strings.Repeat("ff", DigestSize))
maxFloat, b, err = big.ParseFloat(maxFloatString, 0, precision, big.ToNearestEven)
if b != 16 || err != nil {
err = fmt.Errorf("failed to parse big float constant in sortition : %w", err)
panic(err)
}
}