markkurossi / mpc

Secure Multi-Party Computation (MPC) with Go. This project implements secure two-party computation with Garbled circuit protocol.
https://www.markkurossi.com/mpcl/index.html
MIT License
109 stars 22 forks source link

String Type in mpcl #17

Closed lucy-sha512 closed 1 year ago

lucy-sha512 commented 1 year ago

I am trying to convert a byte array to a hex string in mpcl. Here is the code from Sha256 HMAC. I saw a type defined as type stringSize string but didn't get any tests on this.

In the code below I want to convert the variable key to a string and pass it to HMAC hmac.SumSHA256([]byte("message"), []byte("abc")) such that it looks like this: hmac.SumSHA256([]byte("message"), []byte(string_key))

package main

import (
    "crypto/hmac"
)

type Garbler struct {
    msg      [32]byte
    keyShare [64]byte
}

func main(g Garbler, eKeyShare [64]byte) []byte {
    var key [64]byte

    for i := 0; i < len(key); i++ {
        key[i] = g.keyShare[i] ^ eKeyShare[i]
    }

This was the code I wrote to convert byte array to hex string: package main


import "fmt"

func main() {
    byteArray := []byte{0x48, 0x65, 0x6C, 0x6C, 0x6F} // Example byte array

    // Convert the byte array to a hexadecimal string
    var hexString string
    for _, b := range byteArray {
        hexString += byteToHexString(b)
    }

    fmt.Println(hexString)
}

func byteToHexString(b byte) string {
    hexChars := "0123456789ABCDEF"
    high := b >> 4
    low := b & 0x0F
    return string(hexChars[high]) + string(hexChars[low])
}

but string is not accepted by mpcl. Can we write a package that handles this conversion?

lucy-sha512 commented 1 year ago

I figured out the stringSize type and tried to run the following code as a test:

package main

import (

    "crypto/hmac"

)

type Garbler struct {
    msg      [32]byte
    keyShare [64]byte
}

func main(g Garbler, eKeyShare [64]byte) []byte {
    var key [64]byte

    for i := 0; i < len(key); i++ {
        key[i] = g.keyShare[i] ^ eKeyShare[i]
    }

    byteArray := []byte{0x48, 0x65, 0x6C, 0x6C, 0x6F} // Example byte array

    var hexString string10
    for i:=0; i < len(byteArray); i++ {
        hexString += byteToHexString(byteArray[i])
    }

    return hmac.SumSHA256(g.msg,  key)

}

func byteToHexString(b byte) string {
    hexChars := "0123456789ABCDEF"
    high := b >> 4
    low := b & 0x0F
    return string(hexChars[high]) + string(hexChars[low])
}

but it throws an exception

./garbled -e -v -i 0xf87a00ef89c2396de32f6ac0748f6fa1b641013d46f74ce25cc625904215a67501c0c7196a2602f6516527958a82271847933c35d170d98bfdb04d2ddf3bb197 examples/hmac-sha256.mpcl
Listening for connections at :8080
New connection from 127.0.0.1:54384
found PkgRoot from '/home/lucy/go/src/github.com/markkurossi/mpc/pkg'
looking for package hmac (crypto/hmac)
 - parsing @/crypto/hmac/doc.mpcl
 - parsing @/crypto/hmac/sha512.mpcl
looking for package sha512 (crypto/sha512)
 - parsing @/crypto/sha512/sum.mpcl
 - parsing @/crypto/hmac/sha256.mpcl
looking for package sha256 (crypto/sha256)
 - parsing @/crypto/sha256/sum.mpcl
Initializing main
Initializing hmac
Initializing sha512
Initializing sha256
panic: v.Type.Bits == 0: $""

goroutine 1 [running]:
github.com/markkurossi/mpc/compiler/ssa.Value.Check({{0xc000381adb, 0x3}, 0x568, 0x0, 0x1, 0x0, 0x0, {0x0, 0x5, 0x0, ...}, ...})
    /home/lucy/go/src/github.com/markkurossi/mpc/compiler/ssa/value.go:68 +0x85
github.com/markkurossi/mpc/compiler/ssa.Instr.Check(...)
    /home/lucy/go/src/github.com/markkurossi/mpc/compiler/ssa/instructions.go:162
github.com/markkurossi/mpc/compiler/ssa.(*Block).AddInstr(...)
    /home/lucy/go/src/github.com/markkurossi/mpc/compiler/ssa/block.go:79
github.com/markkurossi/mpc/compiler/ast.(*VariableDef).SSA(0xc0000b50a0, 0xc0000fc9a0, 0xc0000b5e30, 0xc0000f7920)
    /home/lucy/go/src/github.com/markkurossi/mpc/compiler/ast/ssagen.go:205 +0x1238
github.com/markkurossi/mpc/compiler/ast.List.SSA({0xc00002c800?, 0x7, 0x1000000000014?}, 0x0?, 0xc0000b5e30, 0x0?)
    /home/lucy/go/src/github.com/markkurossi/mpc/compiler/ast/ssagen.go:39 +0x95
github.com/markkurossi/mpc/compiler/ast.(*Func).SSA(0xc00009e9c0, 0xc0000fc9a0, 0xc0000b5e30, 0x0?)
    /home/lucy/go/src/github.com/markkurossi/mpc/compiler/ast/ssagen.go:74 +0x6c7
github.com/markkurossi/mpc/compiler/ast.(*Package).Compile(0xc0000a08c0, 0xc0000b5e30)
    /home/lucy/go/src/github.com/markkurossi/mpc/compiler/ast/package.go:110 +0xc85
github.com/markkurossi/mpc/compiler.(*Compiler).compile(0xc00007c700, {0x7fff1a48129a, 0x19}, {0x6d02e0, 0xc0000600b8}, {0xc0000f6090, 0x2, 0x2})
    /home/lucy/go/src/github.com/markkurossi/mpc/compiler/compiler.go:85 +0x2e5
github.com/markkurossi/mpc/compiler.(*Compiler).CompileFile(0x0?, {0x7fff1a48129a, 0x19}, {0xc0000f6090, 0x2, 0x2})
    /home/lucy/go/src/github.com/markkurossi/mpc/compiler/compiler.go:60 +0x145
main.loadCircuit({0x7fff1a48129a, 0x19}, 0xc0000fa000, {0xc0000f6090, 0x2, 0x2})
    /home/lucy/go/src/github.com/markkurossi/mpc/apps/garbled/main.go:163 +0x252
main.evaluatorMode({0x6d10d8, 0xc000092320}, {0x7fff1a48129a, 0x19}, 0x66e639?, 0x0)
    /home/lucy/go/src/github.com/markkurossi/mpc/apps/garbled/main.go:260 +0x4e5
main.main()
    /home/lucy/go/src/github.com/markkurossi/mpc/apps/garbled/main.go:142 +0x89f

Can you guide why this happens?

lucy-sha512 commented 1 year ago

I want to import golang package like "fmt" and "hex/encoding" but it throws this error: found PkgRoot from '/home/lucy/go/src/github.com/markkurossi/mpc/pkg' looking for package hmac (crypto/hmac)

 - parsing @/crypto/hmac/doc.mpcl
 - parsing @/crypto/hmac/sha512.mpcl
looking for package sha512 (crypto/sha512)
 - parsing @/crypto/sha512/sum.mpcl
 - parsing @/crypto/hmac/sha256.mpcl
looking for package sha256 (crypto/sha256)
 - parsing @/crypto/sha256/sum.mpcl
looking for package hex (encoding/hex)
package encoding/hex not found

Any way to fix this?

markkurossi commented 1 year ago

The string support is still very limited. I implemented cast operation from []byte to string string([]byte) so now it is possible to convert from bytes to strings and vice versa. I also created encoding/hex package that implements EncodeToString(src []byte) string function.

The fmt package is not currently supported. All supported packages can be found from the pkg directory from the root of the repository.

lucy-sha512 commented 1 year ago

Thank you for your input. I guess the message is also handled in form of hex bytes. We want to have the message in form of utf encoding . We are using this library to fetch API response from a server. According to it, the message string is encoded in utf: mac = hmac.new(bytes(secret_key, encoding='utf8'), bytes(message, encoding='utf-8'), digestmod='sha256')

xomexh commented 11 months ago

The string support is still very limited. I implemented cast operation from []byte to string string([]byte) so now it is possible to convert from bytes to strings and vice versa. I also created encoding/hex package that implements EncodeToString(src []byte) string function.

The fmt package is not currently supported. All supported packages can be found from the pkg directory from the root of the repository.

Hi, I'm trying to cast the msg param of Garbler struct, into string, but it does not seem to be working.

Modified examples/hmac-sha256.mpcl file

type Garbler struct {
    msg      [43]byte,
    keyShare [64]byte
}

func main(g Garbler, eKeyShare [64]byte) []byte {
    var key [64]byte

    for i := 0; i < len(key); i++ {
        key[i] = g.keyShare[i] ^ eKeyShare[i]
    }

    hexBytes := make([]byte, len(key)*2) 
    var b byte
    var hexChars [64]byte
    for i := 0; i < len(key); i++ {
        b = key[i]
        hexChars = byteToHexString(b)
        hexBytes[i*2] = hexChars[0]
        hexBytes[i*2+1] = hexChars[1]
    }

    hexString := string512(hexBytes)
    mType := make([]byte, size(hexString)/8)
    for i := 0; i < len(hexString) ; i++ {
        mType[i] = hexString[i]
    }

    resultString := string344(g.msg)
    sType := make([]byte, size(resultString)/8)
    for i := 0; i < len(resultString) ; i++ {
        sType[i] = resultString[i]
    }

    return hmac.SumSHA256(sType,  mType)

}
func byteToHexString(b byte) []byte {
    hexChars := []byte("0123456789abcdef")
    high := b >> 4
    low := b & 0x0F
    result := make([]byte, 2)
    result[0] = hexChars[high]
    result[1] = hexChars[low]
    return result
}

These changes yield the proper HMAC Key for me. However, as I want to make the msg field dynamic, i.e msg []bytes

I encounter this error:

Initializing hmac
Initializing sha256
Initializing sha512
panic: v.Type.Bits == 0: %_{0,835}arr

goroutine 1 [running]:
github.com/markkurossi/mpc/compiler/ssa.Value.Check({{0x100979202, 0x2}, 0x1cb5, 0x0, 0x0, 0x0, 0x343, {0x0, 0x7, 0x0, ...}, ...})
        /Users/soms/go/src/github.com/markkurossi/mpc/compiler/ssa/value.go:68 +0x8c
github.com/markkurossi/mpc/compiler/ssa.Instr.Check(...)
        /Users/soms/go/src/github.com/markkurossi/mpc/compiler/ssa/instructions.go:162
github.com/markkurossi/mpc/compiler/ssa.(*Block).AddInstr(...)
        /Users/soms/go/src/github.com/markkurossi/mpc/compiler/ssa/block.go:79
github.com/markkurossi/mpc/compiler/ast.(*Call).SSA(0x140001cae00, 0x14000479420, 0x14000420540, 0x140003f38c0)
        /Users/soms/go/src/github.com/markkurossi/mpc/compiler/ast/ssagen.go:681 +0x1084
github.com/markkurossi/mpc/compiler/ast.(*Assign).SSA(0x140001a7020, 0x14000479420?, 0x14000420540, 0x140003f38c0)
        /Users/soms/go/src/github.com/markkurossi/mpc/compiler/ast/ssagen.go:261 +0x430
github.com/markkurossi/mpc/compiler/ast.List.SSA({0x140001ec700?, 0xe, 0x1000000000049?}, 0x0?, 0x14000420540, 0x0?)
        /Users/soms/go/src/github.com/markkurossi/mpc/compiler/ast/ssagen.go:39 +0x8c
github.com/markkurossi/mpc/compiler/ast.(*Func).SSA(0x14000196dd0, 0x14000247180, 0x14000420540, 0x0?)
        /Users/soms/go/src/github.com/markkurossi/mpc/compiler/ast/ssagen.go:74 +0x588
github.com/markkurossi/mpc/compiler/ast.(*Package).Compile(0x14000198960, 0x14000420540)
        /Users/soms/go/src/github.com/markkurossi/mpc/compiler/ast/package.go:110 +0xa18
github.com/markkurossi/mpc/compiler.(*Compiler).compile(0x140001c2700, {0x16f663524, 0x19}, {0x100a46740, 0x140001920a0}, {0x140002060f0, 0x2, 0x2})
        /Users/soms/go/src/github.com/markkurossi/mpc/compiler/compiler.go:85 +0x260
github.com/markkurossi/mpc/compiler.(*Compiler).CompileFile(0x140001d1be8?, {0x16f663524, 0x19}, {0x140002060f0, 0x2, 0x2})
        /Users/soms/go/src/github.com/markkurossi/mpc/compiler/compiler.go:60 +0xf0
main.loadCircuit({0x16f663524, 0x19}, 0x1400020c000, {0x140002060f0, 0x2, 0x2})
        /Users/soms/go/src/github.com/markkurossi/mpc/apps/garbled/main.go:163 +0x1e8
main.garblerMode({0x100a47580, 0x140001ae320}, {0x16f663524, 0x19}, 0x100985a32?)
        /Users/soms/go/src/github.com/markkurossi/mpc/apps/garbled/main.go:322 +0x29c
main.main()
        /Users/soms/go/src/github.com/markkurossi/mpc/apps/garbled/main.go:144 +0x800

I have tried changing msg [43]bytes and resultString := string344(g.msg) to msg []bytes and resultString := string(g.msg), however I face the error

Initializing hmac
Initializing sha256
Initializing sha512
Initializing hex
panic: v.Type.Bits == 0: %_{0,835}arr

goroutine 1 [running]:
github.com/markkurossi/mpc/compiler/ssa.Value.Check({{0x102fad202, 0x2}, 0x1cb5, 0x0, 0x0, 0x0, 0x343, {0x0, 0x7, 0x0, ...}, ...})
        /Users/soms/go/src/github.com/markkurossi/mpc/compiler/ssa/value.go:68 +0x8c
github.com/markkurossi/mpc/compiler/ssa.Instr.Check(...)
        /Users/soms/go/src/github.com/markkurossi/mpc/compiler/ssa/instructions.go:162
github.com/markkurossi/mpc/compiler/ssa.(*Block).AddInstr(...)
        /Users/soms/go/src/github.com/markkurossi/mpc/compiler/ssa/block.go:79
github.com/markkurossi/mpc/compiler/ast.(*Call).SSA(0x14000078e40, 0x1400038b880, 0x14000364540, 0x140003318c0)
        /Users/soms/go/src/github.com/markkurossi/mpc/compiler/ast/ssagen.go:681 +0x1084
github.com/markkurossi/mpc/compiler/ast.(*Assign).SSA(0x1400007b080, 0x1400038b880?, 0x14000364540, 0x140003318c0)
        /Users/soms/go/src/github.com/markkurossi/mpc/compiler/ast/ssagen.go:261 +0x430
github.com/markkurossi/mpc/compiler/ast.List.SSA({0x14000148700?, 0xe, 0x1000000000049?}, 0x0?, 0x14000364540, 0x0?)
        /Users/soms/go/src/github.com/markkurossi/mpc/compiler/ast/ssagen.go:39 +0x8c
github.com/markkurossi/mpc/compiler/ast.(*Func).SSA(0x1400007ed00, 0x14000177180, 0x14000364540, 0x0?)
        /Users/soms/go/src/github.com/markkurossi/mpc/compiler/ast/ssagen.go:74 +0x588
github.com/markkurossi/mpc/compiler/ast.(*Package).Compile(0x14000108960, 0x14000364540)
        /Users/soms/go/src/github.com/markkurossi/mpc/compiler/ast/package.go:110 +0xa18
github.com/markkurossi/mpc/compiler.(*Compiler).compile(0x1400006e720, {0x16d02f524, 0x19}, {0x10307a740, 0x140000520b0}, {0x140001600f0, 0x2, 0x2})
        /Users/soms/go/src/github.com/markkurossi/mpc/compiler/compiler.go:85 +0x260
github.com/markkurossi/mpc/compiler.(*Compiler).CompileFile(0x14000131be8?, {0x16d02f524, 0x19}, {0x140001600f0, 0x2, 0x2})
        /Users/soms/go/src/github.com/markkurossi/mpc/compiler/compiler.go:60 +0xf0
main.loadCircuit({0x16d02f524, 0x19}, 0x14000164000, {0x140001600f0, 0x2, 0x2})
        /Users/soms/go/src/github.com/markkurossi/mpc/apps/garbled/main.go:163 +0x1e8
main.garblerMode({0x10307b580, 0x14000012410}, {0x16d02f524, 0x19}, 0x102fb9a32?)
        /Users/soms/go/src/github.com/markkurossi/mpc/apps/garbled/main.go:322 +0x29c
main.main()
        /Users/soms/go/src/github.com/markkurossi/mpc/apps/garbled/main.go:144 +0x800

Could you give insights on how I can make the message field dynamic accordingly?

markkurossi commented 11 months ago

There has been quite many changes in the string type and type conversions lately. Could you please pull the latest version and test if the problem still persists. I tried you example on top of the master and it seems to work - but it always Works for Me(TM) :)

xomexh commented 10 months ago

Hi Mark, a recent pull and build solved my errors. Thank you so much!