barretenberg
Loading...
Searching...
No Matches
schnorr.hpp
1#pragma once
2
3#include <array>
4#include <memory.h>
5#include <string>
6
7#include "barretenberg/ecc/curves/grumpkin/grumpkin.hpp"
8
9#include "barretenberg/crypto/hashers/hashers.hpp"
10
11#include "barretenberg/common/serialize.hpp"
12#include "barretenberg/common/streams.hpp"
13#include "barretenberg/serialize/msgpack.hpp"
14
15namespace crypto {
16namespace schnorr {
17template <typename Fr, typename G1> struct key_pair {
18 Fr private_key;
19 typename G1::affine_element public_key;
20};
21
22// Raw representation of a Schnorr signature (e,s). We use the short variant of Schnorr
23// where we include the challenge hash `e` instead of the group element R representing
24// the provers initial message.
25struct signature {
26
27 // `s` is a serialized field element (also 32 bytes), representing the prover's response to
28 // to the verifier challenge `e`.
29 // We do not enforce that `s` is canonical since signatures are verified inside a circuit,
30 // and are provided as private inputs. Malleability is not an issue in this case.
31 std::array<uint8_t, 32> s;
32 // `e` represents the verifier's challenge in the protocol. It is encoded as the 32-byte
33 // output of a hash function modeling a random oracle in the Fiat-Shamir transform.
34 std::array<uint8_t, 32> e;
35 MSGPACK_FIELDS(s, e);
36};
37
38template <typename Hash, typename Fq, typename Fr, typename G1>
39bool verify_signature(const std::string& message, const typename G1::affine_element& public_key, const signature& sig);
40
41template <typename Hash, typename Fq, typename Fr, typename G1>
42signature construct_signature(const std::string& message, const key_pair<Fr, G1>& account);
43
44inline bool operator==(signature const& lhs, signature const& rhs)
45{
46 return lhs.s == rhs.s && lhs.e == rhs.e;
47}
48
49inline std::ostream& operator<<(std::ostream& os, signature const& sig)
50{
51 os << "{ " << sig.s << ", " << sig.e << " }";
52 return os;
53}
54
55template <typename B> inline void read(B& it, key_pair<grumpkin::fr, grumpkin::g1>& keypair)
56{
57 read(it, keypair.private_key);
58 read(it, keypair.public_key);
59}
60
61template <typename B> inline void write(B& buf, key_pair<grumpkin::fr, grumpkin::g1> const& keypair)
62{
63 write(buf, keypair.private_key);
64 write(buf, keypair.public_key);
65}
66} // namespace schnorr
67} // namespace crypto
68#include "./schnorr.tcc"
Definition: aes128.cpp:9
Definition: schnorr.hpp:17
Definition: schnorr.hpp:25