Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 12 additions & 46 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,25 +1,15 @@
.PHONY: all build test clean examples fmt vet test-quick test-gates build-rust test-rust test-nocache test-gates-nocache test-rust-nocache test-gates-rust-nocache
.PHONY: all build test clean examples fmt vet test-quick test-gates test-nocache test-gates-nocache

all: build test

build:
@echo "Building go-tfhe (pure Go)..."
@echo "Building go-tfhe..."
go build ./...

build-rust:
@echo "Building Rust FFT bridge..."
cd fft-bridge && cargo build --release
@echo "Building go-tfhe with Rust FFT..."
go build -tags rust ./...

test:
@echo "Running tests (pure Go)..."
@echo "Running tests..."
go test -v ./...

test-rust:
@echo "Running tests with Rust FFT..."
go test -tags rust -v ./...

test-quick:
@echo "Running quick tests (non-gate tests)..."
go test -v ./params ./utils ./bitutils ./tlwe ./trlwe ./key ./cloudkey ./fft
Expand All @@ -29,26 +19,14 @@ test-gates:
@echo "Each gate test takes ~400ms, batch tests take longer..."
go test -v -timeout 30m ./gates

test-gates-rust:
@echo "Running gate tests with Rust FFT (should be 4-5x faster)..."
go test -tags rust -v -timeout 10m ./gates

test-nocache:
@echo "Running tests without cache (pure Go)..."
@echo "Running tests without cache..."
go test -count=1 -v ./...

test-rust-nocache:
@echo "Running tests without cache (Rust FFT)..."
go test -count=1 -tags rust -v ./...

test-gates-nocache:
@echo "Running gate tests without cache (pure Go)..."
@echo "Running gate tests without cache..."
go test -count=1 -v -timeout 30m ./gates

test-gates-rust-nocache:
@echo "Running gate tests without cache (Rust FFT)..."
go test -count=1 -tags rust -v -timeout 10m ./gates

examples:
@echo "Building examples..."
cd examples/add_two_numbers && go build
Expand All @@ -75,8 +53,6 @@ clean:
go clean ./...
rm -f examples/add_two_numbers/add_two_numbers
rm -f examples/simple_gates/simple_gates
@echo "Cleaning Rust FFT bridge..."
cd fft-bridge && cargo clean

install-deps:
@echo "Installing dependencies..."
Expand All @@ -87,32 +63,22 @@ benchmark:
@echo "Running FFT benchmarks..."
go test -bench=. -benchmem ./fft

benchmark-rust:
@echo "Running FFT benchmarks with Rust backend..."
go test -tags rust -bench=. -benchmem ./fft

help:
@echo "Available targets:"
@echo ""
@echo "Building:"
@echo " all - Build and test (pure Go)"
@echo " build - Build all packages (pure Go)"
@echo " build-rust - Build with Rust FFT backend"
@echo " all - Build and test"
@echo " build - Build all packages"
@echo ""
@echo "Testing:"
@echo " test - Run all tests (pure Go)"
@echo " test-rust - Run all tests with Rust FFT"
@echo " test-nocache - Run all tests without cache (pure Go)"
@echo " test-rust-nocache - Run all tests without cache (Rust FFT)"
@echo " test - Run all tests"
@echo " test-nocache - Run all tests without cache"
@echo " test-quick - Run quick tests (no gate tests)"
@echo " test-gates - Run gate tests only (pure Go, slow)"
@echo " test-gates-rust - Run gate tests with Rust FFT (4-5x faster)"
@echo " test-gates-nocache - Run gate tests without cache (pure Go)"
@echo " test-gates-rust-nocache - Run gate tests without cache (Rust FFT)"
@echo " test-gates - Run gate tests only"
@echo " test-gates-nocache - Run gate tests without cache"
@echo ""
@echo "Benchmarking:"
@echo " benchmark - Benchmark FFT (pure Go)"
@echo " benchmark-rust - Benchmark FFT (Rust backend)"
@echo " benchmark - Benchmark FFT"
@echo ""
@echo "Examples:"
@echo " examples - Build all examples"
Expand Down
40 changes: 17 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ Performance characteristics on a typical modern CPU:
| Batch (8 gates) | ~200-300ms | ~120-180ms |
| Addition (8-bit) | ~8-12s | ~5-7s |

*Note: Times are for pure Go implementation. The Rust version with hand-optimized assembly is ~3-5x faster.*
*Note: Performance can vary based on CPU architecture and number of cores.*

## Examples

