Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
schnorr.cpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: not started, auditors: [], date: YYYY-MM-DD }
3// external_1: { status: not started, auditors: [], date: YYYY-MM-DD }
4// external_2: { status: not started, auditors: [], date: YYYY-MM-DD }
5// =====================
6
7#include "schnorr.hpp"
13#include <array>
14
15namespace bb::stdlib {
16
21template <typename C>
23{
25
26 uint256_t s_bigint(0);
27 uint256_t e_bigint(0);
28 const uint8_t* s_ptr = &signature.s[0];
29 const uint8_t* e_ptr = &signature.e[0];
30 numeric::read(s_ptr, s_bigint);
31 numeric::read(e_ptr, e_bigint);
33 .e = cycle_scalar::from_witness_bitstring(context, e_bigint, 256) };
34 return sig;
35}
36
45template <typename C>
47 const cycle_group<C>& pub_key,
49{
51 // compute g1 * sig.s + key * sig,e
52
53 auto x_3 = cycle_group<C>::batch_mul({ g1, pub_key }, { sig.s, sig.e }).x;
54 // build input (pedersen(([s]g + [e]pub).x | pub.x | pub.y) | message) to hash function
55 // pedersen hash ([r].x | pub.x) to make sure the size of `hash_input` is <= 64 bytes for a 32 byte message
56 byte_array<C> hash_input(pedersen_hash<C>::hash({ x_3, pub_key.x, pub_key.y }));
57 hash_input.write(message);
58
59 // compute e' = hash(([s]g + [e]pub).x | message)
60 byte_array<C> output = stdlib::Blake2s<C>::hash(hash_input);
61 static constexpr size_t LO_BYTES = cycle_group<C>::cycle_scalar::LO_BITS / 8;
62 static constexpr size_t HI_BYTES = 32 - LO_BYTES;
63 field_t<C> output_hi(output.slice(0, LO_BYTES));
64 field_t<C> output_lo(output.slice(LO_BYTES, HI_BYTES));
65 return { output_lo, output_hi };
66}
67
74template <typename C>
76 const cycle_group<C>& pub_key,
78{
79 auto [output_lo, output_hi] = schnorr_verify_signature_internal(message, pub_key, sig);
80 output_lo.assert_equal(sig.e.lo, "verify signature failed");
81 output_hi.assert_equal(sig.e.hi, "verify signature failed");
82}
83
89template <typename C>
91 const cycle_group<C>& pub_key,
93{
94 auto [output_lo, output_hi] = schnorr_verify_signature_internal(message, pub_key, sig);
95 bool_t<C> valid = (output_lo == sig.e.lo) && (output_hi == sig.e.hi);
96 return valid;
97}
98
99#define VERIFY_SIGNATURE_INTERNAL(circuit_type) \
100 template std::array<field_t<circuit_type>, 2> schnorr_verify_signature_internal<circuit_type>( \
101 const byte_array<circuit_type>&, \
102 const cycle_group<circuit_type>&, \
103 const schnorr_signature_bits<circuit_type>&)
106#define VERIFY_SIGNATURE(circuit_type) \
107 template void schnorr_verify_signature<circuit_type>(const byte_array<circuit_type>&, \
108 const cycle_group<circuit_type>&, \
109 const schnorr_signature_bits<circuit_type>&)
112#define SIGNATURE_VERIFICATION_RESULT(circuit_type) \
113 template bool_t<circuit_type> schnorr_signature_verification_result<circuit_type>( \
114 const byte_array<circuit_type>&, \
115 const cycle_group<circuit_type>&, \
116 const schnorr_signature_bits<circuit_type>&)
119#define CONVERT_SIGNATURE(circuit_type) \
120 template schnorr_signature_bits<circuit_type> schnorr_convert_signature<circuit_type>( \
121 circuit_type*, const crypto::schnorr_signature&)
124} // namespace bb::stdlib
static constexpr element one
Definition group.hpp:46
static byte_array_ct hash(const byte_array_ct &input)
Definition blake2s.cpp:122
Implements boolean logic in-circuit.
Definition bool.hpp:59
Represents a dynamic array of bytes in-circuit.
byte_array slice(size_t offset) const
Slice bytes from the byte array starting at offset. Does not add any constraints.
cycle_group represents a group Element of the proving system's embedded curve i.e....
static cycle_group batch_mul(const std::vector< cycle_group > &base_points, const std::vector< BigScalarField > &scalars, GeneratorContext context={})
cycle_scalar represents a member of the cycle curve SCALAR FIELD. This is NOT the native circuit fiel...
static cycle_scalar from_witness_bitstring(Builder *context, const uint256_t &bitstring, size_t num_bits)
Use when we want to multiply a group element by a string of bits of known size. N....
stdlib class that evaluates in-circuit pedersen hashes, consistent with behavior in crypto::pedersen_...
Definition pedersen.hpp:23
StrictMock< MockContext > context
void read(B &it, uint256_t &value)
Definition uint256.hpp:255
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
std::array< field_t< C >, 2 > schnorr_verify_signature_internal(const byte_array< C > &message, const cycle_group< C > &pub_key, const schnorr_signature_bits< C > &sig)
Make the computations needed to verify a signature (s, e), i.e., compute e' = hash(([s]g + [e]pub)....
Definition schnorr.cpp:46
bool_t< C > schnorr_signature_verification_result(const byte_array< C > &message, const cycle_group< C > &pub_key, const schnorr_signature_bits< C > &sig)
Attempt to verify a signature (s, e) and return the result, i.e., compute e' = hash(([s]g + [e]pub)....
Definition schnorr.cpp:90
void schnorr_verify_signature(const byte_array< C > &message, const cycle_group< C > &pub_key, const schnorr_signature_bits< C > &sig)
Verify that a signature (s, e) is valid, i.e., compute e' = hash(([s]g + [e]pub).x | message) and che...
Definition schnorr.cpp:75
group< fq, fr, Bn254G1Params > g1
Definition g1.hpp:33
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
#define SIGNATURE_VERIFICATION_RESULT(circuit_type)
Definition schnorr.cpp:112
#define VERIFY_SIGNATURE(circuit_type)
Definition schnorr.cpp:106
#define VERIFY_SIGNATURE_INTERNAL(circuit_type)
Definition schnorr.cpp:99
#define CONVERT_SIGNATURE(circuit_type)
Definition schnorr.cpp:119
std::array< uint8_t, 32 > s
Definition schnorr.hpp:36
std::array< uint8_t, 32 > e
Definition schnorr.hpp:39
cycle_group< C >::cycle_scalar s
Definition schnorr.hpp:18
cycle_group< C >::cycle_scalar e
Definition schnorr.hpp:19