Skip to content

Commit

Permalink
feat: Remove slice from GadgetParameters
Browse files Browse the repository at this point in the history
  • Loading branch information
sp301415 committed Jun 9, 2024
1 parent 583601f commit 94a76d4
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 23 deletions.
2 changes: 1 addition & 1 deletion tfhe/asm_decompose.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

// decomposePolyAssign decomposes p with respect to gadgetParams and writes it to decomposedOut.
func decomposePolyAssign[T TorusInt](p poly.Poly[T], gadgetParams GadgetParameters[T], decomposedOut []poly.Poly[T]) {
lastBaseQLog := gadgetParams.basesQLog[gadgetParams.level-1]
lastBaseQLog := gadgetParams.LastBaseQLog()
for i := 0; i < p.Degree(); i++ {
c := num.RoundRatioBits(p.Coeffs[i], lastBaseQLog)
for j := gadgetParams.level - 1; j >= 1; j-- {
Expand Down
6 changes: 3 additions & 3 deletions tfhe/asm_decompose_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func decomposePolyAssign[T TorusInt](p poly.Poly[T], gadgetParams GadgetParamete
*(*[]uint32)(unsafe.Pointer(&p)),
uint32(gadgetParams.base),
uint32(gadgetParams.baseLog),
uint32(gadgetParams.basesQLog[gadgetParams.level-1]),
uint32(gadgetParams.LastBaseQLog()),
*(*[][]uint32)(unsafe.Pointer(&decomposedOut)),
)
return
Expand All @@ -32,14 +32,14 @@ func decomposePolyAssign[T TorusInt](p poly.Poly[T], gadgetParams GadgetParamete
*(*[]uint64)(unsafe.Pointer(&p)),
uint64(gadgetParams.base),
uint64(gadgetParams.baseLog),
uint64(gadgetParams.basesQLog[gadgetParams.level-1]),
uint64(gadgetParams.LastBaseQLog()),
*(*[][]uint64)(unsafe.Pointer(&decomposedOut)),
)
return
}
}

lastBaseQLog := gadgetParams.basesQLog[gadgetParams.level-1]
lastBaseQLog := gadgetParams.LastBaseQLog()
for i := 0; i < p.Degree(); i++ {
c := num.RoundRatioBits(p.Coeffs[i], lastBaseQLog)
for j := gadgetParams.level - 1; j >= 1; j-- {
Expand Down
2 changes: 1 addition & 1 deletion tfhe/decompose.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func (e *Evaluator[T]) Decompose(x T, gadgetParams GadgetParameters[T]) []T {

// DecomposeAssign decomposes x with respect to gadgetParams and writes it to decomposedOut.
func (e *Evaluator[T]) DecomposeAssign(x T, gadgetParams GadgetParameters[T], decomposedOut []T) {
lastBaseQLog := gadgetParams.basesQLog[gadgetParams.level-1]
lastBaseQLog := gadgetParams.LastBaseQLog()
u := num.RoundRatioBits(x, lastBaseQLog)
for i := gadgetParams.level - 1; i >= 1; i-- {
decomposedOut[i] = u & (gadgetParams.base - 1)
Expand Down
30 changes: 12 additions & 18 deletions tfhe/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,11 @@ func (p GadgetParametersLiteral[T]) Compile() GadgetParameters[T] {
panic("Base * Level larger than Q")
}

baseLog := num.Log2(p.Base)
basesQLog := make([]int, p.Level)
for i := range basesQLog {
basesQLog[i] = num.SizeT[T]() - (i+1)*baseLog
}

return GadgetParameters[T]{
base: p.Base,
baseLog: baseLog,
level: p.Level,
basesQLog: basesQLog,
base: p.Base,
baseLog: num.Log2(p.Base),
level: p.Level,
sizeT: num.SizeT[T](),
}
}

Expand All @@ -70,8 +64,8 @@ type GadgetParameters[T TorusInt] struct {
baseLog int
// Level is a length of gadget.
level int
// basesQLog holds the log of scaled gadget: Log(Q / B^l) for l = 1 ~ Level.
basesQLog []int
// sizeT is the size of T in bits.
sizeT int
}

// Base is a base of gadget. It must be power of two.
Expand All @@ -92,33 +86,33 @@ func (p GadgetParameters[T]) Level() int {
// BaseQ returns Q / Base^(i+1) for 0 <= i < Level.
// For the most common usages i = 0 and i = Level-1, use [GadgetParameters.FirstBaseQ] and [GadgetParameters.LastBaseQ].
func (p GadgetParameters[T]) BaseQ(i int) T {
return T(1 << p.basesQLog[i])
return T(1 << (p.sizeT - (i+1)*p.baseLog))
}

// FirstBaseQ returns Q / Base.
func (p GadgetParameters[T]) FirstBaseQ() T {
return p.BaseQ(0)
return T(1 << (p.sizeT - p.baseLog))
}

// LastBaseQ returns Q / Base^Level.
func (p GadgetParameters[T]) LastBaseQ() T {
return p.BaseQ(p.level - 1)
return T(1 << (p.sizeT - p.level*p.baseLog))
}

// BaseQLog returns log(Q / Base^(i+1)) for 0 <= i < Level.
// For the most common usages i = 0 and i = Level-1, use [GadgetParameters.FirstBaseQLog] and [GadgetParameters.LastBaseQLog].
func (p GadgetParameters[T]) BaseQLog(i int) int {
return p.basesQLog[i]
return p.sizeT - (i+1)*p.baseLog
}

// FirstBaseQLog returns log(Q / Base).
func (p GadgetParameters[T]) FirstBaseQLog() int {
return p.BaseQLog(0)
return p.sizeT - p.baseLog
}

// LastBaseQLog returns log(Q / Base^Level).
func (p GadgetParameters[T]) LastBaseQLog() int {
return p.BaseQLog(p.level - 1)
return p.sizeT - p.level*p.baseLog
}

// Literal returns a GadgetParametersLiteral from this GadgetParameters.
Expand Down

0 comments on commit 94a76d4

Please sign in to comment.