-
Notifications
You must be signed in to change notification settings - Fork 10
/
math_pedersen.go
52 lines (45 loc) · 1.35 KB
/
math_pedersen.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
package starkex
import (
"encoding/json"
"math/big"
)
/**
# Starkware crypto functions implemented in Golang.
#
# python source:
# https://github.com/starkware-libs/starkex-resources/blob/0f08e6c55ad88c93499f71f2af4a2e7ae0185cdf/crypto/starkware/crypto/signature/signature.py
*/
type PedersenCfg struct {
Comment string `json:"_comment"`
FieldPrime *big.Int `json:"FIELD_PRIME"`
FieldGen int `json:"FIELD_GEN"`
EcOrder *big.Int `json:"EC_ORDER"`
ALPHA int `json:"ALPHA"`
BETA *big.Int `json:"BETA"`
ConstantPoints [][2]*big.Int `json:"CONSTANT_POINTS"`
}
var pedersenCfg PedersenCfg
var EC_ORDER = new(big.Int)
var FIELD_PRIME = new(big.Int)
func init() {
_ = json.Unmarshal([]byte(pedersenParams), &pedersenCfg)
EC_ORDER = pedersenCfg.EcOrder
FIELD_PRIME = pedersenCfg.FieldPrime
}
func PedersenHash(str ...string) string {
NElementBitsHash := FIELD_PRIME.BitLen()
point := pedersenCfg.ConstantPoints[0]
for i, s := range str {
x, _ := big.NewInt(0).SetString(s, 10)
pointList := pedersenCfg.ConstantPoints[2+i*NElementBitsHash : 2+(i+1)*NElementBitsHash]
n := big.NewInt(0)
for _, pt := range pointList {
n.And(x, big.NewInt(1))
if n.Cmp(big.NewInt(0)) > 0 {
point = eccAdd(point, pt, FIELD_PRIME)
}
x = x.Rsh(x, 1)
}
}
return point[0].String()
}