forked from threehook/go-pedersen-commitment
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpedersen_test.go
More file actions
57 lines (46 loc) · 1.37 KB
/
pedersen_test.go
File metadata and controls
57 lines (46 loc) · 1.37 KB
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
package pedersen
import (
"github.com/bwesterb/go-ristretto"
"github.com/stretchr/testify/assert"
"math/big"
"testing"
)
// Should commit to a sum of two values
func TestCommitToSuccess(t *testing.T) {
var rX, rY, vX, vY ristretto.Scalar
rX.Rand()
H := generateH() // Secondary point on the Curve
five := big.NewInt(5)
// Transfer amount of 5 tokens
tC := commitTo(&H, &rX, vX.SetBigInt(five))
// Alice 10 - 5 = 5
rY.Rand()
ten := big.NewInt(10)
aC1 := commitTo(&H, &rY, vY.SetBigInt(ten))
assert.NotEqual(t, aC1, tC, "Should not be equal")
var aC2 ristretto.Point
aC2.Sub(&aC1, &tC)
checkAC2 := SubPrivately(&H, &rX, &rY, ten, five)
assert.True(t, checkAC2.Equals(&aC2), "Should be equal")
}
// Should fail if not using the correct blinding factors
func TestCommitToFails(t *testing.T) {
var rX, rY, vX, vY ristretto.Scalar
rX.Rand()
H := generateH() // Secondary point on the Curve
five := big.NewInt(5)
// Transfer amount of 5 tokens
tC := commitTo(&H, &rX, vX.SetBigInt(five))
// Alice 10 - 5 = 5
rY.Rand()
ten := big.NewInt(10)
aC1 := commitTo(&H, &rY, vY.SetBigInt(ten))
assert.NotEqual(t, aC1, tC, "They should not be equal")
var aC2 ristretto.Point
aC2.Sub(&aC1, &tC)
// Create different (and wrong) binding factors
rX.Rand()
rY.Rand()
checkAC2 := SubPrivately(&H, &rX, &rY, ten, five)
assert.False(t, checkAC2.Equals(&aC2), "Should not be equal")
}