Skip to content

Commit

Permalink
Merge pull request #272 from AntelopeIO/bls_base64url_support
Browse files Browse the repository at this point in the history
IF: Change BLS library to use Base64 URL encoding/decoding; update Base64 library to match Leap's
  • Loading branch information
linh2931 authored Mar 11, 2024
2 parents b1eacc8 + 5763afd commit dc1019e
Show file tree
Hide file tree
Showing 11 changed files with 164 additions and 401 deletions.
20 changes: 10 additions & 10 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,19 @@ jobs:
- uses: actions/checkout@v3
with:
submodules: recursive
- name: Download leap-dev.deb (Ubuntu 20 only)
if: matrix.platform == 'ubuntu20'
- name: Download leap-dev.deb (Ubuntu 22 only)
if: matrix.platform == 'ubuntu22'
uses: AntelopeIO/asset-artifact-download-action@v3
with:
owner: AntelopeIO
repo: leap
file: 'leap-dev.*(x86_64|amd64).deb'
file: 'leap-dev.*ubuntu22\.04_amd64.deb'
target: '${{needs.versions.outputs.leap-dev-target}}'
prereleases: ${{fromJSON(needs.versions.outputs.leap-dev-prerelease)}}
artifact-name: leap-dev-ubuntu20-amd64
artifact-name: leap-dev-ubuntu22-amd64
container-package: experimental-binaries
- name: Install leap-dev.deb (Ubuntu 20 only)
if: matrix.platform == 'ubuntu20'
- name: Install leap-dev.deb (Ubuntu 22 only)
if: matrix.platform == 'ubuntu22'
run: |
apt-get update && apt-get upgrade -y
apt install -y ./leap-dev*.deb
Expand All @@ -127,13 +127,13 @@ jobs:
make -j $(nproc)
cd tests
ctest -j $(nproc) --output-on-failure
- name: Package (Ubuntu 20 only)
if: matrix.platform == 'ubuntu20'
- name: Package (Ubuntu 22 only)
if: matrix.platform == 'ubuntu22'
run: |
cd build/packages
bash generate_package.sh deb ubuntu amd64
- name: Upload (Ubuntu 20 only)
if: matrix.platform == 'ubuntu20'
- name: Upload (Ubuntu 22 only)
if: matrix.platform == 'ubuntu22'
uses: actions/upload-artifact@v3
with:
name: cdt_ubuntu_package_amd64
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ CDT currently supports Linux x86_64 Debian packages. Visit the [release page](ht
Download the appropriate version of the Debian package and then install it. To download and install the latest version, run the following:

```sh
wget https://github.com/AntelopeIO/cdt/releases/download/v4.0.0/cdt_4.0.0_amd64.deb
sudo apt install ./cdt_4.0.0_amd64.deb
wget https://github.com/AntelopeIO/cdt/releases/download/v4.0.1/cdt_4.0.1_amd64.deb
sudo apt install ./cdt_4.0.1_amd64.deb
```
### Debian package uninstall

Expand Down
82 changes: 51 additions & 31 deletions libraries/eosiolib/base64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,47 @@

namespace eosio::detail {

unsigned int pos_of_char(const unsigned char chr) {
unsigned int pos_of_char(const unsigned char chr, bool url) {
// Return the position of chr within base64_encode()
const unsigned char from_base64_chars[256] = {
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 62, 64, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 63,
64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
};
auto c = from_base64_chars[chr];
constexpr unsigned char from_base64_chars[2][256] = {
{ // for base64
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64,
64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
},
{ // for base64url
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 63,
64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
}};

auto c = from_base64_chars[url][chr];
eosio::check(c != 64, "encountered non-base64 character");

return c;
Expand All @@ -31,7 +51,7 @@ unsigned int pos_of_char(const unsigned char chr) {
std::string base64_encode(unsigned char const* bytes_to_encode, size_t in_len, bool url) {

const size_t len_encoded = (in_len +2) / 3 * 4;
const unsigned char trailing_char = url ? '.' : '=';
const unsigned char trailing_char = '=';

// Includes performance improvement from unmerged PR: https://github.com/ReneNyffenegger/cpp-base64/pull/27

Expand Down Expand Up @@ -74,14 +94,14 @@ std::string base64_encode(unsigned char const* bytes_to_encode, size_t in_len, b
}
else {
ret.push_back(base64_chars_[(bytes_to_encode[pos + 1] & 0x0f) << 2]);
ret.push_back(trailing_char);
if (!url) ret.push_back(trailing_char);
}
}
else {

ret.push_back(base64_chars_[(bytes_to_encode[pos + 0] & 0x03) << 4]);
ret.push_back(trailing_char);
ret.push_back(trailing_char);
if (!url) ret.push_back(trailing_char);
if (!url) ret.push_back(trailing_char);
}

pos += 3;
Expand All @@ -91,7 +111,7 @@ std::string base64_encode(unsigned char const* bytes_to_encode, size_t in_len, b
return ret;
}

std::string base64_decode(std::string_view encoded_string, bool remove_linebreaks) {
std::string base64_decode(std::string_view encoded_string, bool remove_linebreaks, bool url) {
if (encoded_string.empty()) return std::string{};

if (remove_linebreaks) {
Expand All @@ -100,7 +120,7 @@ std::string base64_decode(std::string_view encoded_string, bool remove_linebreak

copy.erase(std::remove(copy.begin(), copy.end(), '\n'), copy.end());

return base64_decode(copy, false);
return base64_decode(copy, false, url);
}

size_t length_of_string = encoded_string.length();
Expand Down Expand Up @@ -130,12 +150,12 @@ std::string base64_decode(std::string_view encoded_string, bool remove_linebreak
// The last chunk produces at least one and up to three bytes.
//
eosio::check(pos+1 < length_of_string, "wrong encoded string size");
size_t pos_of_char_1 = pos_of_char(encoded_string.at(pos+1) );
size_t pos_of_char_1 = pos_of_char(encoded_string.at(pos+1), url);

//
// Emit the first output byte that is produced in each chunk:
//
ret.push_back(static_cast<std::string::value_type>( ( (pos_of_char(encoded_string.at(pos+0)) ) << 2 ) + ( (pos_of_char_1 & 0x30 ) >> 4)));
ret.push_back(static_cast<std::string::value_type>( ( (pos_of_char(encoded_string.at(pos+0), url) ) << 2 ) + ( (pos_of_char_1 & 0x30 ) >> 4)));

if ( ( pos + 2 < length_of_string ) && // Check for data that is not padded with equal signs (which is allowed by RFC 2045)
encoded_string.at(pos+2) != '=' &&
Expand All @@ -145,7 +165,7 @@ std::string base64_decode(std::string_view encoded_string, bool remove_linebreak
//
// Emit a chunk's second byte (which might not be produced in the last chunk).
//
unsigned int pos_of_char_2 = pos_of_char(encoded_string.at(pos+2) );
unsigned int pos_of_char_2 = pos_of_char(encoded_string.at(pos+2), url);
ret.push_back(static_cast<std::string::value_type>( (( pos_of_char_1 & 0x0f) << 4) + (( pos_of_char_2 & 0x3c) >> 2)));

if ( ( pos + 3 < length_of_string ) &&
Expand All @@ -156,7 +176,7 @@ std::string base64_decode(std::string_view encoded_string, bool remove_linebreak
//
// Emit a chunk's third byte (which might not be produced in the last chunk).
//
ret.push_back(static_cast<std::string::value_type>( ( (pos_of_char_2 & 0x03 ) << 6 ) + pos_of_char(encoded_string.at(pos+3)) ));
ret.push_back(static_cast<std::string::value_type>( ( (pos_of_char_2 & 0x03 ) << 6 ) + pos_of_char(encoded_string.at(pos+3), url)));
}
}

Expand All @@ -166,4 +186,4 @@ std::string base64_decode(std::string_view encoded_string, bool remove_linebreak
return ret;
}

}//eosio::detail
}//eosio::detail
Loading

0 comments on commit dc1019e

Please # to comment.