-
Notifications
You must be signed in to change notification settings - Fork 33
[FEAT] Raw OPRF (no ZK no MPC) for browser extension #66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Scratch-net
wants to merge
5
commits into
main
Choose a base branch
from
oprf-raw
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| import { ethers } from 'ethers' | ||
|
|
||
| import { TOPRF_DOMAIN_SEPARATOR } from '#src/config/index.ts' | ||
| import type { MessageReveal_OPRFRawMarker as OPRFRawMarker } from '#src/proto/api.ts' | ||
| import type { Logger } from '#src/types/index.ts' | ||
| import { getEnvVariable } from '#src/utils/env.ts' | ||
| import { makeDefaultOPRFOperator } from '#src/utils/zk.ts' | ||
|
|
||
| export type OPRFRawResult = { | ||
| /** Location of the data that was OPRF'd */ | ||
| dataLocation: { | ||
| fromIndex: number | ||
| length: number | ||
| } | ||
| /** The OPRF nullifier (hash output) */ | ||
| nullifier: Uint8Array | ||
| } | ||
|
|
||
| /** | ||
| * Compute OPRF for plaintext data marked with oprf-raw. | ||
| * This runs server-side since the attestor has access to the revealed plaintext. | ||
| * | ||
| * @param plaintext - The revealed plaintext from the TLS transcript | ||
| * @param markers - Positions in the plaintext to compute OPRF for | ||
| * @param logger - Logger instance | ||
| * @returns Array of OPRF results with nullifiers | ||
| */ | ||
| export async function computeOPRFRaw( | ||
| plaintext: Uint8Array, | ||
| markers: OPRFRawMarker[], | ||
| logger: Logger | ||
| ): Promise<OPRFRawResult[]> { | ||
| if(!markers.length) { | ||
| return [] | ||
| } | ||
|
|
||
| const PRIVATE_KEY_STR = getEnvVariable('TOPRF_SHARE_PRIVATE_KEY') | ||
| const PUBLIC_KEY_STR = getEnvVariable('TOPRF_SHARE_PUBLIC_KEY') | ||
| if(!PRIVATE_KEY_STR || !PUBLIC_KEY_STR) { | ||
| throw new Error('TOPRF keys not configured. Cannot compute oprf-raw.') | ||
| } | ||
|
|
||
| const privateKey = ethers.utils.arrayify(PRIVATE_KEY_STR) | ||
| const publicKey = ethers.utils.arrayify(PUBLIC_KEY_STR) | ||
|
|
||
| // Use gnark engine for server-side OPRF (same as TOPRF handler) | ||
| const operator = makeDefaultOPRFOperator('chacha20', 'gnark', logger) | ||
|
|
||
| const results: OPRFRawResult[] = [] | ||
|
|
||
| for(const marker of markers) { | ||
| const { dataLocation } = marker | ||
| if(!dataLocation) { | ||
| logger.warn('oprf-raw marker missing dataLocation, skipping') | ||
| continue | ||
| } | ||
|
|
||
| const { fromIndex, length } = dataLocation | ||
| const endIndex = fromIndex + length | ||
|
|
||
| if(endIndex > plaintext.length) { | ||
| throw new Error( | ||
| `oprf-raw marker out of bounds: fromIndex=${fromIndex}, length=${length}, plaintextLength=${plaintext.length}` | ||
| ) | ||
| } | ||
|
|
||
| // Extract the data to OPRF | ||
| const data = plaintext.slice(fromIndex, endIndex) | ||
|
|
||
| // Generate OPRF request (server-side, we do the full flow) | ||
| const request = await operator.generateOPRFRequestData( | ||
| data, | ||
| TOPRF_DOMAIN_SEPARATOR, | ||
| logger | ||
| ) | ||
|
|
||
| // Evaluate OPRF with server's private key | ||
| const response = await operator.evaluateOPRF( | ||
| privateKey, | ||
| request.maskedData, | ||
| logger | ||
| ) | ||
|
|
||
| // Finalize to get the nullifier | ||
| const nullifier = await operator.finaliseOPRF( | ||
| publicKey, | ||
| request, | ||
| [{ ...response, publicKeyShare: publicKey }], | ||
| logger | ||
| ) | ||
|
|
||
| results.push({ | ||
| dataLocation: { fromIndex, length }, | ||
| nullifier | ||
| }) | ||
|
|
||
| logger.debug( | ||
| { fromIndex, length, nullifierHex: Buffer.from(nullifier).toString('hex').slice(0, 16) + '...' }, | ||
| 'computed oprf-raw nullifier' | ||
| ) | ||
| } | ||
|
|
||
| return results | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.