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

Sub-package for CKKS bootstrapping with improved parameters and new functionalities #139

Merged
merged 168 commits into from
Sep 19, 2021
Merged
Show file tree
Hide file tree
Changes from 167 commits
Commits
Show all changes
168 commits
Select commit Hold shift + click to select a range
d85d9ce
working generic CKS base and dbfv+dckks version
ChristianMct Apr 22, 2021
1801947
working dbfv.PublicKeySwitching
ChristianMct Apr 23, 2021
730c1d7
working dbfv.E2SProtocols
ChristianMct Apr 30, 2021
51df793
working dbfv.S2EProtocol
ChristianMct May 3, 2021
e753390
working dbfv.RefreshProtocol
ChristianMct May 3, 2021
ad43dd5
working dbfv.MaskedTransformProtocol and derived dbfv.RefreshProtocol…
ChristianMct May 4, 2021
ea0f713
renamed public_permute.go -> masked_transform.go
ChristianMct May 4, 2021
ce34d3c
fixed dbfv refresh benchmark
ChristianMct May 5, 2021
9838974
improved performance of pubkeyswitch
ChristianMct May 5, 2021
2c7f6c0
changed the default secret-sharing space to PlaintextRingT
ChristianMct May 7, 2021
b029fed
added rlwe.Plaintext type
ChristianMct May 10, 2021
88fb8d7
added MaskedTransformFunc type
ChristianMct May 10, 2021
f75fa1d
refined some API methods of s2e,e2s and refresh + some missing godoc
ChristianMct Jun 7, 2021
56434a0
[dckks] : added E2S and S2E
Pro7ech Jun 10, 2021
2696c17
fixed PublicKeySwitchingProtocol interface
ChristianMct Jun 11, 2021
43f98bf
added tests for key-switcing protocols at different levels
ChristianMct Jun 11, 2021
0905cf5
fixed bad godoc format
ChristianMct Jun 11, 2021
06b0284
drlwe/keyswitch.go : updated how mid level ciphertext are managed, re…
Pro7ech Jun 11, 2021
0477131
drlwe/pubkey_switch.go : fixed
Pro7ech Jun 11, 2021
ef66989
drlwe : reduced pcks and relinkeygen memory footprint
Pro7ech Jun 11, 2021
98f2b75
-
Pro7ech Jun 11, 2021
858cae5
Merge branch 'master' into dev_drlwe_ks
Pro7ech Jun 11, 2021
7691018
DCKKS : fixed s2e and e2s to work with variable levels
Pro7ech Jun 11, 2021
c2b1874
go mod tidy
Pro7ech Jun 11, 2021
e835c2c
Merge remote-tracking branch 'origin/master' into dev_drlwe_ks
Pro7ech Jun 11, 2021
7399d3b
[ckks] : replaced Element by rlwe.Element
Pro7ech Jun 14, 2021
4ccf555
Fixed dckks and examples
Pro7ech Jun 14, 2021
9cf8793
[dckks] : added masked transform (missing tests)
Pro7ech Jun 14, 2021
384e6ba
[dckks] fixed lvl management in masked transform
Pro7ech Jun 14, 2021
026f24f
[dckks] : added masked transform test
Pro7ech Jun 15, 2021
c7d337a
[dckks] : removed dckks context, removed old protocol, added new refresh
Pro7ech Jun 16, 2021
e85ea5f
[dckks] : fixed and revamped S2E, E2S, Refresh and MaskedTransform
Pro7ech Jun 16, 2021
c7f2848
[dckks] : added public API to get the refresh security parameters
Pro7ech Jun 17, 2021
68e338a
[dckks] : added some godoc for E2S, Refresh and Masked transform
Pro7ech Jun 17, 2021
5b3b720
[dckks] : removed unused fields in tests
Pro7ech Jun 17, 2021
fa2a87c
Uniformized dbfv and dckks share generation API. Added DCKKS marshali…
Pro7ech Jun 21, 2021
abf9a60
fixed the shares-allocation interface and dbfv examples
ChristianMct Jul 9, 2021
01fbd94
fixed typo in variable name
ChristianMct Jul 9, 2021
0418951
arguments in dbfv and dckks are now passed by pointer for consistency
ChristianMct Jul 9, 2021
39ed79e
the test for dbfv s2e can now be called standalone
ChristianMct Jul 9, 2021
eb04c10
fixed bug in dckks marshalling test for refresh protocol
ChristianMct Jul 9, 2021
f88bc79
updated changelog
ChristianMct Jul 9, 2021
3b08cce
Centralized rlwe keygen
Pro7ech Jun 17, 2021
0d94b3f
Ring is now instantiated once in parameters and readonly
Pro7ech Jun 18, 2021
8900e06
Moved entire CKKS KeySwitch to rlwe.KeySwitch
Pro7ech Jun 18, 2021
ff00232
[ring] : added IsNTT and IsMForm flags, reduced code footpring, remov…
Pro7ech Jun 18, 2021
ae0e799
[ckks] : adapted to the rlwe.Keyswitch
Pro7ech Jun 18, 2021
ae41dd0
[bfv] : adapted to new centralized key-switch
Pro7ech Jun 18, 2021
df0ab39
-
Pro7ech Jun 21, 2021
d51f67c
[rlwe] centralized encryption
Pro7ech Jun 21, 2021
f19fbd2
centralized decryptor for BFV and CKKS
Pro7ech Jun 22, 2021
c1de4a3
[rlwe] : added test for encryptor and decryptor
Pro7ech Jun 22, 2021
ff80cea
fixed tests of other packages
Pro7ech Jun 22, 2021
6caeb70
happy go vet
Pro7ech Jun 22, 2021
4dfc433
golint
Pro7ech Jun 22, 2021
1c61ad6
[rlwe] : added tests for decryptor
Pro7ech Jun 23, 2021
02cef55
[rlwe] : added test for key-switch
Pro7ech Jun 23, 2021
d3eafb8
[rlwe] : added test params, benchmark
Pro7ech Jun 23, 2021
94271bc
reverted change to e before adding a full proof
Pro7ech Jun 28, 2021
97fc58d
[dckks] : added masked transform benchmarks
Pro7ech Jun 28, 2021
a662f79
Improved StC tests
Pro7ech Jul 5, 2021
3a1599f
Improved CtS tests
Pro7ech Jul 5, 2021
d4e43e0
Small typo and godoc
Pro7ech Jul 8, 2021
f22b68e
fixed bug in the rebased version
ChristianMct Jul 9, 2021
c8488eb
Wrapped rlwe.Encryptor/Decryptor are kept as interfaces
Pro7ech Jul 10, 2021
be33eb2
Switch between sk and pk for encryptor interface moved to rlwe package
Pro7ech Jul 10, 2021
b257d24
[rlwe] split encryptor methods into fast and normal
Pro7ech Jul 10, 2021
0755f81
reduced the rlwe.Encryptor public API
ChristianMct Jul 12, 2021
30da3df
removed the Ciphertext interface and rename rlwe.Element->rlwe.Cipher…
ChristianMct Jul 12, 2021
8e47a0c
improved API and added documentation
ChristianMct Jul 12, 2021
3a7980d
Centralized marshaling test for keys
Pro7ech Jul 12, 2021
3db7822
simplified KeyGenerators
ChristianMct Jul 12, 2021
4c5a4a0
fixed examples
ChristianMct Jul 12, 2021
8b1ac7d
updated godoc
ChristianMct Jul 12, 2021
631413b
simplified the Decryptor API
ChristianMct Jul 12, 2021
8c8b897
Added tests for drlwe, moved keys marshalling tests to drlwe
Pro7ech Jul 13, 2021
6777cf3
Typos
Pro7ech Jul 13, 2021
bc6519a
fixed drlwe interfaces
Pro7ech Jul 13, 2021
94d8092
fixed typo
Pro7ech Jul 13, 2021
a197e1e
updated the CHANGELOG.md [skip ci]
ChristianMct Jul 13, 2021
c2965d2
updated the CHANGELOG.md [skip ci]
Pro7ech Jul 13, 2021
fbcc46b
updated the CHANGELOG.md [skip ci]
Pro7ech Jul 13, 2021
7c6c752
updated the CHANGELOG.md [skip ci]
Pro7ech Jul 13, 2021
da92716
updated CHANGELOG.md [ci skip]
ChristianMct Jul 13, 2021
dd3df2e
reorganised the schemes' package structure
ChristianMct Jul 13, 2021
d66d053
Merged changes of the btp params branch
Pro7ech Jul 15, 2021
a473e46
Merge tag 'v2.2.0' into dev_btp_pacakge
Pro7ech Jul 15, 2021
cf18603
[ckks] : CtS and StC part of Evaluator inferface
Pro7ech Jul 17, 2021
e6b7623
[ckks] : Common API for both CtS and StC
Pro7ech Jul 17, 2021
6bc5be6
Generalized bootstrapping linear transforms
Pro7ech Jul 19, 2021
6349d6f
[ckks] separated homomorphic modular reduction from the bootstrapping
Pro7ech Jul 20, 2021
02315fe
Reduced code footpring of bootstrapping
Pro7ech Jul 20, 2021
3a81e58
Standalone tests for CoeffsToSlots and SlotsToCoeffs
Pro7ech Jul 20, 2021
ddc0306
[ckks] : included second correcting factor of EvalMod in the coeffici…
Pro7ech Jul 21, 2021
6701fe4
Added bootstrapping sub-package
Pro7ech Jul 22, 2021
e5d07bf
[ckks] : improved struct names in sub-package bootstrapping
Pro7ech Jul 22, 2021
7a7e61b
go vet
Pro7ech Jul 22, 2021
a09eca5
[ckks] : added marshalling for Encoding matrices and EvalMod parameters
Pro7ech Jul 22, 2021
595286c
[ckks] : added marshaller for bootstrapping parameters
Pro7ech Jul 22, 2021
4bc00da
[ckks] : added marshalling for advanced functionalities
Pro7ech Jul 23, 2021
f155066
Added LWE repacking example
Pro7ech Jul 23, 2021
e9e4358
[ckks] : corrected doc for EvalMod
Pro7ech Jul 23, 2021
591bc7a
[rlwe] : genswitchingkey can now generate dimension switching switchi…
Pro7ech Jul 23, 2021
2ba56a0
[rlwe] partial change to split Q and P in keys
Pro7ech Jul 23, 2021
ac7a4f6
[rlwe] fixed pk encryptor
Pro7ech Jul 24, 2021
34a461d
Fixed drlwe to work with new keys
Pro7ech Jul 26, 2021
90f18a1
[dbfv] : adapted for new keys
Pro7ech Jul 26, 2021
156a9d8
[dckks] : adapted for new keys
Pro7ech Jul 26, 2021
fc44faa
Fixed examples
Pro7ech Jul 27, 2021
4c79da7
[ring] : any level to any level basis extension
Pro7ech Jul 30, 2021
779572d
cleaned rlwe package
Pro7ech Jul 30, 2021
365f2ef
Updated BFV, CKKS, DBFV, DCKKS, examples
Pro7ech Jul 30, 2021
e3817d9
[ring] : improved moddown params gen
Pro7ech Jul 30, 2021
329f6d1
gofmt
Pro7ech Aug 3, 2021
7ffa316
RLWE : enable key-switch between overlapping parameters
Pro7ech Aug 9, 2021
82b215e
updated example doc [skip ci]
Pro7ech Aug 9, 2021
54b2831
[ckks] : added advanced arithmetic sub-package
Pro7ech Aug 10, 2021
b4ec518
go mod tidy
Pro7ech Aug 10, 2021
c712054
updated makefile
Pro7ech Aug 10, 2021
f8f2933
[rlwe] : prevented a seg fault if the rot-key map doesn't exist
Pro7ech Aug 11, 2021
e0e7491
[rlwe] : simplified and generalized GenSwitchingKey API
Pro7ech Aug 11, 2021
0579d68
[ckks] : rescale use less NTT if only one level is removed
Pro7ech Aug 11, 2021
65b6220
[rlwe] : added struct PolyQP to better represent the format of the keys
Pro7ech Aug 11, 2021
34de9eb
[rlwe] : better godoc for rlwe.PolyQP
Pro7ech Aug 11, 2021
48b8779
[drlwe][dbfv][dckks] : now uses rlwe.PolyQP
Pro7ech Aug 12, 2021
e754bda
[bfv] : encoding now does round((Q*m)/T) instead of floor(Q/T)*m
Pro7ech Aug 12, 2021
8472ba2
[bfv] : removed allocations during plaintext scaling
Pro7ech Aug 12, 2021
36adcf4
[drlwe] : new interface for samplers
Pro7ech Aug 12, 2021
173e88d
gofmt
Pro7ech Aug 12, 2021
f81af4c
[ring] : fixed seg fault in uniform sampler for lower level poly
Pro7ech Aug 12, 2021
b49ec27
[bfv] added min and max error in err stats
Pro7ech Aug 13, 2021
3e3a276
[bfv] improved error print
Pro7ech Aug 18, 2021
ae2452b
Keygen correctness for some cases & bootstrapp does not modify anymor…
Pro7ech Aug 20, 2021
710c5db
extracted the ring.RingQP and ring.PolyQP type
ChristianMct Aug 19, 2021
d88073b
[rlwe] : continued switch to PolyQP
Pro7ech Aug 24, 2021
d251963
[ckks] : encoder now panic of imputs are not of the correct size
Pro7ech Aug 24, 2021
2f5fa97
fmt
Pro7ech Aug 25, 2021
189f29f
[drlwe] : fixed pcks
Pro7ech Aug 25, 2021
de5ba6b
[drlwe] : cks, consistence
Pro7ech Aug 25, 2021
1921f9c
[drlwe] : fixed rot-keygen galois generator
Pro7ech Aug 25, 2021
0019997
fmt
Pro7ech Aug 25, 2021
133177e
[ckks] : full revamp of polynomial evaluation
Pro7ech Aug 26, 2021
fb3da11
gofmt
Pro7ech Aug 26, 2021
dcf273f
[ckks] : prevented overflow in constant scaling during polynomial eva…
Pro7ech Aug 30, 2021
80dd098
Fix for PR review
Pro7ech Sep 1, 2021
106f602
Merge branch 'dev_btp_package_fixes_review' into dev_btp_pacakge
Pro7ech Sep 1, 2021
287346a
old go fmt for CI
Pro7ech Sep 1, 2021
ecae3df
cleaned some old comments
Pro7ech Sep 1, 2021
677e21f
added the CRS and CRP interface and implementation
ChristianMct Sep 1, 2021
13cd1ff
added cloning methods for ckks/advanced.Evaluator and removed some TODOs
ChristianMct Sep 2, 2021
1205af1
renamed the ParamsToString test-related function into GetTestName
ChristianMct Sep 2, 2021
4693ce9
removed allocation in bfv.encoder and fixed some more typos
ChristianMct Sep 2, 2021
638f931
[ring] : improved versatility of rns-rescaling and fixed possible seg…
Pro7ech Sep 2, 2021
63a784c
Merge remote-tracking branch 'origin/dev_btp_pacakge' into dev_btp_pa…
Pro7ech Sep 2, 2021
d4ba9f3
added .ShallowCopy for ckks advanced Evaluator and ckks Bootstrapper
Pro7ech Sep 3, 2021
4981be7
[ckks] : added API to support external use of CKKS
Pro7ech Sep 3, 2021
5a2f710
typo
Pro7ech Sep 3, 2021
2bcc6f1
Added back the per-protocol types for CRP
ChristianMct Sep 6, 2021
ac996b8
added changes in the CHANGELOG.md
ChristianMct Sep 6, 2021
0354d71
[ckks] : made some methods related to rotations publics to facilitate…
Pro7ech Sep 6, 2021
7d886d6
updated CHANGELOG.md
Pro7ech Sep 6, 2021
de141d1
[ckks] : improved Lineartransforms API with in-place and .New() metho…
Pro7ech Sep 7, 2021
e1de976
[ring] : rns scaling does not modify the input anymore
Pro7ech Sep 7, 2021
a1e7709
updated CHANGELOG.md
Pro7ech Sep 8, 2021
77ac89a
[ckks] : added inplace methods for linear transforms.
Pro7ech Sep 8, 2021
5591300
[ckks] : added NewPlaintextAtLevelFromPoly
Pro7ech Sep 9, 2021
8693794
[ckks] updated godoc
Pro7ech Sep 13, 2021
bd987b0
Minor revision
jrtroncoso Sep 17, 2021
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
26 changes: 26 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,32 @@
# Changelog
All notable changes to this project will be documented in this file.