Expand All @@ -241,15 +241,13 @@ cd examples/simple_gates
go run main.go
```

## Comparison with Rust Implementation
## Key Advantages

| Feature | Go Implementation | Rust Implementation |
|---------|-------------------|---------------------|
| Pure Language | ✅ Yes | ❌ No (uses C++/ASM) |
| Easy Build | ✅ Yes | ⚠️ Requires build tools |
| Performance | ~100-150ms/gate | ~30-50ms/gate |
| Parallelization | ✅ Goroutines | ✅ Rayon |
| Security Levels | ✅ 80/110/128-bit | ✅ 80/110/128-bit |
- **Pure Go**: No C dependencies, no build tools required
- **Easy Deployment**: Single binary, cross-platform compilation
- **Simple Integration**: Standard Go modules, no CGO
- **Parallelization**: Built-in concurrency with goroutines
- **Multiple Security Levels**: 80/110/128-bit security parameters

## Building from Source

Expand All @@ -267,32 +265,28 @@ go test ./...

## Limitations

- **Performance**: Pure Go is slower than hand-optimized assembly in Rust version
- **FFT Implementation**: Uses standard Go FFT library (no SIMD optimizations)
- **Memory**: Higher memory usage compared to Rust due to GC overhead
- **FFT Performance**: Uses standard Go FFT library (future: custom SIMD-optimized FFT)
- **Memory Usage**: Go's garbage collector trades memory for convenience

## Future Improvements

- [ ] Add SIMD optimizations using Go assembly
- [ ] Implement custom FFT with better cache locality
- [ ] Add GPU acceleration support
- [ ] Optimize memory allocations
- [ ] Add more example circuits (multiplication, comparison, etc.)
- [ ] Add SIMD-optimized FFT using Go assembly
- [ ] Implement custom FFT with better cache locality
- [ ] Add GPU acceleration support (Metal/CUDA)
- [ ] Optimize memory allocations and reduce GC pressure
- [ ] Add more example circuits (multiplication, comparison, sorting, etc.)
- [ ] Support for wider data types (16-bit, 32-bit operations)

## Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

## License

Same license as the original Rust implementation.
MIT License

## References

- Original TFHE paper: [TFHE: Fast Fully Homomorphic Encryption over the Torus](https://eprint.iacr.org/2018/421)
- Rust implementation: [rs-tfhe](https://github.com/thedonutfactory/rs-tfhe)

## Acknowledgments

This is a port of the Rust TFHE implementation. All credit for the original design and algorithms goes to the original authors.
- Extended FFT paper: [Fast and Error-Free Negacyclic Integer Convolution](https://eprint.iacr.org/2021/480)

11 changes: 7 additions & 4 deletions cloudkey/cloudkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/thedonutfactory/go-tfhe/fft"
"github.com/thedonutfactory/go-tfhe/key"
"github.com/thedonutfactory/go-tfhe/params"
"github.com/thedonutfactory/go-tfhe/poly"
"github.com/thedonutfactory/go-tfhe/tlwe"
"github.com/thedonutfactory/go-tfhe/trgsw"
"github.com/thedonutfactory/go-tfhe/trlwe"
Expand Down Expand Up @@ -42,9 +43,10 @@ func NewCloudKeyNoKSK() *CloudKey {
ksk[i] = tlwe.NewTLWELv0()
}

polyEval := poly.NewEvaluator(n)
bsk := make([]*trgsw.TRGSWLv1FFT, lv0N)
for i := range bsk {
bsk[i] = trgsw.NewTRGSWLv1FFTDummy()
bsk[i] = trgsw.NewTRGSWLv1FFTDummy(polyEval)
}

return &CloudKey{
Expand Down Expand Up @@ -74,12 +76,12 @@ func genTestvec() *trlwe.TRLWELv1 {
n := params.GetTRGSWLv1().N
testvec := trlwe.NewTRLWELv1()
bTorus := utils.F64ToTorus(0.125)

for i := 0; i < n; i++ {
testvec.A[i] = 0
testvec.B[i] = bTorus
}

return testvec
}

Expand Down Expand Up @@ -123,13 +125,14 @@ func genBootstrappingKey(secretKey *key.SecretKey) []*trgsw.TRGSWLv1FFT {
go func(idx int) {
defer wg.Done()
plan := fft.NewFFTPlan(params.GetTRGSWLv1().N)
polyEval := poly.NewEvaluator(params.GetTRGSWLv1().N)
trgswCipher := trgsw.NewTRGSWLv1().EncryptTorus(
secretKey.KeyLv0[idx],
params.BSKAlpha(),
secretKey.KeyLv1,
plan,
)
result[idx] = trgsw.NewTRGSWLv1FFT(trgswCipher, plan)
result[idx] = trgsw.NewTRGSWLv1FFT(trgswCipher, polyEval)
}(i)
}
wg.Wait()
Expand Down
12 changes: 0 additions & 12 deletions fft-bridge/Cargo.toml

This file was deleted.

143 changes: 0 additions & 143 deletions fft-bridge/README.md

This file was deleted.

Loading