-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(engine): add plaintext and gc engine, wip
- Loading branch information
Showing
16 changed files
with
297 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
examples | ||
cmake | ||
docs | ||
build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Copyright 2024 Guowei Ling. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
load("@yacl//bazel:yacl.bzl", "yacl_cc_binary") | ||
|
||
package(default_visibility = ["//visibility:public"]) | ||
|
||
yacl_cc_binary( | ||
name = "sm2_example", | ||
srcs = [ | ||
"ahesm2.cc", | ||
"ahesm2.h", | ||
"ciphertext.h", | ||
"config.cc", | ||
"config.h", | ||
"main.cc", | ||
"private_key.h", | ||
"public_key.h", | ||
"t1.h", | ||
"t2.h", | ||
], | ||
deps = [ | ||
"@yacl//yacl/crypto/ecc:spi", | ||
"@yacl//yacl/crypto/ecc/openssl", | ||
"@yacl//yacl/crypto/tools:cuckoo_index", # 添加 cuckoo_index 依赖 | ||
"@yacl//yacl/math/mpint", | ||
"@yacl//yacl/utils/spi", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Copyright 2022 Ant Group Co., Ltd. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
load("//bazel:yacl.bzl", "yacl_cc_library", "yacl_cc_test") | ||
|
||
package(default_visibility = ["//visibility:public"]) | ||
|
||
yacl_cc_library( | ||
name = "executor", | ||
srcs = ["executor.cc"], | ||
hdrs = ["executor.h"], | ||
deps = [ | ||
"//yacl/base:dynamic_bitset", | ||
"//yacl/base:int128", | ||
"//yacl/io/circuit:bristol_fashion", | ||
], | ||
) | ||
|
||
yacl_cc_test( | ||
name = "executor_test", | ||
srcs = ["executor_test.cc"], | ||
data = ["//yacl/io/circuit:circuit_data"], | ||
deps = [ | ||
"executor", | ||
"//yacl/crypto/block_cipher:symmetric_crypto", | ||
"//yacl/crypto/rand", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// Copyright 2024 Ant Group Co., Ltd. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#pragma once | ||
|
||
#include <memory> | ||
|
||
#include "yacl/base/byte_container_view.h" | ||
#include "yacl/base/dynamic_bitset.h" | ||
#include "yacl/base/int128.h" | ||
#include "yacl/io/circuit/bristol_fashion.h" | ||
|
||
namespace yacl::engine { | ||
|
||
// plaintext protocol that executes everything without link | ||
|
||
class PlainExecutor { | ||
public: | ||
// Constructor | ||
explicit PlainExecutor() = default; | ||
|
||
// Load circuit from file (local operation) | ||
void LoadCircuitFile(const std::string &path); | ||
|
||
// Setup the input wire (local operation) | ||
template <typename T = uint64_t> | ||
void SetupInputs(absl::Span<T> inputs) { | ||
YACL_ENFORCE(inputs.size() == circ_->niv); | ||
|
||
dynamic_bitset<uint128_t> input_wires; | ||
input_wires.resize(sizeof(T) * 8 * inputs.size()); | ||
std::memcpy(input_wires.data(), inputs.data(), inputs.size() * sizeof(T)); | ||
wires_.append(input_wires); | ||
wires_.resize(circ_->nw); | ||
} | ||
|
||
// Setup the input wire | ||
void SetupInputBytes(ByteContainerView bytes) { | ||
wires_.resize(circ_->nw); | ||
std::memcpy(wires_.data(), bytes.data(), bytes.size()); | ||
} | ||
|
||
// Execute the circuit | ||
void Exec(); | ||
|
||
// Finalize and get the result | ||
template <typename T = uint64_t> | ||
void Finalize(absl::Span<T> outputs) { | ||
YACL_ENFORCE(outputs.size() >= circ_->nov); | ||
size_t index = wires_.size(); | ||
for (size_t i = 0; i < circ_->nov; ++i) { | ||
dynamic_bitset<T> result(circ_->now[i]); | ||
for (size_t j = 0; j < circ_->now[i]; ++j) { | ||
result[j] = wires_[index - circ_->now[i] + j]; | ||
} | ||
outputs[circ_->nov - i - 1] = *(T *)result.data(); | ||
index -= circ_->now[i]; | ||
} | ||
} | ||
|
||
std::vector<uint8_t> FinalizeBytes() { | ||
// Count the totoal number of output wires (a.k.a. output bits) | ||
size_t total_out_bitnum = 0; | ||
for (size_t i = 0; i < circ_->nov; ++i) { | ||
total_out_bitnum += circ_->now[i]; | ||
} | ||
|
||
// Make sure that the circuit output wire is full bytes | ||
YACL_ENFORCE(total_out_bitnum % 8 == 0); | ||
|
||
const size_t wire_size = wires_.size(); | ||
dynamic_bitset<uint128_t> result(total_out_bitnum); | ||
for (size_t i = 0; i < total_out_bitnum; ++i) { | ||
result[total_out_bitnum - i - 1] = wires_[wire_size - i - 1]; | ||
} | ||
YACL_ENFORCE(result.size() == total_out_bitnum); | ||
std::vector<uint8_t> out(total_out_bitnum / 8); | ||
std::memcpy(out.data(), result.data(), out.size()); | ||
return out; | ||
} | ||
|
||
private: | ||
// NOTE: please make sure you use the correct order of wires | ||
dynamic_bitset<uint128_t> wires_; // shares | ||
std::shared_ptr<io::BFCircuit> circ_; // bristol fashion circuit | ||
}; | ||
|
||
} // namespace yacl::engine |
Oops, something went wrong.