Commit 66bed561 authored by zokyo-secured's avatar zokyo-secured
Browse files

add deposit logic

parent 65043e9c
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/offshift-client.iml" filepath="$PROJECT_DIR$/.idea/offshift-client.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="Go" enabled="true" />
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="Go" enabled="true" />
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
package bulletproof
import (
"math/big"
)
/*
Pedersen Commitment with Gens
Given a value, we commit the array with different generators.
We also pass in the Generators we want to use
*/
func PCommitWithGens(G, H ECPoint, x, r *big.Int) ECPoint {
commitment := EC.Zero()
modX := new(big.Int).Mod(x, EC.N)
modR := new(big.Int).Mod(r, EC.N)
commitment = commitment.Add(G.Mult(modX)).Add(H.Mult(modR))
return commitment
}
/*
Vector Pedersen Commitment with Gens
Given an array of values, we commit the array with different generators
for each element and for each randomness.
We also pass in the Generators we want to use
*/
func VectorPCommitWithGens(G, H []ECPoint, x, r []*big.Int) ECPoint {
modX := new(big.Int).Mod(x[0], EC.N)
modR := new(big.Int).Mod(r[0], EC.N)
commitment := (G[0].Mult(modX)).Add(H[0].Mult(modR))
for i := 1; i < len(G); i++ {
modX = new(big.Int).Mod(x[i], EC.N)
modR = new(big.Int).Mod(r[i], EC.N)
commitment = commitment.Add(G[i].Mult(modX)).Add(H[i].Mult(modR))
}
return commitment
}
package bulletproof
import (
"crypto/rand"
"fmt"
"math/big"
"testing"
)
func TestPCommitWithGens(t *testing.T) {
EC, _ = NewECPrimeGroupKey(64)
valueString := "95000000000000000000000000005"
value,_ := new(big.Int).SetString(valueString, 10)
r, err := rand.Int(rand.Reader, EC.N)
check(err)
rand := r
commitment := PCommitWithGens(EC.G, EC.H, value, rand)
comm := EC.Zero()
modX := new(big.Int).Mod(value, EC.N)
modR := new(big.Int).Mod(rand, EC.N)
comm = comm.Add(EC.G.Mult(modX)).Add(EC.H.Mult(modR))
if commitment.Equal(comm) {
fmt.Println("Commitment correct")
} else {
t.Error("Commitment failed")
}
}
func TestVectorPCommitWithGens(t *testing.T) {
EC, _ = NewECPrimeGroupKey(4)
valArr := []*big.Int{big.NewInt(19), big.NewInt(12), big.NewInt(299), big.NewInt(19909)}
G := EC.GVector[0:4]
H := EC.HVector[0:4]
randArr := make([]*big.Int, EC.V)
for i := 0; i < int(EC.V); i++ {
r, err := rand.Int(rand.Reader, EC.N)
check(err)
randArr[i] = r
}
commitment := VectorPCommitWithGens(G, H, valArr, randArr)
comm := EC.Zero()
for i := 0; i < len(G); i++ {
comm = comm.Add(G[i].Mult(valArr[i])).Add(H[i].Mult(randArr[i]))
}
if commitment.Equal(comm) {
fmt.Println("Commitment correct")
} else {
t.Error("Commitment failed")
}
}
package bulletproof
import (
"errors"
"fmt"
"github.com/ethereum/go-ethereum/crypto/secp256k1"
"github.com/ing-bank/zkrp/crypto/p256"
"math/big"
"offshift/hash"
"offshift/secp256k1/ecc_math"
)
var VecLength = 64
type CryptoParams struct {
//C *p256.MyBitCurve
C *secp256k1.BitCurve // secp256k1 ec
G ECPoint // G value for commitments of a single value
H ECPoint // H value for commitments of a single value
U ECPoint // U point that is a fixed group element with an unknown discrete-log relative to g,h
GVector []ECPoint // G values for commitments of a multi values
HVector []ECPoint // H values for commitments of a multi values
N *big.Int // scalar prime
V int64 // Vector length
}
func (c CryptoParams) Zero() ECPoint {
return ECPoint{big.NewInt(0), big.NewInt(0)}
}
func check(e error) {
if e != nil {
panic(e)
}
}
func IsPowerOfTwo(x int64) bool {
return (x != 0) && ((x & (x - 1)) == 0)
}
func NewECPrimeGroupKey(n int64) (CryptoParams, error){
var err error
if !IsPowerOfTwo(n) {
return CryptoParams{}, errors.New("range end is not a power of 2")
}
params := CryptoParams{}
params.C = secp256k1.S256()
G := ECPoint{params.C.Gx, params.C.Gy}
params.G = G
H, err := hash.SHA256MapToGroup(ecc_math.ECPointToHexString(ecc_math.ECPoint{X: G.X, Y: G.Y})) // working good
params.H = ECPoint{H.X, H.Y}
U, _ := p256.MapToGroup("setupUPoint")
params.U = ECPoint{U.X, U.Y}
params.N = secp256k1.S256().P
params.V = n
GVector := make([]*ecc_math.ECPoint, 64) // todo: 64
HVector := make([]*ecc_math.ECPoint, 64)
params.GVector = make([]ECPoint, 64)
params.HVector = make([]ECPoint, 64)
// in case when 1 member
stringForG := ecc_math.FillLeftHexToNBytes(fmt.Sprintf("%x", big.NewInt(0)), 1)
stringForG = ecc_math.ECPointToHexString(ecc_math.ECPoint{X: G.X, Y: G.Y}) + stringForG // G.x + G.y + 00
stringForH := ecc_math.FillLeftHexToNBytes(fmt.Sprintf("%x", big.NewInt(0)), 1)
stringForH = ecc_math.ECPointToHexString(ecc_math.ECPoint{X: H.X, Y: H.Y}) + stringForH // H.X + H.Y + 00
for i := int64(0); i < params.V; i++ {
GVector[i], err = hash.SHA256MapToGroup(stringForG + ecc_math.FillLeftHexToNBytes(fmt.Sprintf("%x", big.NewInt(i)), 1)) // x + y + 00 + i
for err != nil {
stringForG = stringForG + ecc_math.FillLeftHexToNBytes(fmt.Sprintf("%x", big.NewInt(i)), 1)
GVector[i], err = hash.SHA256MapToGroup(stringForG)
fmt.Println(GVector[i])
}
if new(big.Int).Div( params.N,big.NewInt(2) ).Cmp( GVector[i].Y ) == 1 { GVector[i].Y = new(big.Int).Sub(params.N, GVector[i].Y) }
params.GVector[i] = ECPoint{GVector[i].X, GVector[i].Y}
HVector[i], err = hash.SHA256MapToGroup(stringForH + ecc_math.FillLeftHexToNBytes(fmt.Sprintf("%x", big.NewInt(i)), 1))
for err != nil {
stringForH = stringForH + ecc_math.FillLeftHexToNBytes(fmt.Sprintf("%x", big.NewInt(i)), 1)
HVector[i], err = hash.SHA256MapToGroup(stringForH)
fmt.Println(HVector[i])
}
if new(big.Int).Div( params.N,big.NewInt(2) ).Cmp( HVector[i].Y ) == 1 { HVector[i].Y = new(big.Int).Sub(params.N, HVector[i].Y) }
params.HVector[i] = ECPoint{HVector[i].X, HVector[i].Y}
}
return params, nil
}
package bulletproof
import (
"fmt"
"testing"
)
func TestNewECPrimeGroupKey(t *testing.T) {
params, _ := NewECPrimeGroupKey(64)
//params = CryptoParams{}
fmt.Println("params G", params.G)
fmt.Println("params H", params.H)
i :=0
l := len(params.GVector)
fmt.Println("params Gvector ... len ", l)
for i < l {
fmt.Println(params.GVector[i])
i++
}
i = 0
l = len(params.HVector)
fmt.Println("params Gvector ... len ", l)
for i < l {
fmt.Println(params.HVector[i])
i++
}
}
package bulletproof
import (
"math/big"
)
var EC CryptoParams
type ECPoint struct {
X, Y *big.Int
}
// Equal returns true if points p (self) and p2 (arg) are the same.
func (p ECPoint) Equal(p2 ECPoint) bool {
if p.X.Cmp(p2.X) == 0 && p2.Y.Cmp(p2.Y) == 0 {
return true
}
return false
}
// Mult multiplies point p by scalar s and returns the resulting point
func (p ECPoint) Mult(s *big.Int) ECPoint {
modS := new(big.Int).Mod(s, EC.N)
X, Y := EC.C.ScalarMult(p.X, p.Y, modS.Bytes())
if X == nil {
X = new(big.Int).SetInt64(0)
}
if Y == nil {
Y = new(big.Int).SetInt64(0)
}
return ECPoint{X, Y}
}
// Add adds points p and p2 and returns the resulting point
func (p ECPoint) Add(p2 ECPoint) ECPoint {
X, Y := EC.C.Add(p.X, p.Y, p2.X, p2.Y)
return ECPoint{X, Y}
}
// Neg returns the additive inverse of point p
func (p ECPoint) Neg() ECPoint {
negY := new(big.Int).Neg(p.Y)
modValue := negY.Mod(negY, EC.C.Params().P) // mod P is fine here because we're describing a curve point
return ECPoint{p.X, modValue}
}
func SumPoints(points []ECPoint) ECPoint {
sum := points[0]
for i := 1; i < len(points); i++ {
sum = sum.Add(points[i])
}
return sum
}
This diff is collapsed.
package bulletproof
import (
"fmt"
"reflect"
"testing"
)
func TestRP(t *testing.T) {
EC, _ = NewECPrimeGroupKey(64)
v := int32(500) // 111110100
varr := make([]int32, 2)
varr[0] = v
varr[1] = int32(98760)
rp, _ := RP(v)
//tx*B + tx'*B' = z^2*V + delta*B + x*T1 + x^2*T2
i := 0
for (!rp.verifytx) {
rp, _ = RP(v)
i++
}
fmt.Println(i, "verify N1: ", rp.verifytx)
//fmt.Println("rp: ", rp.Comm)
////rp, _ = RPVector2(varr[0])
//
fmt.Println("comm: ", rp.Comm)
fmt.Println("A: ", rp.A)
fmt.Println("S: ", rp.S)
fmt.Println("T1: ", rp.T1)
fmt.Println("T2: ", rp.T2)
fmt.Println("tx: ", rp.tx) // ???? not use
fmt.Println("tx3 ", rp.tx3)
fmt.Println("tsjx: ", rp.tsjx) // ???? not use
fmt.Println("ttx3 ", rp.ttx3)
fmt.Println("es: ", rp.es)
fmt.Println("IPP.L: ", rp.IPP.L)
fmt.Println("IPP.R: ", rp.IPP.R)
fmt.Println("IPP.A: ", rp.IPP.A)
fmt.Println("IPP.B: ", rp.IPP.B)
fmt.Println("y: ", rp.y)
fmt.Println("z: ", rp.z)
fmt.Println("x: ", rp.x)
}
func TestRP1(t *testing.T) {
type args struct {
_v int32
}
tests := []struct {
name string
args args
want RangeProof
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := RP(tt.args._v)
if (err != nil) != tt.wantErr {
t.Errorf("RP() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("RP() got = %v, want %v", got, tt.want)
}
})
}
}
package bulletproof
import (
"crypto/rand"
"fmt"
"math/big"
)
func VectorAdd(v []*big.Int, w []*big.Int) []*big.Int {
if len(v) != len(w) {
fmt.Println("VectorAdd: Arrays not of the same length")
fmt.Printf("len(v): %d\n", len(v))
fmt.Printf("len(w): %d\n", len(w))
}
result := make([]*big.Int, len(v))
for i := range v {
result[i] = new(big.Int).Mod(new(big.Int).Add(v[i], w[i]), EC.N)
}
return result
}
func RandVector(l int) []*big.Int {
result := make([]*big.Int, l)
for i := 0; i < l; i++ {
x, err := rand.Int(rand.Reader, EC.N)
check(err)
result[i] = x
}
return result
}
func VectorAddScalar(v []*big.Int, s *big.Int) []*big.Int {
result := make([]*big.Int, len(v))
for i := range v {
result[i] = new(big.Int).Mod(new(big.Int).Add(v[i], s), EC.N)
}
return result
}
func VectorSubScalar(v []*big.Int, s *big.Int) []*big.Int {
result := make([]*big.Int, len(v))
for i := 0; i < len(v); i++ {
result[i] = new(big.Int).Mod(new(big.Int).Sub(v[i], s), EC.N)
}
return result
}
func ScalarVectorMul(v []*big.Int, s *big.Int) []*big.Int {
result := make([]*big.Int, len(v))
for i := range v {
result[i] = new(big.Int).Mod(new(big.Int).Mul(v[i], s), EC.N)
}
return result
}
func VectorSum(y []*big.Int) *big.Int {
result := big.NewInt(0)
for _, j := range y {
result = new(big.Int).Mod(new(big.Int).Add(result, j), EC.N)
}
return result
}
func VectorHadamard(v, w []*big.Int) []*big.Int {
if len(v) != len(w) {
fmt.Println("VectorHadamard: Arrays not of the same length")
fmt.Printf("len(v): %d\n", len(w))
fmt.Printf("len(w): %d\n", len(v))
}
result := make([]*big.Int, len(v))
for i := range v {
result[i] = new(big.Int).Mod(new(big.Int).Mul(v[i], w[i]), EC.N)
}
return result
}
func PowerVector(l int, base *big.Int) []*big.Int {
result := make([]*big.Int, l)
fmt.Println("PowerVector : base= ", base, " l= ", l)
for i := 0; i < l; i++ {
result[i] = new(big.Int).Exp(base, big.NewInt(int64(i)), EC.N)
}
return result
}
func reverse(l []*big.Int) []*big.Int {
result := make([]*big.Int, len(l))
for i := range l {
result[i] = l[len(l)-i-1]
}
return result
}
This diff is collapsed.
module offshift
go 1.17
require (
github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 // indirect
github.com/btcsuite/btcd v0.20.1-beta // indirect
github.com/deckarep/golang-set v0.0.0-20180603214616-504e848d77ea // indirect
github.com/ethereum/go-ethereum v1.10.12 // indirect
github.com/go-ole/go-ole v1.2.1 // indirect
github.com/go-stack/stack v1.8.0 // indirect
github.com/google/uuid v1.1.5 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/ing-bank/zkrp v0.0.0-20211018091920-bc4eff1b3466 // indirect
github.com/rjeczalik/notify v0.9.1 // indirect
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible // indirect
github.com/tklauser/go-sysconf v0.3.5 // indirect
github.com/tklauser/numcpus v0.2.2 // indirect
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 // indirect
golang.org/x/sys v0.0.0-20210816183151-1e6c022a8912 // indirect
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect
)
This diff is collapsed.
package hash
import (
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"math/big"
"github.com/ing-bank/zkrp/crypto/p256"
"offshift/secp256k1/ecc_math"
)
func SHA256StringToString(message string) (digest string) {
var dig [32]byte = sha256.Sum256([]byte(message))
digest = string(dig[:])
return
} // string -SHA256> string
func SHA256StringToByteArray(message string) (digest [32]byte) {
digest = sha256.Sum256([]byte(message))
return
} // string -SHA256> []byteSHA256StringToString