package base58

import (
	
	
)

var (
	bn0  = big.NewInt(0)
	bn58 = big.NewInt(58)
)

// Encode encodes the passed bytes into a base58 encoded string.
func ( []byte) string {
	return FastBase58Encoding()
}

// EncodeAlphabet encodes the passed bytes into a base58 encoded string with the
// passed alphabet.
func ( []byte,  *Alphabet) string {
	return FastBase58EncodingAlphabet(, )
}

// FastBase58Encoding encodes the passed bytes into a base58 encoded string.
func ( []byte) string {
	return FastBase58EncodingAlphabet(, BTCAlphabet)
}

// FastBase58EncodingAlphabet encodes the passed bytes into a base58 encoded
// string with the passed alphabet.
func ( []byte,  *Alphabet) string {
	 := .encode[0]

	 := len()
	var , , ,  int
	var  uint32

	for  <  && [] == 0 {
		++
	}

	 := ((-)*138/100 + 1)

	// allocate one big buffer up front
	 := make([]byte, *2+)

	// use the second half for the temporary buffer
	 := [+:]

	 =  - 1
	for  = ;  < ; ++ {
		 =  - 1
		for  = uint32([]);  >  ||  != 0; -- {
			 =  + 256*uint32([])
			[] = byte( % 58)
			 /= 58
		}
		 = 
	}

	for  = 0;  <  && [] == 0; ++ {
	}

	// Use the first half for the result
	 := [:-+]

	if  != 0 {
		for  = 0;  < ; ++ {
			[] = 
		}
	}

	for  = ;  < ; ++ {
		[] = .encode[[]]
		++
	}

	return string()
}

// TrivialBase58Encoding encodes the passed bytes into a base58 encoded string
// (inefficiently).
func ( []byte) string {
	return TrivialBase58EncodingAlphabet(, BTCAlphabet)
}

// TrivialBase58EncodingAlphabet encodes the passed bytes into a base58 encoded
// string (inefficiently) with the passed alphabet.
func ( []byte,  *Alphabet) string {
	 := .encode[0]
	 := len()*138/100 + 1
	 := make([]byte, )
	 := new(big.Int).SetBytes()
	var  *big.Int
	for .Cmp(bn0) != 0 {
		,  = .DivMod(, bn58, new(big.Int))
		--
		[] = .encode[.Int64()]
	}
	for  := range  {
		if [] != 0 {
			break
		}
		--
		[] = 
	}
	return string([:])
}

// Decode decodes the base58 encoded bytes.
func ( string) ([]byte, error) {
	return FastBase58Decoding()
}

// DecodeAlphabet decodes the base58 encoded bytes using the given b58 alphabet.
func ( string,  *Alphabet) ([]byte, error) {
	return FastBase58DecodingAlphabet(, )
}

// FastBase58Decoding decodes the base58 encoded bytes.
func ( string) ([]byte, error) {
	return FastBase58DecodingAlphabet(, BTCAlphabet)
}

// FastBase58DecodingAlphabet decodes the base58 encoded bytes using the given
// b58 alphabet.
func ( string,  *Alphabet) ([]byte, error) {
	if len() == 0 {
		return nil, fmt.Errorf("zero length string")
	}

	var (
		        uint64
		,  uint32
		   int

		  = []rune()
		 = len()

		    = ( + 3) / 4 // check to see if we need to change this buffer size to optimize
		      = make([]byte, (+3)*3)
		 =  % 4

		 = rune(.encode[0])
	)

	if  > 0 {
		 = (0xffffffff << uint32(*8))
	} else {
		 = 4
	}

	var  = make([]uint32, )

	for  := 0;  <  && [] == ; ++ {
		++
	}

	for ,  := range  {
		if  > 127 {
			return nil, fmt.Errorf("High-bit set on invalid digit")
		}
		if .decode[] == -1 {
			return nil, fmt.Errorf("Invalid base58 digit (%q)", )
		}

		 = uint32(.decode[])

		for  := ( - 1);  >= 0; -- {
			 = uint64([])*58 + uint64()
			 = uint32(>>32) & 0x3f
			[] = uint32( & 0xffffffff)
		}

		if  > 0 {
			return nil, fmt.Errorf("Output number too big (carry to the next int32)")
		}

		if [0]& != 0 {
			return nil, fmt.Errorf("Output number too big (last int32 filled too far)")
		}
	}

	// the nested for-loop below is the same as the original code:
	// switch (bytesleft) {
	// 	case 3:
	// 		*(binu++) = (outi[0] & 0xff0000) >> 16;
	// 		//-fallthrough
	// 	case 2:
	// 		*(binu++) = (outi[0] & 0xff00) >>  8;
	// 		//-fallthrough
	// 	case 1:
	// 		*(binu++) = (outi[0] & 0xff);
	// 		++j;
	// 		//-fallthrough
	// 	default:
	// 		break;
	// }
	//
	// for (; j < outisz; ++j)
	// {
	// 	*(binu++) = (outi[j] >> 0x18) & 0xff;
	// 	*(binu++) = (outi[j] >> 0x10) & 0xff;
	// 	*(binu++) = (outi[j] >>    8) & 0xff;
	// 	*(binu++) = (outi[j] >>    0) & 0xff;
	// }
	var ,  int
	for ,  = 0, 0;  < ; ++ {
		for  := byte(-1) * 8;  <= 0x18; ,  = -8, +1 {
			[] = byte([] >> )
		}
		if  == 0 {
			 = 4 // because it could be less than 4 the first time through
		}
	}

	for ,  := range  {
		if  > 0 {
			 :=  - 
			if  < 0 {
				 = 0
			}
			return [:], nil
		}
	}
	return [:], nil
}

// TrivialBase58Decoding decodes the base58 encoded bytes (inefficiently).
func ( string) ([]byte, error) {
	return TrivialBase58DecodingAlphabet(, BTCAlphabet)
}

// TrivialBase58DecodingAlphabet decodes the base58 encoded bytes
// (inefficiently) using the given b58 alphabet.
func ( string,  *Alphabet) ([]byte, error) {
	 := .encode[0]

	var  int
	for  := 0;  < len() && [] == ; ++ {
		++
	}
	 := make([]byte, )

	var  rune = -1
	 := []byte()
	 := 0
	for ;  < len() && [] == byte(); ++ {
	}

	 := new(big.Int)
	for  := range [:] {
		 := .decode[[]]
		if  == -1 {
			return nil, fmt.Errorf("illegal base58 data at input index: %d", )
		}
		.Mul(, bn58)
		.Add(, big.NewInt(int64()))
	}
	return append(, .Bytes()...), nil
}