Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(engine): add sha256 circuit #3

Merged
merged 6 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .bazelignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
examples
cmake
docs
build
4 changes: 4 additions & 0 deletions .licenserc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ header: # <1>
- 'yacl/crypto/aes/aes_opt.h'
- 'yacl/io/circuit/data/**.txt'
- 'docs/requirements.txt'
- '.whitesource'
- 'MODULE.bazel'
- 'MODULE.bazel.lock'
- 'examples/cmake-project/.keep'

comment: never # <9>

Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ Yacl-r is a fork and extension of the C++ crypto library [secretflow/yacl](https

- **bazel**: [.bazelversion](.bazelversion) file describes the recommended version of bazel. We recommend to use the official [bazelisk](https://github.com/bazelbuild/bazelisk?tab=readme-ov-file#installation) to manage bazel version.
- **gcc >= 10.3**
- **[cmake](https://cmake.org/getting-started/)**
- **[ninja/ninja-build](https://ninja-build.org/)**
- **Perl 5 with core modules** (Required by [OpenSSL](https://github.com/openssl/openssl/blob/master/INSTALL.md#prerequisites))

Expand Down
40 changes: 40 additions & 0 deletions examples/hesm2/BUILD.bazel
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",
],
)
2 changes: 1 addition & 1 deletion yacl/crypto/aead/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ yacl_cc_library(
srcs = ["all_gcm.cc"],
hdrs = ["all_gcm.h"],
deps = [
"//yacl/base:secparam",
"//yacl/base:int128",
"//yacl/base:secparam",
"//yacl/crypto:key_utils",
],
)
Expand Down
2 changes: 1 addition & 1 deletion yacl/crypto/block_cipher/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ yacl_cc_library(
],
copts = AES_COPT_FLAGS,
deps = [
"//yacl/base:secparam",
"//yacl/base:int128",
"//yacl/base:secparam",
"//yacl/crypto:ossl_wrappers",
],
)
Expand Down
2 changes: 1 addition & 1 deletion yacl/crypto/hash/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ yacl_cc_library(
name = "hash_interface",
srcs = ["hash_interface.h"],
deps = [
"//yacl/base:secparam",
"//yacl/base:byte_container_view",
"//yacl/base:secparam",
"//yacl/crypto:ossl_wrappers",
],
)
Expand Down
4 changes: 2 additions & 2 deletions yacl/crypto/pke/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ yacl_cc_library(
hdrs = ["sm2_enc.h"],
deps = [
":pke_interface",
"//yacl/base:secparam",
"//yacl/base:exception",
"//yacl/base:secparam",
"//yacl/crypto:key_utils",
],
)
Expand All @@ -50,8 +50,8 @@ yacl_cc_library(
hdrs = ["rsa_enc.h"],
deps = [
":pke_interface",
"//yacl/base:secparam",
"//yacl/base:exception",
"//yacl/base:secparam",
"//yacl/crypto:key_utils",
],
)
Expand Down
2 changes: 1 addition & 1 deletion yacl/crypto/rand/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ yacl_cc_library(
srcs = ["rand.cc"],
hdrs = ["rand.h"],
deps = [
"//yacl/base:secparam",
"//yacl/base:dynamic_bitset",
"//yacl/base:exception",
"//yacl/base:int128",
"//yacl/base:secparam",
"//yacl/crypto/rand/drbg",
"//yacl/crypto/tools:prg",
"//yacl/math/mpint",
Expand Down
6 changes: 3 additions & 3 deletions yacl/crypto/rand/drbg/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ yacl_cc_library(
],
visibility = ["//visibility:private"],
deps = [
"//yacl/base:secparam",
"//yacl/base:byte_container_view",
"//yacl/base:int128",
"//yacl/base:secparam",
"//yacl/crypto/rand/entropy_source",
"//yacl/utils/spi",
],
Expand All @@ -56,8 +56,8 @@ yacl_cc_library(
deps = [
":spi",
"//yacl/crypto:ossl_wrappers",
"//yacl/ossl_provider:helper", # helper
"//yacl/crypto/rand/entropy_source",
"//yacl/ossl_provider:helper", # helper
],
alwayslink = 1,
)
Expand All @@ -80,8 +80,8 @@ yacl_cc_library(
"//yacl/crypto:ossl_wrappers",
"//yacl/crypto/block_cipher:symmetric_crypto",
"//yacl/crypto/hash:hash_utils",
"//yacl/ossl_provider:helper", # helper
"//yacl/crypto/rand/entropy_source",
"//yacl/ossl_provider:helper", # helper
],
alwayslink = 1,
)
Expand Down
23 changes: 22 additions & 1 deletion yacl/crypto/tools/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ yacl_cc_library(
srcs = ["prg.cc"],
hdrs = ["prg.h"],
deps = [
"//yacl/base:secparam",
"//yacl/base:dynamic_bitset",
"//yacl/base:secparam",
"//yacl/crypto/block_cipher:symmetric_crypto",
"//yacl/math/mpint",
],
Expand Down Expand Up @@ -119,3 +119,24 @@ yacl_cc_binary(
"@com_github_google_benchmark//:benchmark_main",
],
)

yacl_cc_library(
name = "cuckoo_index",
srcs = ["cuckoo_index.cc"],
hdrs = ["cuckoo_index.h"],
linkopts = ["-lm"],
deps = [
"//yacl/base:exception",
"//yacl/base:int128",
"@com_google_absl//absl/types:span",
],
)

yacl_cc_test(
name = "cuckoo_index_test",
srcs = ["cuckoo_index_test.cc"],
deps = [
":cuckoo_index",
"//yacl/crypto/rand",
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "yacl/utils/cuckoo_index.h"
#include "yacl/crypto/tools/cuckoo_index.h"

#include <cmath>
#include <set>
Expand Down Expand Up @@ -147,4 +147,4 @@ uint8_t CuckooIndex::MinCollidingHashIdx(uint64_t bin_index) const {
return -1;
}

} // namespace yacl
} // namespace yacl
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "yacl/utils/cuckoo_index.h"
#include "yacl/crypto/tools/cuckoo_index.h"

#include <random>

Expand Down
39 changes: 39 additions & 0 deletions yacl/engine/plaintext/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright 2024 Jamie Cui and 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",
],
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Ant Group Co., Ltd.
// Copyright 2024 Jamie Cui and 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.
Expand All @@ -12,9 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "yacl/utils/circuit_executor.h"
#include "yacl/engine/plaintext/executor.h"

namespace yacl {
namespace yacl::engine {

namespace {
class PlaintextCore {
Expand All @@ -25,24 +25,13 @@ class PlaintextCore {
};
} // namespace

template <typename T>
void PlainExecutor<T>::LoadCircuitFile(const std::string& path) {
void PlainExecutor::LoadCircuitFile(const std::string& path) {
io::CircuitReader reader(path);
reader.ReadAll();
circ_ = reader.StealCirc();
}

template <typename T>
void PlainExecutor<T>::SetupInputs(absl::Span<T> inputs) {
YACL_ENFORCE(inputs.size() == circ_->niv);
for (auto input : inputs) {
wires_.append(input);
}
wires_.resize(circ_->nw);
}

template <typename T>
void PlainExecutor<T>::Exec() {
void PlainExecutor::Exec() {
// Evaluate all gates, sequentially
for (const auto& gate : circ_->gates) {
switch (gate.op) {
Expand Down Expand Up @@ -82,22 +71,5 @@ void PlainExecutor<T>::Exec() {
}
}

template <typename T>
void PlainExecutor<T>::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];
}
}

template class PlainExecutor<uint64_t>;
template class PlainExecutor<uint128_t>;

} // namespace yacl
} // namespace yacl::engine
Loading
Loading