## [Unreleased]
- RING: added `MapSmallDimensionToLargerDimensionNTT` method which map from Y = X^{N/n} to X in the NTT domain.
- RING: `FastBasisExtender` type can now extend the basis of polynomials of any level in base Q to polynomials of any level in base P.
- RING: changed RNS division `Div[floor/round]BylastModulus[NTT]` to `Div[floor/round]BylastModulus[NTT]Lvl` (the level of the last modulus must always be provided).
- RING: RNS division no longer modifies the output polynomial's level, this is to facilitate the usage of memory pools.
- RING: added the method `MFormVector`, which switches a slice of `uint64` into the Montgomery domain.
- RING: RNS scaler (used in BFV) does not modify the input anymore.
- RLWE: `GenSwitchingKey` now accepts secret-keys of different dimensions and level as input to enable re-encryption between different ciphertext degrees.
- RLWE: added `SwitchCiphertextRingDegreeNTT` and `SwitchCiphertextRingDegree` to switch ciphertext ring degrees.
- RLWE: added the `rlwe.RingQP` type to represent the extended ring R_qp.
- RLWE: added the `rlwe.PolyQP` type to represent polynomials in the extended ring R_qp.
- DRLWE: added the `CKGCRP`, `RKGCRP`, `RTGCRP` and `CKSCRP` types to represent the common reference polynomials in these protocols.
- DRLWE: added the `CRS` interface for PRNGs that implement a common reference string among the parties.
- DRLWE: added the `SampleCRP(crs CRS)` method to each protocol types to sample their respective CRP type.
- BFV: changed the plaintext scaling from `floor(Q/T)*m` to `round((Q*m)/T)` to reduces the initial ciphertext noise.
- CKKS: added the `ckks/advanced` sub-package and extracted the homomorphic encoding, decoding and modular reduction into it.
- CKKS: added the `ckks/bootstrapping` sub-package and extracted the CKKS bootstrapping into it. This package now mostly rely on the `ckks/advanced` package.
- CKKS: renamed the `ChebyshevInterpolation` type to `Polynomial`.
- CKKS: removed the `EvaluateCheby` method duplicating the `EvaluatePoly` one.
- CKKS: optimized the `EvaluatePoly` to account for odd/even polynomials and fixed some small imprecisions in scale management occurring for some specific polynomial degrees.
- CKKS: some advanced methods related to automorphism are now public to facilitate their external use.
- CKKS: improved the consistency of the API for inplace and `[..]New` methods.
- CKKS: added the method `NewCiphertextAtLevelFromPoly` which creates a ciphertext at a specific level from two polynomials.
- DBFV/DCKKS: are now using their respective CRP type for each protocols.
- EXAMPLE: added showcase of the `ckks/advanced` sub-package: a bridge between between CKKS and FHEW ciphertexts using homomorphic decoding, ring dimension switching, homomorphic matrix multiplication and homomorphic modular reduction.

