Skip to content

Commit 398eac8

Browse files
committed
Merge branch 'bright_boson_2.2RC' into mainnet_bright_boson_2.2RC
2 parents dca77cf + 3a3d02c commit 398eac8

File tree

12 files changed

+229
-209
lines changed

12 files changed

+229
-209
lines changed

core/block_crypt.cpp

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ namespace beam
133133
if (!iSubkey)
134134
return pKdf; // by convention: scheme V0, Subkey=0 - is a master key
135135

136+
if (Key::IDV::Scheme::BB21 == kidv.get_Scheme())
137+
return pKdf; // BB2.1 workaround
138+
136139
return get_Child(*pKdf, iSubkey);
137140
}
138141

@@ -178,17 +181,27 @@ namespace beam
178181
void SwitchCommitment::get_Hash(ECC::Hash::Value& hv, const Key::IDV& kidv)
179182
{
180183
Key::Index nScheme = kidv.get_Scheme();
181-
if (nScheme)
184+
if (nScheme > Key::IDV::Scheme::V0)
182185
{
183-
// newer scheme - account for the Value.
184-
// Make it infeasible to tamper with value for unknown blinding factor
185-
ECC::Hash::Processor()
186-
<< "kidv-1"
187-
<< kidv.m_Idx
188-
<< kidv.m_Type.V
189-
<< kidv.m_SubIdx
190-
<< kidv.m_Value
191-
>> hv;
186+
if (Key::IDV::Scheme::BB21 == nScheme)
187+
{
188+
// BB2.1 workaround
189+
Key::IDV kidv2 = kidv;
190+
kidv2.set_Subkey(kidv.get_Subkey(), Key::IDV::Scheme::V0);
191+
kidv2.get_Hash(hv);
192+
}
193+
else
194+
{
195+
// newer scheme - account for the Value.
196+
// Make it infeasible to tamper with value for unknown blinding factor
197+
ECC::Hash::Processor()
198+
<< "kidv-1"
199+
<< kidv.m_Idx
200+
<< kidv.m_Type.V
201+
<< kidv.m_SubIdx
202+
<< kidv.m_Value
203+
>> hv;
204+
}
192205
}
193206
else
194207
kidv.get_Hash(hv); // legacy

core/ecc.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1646,14 +1646,13 @@ namespace ECC {
16461646
InnerProduct::BatchContext* pBc = InnerProduct::BatchContext::s_pInstance;
16471647
if (pBc)
16481648
{
1649-
if (!pBc->EquationBegin(2))
1650-
return false;
1649+
pBc->EquationBegin();
16511650

16521651
pBc->AddPrepared(InnerProduct::BatchContext::s_Idx_G, m_k);
16531652
pBc->AddCasual(pk, e);
16541653
pBc->AddCasual(pubNonce, 1U);
16551654

1656-
return pBc->EquationEnd();
1655+
return true;
16571656
}
16581657

16591658
Point::Native pt = Context::get().G * m_k;

core/ecc.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ namespace ECC
237237
{
238238
static const uint8_t V0 = 0;
239239
static const uint8_t V1 = 1;
240+
static const uint8_t BB21 = 2; // worakround for BB.2.1
240241

241242
static const uint32_t s_SubKeyBits = 24;
242243
static const Index s_SubKeyMask = (static_cast<Index>(1) << s_SubKeyBits) - 1;
@@ -284,6 +285,16 @@ namespace ECC
284285
#pragma pack (pop)
285286

286287
void operator = (const Packed&);
288+
289+
bool IsBb21Possible() const
290+
{
291+
return m_SubIdx && (Scheme::V0 == get_Scheme());
292+
}
293+
294+
void set_WorkaroundBb21()
295+
{
296+
set_Subkey(get_Subkey(), Scheme::BB21);
297+
}
287298

288299
int cmp(const IDV&) const;
289300
COMPARISON_VIA_CMP

core/ecc_bulletproof.cpp

Lines changed: 43 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ namespace ECC {
2424

2525
InnerProduct::BatchContext::BatchContext(uint32_t nCasualTotal)
2626
:m_CasualTotal(nCasualTotal)
27-
,m_bEnableBatch(false)
27+
,m_bDirty(false)
2828
{
29+
assert(nCasualTotal);
2930
m_Multiplier = Zero;
3031

3132
m_ppPrepared = m_Bufs.m_ppPrepared;
@@ -42,20 +43,15 @@ namespace ECC {
4243
m_ppPrepared[s_Idx_H] = &Context::get().m_Ipp.H_;
4344

4445
m_Prepared = s_CountPrepared;
45-
Reset();
4646
}
4747

48-
void InnerProduct::BatchContext::Reset()
49-
{
50-
m_Casual = 0;
51-
ZeroObject(m_Bufs.m_pKPrep);
52-
m_bDirty = false;
53-
}
54-
55-
void InnerProduct::BatchContext::Calculate(Point::Native& res)
48+
void InnerProduct::BatchContext::Calculate()
5649
{
50+
Point::Native res;
5751
Mode::Scope scope(Mode::Fast);
5852
MultiMac::Calculate(res);
53+
54+
m_Sum += res;
5955
}
6056

6157
bool InnerProduct::BatchContext::AddCasual(const Point& p, const Scalar::Native& k)
@@ -70,82 +66,61 @@ namespace ECC {
7066

7167
void InnerProduct::BatchContext::AddCasual(const Point::Native& pt, const Scalar::Native& k)
7268
{
73-
assert(uint32_t(m_Casual) < m_CasualTotal);
69+
if (uint32_t(m_Casual) == m_CasualTotal)
70+
{
71+
assert(s_CountPrepared == m_Prepared);
72+
m_Prepared = 0; // don't count them now
73+
Calculate();
74+
75+
m_Casual = 0;
76+
m_Prepared = s_CountPrepared;
77+
}
7478

7579
Casual& c = m_pCasual[m_Casual++];
7680

7781
c.Init(pt, k);
78-
if (m_bEnableBatch)
79-
c.m_K *= m_Multiplier;
82+
c.m_K *= m_Multiplier;
8083
}
8184

8285
void InnerProduct::BatchContext::AddPrepared(uint32_t i, const Scalar::Native& k)
8386
{
8487
assert(i < s_CountPrepared);
8588
Scalar::Native& trg = m_Bufs.m_pKPrep[i];
8689

87-
trg += m_bEnableBatch ? (k * m_Multiplier) : k;
90+
trg += (k * m_Multiplier);
91+
}
92+
93+
void InnerProduct::BatchContext::Reset()
94+
{
95+
m_bDirty = false;
8896
}
8997

9098
bool InnerProduct::BatchContext::Flush()
9199
{
92100
if (!m_bDirty)
93101
return true;
102+
m_bDirty = false;
94103

95-
Point::Native pt;
96-
Calculate(pt);
97-
if (!(pt == Zero))
98-
return false;
99-
100-
Reset();
101-
return true;
104+
Calculate();
105+
return (m_Sum == Zero);
102106
}
103107

104-
bool InnerProduct::BatchContext::EquationBegin(uint32_t nCasualNeeded)
108+
void InnerProduct::BatchContext::EquationBegin()
105109
{
106-
if (nCasualNeeded > m_CasualTotal)
107-
{
108-
assert(false);
109-
return false; // won't fit!
110-
}
111-
112-
#ifndef NDEBUG
113-
m_CasualAtEndExpected = nCasualNeeded;
114-
#endif // NDEBUG
115-
116-
nCasualNeeded += m_Casual;
117-
if (nCasualNeeded > m_CasualTotal)
110+
if (!m_bDirty)
118111
{
119-
if (!Flush())
120-
return false;
121-
}
112+
m_bDirty = true;
122113

123-
if (m_bEnableBatch)
124-
{
125-
// mutate multiplier
126-
if (m_Multiplier == Zero)
127-
m_Multiplier.GenRandomNnz();
128-
else
129-
Oracle() << m_Multiplier >> m_Multiplier;
114+
m_Sum = Zero;
115+
m_Casual = 0;
116+
ZeroObject(m_Bufs.m_pKPrep);
130117
}
131118

132-
#ifndef NDEBUG
133-
m_CasualAtEndExpected += m_Casual;
134-
#endif // NDEBUG
135-
136-
m_bDirty = true;
137-
return true;
138-
}
139-
140-
bool InnerProduct::BatchContext::EquationEnd()
141-
{
142-
assert(m_bDirty);
143-
assert(m_Casual == m_CasualAtEndExpected);
144-
145-
if (!m_bEnableBatch)
146-
return Flush();
147-
148-
return true;
119+
// mutate multiplier
120+
if (m_Multiplier == Zero)
121+
m_Multiplier.GenRandomNnz();
122+
else
123+
Oracle() << m_Multiplier >> m_Multiplier;
149124
}
150125

151126

@@ -495,14 +470,10 @@ namespace ECC {
495470
Challenges cs_;
496471
cs_.Init(oracle, dotAB, *this);
497472

498-
if (!bc.EquationBegin(1 + nCycles * 2))
499-
return false;
500-
473+
bc.EquationBegin();
501474
bc.AddCasual(commAB, cs_.m_Mul2);
502475

503-
return
504-
IsValid(bc, cs_, dotAB, mod) &&
505-
bc.EquationEnd();
476+
return IsValid(bc, cs_, dotAB, mod);
506477
}
507478

508479
bool InnerProduct::IsValid(BatchContext& bc, Challenges& cs_, const Scalar::Native& dotAB, const Modifier& mod) const
@@ -569,8 +540,7 @@ namespace ECC {
569540
k = m_pCondensed[j];
570541
k = -k;
571542

572-
if (bc.m_bEnableBatch)
573-
k *= bc.m_Multiplier;
543+
k *= bc.m_Multiplier;
574544

575545
k *= cs_.m_Mul1;
576546

@@ -1039,8 +1009,6 @@ namespace ECC {
10391009
return IsValid(commitment, oracle, *InnerProduct::BatchContext::s_pInstance, pHGen);
10401010

10411011
InnerProduct::BatchContextEx<1> bc;
1042-
bc.m_bEnableBatch = true; // why not?
1043-
10441012
return
10451013
IsValid(commitment, oracle, bc, pHGen) &&
10461014
bc.Flush();
@@ -1090,10 +1058,9 @@ namespace ECC {
10901058

10911059
Point::Native p;
10921060

1093-
if (!bc.EquationBegin(3 + (bCustom != false)))
1094-
return false;
1095-
1061+
bc.EquationBegin();
10961062
bc.AddCasual(commitment, -zz);
1063+
10971064
if (!bc.AddCasual(m_Part2.m_T1, -cs.x))
10981065
return false;
10991066
if (!bc.AddCasual(m_Part2.m_T2, -xx))
@@ -1110,13 +1077,9 @@ namespace ECC {
11101077
else
11111078
bc.AddPrepared(InnerProduct::BatchContext::s_Idx_H, sumY);
11121079

1113-
if (!bc.EquationEnd())
1114-
return false;
1115-
11161080
// (P - m_Mu*G) + m_Mu*G =?= m_A + m_S*x - vec(G)*vec(z) + vec(H)*( vec(z) + vec(z^2*2^n*y^-n) )
11171081

1118-
if (!bc.EquationBegin(2 + InnerProduct::nCycles * 2))
1119-
return false;
1082+
bc.EquationBegin();
11201083

11211084
InnerProduct::Challenges cs_;
11221085
cs_.Init(oracle, tDot, m_P_Tag);
@@ -1156,10 +1119,7 @@ namespace ECC {
11561119
InnerProduct::Modifier mod;
11571120
mod.m_pMultiplier[1] = &cs.yInv;
11581121

1159-
if (!m_P_Tag.IsValid(bc, cs_, tDot, mod))
1160-
return false;
1161-
1162-
return bc.EquationEnd();
1122+
return m_P_Tag.IsValid(bc, cs_, tDot, mod);
11631123
}
11641124

11651125
int RangeProof::Confidential::cmp(const Confidential& x) const

core/ecc_native.h

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -669,25 +669,20 @@ namespace ECC
669669
} m_Bufs;
670670

671671

672-
void Reset();
673-
void Calculate(Point::Native& res);
672+
void Calculate();
674673

675674
const uint32_t m_CasualTotal;
676-
bool m_bEnableBatch;
677675
bool m_bDirty;
678676
Scalar::Native m_Multiplier; // must be initialized in a non-trivial way
679-
680-
#ifndef NDEBUG
681-
int m_CasualAtEndExpected;
682-
#endif // NDEBUG
677+
Point::Native m_Sum; // intermediate result, sum of Casuals
683678

684679
bool AddCasual(const Point& p, const Scalar::Native& k);
685680
void AddCasual(const Point::Native& pt, const Scalar::Native& k);
686681
void AddPrepared(uint32_t i, const Scalar::Native& k);
687682

688-
bool EquationBegin(uint32_t nCasualNeeded);
689-
bool EquationEnd();
683+
void EquationBegin();
690684

685+
void Reset();
691686
bool Flush();
692687

693688
protected:

core/treasury.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,9 @@ namespace beam
100100

101101
virtual void Do(size_t iTask) override
102102
{
103-
typedef InnerProduct::BatchContextEx<100> MyBatch;
103+
typedef InnerProduct::BatchContextEx<4> MyBatch;
104104

105105
std::unique_ptr<MyBatch> p(new MyBatch);
106-
p->m_bEnableBatch = true;
107106
MyBatch::Scope scope(*p);
108107

109108
if (!Verify(iTask) || !p->Flush())

core/unittest/ecc_test.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,6 @@ void TestRangeProof(bool bCustomTag)
721721
}
722722

723723
InnerProduct::BatchContextEx<2> bc;
724-
bc.m_bEnableBatch = true;
725724

726725
{
727726
Oracle oracle;
@@ -2451,9 +2450,8 @@ void RunBenchmark()
24512450
const uint32_t nBatch = 100;
24522451
bm.N = 10 * nBatch;
24532452

2454-
typedef InnerProduct::BatchContextEx<100> MyBatch;
2453+
typedef InnerProduct::BatchContextEx<4> MyBatch;
24552454
std::unique_ptr<MyBatch> p(new MyBatch);
2456-
p->m_bEnableBatch = true;
24572455

24582456
InnerProduct::BatchContext::Scope scope(*p);
24592457

0 commit comments

Comments
 (0)