| 128 | } |
| 129 | |
| 130 | func FinalCheck(api frontend.Variable, column_combine_option ColumnCombineOptions) { |
| 131 | if len(Table) == 0 || len(QueryID) == 0 { |
| 132 | panic("empty table or empty query") |
| 133 | } // Should we allow this? |
| 134 | //if len(QueryID) != a power of 2, padding with query0 |
| 135 | if !IsPowerOf2(len(QueryID)) { |
| 136 | nextPower2 := 1 << uint(math.Ceil(math.Log2(float64(len(QueryID))))) |
| 137 | for i := len(QueryID); i < nextPower2; i++ { |
| 138 | QueryID = append(QueryID, QueryID[0]) |
| 139 | QueryResult = append(QueryResult, QueryResult[0]) |
| 140 | } |
| 141 | } |
| 142 | ecgoApi := api.(ecgo.API) |
| 143 | // The challenge used to complete polynomial identity check |
| 144 | alpha := ecgoApi.GetRandomValue() |
| 145 | |
| 146 | column_combine_randomness := GetColumnRandomness(ecgoApi, uint(len(Table[0])), column_combine_option) |
| 147 | |
| 148 | // Table Polynomial |
| 149 | table_single_column := CombineColumn(ecgoApi, Table, column_combine_randomness) |
| 150 | // inputs := |
| 151 | inputs := []frontend.Variable{frontend.Variable(len(Table))} |
| 152 | for i := 0; i < len(Table); i++ { |
| 153 | inputs = append(inputs, Table[i][0]) |
| 154 | } |
| 155 | inputs = append(inputs, QueryID...) |
| 156 | query_count, _ := ecgoApi.NewHint( |
| 157 | QueryCountBaseKeysHintFn, |
| 158 | len(Table), |
| 159 | inputs..., |
| 160 | ) |
| 161 | |
| 162 | table_poly := make([]RationalNumber, len(table_single_column)) |
| 163 | for i := 0; i < len(table_single_column); i++ { |
| 164 | table_poly[i] = RationalNumber{ |
| 165 | Numerator: query_count[i], |
| 166 | Denominator: ecgoApi.Sub(alpha, table_single_column[i]), |
| 167 | } |
| 168 | } |
| 169 | table_poly_at_alpha := SumRationalNumbers(ecgoApi, table_poly) |
| 170 | // Query Polynomial |
| 171 | query_single_column := CombineColumn(ecgoApi, QueryResult, column_combine_randomness) |
| 172 | query_poly := make([]RationalNumber, len(query_single_column)) |
| 173 | for i := 0; i < len(query_single_column); i++ { |
| 174 | query_poly[i] = RationalNumber{ |
| 175 | Numerator: 1, |
| 176 | Denominator: ecgoApi.Sub(alpha, query_single_column[i]), |
| 177 | } |
| 178 | } |
| 179 | query_poly_at_alpha := SumRationalNumbers(ecgoApi, query_poly) |
| 180 | ecgoApi.AssertIsEqual( |
| 181 | ecgoApi.Mul(table_poly_at_alpha.Numerator, query_poly_at_alpha.Denominator), |
| 182 | ecgoApi.Mul(query_poly_at_alpha.Numerator, table_poly_at_alpha.Denominator), |
| 183 | ) |
| 184 | } |
| 185 | |
| 186 | func NewRandomCircuit( |
| 187 | n_table_rows uint, |