package main | |
import ( | |
"crypto/hmac" | |
"crypto/rand" | |
"crypto/sha1" | |
"fmt" | |
"math/big" | |
) | |
var ( | |
n = []byte{ | |
1, | |
3, | |
10, | |
20, | |
62, | |
95, | |
} | |
nPi = map[byte]byte{ | |
1: 3, | |
3: 10, | |
10: 20, | |
20: 62, | |
62: 95, | |
95: 1, | |
} | |
piN = map[byte]byte{ | |
3: 1, | |
10: 3, | |
20: 10, | |
62: 20, | |
95: 62, | |
1: 95, | |
} | |
) | |
func cmp(x, y int) int { | |
if x > y { | |
return 1 | |
} | |
if x < y { | |
return -1 | |
} | |
return 0 | |
} | |
func hash(input []byte) []byte { | |
sha := sha1.New() | |
sha.Write(input) | |
return sha.Sum(nil) | |
} | |
func prf(secret, input []byte) []byte { | |
mac := hmac.New(sha1.New, secret) | |
mac.Write(input) | |
return mac.Sum(nil) | |
} | |
func main() { | |
fmt.Println("Example LW 2016 Implementation") | |
ctr := [][]byte{} | |
ctl := [][]byte{} | |
var secret = []byte("secret") | |
for _, vv := range n { | |
// encrypt left value 1 | |
var nonce = make([]byte, 1) | |
rand.Read(nonce) | |
encLeft := prf(secret, append([]byte{}, nPi[vv])) | |
var encRight = []byte{nonce[0]} | |
for _, v := range n { | |
var vi = big.NewInt(int64(cmp(int(v), int(vv)))) | |
//fmt.Println(vi) | |
var tmp = big.NewInt(0) | |
tmp.SetBytes(hash( | |
append( | |
prf(secret, append([]byte{}, nPi[v])), | |
nonce[0]))) | |
vi = vi.Add(vi, tmp) | |
vi = vi.Mod(vi, big.NewInt(3)) | |
switch vi.Int64() { | |
case 0: | |
encRight = append(encRight, byte(0)) | |
case 1: | |
encRight = append(encRight, byte(1)) | |
case 2: | |
encRight = append(encRight, byte(2)) | |
} | |
} | |
ctl = append(ctl, encLeft) | |
ctr = append(ctr, encRight) | |
} | |
// lets now do some compares! | |
// take ctr[0] and compare to ctl[0] should equal | |
left := ctl[0] | |
right := ctr[0] | |
fmt.Println("left: ", left) | |
fmt.Println("right: ", right) | |
vh := big.NewInt(int64(right[1])) | |
h := big.NewInt(0) | |
h.SetBytes(hash(append(left, right[0]))) | |
v := vh.Sub(vh, h) | |
v = v.Mod(v, big.NewInt(3)) | |
fmt.Println("compare result for N[0] to N[0] is ", v) | |
// take ctr[1] and compare to ctl[0] should greater than | |
left = ctl[0] | |
right = ctr[1] | |
fmt.Println("left: ", left) | |
fmt.Println("right: ", right) | |
vh = big.NewInt(int64(right[1])) | |
h = big.NewInt(0) | |
h.SetBytes(hash(append(left, right[0]))) | |
v = vh.Sub(vh, h) | |
v = v.Mod(v, big.NewInt(3)) | |
fmt.Println("compare result for N[1] to N[0] is ", v) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.