## [2.2.0] - 2020-07-15

- Added SECURITY.md
Expand Down
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ test_examples:
@echo ok
@echo Building resources-heavy examples
go build -o /dev/null ./examples/ckks/bootstrapping
go build -o /dev/null ./examples/ckks/advanced
@echo ok

.PHONY: test_gotest
test_gotest:
go test -v -timeout=0 ./utils ./ring ./bfv ./dbfv ./dckks
go test -v -timeout=0 ./ckks -test-bootstrapping
go test -v -timeout=0 ./ckks/advanced
go test -v -timeout=0 ./ckks/bootstrapping -test-bootstrapping -short

.PHONY: test
test: test_fmt test_gotest test_examples
Expand Down
2 changes: 0 additions & 2 deletions bfv/bfv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ func testString(opname string, p Parameters) string {
type testContext struct {
params Parameters
ringQ *ring.Ring
ringQP *ring.Ring
ringT *ring.Ring
prng utils.PRNG
uSampler *ring.UniformSampler
Expand Down Expand Up @@ -90,7 +89,6 @@ func genTestParams(params Parameters) (testctx *testContext, err error) {
}

testctx.ringQ = params.RingQ()
testctx.ringQP = params.RingQP()
testctx.ringT = params.RingT()

testctx.uSampler = ring.NewUniformSampler(testctx.prng, testctx.ringT)
Expand Down
126 changes: 68 additions & 58 deletions bfv/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,10 @@ type Encoder interface {
type encoder struct {
params Parameters

ringQ *ring.Ring
ringT *ring.Ring

indexMatrix []uint64
scaler ring.Scaler
deltaMont []uint64

rescaleParams []uint64

tmpPoly *ring.Poly
tmpPtRt *PlaintextRingT
Expand Down Expand Up @@ -101,33 +99,19 @@ func NewEncoder(params Parameters) Encoder {
pos &= (m - 1)
}

return &encoder{
params: params,
ringQ: ringQ,
ringT: ringT,
indexMatrix: indexMatrix,
deltaMont: GenLiftParams(ringQ, params.T()),
scaler: ring.NewRNSScaler(params.T(), ringQ),
tmpPoly: ringT.NewPoly(),
tmpPtRt: NewPlaintextRingT(params),
rescaleParams := make([]uint64, len(ringQ.Modulus))
for i, qi := range ringQ.Modulus {
rescaleParams[i] = ring.MForm(ring.ModExp(params.T(), qi-2, qi), qi, ringQ.BredParams[i])
}
}

// GenLiftParams generates the lifting parameters.
func GenLiftParams(ringQ *ring.Ring, t uint64) (deltaMont []uint64) {

delta := new(big.Int).Quo(ringQ.ModulusBigint, ring.NewUint(t))

deltaMont = make([]uint64, len(ringQ.Modulus))

tmp := new(big.Int)
bredParams := ringQ.BredParams
for i, Qi := range ringQ.Modulus {
deltaMont[i] = tmp.Mod(delta, ring.NewUint(Qi)).Uint64()
deltaMont[i] = ring.MForm(deltaMont[i], Qi, bredParams[i])
return &encoder{
params: params,
indexMatrix: indexMatrix,
scaler: ring.NewRNSScaler(ringQ, ringT),
rescaleParams: rescaleParams,
tmpPoly: ringT.NewPoly(),
tmpPtRt: NewPlaintextRingT(params),
}

return
}

// EncodeUintRingT encodes a slice of uint64 into a Plaintext in R_t
Expand All @@ -148,7 +132,7 @@ func (encoder *encoder) EncodeUintRingT(coeffs []uint64, p *PlaintextRingT) {
p.Value.Coeffs[0][encoder.indexMatrix[i]] = 0
}

encoder.ringT.InvNTT(p.Value, p.Value)
encoder.params.RingT().InvNTT(p.Value, p.Value)
}

// EncodeUint encodes an uint64 slice of size at most N on a plaintext.
Expand Down Expand Up @@ -198,7 +182,7 @@ func (encoder *encoder) EncodeIntRingT(coeffs []int64, p *PlaintextRingT) {
p.Value.Coeffs[0][encoder.indexMatrix[i]] = 0
}

encoder.ringT.InvNTTLazy(p.Value, p.Value)
encoder.params.RingT().InvNTTLazy(p.Value, p.Value)
}

func (encoder *encoder) EncodeInt(coeffs []int64, p *Plaintext) {
Expand All @@ -223,31 +207,57 @@ func (encoder *encoder) EncodeIntMul(coeffs []int64, p *PlaintextMul) {

// ScaleUp transforms a PlaintextRingT (R_t) into a Plaintext (R_q) by scaling up the coefficient by Q/t.
func (encoder *encoder) ScaleUp(ptRt *PlaintextRingT, pt *Plaintext) {
scaleUp(encoder.ringQ, encoder.deltaMont, ptRt.Value, pt.Value)
encoder.scaleUp(encoder.params.RingQ(), encoder.params.RingT(), encoder.tmpPoly.Coeffs[0], ptRt.Value, pt.Value)
}

func scaleUp(ringQ *ring.Ring, deltaMont []uint64, pIn, pOut *ring.Poly) {
// takes m mod T and returns round((m*Q)/T) mod Q
func (encoder *encoder) scaleUp(ringQ, ringT *ring.Ring, tmp []uint64, pIn, pOut *ring.Poly) {

qModTmontgomery := ring.MForm(new(big.Int).Mod(ringQ.ModulusBigint, ringT.ModulusBigint).Uint64(), ringT.Modulus[0], ringT.BredParams[0])

t := ringT.Modulus[0]
tHalf := t >> 1
tInv := ringT.MredParams[0]

for i := len(ringQ.Modulus) - 1; i >= 0; i-- {
out := pOut.Coeffs[i]
in := pIn.Coeffs[0]
d := deltaMont[i]
// (x * Q + T/2) mod T
for i := 0; i < ringQ.N; i = i + 8 {
x := (*[8]uint64)(unsafe.Pointer(&pIn.Coeffs[0][i]))
z := (*[8]uint64)(unsafe.Pointer(&tmp[i]))

z[0] = ring.CRed(ring.MRed(x[0], qModTmontgomery, t, tInv)+tHalf, t)
z[1] = ring.CRed(ring.MRed(x[1], qModTmontgomery, t, tInv)+tHalf, t)
z[2] = ring.CRed(ring.MRed(x[2], qModTmontgomery, t, tInv)+tHalf, t)
z[3] = ring.CRed(ring.MRed(x[3], qModTmontgomery, t, tInv)+tHalf, t)
z[4] = ring.CRed(ring.MRed(x[4], qModTmontgomery, t, tInv)+tHalf, t)
z[5] = ring.CRed(ring.MRed(x[5], qModTmontgomery, t, tInv)+tHalf, t)
z[6] = ring.CRed(ring.MRed(x[6], qModTmontgomery, t, tInv)+tHalf, t)
z[7] = ring.CRed(ring.MRed(x[7], qModTmontgomery, t, tInv)+tHalf, t)
}

// (x * T^-1 - T/2) mod Qi
for i := 0; i < len(pOut.Coeffs); i++ {
p0tmp := tmp
p1tmp := pOut.Coeffs[i]
qi := ringQ.Modulus[i]
bredParams := ringQ.BredParams[i]
mredParams := ringQ.MredParams[i]
rescaleParams := qi - encoder.rescaleParams[i]

tHalfNegQi := qi - ring.BRedAdd(tHalf, qi, bredParams)

for j := 0; j < ringQ.N; j = j + 8 {

x := (*[8]uint64)(unsafe.Pointer(&in[j]))
z := (*[8]uint64)(unsafe.Pointer(&out[j]))

z[0] = ring.MRed(x[0], d, qi, mredParams)
z[1] = ring.MRed(x[1], d, qi, mredParams)
z[2] = ring.MRed(x[2], d, qi, mredParams)
z[3] = ring.MRed(x[3], d, qi, mredParams)
z[4] = ring.MRed(x[4], d, qi, mredParams)
z[5] = ring.MRed(x[5], d, qi, mredParams)
z[6] = ring.MRed(x[6], d, qi, mredParams)
z[7] = ring.MRed(x[7], d, qi, mredParams)
x := (*[8]uint64)(unsafe.Pointer(&p0tmp[j]))
z := (*[8]uint64)(unsafe.Pointer(&p1tmp[j]))

z[0] = ring.MRed(x[0]+tHalfNegQi, rescaleParams, qi, mredParams)
z[1] = ring.MRed(x[1]+tHalfNegQi, rescaleParams, qi, mredParams)
z[2] = ring.MRed(x[2]+tHalfNegQi, rescaleParams, qi, mredParams)
z[3] = ring.MRed(x[3]+tHalfNegQi, rescaleParams, qi, mredParams)
z[4] = ring.MRed(x[4]+tHalfNegQi, rescaleParams, qi, mredParams)
z[5] = ring.MRed(x[5]+tHalfNegQi, rescaleParams, qi, mredParams)
z[6] = ring.MRed(x[6]+tHalfNegQi, rescaleParams, qi, mredParams)
z[7] = ring.MRed(x[7]+tHalfNegQi, rescaleParams, qi, mredParams)
}
}
}
Expand All @@ -263,19 +273,19 @@ func (encoder *encoder) RingTToMul(ptRt *PlaintextRingT, ptMul *PlaintextMul) {
if ptRt.Value != ptMul.Value {
copy(ptMul.Value.Coeffs[0], ptRt.Value.Coeffs[0])
}
for i := 1; i < len(encoder.ringQ.Modulus); i++ {
for i := 1; i < len(encoder.params.RingQ().Modulus); i++ {
copy(ptMul.Value.Coeffs[i], ptRt.Value.Coeffs[0])
}

encoder.ringQ.NTTLazy(ptMul.Value, ptMul.Value)
encoder.ringQ.MForm(ptMul.Value, ptMul.Value)
encoder.params.RingQ().NTTLazy(ptMul.Value, ptMul.Value)
encoder.params.RingQ().MForm(ptMul.Value, ptMul.Value)
}

// MulToRingT transforms a PlaintextMul into PlaintextRingT by operating the inverse NTT transform of R_q and
// putting the coefficients out of the Montgomery form.
func (encoder *encoder) MulToRingT(pt *PlaintextMul, ptRt *PlaintextRingT) {
encoder.ringQ.InvNTTLvl(0, pt.Value, ptRt.Value)
encoder.ringQ.InvMFormLvl(0, ptRt.Value, ptRt.Value)
encoder.params.RingQ().InvNTTLvl(0, pt.Value, ptRt.Value)
encoder.params.RingQ().InvMFormLvl(0, ptRt.Value, ptRt.Value)
}

// DecodeRingT decodes any plaintext type into a PlaintextRingT. It panics if p is not PlaintextRingT, Plaintext or PlaintextMul.
Expand All @@ -302,17 +312,17 @@ func (encoder *encoder) DecodeUint(p interface{}, coeffs []uint64) {
ptRt = encoder.tmpPtRt
}

encoder.ringT.NTT(ptRt.Value, encoder.tmpPoly)
encoder.params.RingT().NTT(ptRt.Value, encoder.tmpPoly)

for i := 0; i < encoder.ringQ.N; i++ {
for i := 0; i < encoder.params.RingQ().N; i++ {
coeffs[i] = encoder.tmpPoly.Coeffs[0][encoder.indexMatrix[i]]
}
}

// DecodeUintNew decodes any plaintext type and returns the coefficients in a new []uint64.
// It panics if p is not PlaintextRingT, Plaintext or PlaintextMul.
func (encoder *encoder) DecodeUintNew(p interface{}) (coeffs []uint64) {
coeffs = make([]uint64, encoder.ringQ.N)
coeffs = make([]uint64, encoder.params.RingQ().N)
encoder.DecodeUint(p, coeffs)
return
}
Expand All @@ -323,12 +333,12 @@ func (encoder *encoder) DecodeInt(p interface{}, coeffs []int64) {

encoder.DecodeRingT(p, encoder.tmpPtRt)

encoder.ringT.NTT(encoder.tmpPtRt.Value, encoder.tmpPoly)
encoder.params.RingT().NTT(encoder.tmpPtRt.Value, encoder.tmpPoly)

modulus := int64(encoder.params.T())
modulusHalf := modulus >> 1
var value int64
for i := 0; i < encoder.ringQ.N; i++ {
for i := 0; i < encoder.params.RingQ().N; i++ {

value = int64(encoder.tmpPoly.Coeffs[0][encoder.indexMatrix[i]])
coeffs[i] = value
Expand All @@ -341,7 +351,7 @@ func (encoder *encoder) DecodeInt(p interface{}, coeffs []int64) {
// DecodeIntNew decodes any plaintext type and returns the coefficients in a new []int64. It also decodes the sign
// modulus (by centering the values around the plaintext). It panics if p is not PlaintextRingT, Plaintext or PlaintextMul.
func (encoder *encoder) DecodeIntNew(p interface{}) (coeffs []int64) {
coeffs = make([]int64, encoder.ringQ.N)
coeffs = make([]int64, encoder.params.RingQ().N)
encoder.DecodeInt(p, coeffs)
return
}
Loading