-
Notifications
You must be signed in to change notification settings - Fork 0
/
pedersen_test.go
112 lines (88 loc) · 2.37 KB
/
pedersen_test.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Package pedersen
// Copyright 2023 Oleg Fomenko. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package pedersen
import (
"crypto/rand"
"fmt"
"github.com/davecgh/go-spew/spew"
"math/big"
"strconv"
"testing"
"github.com/ethereum/go-ethereum/common/hexutil"
bn256 "github.com/ethereum/go-ethereum/crypto/bn256/cloudflare"
)
func init() {
_, G, _ = bn256.RandomG1(rand.Reader)
_, H, _ = bn256.RandomG1(rand.Reader)
}
func TestBitRepresentation(t *testing.T) {
fmt.Println(strconv.FormatUint(17, 2))
fmt.Println(hexutil.Encode(bn256.Order.Bytes()))
}
func TestPedersenCommitment(t *testing.T) {
proof, commitment, prv, err := CreatePedersenCommitment(10, 64)
if err != nil {
panic(err)
}
reconstructedCommitment := PedersenCommitment(big.NewInt(10), prv)
fmt.Println("Constructed commitment with prv key: " + reconstructedCommitment.String())
fmt.Println("Response commitment: " + commitment.String())
fmt.Println("Private Key: " + hexutil.Encode(prv.Bytes()))
spew.Dump(proof)
if err = VerifyPedersenCommitment(commitment, proof); err != nil {
panic(err)
}
}
func TestPedersenCommitmentFails(t *testing.T) {
_, _, _, err := CreatePedersenCommitment(128, 5)
if err == nil {
panic("Should fail")
}
}
func TestSchnorrSignature(t *testing.T) {
prv, err := rand.Int(rand.Reader, bn256.Order)
if err != nil {
panic(err)
}
pk := ScalarMul(G, prv)
message := Hash([]byte("Hello world"))
sig, err := SignSchnorr(prv, pk, message)
if err != nil {
panic(err)
}
if err := VerifySchnorr(sig, pk, message); err != nil {
panic(err)
}
}
func TestSchnorrSignatureAggregation(t *testing.T) {
prvAlice, err := rand.Int(rand.Reader, bn256.Order)
if err != nil {
panic(err)
}
pubAlice := ScalarMul(G, prvAlice)
prvBob, err := rand.Int(rand.Reader, bn256.Order)
if err != nil {
panic(err)
}
pubBob := ScalarMul(G, prvBob)
pubCombined := Add(pubAlice, pubBob)
message := Hash([]byte("Hello world"))
sigAlice, err := SignSchnorr(prvAlice, pubCombined, message)
if err != nil {
panic(err)
}
sigBob, err := SignSchnorr(prvBob, pubCombined, message)
if err != nil {
panic(err)
}
rCom := Add(sigAlice.R, sigBob.R)
sigCom := SchnorrSignature{
S: add(sigAlice.S, sigBob.S),
R: rCom,
}
if err := VerifySchnorr(sigCom, pubCombined, message); err != nil {
panic(err)
}
}