Skip to content

Commit 8965322

Browse files
authored
Bulletproofs 零知识证明算法实现 —— IPA实现 (#505)
实现Bulletproofs中的IPA Fixed #480
1 parent c2b6479 commit 8965322

22 files changed

+3843
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# Copyright 2025 @yangjucai.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
load("@yacl//bazel:yacl.bzl", "yacl_cc_library", "yacl_cc_test")
16+
17+
package(default_visibility = ["//visibility:public"])
18+
19+
yacl_cc_library(
20+
name = "simple_transcript",
21+
srcs = ["simple_transcript.cc"],
22+
hdrs = ["simple_transcript.h"],
23+
deps = [
24+
"@yacl//yacl/base:buffer",
25+
"@yacl//yacl/base:byte_container_view",
26+
"@yacl//yacl/base:exception",
27+
"@yacl//yacl/crypto/ecc",
28+
"@yacl//yacl/crypto/ecc:ec_point",
29+
"@yacl//yacl/crypto/hash:hash_utils",
30+
"@yacl//yacl/math/mpint",
31+
],
32+
)
33+
34+
yacl_cc_library(
35+
name = "generators",
36+
srcs = ["generators.cc"],
37+
hdrs = ["generators.h"],
38+
deps = [
39+
":util",
40+
"@yacl//yacl/base:exception",
41+
"@yacl//yacl/crypto/ecc",
42+
"@yacl//yacl/crypto/ecc:ec_point",
43+
"@yacl//yacl/crypto/hash:hash_utils",
44+
"@yacl//yacl/math/mpint",
45+
],
46+
)
47+
48+
yacl_cc_library(
49+
name = "bp_config",
50+
hdrs = ["bp_config.h"],
51+
)
52+
53+
yacl_cc_test(
54+
name = "generators_test",
55+
srcs = ["generators_test.cc"],
56+
deps = [
57+
":bp_config",
58+
"//yacl/crypto/experimental/zkp/bulletproofs:generators",
59+
"@yacl//yacl/base:exception",
60+
"@yacl//yacl/crypto/ecc",
61+
"@yacl//yacl/crypto/ecc:ec_point",
62+
"@yacl//yacl/crypto/hash:hash_utils",
63+
"@yacl//yacl/math/mpint",
64+
],
65+
)
66+
67+
yacl_cc_test(
68+
name = "simple_transcript_test",
69+
srcs = ["simple_transcript_test.cc"],
70+
deps = [
71+
":bp_config",
72+
"//yacl/crypto/experimental/zkp/bulletproofs:simple_transcript",
73+
"@yacl//yacl/base:exception",
74+
"@yacl//yacl/crypto:openssl_wrappers",
75+
"@yacl//yacl/crypto/ecc",
76+
"@yacl//yacl/crypto/hash:hash_utils",
77+
"@yacl//yacl/crypto/rand",
78+
"@yacl//yacl/crypto/tools:ro",
79+
"@yacl//yacl/math/mpint",
80+
"@yacl//yacl/math/mpint:mp_int_enforce",
81+
"@yacl//yacl/utils:scope_guard",
82+
],
83+
)
84+
85+
yacl_cc_test(
86+
name = "util_test",
87+
srcs = ["util_test.cc"],
88+
deps = [
89+
":bp_config",
90+
"//yacl/crypto/experimental/zkp/bulletproofs:util",
91+
"@yacl//yacl/base:exception",
92+
"@yacl//yacl/crypto/ecc",
93+
"@yacl//yacl/crypto/ecc:ec_point",
94+
"@yacl//yacl/crypto/hash:hash_utils",
95+
"@yacl//yacl/math/mpint",
96+
"@yacl//yacl/math/mpint:mp_int_enforce",
97+
],
98+
)
99+
100+
yacl_cc_library(
101+
name = "util",
102+
srcs = ["util.cc"],
103+
hdrs = ["util.h"],
104+
deps = [
105+
"@yacl//yacl/base:exception",
106+
"@yacl//yacl/crypto/ecc",
107+
"@yacl//yacl/crypto/ecc:ec_point",
108+
"@yacl//yacl/crypto/hash:hash_utils",
109+
"@yacl//yacl/math/mpint",
110+
"@yacl//yacl/math/mpint:mp_int_enforce",
111+
],
112+
)
113+
114+
yacl_cc_library(
115+
name = "errors",
116+
hdrs = ["errors.h"],
117+
)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2025 @yangjucai.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#pragma once
16+
17+
namespace examples::zkp {
18+
19+
// Constants for the curve used in inner product argument
20+
inline constexpr const char* kBpEcName = "secp256k1";
21+
inline constexpr const char* kBpEcLib = "openssl";
22+
23+
} // namespace examples::zkp
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
// Copyright 2025 @yangjucai.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#pragma once
16+
17+
#include <optional>
18+
#include <stdexcept>
19+
#include <string>
20+
#include <variant>
21+
#include <vector>
22+
23+
namespace examples::zkp {
24+
25+
// Represents an error in proof creation, verification, or parsing.
26+
class ProofError : public std::runtime_error {
27+
public:
28+
enum class ErrorType {
29+
// This error occurs when a proof failed to verify.
30+
VerificationError,
31+
// This error occurs when the proof encoding is malformed.
32+
FormatError,
33+
// This error occurs during proving if the number of blinding
34+
// factors does not match the number of values.
35+
WrongNumBlindingFactors,
36+
// This error occurs when attempting to create a proof with
37+
// bitsize other than 8, 16, 32, or 64.
38+
InvalidBitsize,
39+
// This error occurs when attempting to create an aggregated
40+
// proof with non-power-of-two aggregation size.
41+
InvalidAggregation,
42+
// This error occurs when there are insufficient generators for the proof.
43+
InvalidGeneratorsLength,
44+
// This error occurs when inputs are the incorrect length for the proof.
45+
InvalidInputLength,
46+
// This error results from an internal error during proving.
47+
ProvingError
48+
};
49+
50+
explicit ProofError(ErrorType type, const std::string& msg = "")
51+
: std::runtime_error(GetErrorMessage(type, msg)), type_(type) {}
52+
53+
ErrorType GetType() const { return type_; }
54+
55+
private:
56+
static std::string GetErrorMessage(ErrorType type, const std::string& msg) {
57+
switch (type) {
58+
case ErrorType::VerificationError:
59+
return "Proof verification failed.";
60+
case ErrorType::FormatError:
61+
return "Proof data could not be parsed.";
62+
case ErrorType::WrongNumBlindingFactors:
63+
return "Wrong number of blinding factors supplied.";
64+
case ErrorType::InvalidBitsize:
65+
return "Invalid bitsize, must have n = 8,16,32,64.";
66+
case ErrorType::InvalidAggregation:
67+
return "Invalid aggregation size, m must be a power of 2.";
68+
case ErrorType::InvalidGeneratorsLength:
69+
return "Invalid generators size, too few generators for proof";
70+
case ErrorType::InvalidInputLength:
71+
return "Invalid input size, incorrect input length for proof";
72+
case ErrorType::ProvingError:
73+
return "Internal error during proof creation: " + msg;
74+
}
75+
return "Unknown error";
76+
}
77+
78+
ErrorType type_;
79+
};
80+
81+
// Result type for operations that can fail
82+
template <typename T = void>
83+
class Result {
84+
public:
85+
// Construct a successful result
86+
static Result<T> Ok(const T& value) { return Result<T>(value); }
87+
88+
static Result<T> Ok(T&& value) { return Result<T>(std::move(value)); }
89+
90+
// Construct an error result
91+
static Result<T> Err(const ProofError& error) { return Result<T>(error); }
92+
93+
// Check if result is successful
94+
bool IsOk() const { return !error_.has_value(); }
95+
96+
// Get the value (must check IsOk() first)
97+
const T& Value() const {
98+
if (!IsOk()) {
99+
throw std::runtime_error("Attempted to get value from error result");
100+
}
101+
return value_;
102+
}
103+
104+
T&& TakeValue() && {
105+
if (!IsOk()) {
106+
throw std::runtime_error("Attempted to take value from error result");
107+
}
108+
return std::move(value_);
109+
}
110+
111+
// Get the error (must check !IsOk() first)
112+
const ProofError& Error() const {
113+
if (IsOk()) {
114+
throw std::runtime_error("Attempted to get error from successful result");
115+
}
116+
return *error_;
117+
}
118+
119+
private:
120+
explicit Result(const T& value) : value_(value) {}
121+
explicit Result(T&& value) : value_(std::move(value)) {}
122+
explicit Result(const ProofError& error) : error_(error) {}
123+
124+
T value_;
125+
std::optional<ProofError> error_;
126+
};
127+
128+
// Specialization for void
129+
template <>
130+
class Result<void> {
131+
public:
132+
static Result<void> Ok() { return Result<void>(); }
133+
134+
static Result<void> Err(const ProofError& error) {
135+
return Result<void>(error);
136+
}
137+
138+
bool IsOk() const { return !error_.has_value(); }
139+
140+
const ProofError& Error() const {
141+
if (IsOk()) {
142+
throw std::runtime_error("Attempted to get error from successful result");
143+
}
144+
return *error_;
145+
}
146+
147+
private:
148+
Result() = default;
149+
explicit Result(const ProofError& error) : error_(error) {}
150+
151+
std::optional<ProofError> error_;
152+
};
153+
154+
} // namespace examples::zkp

0 commit comments

Comments
 (0)