-
Notifications
You must be signed in to change notification settings - Fork 3
Linear Algebra operations: Jiang et al #119
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
Draft
Tom-Finke
wants to merge
20
commits into
main
Choose a base branch
from
linalg_jiang-et-al
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
d95fdb8
feat: implemented multiplication of two d x d matrices in encoded in …
Tom-Finke d0c0e40
fix: typo
Tom-Finke 41c8723
fix: indentation
Tom-Finke f69e2ce
Merge branch 'main' into linalg_jiang-et-al
Tom-Finke d68e2ec
refactor: use broadcasted multiply ".*" for element-wise SecureArray …
Tom-Finke 4e44446
Change multiplication to element-wise operation
Tom-Finke 0b33072
fix: julia codeblock instead of doctest
Tom-Finke bb6f77e
refactor: use broadcasted multiply ".*" for element-wise SecureArray …
Tom-Finke cfbf52f
feat: extend main operations to include PlainArray
Tom-Finke f9ce3f9
refactor: matrix multiplication with * operator for SecureMatrix
Tom-Finke 05fd07d
fix[docs]: fix references
Tom-Finke fc3b28f
fix: broadcasting for scalar multiplication
Tom-Finke da38bbc
Merge branch 'refactor_broadcast-element-wise-multiply' into linalg_j…
Tom-Finke 3abb913
include lin alg test in runtests
Tom-Finke 43afc71
removed dangling transposition comment
Tom-Finke 0a6f9d9
Get num rows for mat mul from shape instead of square root
Tom-Finke 93b405d
fix: element wise multiply instead of mat mul
Tom-Finke 05e454c
use i instead of l as counter variable
Tom-Finke 9e39b0e
fix: merge error, remove duplicated array style
Tom-Finke 0f516a6
feat: resize PlainVector and SecureVector
Tom-Finke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| """ | ||
| Multiply two d x d matrices | ||
|
|
||
|
|
||
| See https://eprint.iacr.org/2018/1041.pdf | ||
|
|
||
| """ | ||
| function mat_times_mat(A, B) | ||
|
|
||
| n = length(A) | ||
| d = Int(sqrt(n)) | ||
| shape = A.shape | ||
| ctx = A.context | ||
|
|
||
| # Flatten the matrices so that rotations act on flat vectors instead of being 2d rotations | ||
| A = reshape(A, (n)) | ||
| B = reshape(B, (n)) | ||
|
|
||
| # Step 1-1 | ||
| # Compute linear Transformation sigma on A | ||
|
|
||
|
|
||
| A_0 = PlainArray(zeros(n), ctx) | ||
|
|
||
| for k in -d+1:d-1 | ||
| if k >= 0 | ||
| u_k_sigma = PlainArray([0 <= l-d*k && l-d*k < d-k ? 1 : 0 for l in 0:n-1], ctx) | ||
| else | ||
| u_k_sigma = PlainArray([-k <= l-(d+k)*d && l-(d+k)*d < d ? 1 : 0 for l in 0:n-1], ctx) | ||
| end | ||
| # Note that Rot(ct; l) in the paper is a leftshift, i.e. circshift(ct, -l) | ||
| A_0 += circshift(A, -(k)) * u_k_sigma | ||
| end | ||
|
|
||
| # Step 1-2 | ||
| # Compute linear Transformation tau on B | ||
|
|
||
|
|
||
| B_0 = PlainArray(zeros(n), ctx) | ||
|
|
||
| for k in 0:d-1 | ||
| u_dk_tau = PlainArray([(l - k) / d in 0:d-1 ? 1 : 0 for l in 0:n-1], ctx) | ||
| B_0 += circshift(B, -(d*k)) * u_dk_tau | ||
| end | ||
|
|
||
| # Step 2 and 3 | ||
| AB = A_0 * B_0 | ||
| for k in 1:d-1 | ||
| v_k = PlainArray([0 <= (l % d) && (l % d) < (d-k) ? 1 : 0 for l in 0:n-1], ctx) | ||
| v_k_d = PlainArray([(d-k) <= (l % d) && (l % d) < d ? 1 : 0 for l in 0:n-1], ctx) | ||
| A_k = circshift(A_0, -(k)) * v_k + circshift(A_0, -(k-d)) * v_k_d | ||
| B_k = circshift(B_0, -(d*k)) | ||
| AB += A_k * B_k | ||
| end | ||
| return reshape(AB, shape) | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| module TestLinearAlgebra | ||
|
|
||
| using Test | ||
| using SecureArithmetic | ||
| using OpenFHE | ||
|
|
||
| function make_context() | ||
| level_budget = [4, 4] | ||
| levels_after_bootstrap = 10 | ||
| depth = levels_after_bootstrap + GetBootstrapDepth(level_budget, UNIFORM_TERNARY) | ||
|
|
||
| parameters = CCParams{CryptoContextCKKSRNS}() | ||
| SetSecretKeyDist(parameters, UNIFORM_TERNARY) | ||
| SetSecurityLevel(parameters, HEStd_NotSet) | ||
| SetRingDim(parameters, 1 << 12) | ||
| SetScalingModSize(parameters, 59) | ||
| SetScalingTechnique(parameters, FLEXIBLEAUTO) | ||
| SetFirstModSize(parameters, 60) | ||
| SetMultiplicativeDepth(parameters, depth) | ||
|
|
||
| cc = GenCryptoContext(parameters) | ||
| Enable(cc, PKE) | ||
| Enable(cc, KEYSWITCH) | ||
| Enable(cc, LEVELEDSHE) | ||
| Enable(cc, ADVANCEDSHE) | ||
| Enable(cc, FHE) | ||
|
|
||
| EvalBootstrapSetup(cc; level_budget) | ||
|
|
||
| context = SecureContext(OpenFHEBackend(cc)) | ||
| public_key, private_key = generate_keys(context) | ||
|
|
||
|
|
||
| return (context, public_key, private_key) | ||
| end | ||
|
|
||
| @testset verbose=true showtiming=true "test_linear_algebra.jl" begin | ||
|
|
||
|
|
||
| @testset verbose=true showtiming=true "square_mat_mat_nul" begin | ||
| (context, public_key, private_key) = make_context() | ||
|
|
||
| # SecureArithmetic stores matrices in column order, thus we need to transpose before eval mult | ||
| m1 = collect(transpose([0.25 0.5 0.75; | ||
| 1.0 2.0 3.0; | ||
| 4.0 5.0 6.0])) | ||
|
|
||
| m2 = collect(transpose([6.0 5.0 4.0; | ||
| 3.0 2.0 1.0; | ||
| 0.75 0.5 0.25])) | ||
|
|
||
| pm1 = PlainMatrix(m1, context) | ||
| pm2 = PlainMatrix(m2, context) | ||
|
|
||
|
|
||
| sm1 = encrypt(pm1, public_key) | ||
| sm2 = encrypt(pm2, public_key) | ||
|
|
||
| init_rotation!(context, private_key, (length(m1)), 1:length(m1)...) | ||
| init_multiplication!(context, private_key) | ||
|
|
||
|
|
||
|
|
||
| # the result is in row order. This we need to transpose to get the correct matrix in column order | ||
| result = transpose(collect(decrypt(SecureArithmetic.mat_times_mat(sm1, sm2), private_key))) | ||
| @test result ≈ [ 3.5625 2.625 1.6875; | ||
| 14.25 10.5 6.75; | ||
| 43.5 33.0 22.5] | ||
|
|
||
| end | ||
|
|
||
| release_context_memory() | ||
| GC.gc() | ||
|
|
||
| end # @testset "test_serialization.jl" | ||
|
|
||
| end # module |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.