Skip to content

Commit

Permalink
adding prcompile
Browse files Browse the repository at this point in the history
Add unary tests for member funcitons
  • Loading branch information
haimbj1 committed Sep 22, 2024
1 parent 23713b1 commit dc17963
Show file tree
Hide file tree
Showing 18 changed files with 971 additions and 116 deletions.
35 changes: 33 additions & 2 deletions chains/arbitrum/contractsgen/contractsgen.go

Large diffs are not rendered by default.

36 changes: 36 additions & 0 deletions nitro-overrides/precompiles/FheOps.go
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,42 @@ func (con FheOps) Shr(c ctx, evm mech, utype byte, lhsHash []byte, rhsHash []byt
return ret, err
}

func (con FheOps) Square(c ctx, evm mech, utype byte, value []byte) ([]byte, error) {
tp := fheos.TxParamsFromEVM(evm, c.caller)
if metrics.Enabled {
h := fmt.Sprintf("%s/%s/%s", "fheos", "Square", fheos.UtypeToString(utype))
defer func(start time.Time) {
sampler := func() metrics.Sample {
return metrics.NewBoundedHistogramSample()
}
metrics.GetOrRegisterHistogramLazy(h, nil, sampler).Update(time.Since(start).Microseconds())
}(time.Now())
}

ret, gas, err := fheos.Square(utype, value, &tp)

if err != nil {
if metrics.Enabled {
c := fmt.Sprintf("%s/%s/%s/%s", "fheos", "Square", fheos.UtypeToString(utype), "error/fhe_failure")
metrics.GetOrRegisterCounter(c, nil).Inc(1)
}
return ret, err
}

err = c.Burn(gas)

if metrics.Enabled {
metricPath := fmt.Sprintf("%s/%s/%s/%s", "fheos", "Square", fheos.UtypeToString(utype), "success/total")
if err != nil {
metricPath = fmt.Sprintf("%s/%s/%s/%s", "fheos", "Square", fheos.UtypeToString(utype), "error/fhe_gas_failure")
}

metrics.GetOrRegisterCounter(metricPath, nil).Inc(1)
}

return ret, err
}

func (con FheOps) Sub(c ctx, evm mech, utype byte, lhsHash []byte, rhsHash []byte) ([]byte, error) {
tp := fheos.TxParamsFromEVM(evm, c.caller)
if metrics.Enabled {
Expand Down
6 changes: 6 additions & 0 deletions precompiles/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -1583,3 +1583,9 @@ func GetNetworkPublicKey(securityZone int32, tp *TxParams) ([]byte, error) {

return pk, nil
}

func Square(utype byte, value []byte, tp *TxParams) ([]byte, uint64, error) {
return Mul(utype, value, value, tp)
// Please don't delete the below comment, this is intentionally left here for code generation.
//ct := getCiphertext(storage, fhe.BytesToHash(value), tp.ContractAddress)
}
2 changes: 1 addition & 1 deletion precompiles/gas.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func getRawPrecompileGas(precompileName types.PrecompileName, uintType fhe.Encry
case fhe.Uint128:
return 290000
}
case types.Mul:
case types.Mul, types.Square:
switch uintType {
case fhe.Uint8:
return 40000
Expand Down
2 changes: 2 additions & 0 deletions precompiles/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
},
"devDependencies": {
"hardhat": "^2.19.3",
"rimraf": "^6.0.1",
"ts-node": "10.9.2",
"typechain": "^8.3.2",
"typescript": "4.9.5"
},
"files": [
Expand Down
Loading

0 comments on commit dc17963

Please sign in to comment.