Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
schnorr.test.cpp
Go to the documentation of this file.
1#include <gtest/gtest.h>
2
7#include "schnorr.hpp"
8
9using namespace bb;
10using namespace bb::crypto;
11
17
24{
25 std::string longer_string = "This is a test string of length 34";
26
27 std::vector<size_t> test_lengths({ 0, 1, 32, 33 });
28 for (size_t i : test_lengths) {
30 auto message_string = longer_string.substr(0, i);
31
34 account.public_key = grumpkin::g1::one * account.private_key;
35
36 schnorr_signature signature =
37 schnorr_construct_signature<Blake2sHasher, grumpkin::fq, grumpkin::fr, grumpkin::g1>(message_string,
38 account);
39
40 bool first_result = schnorr_verify_signature<Blake2sHasher, grumpkin::fq, grumpkin::fr, grumpkin::g1>(
41 message_string, account.public_key, signature);
42 EXPECT_EQ(first_result, true);
43
45 witness_ct(&builder, account.public_key.y),
46 false };
48 byte_array_ct message(&builder, message_string);
49 schnorr_verify_signature(message, pub_key, sig);
50
51 info("num gates = ", builder.get_estimated_num_finalized_gates());
52 bool result = CircuitChecker::check(builder);
53 EXPECT_EQ(result, true);
54 }
55}
56
61TEST(stdlib_schnorr, verify_signature_failure)
62{
64 std::string message_string = "This is a test string of length 34";
65
66 // create key pair 1
69 account1.public_key = grumpkin::g1::one * account1.private_key;
70
71 // create key pair 2
74 account2.public_key = grumpkin::g1::one * account2.private_key;
75
76 // sign the message with account 1 private key
77 schnorr_signature signature =
78 schnorr_construct_signature<Blake2sHasher, grumpkin::fq, grumpkin::fr, grumpkin::g1>(message_string, account1);
79
80 // check native verification with account 2 public key fails
81 bool native_result = schnorr_verify_signature<Blake2sHasher, grumpkin::fq, grumpkin::fr, grumpkin::g1>(
82 message_string, account2.public_key, signature);
83 EXPECT_EQ(native_result, false);
84
85 // check stdlib verification with account 2 public key fails
87 witness_ct(&builder, account2.public_key.y),
88 false };
90 byte_array_ct message(&builder, message_string);
91 schnorr_verify_signature(message, pub_key2_ct, sig);
92
93 info("num gates = ", builder.get_estimated_num_finalized_gates());
94
95 bool verification_result = CircuitChecker::check(builder);
96 EXPECT_EQ(verification_result, false);
97}
98
103TEST(stdlib_schnorr, schnorr_signature_verification_result)
104{
105 std::string longer_string = "This is a test string of length 34";
106
108
111 account.public_key = grumpkin::g1::one * account.private_key;
112
113 schnorr_signature signature =
114 schnorr_construct_signature<Blake2sHasher, grumpkin::fq, grumpkin::fr, grumpkin::g1>(longer_string, account);
115
116 bool first_result = schnorr_verify_signature<Blake2sHasher, grumpkin::fq, grumpkin::fr, grumpkin::g1>(
117 longer_string, account.public_key, signature);
118 EXPECT_EQ(first_result, true);
119
121 witness_ct(&builder, account.public_key.y),
122 false };
124 byte_array_ct message(&builder, longer_string);
125 bool_ct signature_result = schnorr_signature_verification_result(message, pub_key, sig);
126 EXPECT_EQ(signature_result.witness_bool, true);
127
128 info("num gates = ", builder.get_estimated_num_finalized_gates());
129
130 bool result = CircuitChecker::check(builder);
131 EXPECT_EQ(result, true);
132}
133
138TEST(stdlib_schnorr, signature_verification_result_failure)
139{
141 std::string message_string = "This is a test string of length 34";
142
143 // create key pair 1
146 account1.public_key = grumpkin::g1::one * account1.private_key;
147
148 // create key pair 2
151 account2.public_key = grumpkin::g1::one * account2.private_key;
152
153 // sign the message with account 1 private key
154 schnorr_signature signature =
155 schnorr_construct_signature<Blake2sHasher, grumpkin::fq, grumpkin::fr, grumpkin::g1>(message_string, account1);
156
157 // check native verification with account 2 public key fails
158 bool native_result = schnorr_verify_signature<Blake2sHasher, grumpkin::fq, grumpkin::fr, grumpkin::g1>(
159 message_string, account2.public_key, signature);
160 EXPECT_EQ(native_result, false);
161
162 // check stdlib verification with account 2 public key fails
164 witness_ct(&builder, account2.public_key.y),
165 false };
167 byte_array_ct message(&builder, message_string);
168 bool_ct signature_result = schnorr_signature_verification_result(message, pub_key2_ct, sig);
169 EXPECT_EQ(signature_result.witness_bool, false);
170
171 info("num gates = ", builder.get_estimated_num_finalized_gates());
172
173 bool verification_result = CircuitChecker::check(builder);
174 EXPECT_EQ(verification_result, true);
175}
static bool check(const Builder &circuit)
Check the witness satisifies the circuit.
static constexpr element one
Definition group.hpp:46
Implements boolean logic in-circuit.
Definition bool.hpp:59
Represents a dynamic array of bytes in-circuit.
cycle_group represents a group Element of the proving system's embedded curve i.e....
void info(Args... args)
Definition log.hpp:70
AluTraceBuilder builder
Definition alu.test.cpp:123
bool schnorr_verify_signature(const std::string &message, const typename G1::affine_element &public_key, const schnorr_signature &sig)
schnorr_signature_bits< C > schnorr_convert_signature(C *context, const crypto::schnorr_signature &signature)
Instantiate a witness containing the signature (s, e) as a quadruple of field_t elements (s_lo,...
Definition schnorr.cpp:22
Entry point for Barretenberg command-line interface.
TEST(MegaCircuitBuilder, CopyConstructor)
UltraCircuitBuilder_< UltraExecutionTraceBlocks > UltraCircuitBuilder
stdlib::witness_t< Builder > witness_ct
UltraCircuitBuilder Builder
G1::affine_element public_key
Definition schnorr.hpp:24
static field random_element(numeric::RNG *engine=nullptr) noexcept