Skip to content

Commit

Permalink
feat(coproc): handle fheDiv operations (#249)
Browse files Browse the repository at this point in the history
* feat(coproc): handle fheDiv operations

* feat(coproc): add an empty handler for verifyCiphertext
  • Loading branch information
goshawk-3 authored Jan 16, 2025
1 parent 5a70c19 commit 6d6f6f5
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
2 changes: 2 additions & 0 deletions fhevm-engine/fhevm-go-coproc/fhevm/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,8 @@ func flushWorkItemsToCoprocessor(store *SqliteComputationStore) (int, error) {
}
defer conn.Close()

fmt.Printf("Sending %d computations to coproc\n", len(asyncCompReq.Computations))

client := NewFhevmCoprocessorClient(conn)
md := metadata.Pairs("Authorization", fmt.Sprintf("Bearer %s", store.coprocessorApiKey))
grpcContext := metadata.NewOutgoingContext(context.Background(), md)
Expand Down
11 changes: 11 additions & 0 deletions fhevm-engine/fhevm-go-coproc/fhevm/fhelib.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ func FheLibMethods() []*FheLibMethod {
runFunction: fheMulRun,
ScalarSupport: true,
},
{
Name: "fheDiv",
ArgTypes: "(uint256,uint256,bytes1)",
runFunction: fheDivRun,
ScalarSupport: true,
},
{
Name: "fheRem",
ArgTypes: "(uint256,uint256,bytes1)",
Expand Down Expand Up @@ -188,6 +194,11 @@ func FheLibMethods() []*FheLibMethod {
ArgTypes: "(bytes,bytes1)",
runFunction: trivialEncryptBytesRun,
},
{
Name: "verifyCiphertext",
ArgTypes: "(bytes32,address,bytes,bytes1)",
runFunction: fheVerifyCiphertext,
},
}
}

Expand Down
46 changes: 46 additions & 0 deletions fhevm-engine/fhevm-go-coproc/fhevm/fhelib_ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,47 @@ func fheSubRun(sess CoprocessorSession, unslicedInput []byte, _ ExtraData, outpu
}
}

func fheDivRun(sess CoprocessorSession, unslicedInput []byte, _ ExtraData, outputHandle []byte) error {
if len(unslicedInput) < 65 {
return fmt.Errorf("expected at least 65 bytes as input, got %d", len(unslicedInput))
}
input := unslicedInput[:65]

isScalar, err := isScalarOp(input)
if err != nil {
return err
}

var lhs, rhs []byte
if isScalar {
lhs, rhs, err = getScalarOperands(sess, input)
} else {
lhs, rhs, err = get2FheOperands(sess, input)
}
if err != nil {
return err
}

computation := ComputationToInsert{
Operation: FheDiv,
OutputHandle: outputHandle,
Operands: []ComputationOperand{
{
Handle: lhs,
FheUintType: handleType(lhs),
IsScalar: false,
},
{
Handle: rhs,
FheUintType: handleType(rhs),
IsScalar: isScalar,
},
},
}

return sess.GetStore().InsertComputation(computation)
}

func fheMulRun(sess CoprocessorSession, unslicedInput []byte, _ ExtraData, outputHandle []byte) error {
if len(unslicedInput) < 65 {
return fmt.Errorf("expected at least 65 bytes as input, got %d", len(unslicedInput))
Expand Down Expand Up @@ -1652,6 +1693,11 @@ func trivialEncryptBytesRun(sess CoprocessorSession, unslicedInput []byte, ed Ex
return nil
}

func fheVerifyCiphertext(_ CoprocessorSession, _ []byte, _ ExtraData, _ []byte) error {
// Added to suppress an unregistered operation error
return nil
}

func isScalarOp(input []byte) (bool, error) {
if len(input) != 65 {
return false, errors.New("input needs to contain two 256-bit sized values and 1 8-bit value")
Expand Down

0 comments on commit 6d6f6f5

Please sign in to comment.