func TestBigIntAnd(t *testing.T) { found := bigIntAnd([][]byte{common.EncodeBigInt(big.NewInt(1))}, [][]byte{common.EncodeBigInt(big.NewInt(2)), common.EncodeBigInt(big.NewInt(3)), common.EncodeBigInt(big.NewInt(4))}, 1) expected := [][]byte{common.EncodeBigInt(big.NewInt(0))} if !reflect.DeepEqual(found, expected) { t.Errorf("%v should be %v", fmt.Sprint(common.DecodeBigInt(found[0])), fmt.Sprint(common.DecodeBigInt(expected[0]))) } found = bigIntAnd([][]byte{common.EncodeBigInt(big.NewInt(1)), common.EncodeBigInt(big.NewInt(3))}, [][]byte{common.EncodeBigInt(big.NewInt(3)), common.EncodeBigInt(big.NewInt(5))}, 1) expected = [][]byte{common.EncodeBigInt(big.NewInt(1))} if !reflect.DeepEqual(found, expected) { t.Errorf("%v should be %v", fmt.Sprint(common.DecodeBigInt(found[0])), fmt.Sprint(common.DecodeBigInt(expected[0]))) } found = bigIntAnd(nil, [][]byte{common.EncodeBigInt(big.NewInt(1)), common.EncodeBigInt(big.NewInt(2)), common.EncodeBigInt(big.NewInt(3)), common.EncodeBigInt(big.NewInt(4))}, 1) expected = [][]byte{common.EncodeBigInt(big.NewInt(0))} if !reflect.DeepEqual(found, expected) { t.Errorf("%v should be %v", fmt.Sprint(common.DecodeBigInt(found[0])), fmt.Sprint(common.DecodeBigInt(expected[0]))) } found = bigIntAnd(nil, [][]byte{common.EncodeBigInt(big.NewInt(1)), common.EncodeBigInt(big.NewInt(3)), common.EncodeBigInt(big.NewInt(3)), common.EncodeBigInt(big.NewInt(5))}, 1) expected = [][]byte{common.EncodeBigInt(big.NewInt(1))} if !reflect.DeepEqual(found, expected) { t.Errorf("%v should be %v", fmt.Sprint(common.DecodeBigInt(found[0])), fmt.Sprint(common.DecodeBigInt(expected[0]))) } found = bigIntAnd([][]byte{common.EncodeBigInt(big.NewInt(15))}, [][]byte{common.EncodeBigInt(big.NewInt(3))}, 2) expected = [][]byte{common.EncodeBigInt(big.NewInt(6))} if !reflect.DeepEqual(found, expected) { t.Errorf("%v should be %v", fmt.Sprint(common.DecodeBigInt(found[0])), fmt.Sprint(common.DecodeBigInt(expected[0]))) } }
func bigIntMul(oldValues [][]byte, newValues [][]byte, w float64) (result [][]byte) { sum := big.NewInt(1) for _, b := range oldValues { sum.Mul(sum, common.DecodeBigInt(b)) } for _, b := range newValues { sum.Mul(sum, new(big.Int).Mul(common.DecodeBigInt(b), big.NewInt(int64(w)))) } return [][]byte{sum.Bytes()} }
func bigIntXor(oldValues [][]byte, newValues [][]byte, w float64) (result [][]byte) { var sum *big.Int if oldValues != nil { sum = new(big.Int).SetBytes(oldValues[0]) for _, b := range newValues { sum.Xor(sum, new(big.Int).Mul(common.DecodeBigInt(b), big.NewInt(int64(w)))) } } else { sum = new(big.Int).Mul(new(big.Int).SetBytes(newValues[0]), big.NewInt(int64(w))) for _, b := range newValues[1:] { sum.Xor(sum, new(big.Int).Mul(common.DecodeBigInt(b), big.NewInt(int64(w)))) } } return [][]byte{sum.Bytes()} }
func decode(b []byte) string { switch *enc { case stringFormat: return string(b) case floatFormat: res, err := common.DecodeFloat64(b) if err != nil { return fmt.Sprint(b) } return fmt.Sprint(res) case intFormat: res, err := common.DecodeInt64(b) if err != nil { return fmt.Sprint(b) } return fmt.Sprint(res) case bigFormat: return fmt.Sprint(common.DecodeBigInt(b)) } panic(fmt.Errorf("Unknown encoding: %v", *enc)) }