Verify complete Dark Tariff proof commitments

This commit is contained in:
takuya committed 2026-11-29 14:39:16 +03:00
1 parent 20cf14d953
commit 0b98b70bf9
3 files changed
+22 -7

No files matched your search

@@ -98,11 +98,12 @@ begin
v_metrics := v_metrics || jsonb_build_object(v_metric ->> 'key', v_value);
end loop;
v_parameters := chat.make_parameters();
insert into billing.reading (serial, kind, metrics, n, h, y, w, parameters)
insert into billing.reading (serial, kind, metrics, n, h, y, w, parameters, verification_witness)
values (btrim(p_serial), p_kind, v_metrics, (v_parameters ->> 'n')::integer,
array(select x::bit varying from jsonb_array_elements_text(v_parameters -> 'h') x),
(v_parameters ->> 'y')::bit varying, (v_parameters ->> 'w')::integer,
v_parameters ->> 'parameters') returning * into v_reading;
v_parameters ->> 'parameters', (v_parameters ->> 'witness')::bit varying)
returning * into v_reading;
return jsonb_build_object('id', v_reading.id, 'parameters', v_reading.parameters,
'verification_id', v_parameters ->> 'verification_id');
end;
@@ -61,6 +61,7 @@ begin
|| v_y::text || '|' || v_w::text, 'UTF8'), 'base64'), E'\n', '');
return jsonb_build_object('n', v_n, 'h', to_jsonb(v_h), 'y', v_y::text, 'w', v_w,
'parameters', v_params,
'witness', v_s::text,
'verification_id', replace(encode(chat.pack(v_s), 'base64'), E'\n', ''));
end;
$$;
@@ -106,10 +107,14 @@ begin
else repeat('0', bit_length(p_reading.y))::bit varying end)::text);
if p_challenge = 0 then
v_b := chat.hash('b|' || chat.permute(v_v, v_p)::text);
return v_a = p_values[1] and v_b = p_values[2];
return v_a = p_values[1] and v_b = p_values[2]
and (p_reading.verification_witness is null
or chat.hash('c|' || chat.permute(v_v # p_reading.verification_witness, v_p)::text) = p_values[3]);
end if;
v_c := chat.hash('c|' || chat.permute(v_v, v_p)::text);
return v_a = p_values[1] and v_c = p_values[3];
return v_a = p_values[1] and v_c = p_values[3]
and (p_reading.verification_witness is null
or chat.hash('b|' || chat.permute(v_v # p_reading.verification_witness, v_p)::text) = p_values[2]);
elsif p_challenge = 2 then
v_text := p_data ->> 't';
if v_text is null or length(v_text) <> p_reading.n or v_text !~ '^[01]+$' then
@@ -156,7 +161,14 @@ begin
or exists (select 1 from unnest(p_values) x where x is null or x !~ '^[0-9a-f]{64}$') then
raise exception 'Invalid data format.' using errcode = 'P0400';
end if;
v_challenge := chat.challenge();
-- Both of these challenges let the server check all three commitments.
if p_round = 1 and exists (
select 1 from billing.reading where id = v_session.reading_id and verification_witness is not null
) then
v_challenge := get_byte(gen_random_bytes(1), 0) % 2;
else
v_challenge := chat.challenge();
end if;
update chat.check_session set data = p_values, challenge = v_challenge where id = p_id;
return jsonb_build_object('round', p_round, 'stage', 'answer', 'challenge', v_challenge,
'message', format('Step %s of 10. Challenge: %s. Submit an answer.', p_round, v_challenge));
@@ -13,11 +13,13 @@ create table if not exists billing.reading (
y bit varying not null,
w integer not null,
parameters text not null,
failed_checks integer not null default 0
failed_checks integer not null default 0,
verification_witness bit varying
);
alter table billing.reading
add column if not exists failed_checks integer not null default 0;
add column if not exists failed_checks integer not null default 0,
add column if not exists verification_witness bit varying;
create index if not exists reading_created_at_idx
on billing.reading (created_at);