Skip to content

Commit d70aee5

Browse files
committed
add ProverInput and VerifierInput abstraction
1 parent cda6f3a commit d70aee5

File tree

3 files changed

+96
-14
lines changed

3 files changed

+96
-14
lines changed

src.ts/index.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import {
77
verifyExecutionWitnessPreState as verifyExecutionWitnessPreStateBase,
88
createProof as createProofBase,
99
verifyProof as verifyProofBase,
10+
ProverInput as ProverInputBase,
11+
VerifierInput as VerifierInputBase,
1012
} from './verkleFFIBindings/index.js'
1113
import { Context as VerkleFFI } from './wasm/rust_verkle_wasm.js'
1214

@@ -29,15 +31,18 @@ export const loadVerkleCrypto = async () => {
2931
): Commitment =>
3032
updateCommitmentBase(verkleFFI, commitment, commitmentIndex, oldScalarValue, newScalarValue)
3133

32-
const verifyExecutionWitnessPreState = (prestateRoot: string, execution_witness_json: string): boolean =>
33-
verifyExecutionWitnessPreStateBase(prestateRoot, execution_witness_json)
34+
const verifyExecutionWitnessPreState = (
35+
prestateRoot: string,
36+
execution_witness_json: string,
37+
): boolean => verifyExecutionWitnessPreStateBase(prestateRoot, execution_witness_json)
3438

3539
const zeroCommitment = zeroCommitmentBase()
3640

3741
const hashCommitment = (commitment: Uint8Array) => verkleFFI.hashCommitment(commitment)
3842
const serializeCommitment = (commitment: Uint8Array) => verkleFFI.serializeCommitment(commitment)
39-
const createProof = (input: Uint8Array) => verkleFFI.createProof(input)
40-
const verifyProof = (proofInput: Uint8Array) => verkleFFI.verifyProof(proofInput)
43+
const createProof = (proverInputs: ProverInput[]) => createProofBase(verkleFFI, proverInputs)
44+
const verifyProof = (proof: Uint8Array, verifierInputs: VerifierInput[]) =>
45+
verifyProofBase(verkleFFI, proof, verifierInputs)
4146
return {
4247
getTreeKey,
4348
getTreeKeyHash,
@@ -47,13 +52,19 @@ export const loadVerkleCrypto = async () => {
4752
hashCommitment,
4853
serializeCommitment,
4954
createProof,
50-
verifyProof
55+
verifyProof,
5156
}
5257
}
5358

59+
// Input used to create proofs over vectors
60+
export type ProverInput = ProverInputBase
61+
// Input needed to verify proofs over vectors
62+
// alongside the proof.
63+
export type VerifierInput = VerifierInputBase
64+
5465
// This is a 32 byte serialized field element
5566
export type Scalar = Uint8Array
5667

5768
// This is a 64 byte serialized point.
5869
// It is 64 bytes because the point is serialized in uncompressed format.
59-
export type Commitment = Uint8Array
70+
export type Commitment = Uint8Array

src.ts/verkleFFIBindings/index.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
1-
import { initVerkleWasm, zeroCommitment, verifyExecutionWitnessPreState } from '../wasm/rust_verkle_wasm.js'
1+
import {
2+
initVerkleWasm,
3+
zeroCommitment,
4+
verifyExecutionWitnessPreState,
5+
} from '../wasm/rust_verkle_wasm.js'
26

3-
import { getTreeKey, getTreeKeyHash, updateCommitment, createProof, verifyProof } from './verkleFFI.js'
7+
import {
8+
getTreeKey,
9+
getTreeKeyHash,
10+
updateCommitment,
11+
createProof,
12+
verifyProof,
13+
ProverInput,
14+
VerifierInput,
15+
} from './verkleFFI.js'
416

517
export {
618
initVerkleWasm,
@@ -10,5 +22,7 @@ export {
1022
zeroCommitment,
1123
verifyExecutionWitnessPreState,
1224
createProof,
13-
verifyProof
25+
verifyProof,
26+
ProverInput,
27+
VerifierInput,
1428
}

src.ts/verkleFFIBindings/verkleFFI.ts

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { concatBytes } from '@ethereumjs/util'
2+
13
import { Context as VerkleFFI } from '../wasm/rust_verkle_wasm.js'
24

35
// This is equal to 2n + 256n * 64n.
@@ -114,10 +116,65 @@ export function updateCommitment(
114116
return verkleFFI.updateCommitment(commitment, commitmentIndex, oldScalarValue, newScalarValue)
115117
}
116118

117-
export function createProof(verkleFFI: VerkleFFI, bytes: Uint8Array): Uint8Array {
118-
return verkleFFI.createProof(bytes)
119+
export function createProof(verkleFFI: VerkleFFI, proverInputs: ProverInput[]): Uint8Array {
120+
const serializedProofInputs = serializedProverInputs(proverInputs)
121+
return verkleFFI.createProof(serializedProofInputs)
122+
}
123+
124+
export function verifyProof(
125+
verkleFFI: VerkleFFI,
126+
proof: Uint8Array,
127+
verifierInputs: VerifierInput[],
128+
): boolean {
129+
const serializedVerifierInput = serializeVerifierInputs(proof, verifierInputs)
130+
return verkleFFI.verifyProof(serializedVerifierInput)
131+
}
132+
133+
export interface ProverInput {
134+
// Commitment to the vector we want to create a proof for
135+
serializedCommitment: Uint8Array
136+
// The vector that we want to make proofs over
137+
vector: Uint8Array[]
138+
// The indices that we want to prove exist in the vector
139+
indices: number[]
119140
}
120141

121-
export function verifyProof(verkleFFI: VerkleFFI, proof: Uint8Array): boolean {
122-
return verkleFFI.verifyProof(proof)
123-
}
142+
function serializedProverInputs(proofInputs: ProverInput[]): Uint8Array {
143+
const serializedProverInputs = proofInputs.flatMap(({ serializedCommitment, vector, indices }) =>
144+
indices.flatMap((index) => [
145+
serializedCommitment,
146+
...vector,
147+
new Uint8Array([index]),
148+
vector[index],
149+
]),
150+
)
151+
152+
return concatBytes(...serializedProverInputs)
153+
}
154+
155+
export interface VerifierInput {
156+
// A commitment to the vector that we want to verify
157+
// proofs over.
158+
serializedCommitment: Uint8Array
159+
// A tuple of index and values that we want to verify about the
160+
// committed vector.
161+
//
162+
// ie (index_i, value_i) will verify that the value of the committed
163+
// vector at index `index_i` was `value_i`
164+
indexValuePairs: Array<{ index: number; value: Uint8Array }>
165+
}
166+
167+
function serializeVerifierInputs(proof: Uint8Array, verifierInputs: VerifierInput[]): Uint8Array {
168+
const serializedVerifierInputs = [
169+
proof,
170+
...verifierInputs.flatMap(({ serializedCommitment, indexValuePairs }) =>
171+
indexValuePairs.flatMap(({ index, value }) => [
172+
serializedCommitment,
173+
new Uint8Array([index]),
174+
value,
175+
]),
176+
),
177+
]
178+
179+
return concatBytes(...serializedVerifierInputs)
180+
}

0 commit comments

Comments
 (0)