barretenberg
Loading...
Searching...
No Matches
address.hpp
1#pragma once
2#include "barretenberg/numeric/uint256/uint256.hpp"
3#include "barretenberg/stdlib/commitment/pedersen/pedersen.hpp"
4#include "barretenberg/stdlib/primitives/bool/bool.hpp"
5#include "barretenberg/stdlib/primitives/field/field.hpp"
6#include "barretenberg/stdlib/primitives/group/cycle_group.hpp"
7#include "barretenberg/stdlib/primitives/witness/witness.hpp"
8
9namespace proof_system::plonk {
10namespace stdlib {
11
14using stdlib::bool_t;
15using stdlib::cycle_group;
16using stdlib::field_t;
17using stdlib::pedersen_commitment;
18using stdlib::witness_t;
19
20// Native type
21class address {
22 public:
23 fr address_;
24
25 address() noexcept { address_ = fr(); }
26
27 address(address const& other)
28 : address_(other.address_){};
29
30 address(fr const& address)
31 : address_(address){};
32
34 : address_(address){};
35
36 address(int const& address)
37 : address_(fr(address)){};
38
39 operator fr() { return address_; }
40
41 operator fr() const { return address_; }
42
43 constexpr bool operator==(address const& other) const { return this->address_ == other.address_; }
44
45 friend std::ostream& operator<<(std::ostream& os, address const& v) { return os << v.address_; }
46
47 fr to_field() const { return address_; }
48
49 // delegate serialization to field
50 void msgpack_pack(auto& packer) const { address_.msgpack_pack(packer); }
51 void msgpack_unpack(auto const& o) { address_.msgpack_unpack(o); }
52 // help our msgpack schema compiler with this buffer alias (as far as wire representation is concerned) class
53 void msgpack_schema(auto& packer) const { packer.pack_alias("Address", "bin32"); }
54};
55
56template <typename B> void read(B& it, address& addr)
57{
58 using serialize::read;
59 fr address_field;
60 read(it, address_field);
61 addr = address(address_field);
62}
63
64template <typename B> void write(B& buf, address const& addr)
65{
66 using serialize::write;
67 write(buf, addr.address_);
68}
69
70// Circuit type
71template <typename Builder> class address_t {
72 public:
73 field_t<Builder> address_;
74 Builder* context_;
75
76 address_t() = default;
77
78 address_t(address_t<Builder> const& other)
79 : address_(other.address_)
80 , context_(other.context_){};
81
83 : address_(address)
84 , context_(address.context){};
85
87 : address_(address)
88 , context_(nullptr){};
89
90 address_t(int const& address)
91 : address_(address)
92 , context_(nullptr){};
93
94 address_t(witness_t<Builder> const& witness)
95 {
96 address_ = field_t(witness);
97 context_ = witness.context;
98 }
99
100 address_t<Builder>& operator=(const address_t<Builder>& other)
101 {
102 address_ = other.address_;
103 context_ = other.context_;
104 return *this;
105 }
106
107 bool_t<Builder> operator==(const address_t& other) const { return this->to_field() == other.to_field(); }
108
109 bool_t<Builder> operator!=(const address_t& other) const { return this->to_field() != other.to_field(); }
110
111 field_t<Builder> to_field() const { return address_; }
112
113 fr get_value() const { return address_.get_value(); };
114
115 void assert_equal(const address_t& rhs, std::string const& msg = "address_t::assert_equal") const
116 {
117 address_.assert_equal(rhs.address_, msg);
118 };
119
120 void assert_is_in_set(const std::vector<address_t>& set,
121 std::string const& msg = "address_t::assert_is_in_set") const
122 {
123 std::vector<field_t<Builder>> field_set;
124 for (const auto& e : set) {
125 field_set.push_back(e.address_);
126 }
127 address_.assert_is_in_set(field_set, msg);
128 }
129
130 static address_t conditional_assign(const bool_t<Builder>& predicate, const address_t& lhs, const address_t& rhs)
131 {
132 return field_t<Builder>::conditional_assign(predicate, lhs.address_, rhs.address_);
133 };
134
135 static address_t<Builder> derive_from_private_key(field_t<Builder> const& private_key)
136 {
137 // TODO: Dummy logic, for now. Proper derivation undecided.
138 cycle_group<Builder> public_key = cycle_group<Builder>(grumpkin::g1::affine_one) *
140 return address_t<Builder>(public_key.x);
141 }
142
143 friend std::ostream& operator<<(std::ostream& os, address_t<Builder> const& v) { return os << v.address_; }
144};
145
146} // namespace stdlib
147} // namespace proof_system::plonk
Definition: uint256.hpp:25
Definition: standard_circuit_builder.hpp:12
Definition: address.hpp:71
Definition: address.hpp:21
cycle_group represents a group Element of the proving system's embedded curve i.e....
Definition: cycle_group.hpp:27
Definition: field.hpp:10
void assert_equal(const field_t &rhs, std::string const &msg="field_t::assert_equal") const
Constrain that this field is equal to the given field.
Definition: field.cpp:749
Definition: witness.hpp:10
Definition: widget.bench.cpp:13
static cycle_scalar create_from_bn254_scalar(const field_t &_in, bool skip_primality_test=false)
Use when we want to multiply a group element by a string of bits of known size. N....
Definition: cycle_group.cpp:610