barretenberg
Loading...
Searching...
No Matches
ecdsa_circuit.hpp
1
2#pragma once
3#include "barretenberg/crypto/ecdsa/ecdsa.hpp"
4#include "barretenberg/crypto/hashers/hashers.hpp"
5#include "barretenberg/ecc/curves/grumpkin/grumpkin.hpp"
6#include "barretenberg/stdlib/encryption/ecdsa/ecdsa.hpp"
7#include "barretenberg/stdlib/encryption/ecdsa/ecdsa_impl.hpp"
8#include "barretenberg/stdlib/primitives/bigfield/bigfield.hpp"
9#include "barretenberg/stdlib/primitives/biggroup/biggroup.hpp"
10#include "barretenberg/stdlib/primitives/bool/bool.hpp"
11#include "barretenberg/stdlib/primitives/curves/secp256k1.hpp"
12#include "barretenberg/stdlib/primitives/field/field.hpp"
13#include "barretenberg/stdlib/primitives/witness/witness.hpp"
14
15using namespace proof_system::plonk;
16using namespace stdlib;
18
19template <typename Builder> class EcdsaCircuit {
20 public:
26
27 static constexpr size_t NUM_PUBLIC_INPUTS = 6;
28
29 static Builder generate(uint256_t public_inputs[])
30 {
31 Builder builder;
32
33 // IN CIRCUIT
34 // Create an input buffer the same size as our inputs
35 typename curve::byte_array_ct input_buffer(&builder, NUM_PUBLIC_INPUTS);
36 for (size_t i = 0; i < NUM_PUBLIC_INPUTS; ++i) {
37 input_buffer.set_byte(i, public_witness_ct(&builder, public_inputs[i]));
38 }
39
40 // This is the message that we would like to confirm
41 std::string message_string = "goblin";
42 auto message = typename curve::byte_array_ct(&builder, message_string);
43
44 // Assert that the public inputs buffer matches the message we want
45 for (size_t i = 0; i < NUM_PUBLIC_INPUTS; ++i) {
46 input_buffer[i].assert_equal(message[i]);
47 }
48
49 // UNCONSTRAINED: create a random keypair to sign with
51 account.private_key = curve::fr::random_element();
52 account.public_key = curve::g1::one * account.private_key;
53
54 // UNCONSTRAINED: create a sig
55 crypto::ecdsa::signature signature = crypto::ecdsa::
56 construct_signature<Sha256Hasher, typename curve::fq, typename curve::fr, typename curve::g1>(
57 message_string, account);
58
59 // UNCONSTRAINED: verify the created signature
60 bool dry_run =
61 crypto::ecdsa::verify_signature<Sha256Hasher, typename curve::fq, typename curve::fr, typename curve::g1>(
62 message_string, account.public_key, signature);
63 if (!dry_run) {
64 throw_or_abort("[non circuit]: Sig verification failed");
65 }
66
67 // IN CIRCUIT: create a witness with the pub key in our circuit
68 typename curve::g1_bigfr_ct public_key = curve::g1_bigfr_ct::from_witness(&builder, account.public_key);
69
70 std::vector<uint8_t> rr(signature.r.begin(), signature.r.end());
71 std::vector<uint8_t> ss(signature.s.begin(), signature.s.end());
72 uint8_t vv = signature.v;
73
74 // IN CIRCUIT: create a witness with the sig in our circuit
76 typename curve::byte_array_ct(&builder, ss),
77 stdlib::uint8<Builder>(&builder, vv) };
78
79 // IN CIRCUIT: verify the signature
80 typename curve::bool_ct signature_result = stdlib::ecdsa::verify_signature<Builder,
81 curve,
82 typename curve::fq_ct,
83 typename curve::bigfr_ct,
84 typename curve::g1_bigfr_ct>(
85 // input_buffer, public_key, sig);
86 input_buffer,
87 public_key,
88 sig);
89
90 // Assert the signature is true, we hash the message inside the verify sig stdlib call
91 bool_ct is_true = bool_ct(1);
92 signature_result.must_imply(is_true, "signature verification failed");
93
94 return builder;
95 }
96};
Definition: ecdsa_circuit.hpp:19
Definition: uint256.hpp:25
Definition: standard_circuit_builder.hpp:12
Definition: bigfield.hpp:17
void must_imply(const bool_t &other, std::string const &msg="bool_t::must_imply") const
Definition: bool.cpp:447
Definition: byte_array.hpp:9
Definition: biggroup.hpp:22
Definition: field.hpp:10
Definition: widget.bench.cpp:13
Definition: ecdsa.hpp:13
Definition: ecdsa.hpp:20
Definition: secp256k1.hpp:12