@@ -24,8 +24,9 @@ namespace ECC {
24
24
25
25
InnerProduct::BatchContext::BatchContext (uint32_t nCasualTotal)
26
26
:m_CasualTotal(nCasualTotal)
27
- ,m_bEnableBatch (false )
27
+ ,m_bDirty (false )
28
28
{
29
+ assert (nCasualTotal);
29
30
m_Multiplier = Zero;
30
31
31
32
m_ppPrepared = m_Bufs.m_ppPrepared ;
@@ -42,20 +43,15 @@ namespace ECC {
42
43
m_ppPrepared[s_Idx_H] = &Context::get ().m_Ipp .H_ ;
43
44
44
45
m_Prepared = s_CountPrepared;
45
- Reset ();
46
46
}
47
47
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 ()
56
49
{
50
+ Point::Native res;
57
51
Mode::Scope scope (Mode::Fast);
58
52
MultiMac::Calculate (res);
53
+
54
+ m_Sum += res;
59
55
}
60
56
61
57
bool InnerProduct::BatchContext::AddCasual (const Point& p, const Scalar::Native& k)
@@ -70,82 +66,61 @@ namespace ECC {
70
66
71
67
void InnerProduct::BatchContext::AddCasual (const Point::Native& pt, const Scalar::Native& k)
72
68
{
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
+ }
74
78
75
79
Casual& c = m_pCasual[m_Casual++];
76
80
77
81
c.Init (pt, k);
78
- if (m_bEnableBatch)
79
- c.m_K *= m_Multiplier;
82
+ c.m_K *= m_Multiplier;
80
83
}
81
84
82
85
void InnerProduct::BatchContext::AddPrepared (uint32_t i, const Scalar::Native& k)
83
86
{
84
87
assert (i < s_CountPrepared);
85
88
Scalar::Native& trg = m_Bufs.m_pKPrep [i];
86
89
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 ;
88
96
}
89
97
90
98
bool InnerProduct::BatchContext::Flush ()
91
99
{
92
100
if (!m_bDirty)
93
101
return true ;
102
+ m_bDirty = false ;
94
103
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);
102
106
}
103
107
104
- bool InnerProduct::BatchContext::EquationBegin (uint32_t nCasualNeeded )
108
+ void InnerProduct::BatchContext::EquationBegin ()
105
109
{
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)
118
111
{
119
- if (!Flush ())
120
- return false ;
121
- }
112
+ m_bDirty = true ;
122
113
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 );
130
117
}
131
118
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;
149
124
}
150
125
151
126
@@ -495,14 +470,10 @@ namespace ECC {
495
470
Challenges cs_;
496
471
cs_.Init (oracle, dotAB, *this );
497
472
498
- if (!bc.EquationBegin (1 + nCycles * 2 ))
499
- return false ;
500
-
473
+ bc.EquationBegin ();
501
474
bc.AddCasual (commAB, cs_.m_Mul2 );
502
475
503
- return
504
- IsValid (bc, cs_, dotAB, mod) &&
505
- bc.EquationEnd ();
476
+ return IsValid (bc, cs_, dotAB, mod);
506
477
}
507
478
508
479
bool InnerProduct::IsValid (BatchContext& bc, Challenges& cs_, const Scalar::Native& dotAB, const Modifier& mod) const
@@ -569,8 +540,7 @@ namespace ECC {
569
540
k = m_pCondensed[j];
570
541
k = -k;
571
542
572
- if (bc.m_bEnableBatch )
573
- k *= bc.m_Multiplier ;
543
+ k *= bc.m_Multiplier ;
574
544
575
545
k *= cs_.m_Mul1 ;
576
546
@@ -1039,8 +1009,6 @@ namespace ECC {
1039
1009
return IsValid (commitment, oracle, *InnerProduct::BatchContext::s_pInstance, pHGen);
1040
1010
1041
1011
InnerProduct::BatchContextEx<1 > bc;
1042
- bc.m_bEnableBatch = true ; // why not?
1043
-
1044
1012
return
1045
1013
IsValid (commitment, oracle, bc, pHGen) &&
1046
1014
bc.Flush ();
@@ -1090,10 +1058,9 @@ namespace ECC {
1090
1058
1091
1059
Point::Native p;
1092
1060
1093
- if (!bc.EquationBegin (3 + (bCustom != false )))
1094
- return false ;
1095
-
1061
+ bc.EquationBegin ();
1096
1062
bc.AddCasual (commitment, -zz);
1063
+
1097
1064
if (!bc.AddCasual (m_Part2.m_T1 , -cs.x ))
1098
1065
return false ;
1099
1066
if (!bc.AddCasual (m_Part2.m_T2 , -xx))
@@ -1110,13 +1077,9 @@ namespace ECC {
1110
1077
else
1111
1078
bc.AddPrepared (InnerProduct::BatchContext::s_Idx_H, sumY);
1112
1079
1113
- if (!bc.EquationEnd ())
1114
- return false ;
1115
-
1116
1080
// (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) )
1117
1081
1118
- if (!bc.EquationBegin (2 + InnerProduct::nCycles * 2 ))
1119
- return false ;
1082
+ bc.EquationBegin ();
1120
1083
1121
1084
InnerProduct::Challenges cs_;
1122
1085
cs_.Init (oracle, tDot, m_P_Tag);
@@ -1156,10 +1119,7 @@ namespace ECC {
1156
1119
InnerProduct::Modifier mod;
1157
1120
mod.m_pMultiplier [1 ] = &cs.yInv ;
1158
1121
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);
1163
1123
}
1164
1124
1165
1125
int RangeProof::Confidential::cmp (const Confidential& x) const
0 commit comments