Skip to content

Commit 89e9440

Browse files
committed
refactor: simplify public signal serialisation
1 parent 01ad4c6 commit 89e9440

File tree

2 files changed

+31
-35
lines changed

2 files changed

+31
-35
lines changed

js/src/gnark/toprf.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export function makeGnarkOPRFOperator({
2727
const rslt = await executeGnarkFnAndGetJson(lib.prove, witness)
2828
if(typeof rslt !== 'object' || !('proof' in rslt) || !rslt.proof) {
2929
throw new Error(
30-
`Failed to create gnark proof: ${JSON.stringify(rslt)}`
30+
`Failed to create gnark TOPRF proof: ${JSON.stringify(rslt)}`
3131
)
3232
}
3333

js/src/zk.ts

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@ export async function verifyProof(opts: VerifyProofOpts): Promise<void> {
4747
let verified: boolean
4848
if('toprf' in opts) {
4949
verified = await operator.groth16Verify(
50-
{ ...publicSignals, toprf: opts.toprf },
51-
proofData,
52-
logger
50+
{ ...publicSignals, toprf: opts.toprf }, proofData, logger
5351
)
5452
} else {
5553
// serialise to array of numbers for the ZK circuit
@@ -100,37 +98,36 @@ export async function getPublicSignals(
10098
const expSize = getExpectedChunkSizeBytes(algorithm)
10199

102100
publicInput = Array.isArray(publicInput) ? publicInput : [publicInput]
103-
if(publicInput.length) {
104-
for(const [i, { ciphertext, iv, offsetBytes = 0 }] of publicInput.entries()) {
105-
const blocks = splitCiphertextToBlocks(algorithm, ciphertext, iv)
106-
for(const block of blocks) {
107-
await addCiphertextBlock(
108-
{ ...block, offsetBytes: offsetBytes + (block.offsetBytes || 0) }
109-
)
110-
}
111-
112-
if(i < publicInput.length - 1) {
113-
continue
114-
}
115-
116-
const bytesDone = ciphertextBlocks.reduce((a, b) => a + b.length, 0)
117-
if(bytesDone >= expSize) {
118-
continue
119-
}
120-
121-
const padding = new Uint8Array(expSize - bytesDone)
122-
const offset = offsetBytes + ceilToMultipleOf(ciphertext.length, blockSize)
123-
const paddingBlocks = splitCiphertextToBlocks(algorithm, padding, iv)
124-
for(const block of paddingBlocks) {
125-
await addCiphertextBlock(
126-
{ ...block, offsetBytes: offset + (block.offsetBytes || 0) }
127-
)
128-
}
129-
}
130-
} else {
101+
if(!publicInput.length) {
131102
throw new Error('at least one public input is required')
132103
}
133104

105+
for(const [i, { ciphertext, iv, offsetBytes = 0 }] of publicInput.entries()) {
106+
const blocks = splitCiphertextToBlocks(algorithm, ciphertext, iv)
107+
for(const block of blocks) {
108+
await addCiphertextBlock(
109+
{ ...block, offsetBytes: offsetBytes + (block.offsetBytes || 0) }
110+
)
111+
}
112+
113+
if(i < publicInput.length - 1) {
114+
continue
115+
}
116+
117+
const bytesDone = ciphertextBlocks.reduce((a, b) => a + b.length, 0)
118+
if(bytesDone >= expSize) {
119+
continue
120+
}
121+
122+
const padding = expSize - bytesDone
123+
const offset = offsetBytes + ceilToMultipleOf(ciphertext.length, blockSize)
124+
for(let i = 0;i < padding; i += blockSize) {
125+
await addCiphertextBlock(
126+
{ ciphertext: new Uint8Array(), iv, offsetBytes: offset + i }
127+
)
128+
}
129+
}
130+
134131
const pubSigs: ZKProofPublicSignals = {
135132
noncesAndCounters,
136133
in: concatenateUint8Arrays(ciphertextBlocks),
@@ -155,9 +152,8 @@ export async function getPublicSignals(
155152
}
156153

157154
const startCounter = getCounterForByteOffset(algorithm, offsetBytes)
158-
noncesAndCounters.push(
159-
{ nonce: iv, counter: startCounter, boundary: ciphertext.length }
160-
)
155+
const boundary = ciphertext.length
156+
noncesAndCounters.push({ nonce: iv, counter: startCounter, boundary })
161157

162158
ciphertext = padCiphertextToSize(ciphertext, blockSize)
163159
ciphertextBlocks.push(ciphertext)

0 commit comments

Comments
 (0)