Skip to content
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

native: Reduce the storage requirements of FHE operations #231

Open
goshawk-3 opened this issue Jan 7, 2025 · 1 comment · May be fixed by #236
Open

native: Reduce the storage requirements of FHE operations #231

goshawk-3 opened this issue Jan 7, 2025 · 1 comment · May be fixed by #236
Assignees
Labels
enhancement New feature or request

Comments

@goshawk-3
Copy link
Contributor

Description

Currently, all computed ciphertexts are stored in the host state, whether it results from a SSTORE operation or serves as an intermediate value.

for _, handle := range handles {
	ciphertext, ok := blockData.materializedCiphertexts[string(handle[:])]
	if !ok {
		return errors.New("ciphertext not found in cache")
	}

	log.Info("Persist ciphertext to state", "block number", blockNumber, "handle",
		handle.TerminalString(), "ciphertext length", len(ciphertext))

	putBytesToAddress(api, contractAddr, handle, ciphertext)
}

In addition, every computation is represented as an entry in the so-called LateCommit queue, which is also persisted in the host state.

	// zero out queue ciphertexts
	for i := 0; i < int(ctCount); i++ {
		ctAddr := blockQueueStorageLayout(blockNumber, int64(i))
		metadata := bytesToMetadata(api.GetState(executorApi.contractStorageAddress, ctAddr.metadata))
		outputHandle := api.GetState(executorApi.contractStorageAddress, ctAddr.outputHandle)

		log.Debug("Reset computation LateCommit queue", "block number", blockNumber,
			"handle", outputHandle.TerminalString())

		handlesToMaterialize = append(handlesToMaterialize, outputHandle)
		api.SetState(executorApi.contractStorageAddress, ctAddr.metadata, zero)
		api.SetState(executorApi.contractStorageAddress, ctAddr.outputHandle, zero)
		api.SetState(executorApi.contractStorageAddress, ctAddr.firstOperand, zero)
		api.SetState(executorApi.contractStorageAddress, ctAddr.secondOperand, zero)
		if metadata.IsBigScalar {
			counter := new(big.Int)
			counter.SetBytes(ctAddr.bigScalarOperand[:])
			// max supported number 2048 is 2048
			for i := 0; i < 2048/256; i++ {
				api.SetState(executorApi.contractStorageAddress, common.BigToHash(counter), zero)
				counter.Add(counter, one)
			}
		}
	}

Solution

Given that, the state is fully replicated across all network participants, we should minimize the storage cost of FHE operations.
Only SSTOREd ciphertexts, whether in the ACL contract or the User contract, should be persisted in the host state.

Moreover, LateCommit should not be kept in the state, if LateCommit feature is disabled (FHEVM_COMMIT_BLOCK_OFFSET=0).

@goshawk-3 goshawk-3 added the enhancement New feature or request label Jan 7, 2025
@goshawk-3 goshawk-3 self-assigned this Jan 7, 2025
@dartdart26
Copy link
Collaborator

@goshawk-3 goshawk-3 linked a pull request Jan 9, 2025 that will close this issue
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants