-
Notifications
You must be signed in to change notification settings - Fork 1
/
math_scalars.go
72 lines (59 loc) · 1.35 KB
/
math_scalars.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
71
72
// Package bulletproofs
// Copyright 2024 Distributed Lab. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package bulletproofs
import (
"github.com/cloudflare/bn256"
"math/big"
)
func pow(x *big.Int, y int) *big.Int {
if y < 0 {
return new(big.Int).Exp(inv(x), big.NewInt(-int64(y)), bn256.Order)
}
return new(big.Int).Exp(x, big.NewInt(int64(y)), bn256.Order)
}
func inv(x *big.Int) *big.Int {
return new(big.Int).ModInverse(x, bn256.Order)
}
func minus(x *big.Int) *big.Int {
return sub(bint(0), x)
}
func powerOfTwo(x int) (p2 int) {
p2 = 1
for p2 < x {
p2 *= 2
}
return
}
func bint(v int) *big.Int {
return new(big.Int).Mod(new(big.Int).SetInt64(int64(v)), bn256.Order)
}
func bbool(v bool) *big.Int {
if v {
return bint(1)
}
return bint(0)
}
func zeroIfNil(x *big.Int) *big.Int {
if x == nil {
return bint(0)
}
return x
}
func add(x *big.Int, y *big.Int) *big.Int {
x = zeroIfNil(x)
y = zeroIfNil(y)
return new(big.Int).Mod(new(big.Int).Add(x, y), bn256.Order)
}
func sub(x *big.Int, y *big.Int) *big.Int {
x = zeroIfNil(x)
y = zeroIfNil(y)
return new(big.Int).Mod(new(big.Int).Sub(x, y), bn256.Order)
}
func mul(x *big.Int, y *big.Int) *big.Int {
if x == nil || y == nil {
return bint(0)
}
return new(big.Int).Mod(new(big.Int).Mul(x, y), bn256.Order)
}