From 701b938562e61e337637be60d3fc9681f117eaa3 Mon Sep 17 00:00:00 2001 From: Brendan McMillion Date: Wed, 4 Nov 2015 11:45:32 -0800 Subject: [PATCH] Import bug fixes from MSP. --- msp/msp.go | 25 ++++++++++++++++++------- msp/raw.go | 9 ++++++++- msp/raw_test.go | 8 ++++++++ 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/msp/msp.go b/msp/msp.go index 3642593..c7e3db3 100644 --- a/msp/msp.go +++ b/msp/msp.go @@ -198,16 +198,16 @@ func (m MSP) DistributeShares(sec []byte, modulus *big.Int, db *UserDatabase) (m case String: name := cond.(String).string if _, ok := out[name]; ok { - out[name] = append(out[name], share.Bytes()) + out[name] = append(out[name], m.encode(share, modulus)) } else if (*db).ValidUser(name) { - out[name] = [][]byte{share.Bytes()} + out[name] = [][]byte{m.encode(share, modulus)} } else { return out, errors.New("Unknown user in predicate.") } default: below := MSP(cond.(Formatted)) - subOut, err := below.DistributeShares(share.Bytes(), modulus, db) + subOut, err := below.DistributeShares(m.encode(share, modulus), modulus, db) if err != nil { return out, err } @@ -343,8 +343,6 @@ func (m MSP) recoverSecret(modulus *big.Int, db *UserDatabase, cache map[string] // Compute dot product of the shares vector and the reconstruction vector to // reconstruct the secret. - size := len(modulus.Bytes()) - out := make([]byte, size) secInt := big.NewInt(0) for i, share := range shares { @@ -364,6 +362,19 @@ func (m MSP) recoverSecret(modulus *big.Int, db *UserDatabase, cache map[string] secInt.Add(secInt, shareInt).Mod(secInt, modulus) } - out = append(out, secInt.Bytes()...) - return out[len(out)-size:], nil + return m.encode(secInt, modulus), nil +} + +func (m MSP) encode(x *big.Int, modulus *big.Int) (out []byte) { + tmp := x.Bytes() + tmpSize := len(tmp) + + modSize := len(modulus.Bytes()) + out = make([]byte, modSize) + + for pos := 0; pos < tmpSize; pos++ { + out[modSize-pos-1] = tmp[tmpSize-pos-1] + } + + return out } diff --git a/msp/raw.go b/msp/raw.go index baae187..cc939b4 100644 --- a/msp/raw.go +++ b/msp/raw.go @@ -133,7 +133,14 @@ func StringToRaw(r string) (out Raw, err error) { if staging.Len() == 0 { if len(r) == 0 { - return top[0].Front().Value.(Raw), nil + res := top[0].Front().Value + + switch res.(type) { + case Raw: + return res.(Raw), nil + default: + return out, errors.New("Invalid string: Only one condition was found.") + } } return out, errors.New("Invalid string: Can't parse anymore, but there's still data. Too many closing parentheses or too few opening parentheses?") } diff --git a/msp/raw_test.go b/msp/raw_test.go index c9e69ab..1343c10 100644 --- a/msp/raw_test.go +++ b/msp/raw_test.go @@ -72,3 +72,11 @@ func TestRaw(t *testing.T) { t.Fatalf("Query #2 formatted wrong: %v", query2.Formatted().String()) } } + +func TestOneCondition(t *testing.T) { + _, err := StringToRaw("(Alice or Bob)") + + if err == nil { + t.Fatalf("A predicate with only one condition should fail to parse!") + } +}