Skip to content

Commit

Permalink
Reg24 parameters. fix #67
Browse files Browse the repository at this point in the history
  • Loading branch information
aidevnn committed Jan 3, 2025
1 parent 4712053 commit 1bc31b6
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 21 deletions.
17 changes: 17 additions & 0 deletions FastGoat/UserGroup/Lattice/LWEtests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,21 @@ public static void TestLWERegev()
Console.WriteLine();
}
}
// Regev N:10 Q:223 M:156 a:0.0287 aq:6.3903 Q/M:1.4295
// Private key:(131, 38, 186, 116, 176, 7, 142, 38, 193, 84)
//
// hello world lwe
// seqInput :[000101101010011000110110001101101111011000000100111011101111011001001110001101100010011000000100001101101110111010100110]
// seqDecrypt:[000101101010011000110110001101101111011000000100111011101111011001001110001101100010011000000100001101101110111010100110]
// hello world lwe
// SUCCESS
// ...
// Regev N:70 Q:5261 M:1730 a:0.0032 aq:16.7379 Q/M:3.0410
// Private key:(2775, 254, 2127, 2519, 1431, 4476, 2533, 2966, 4346, 1747, 3786, 3773, 3490, 1261, 4158, 1031, 4407, 1642, 637, 1090, 146, 5114, 2482, 4022, 3940, 5101, 812, 3243, 3980, 1615, 2785, 3247, 1741, 37, 5096, 1367, 1125, 1408, 3695, 1265, 1928, 2761, 3373, 2105, 316, 4651, 2839, 3338, 2325, 2335, 1904, 3176, 4268, 5077, 803, 5247, 4754, 5122, 4505, 1907, 3919, 2791, 2049, 4080, 1707, 4404, 584, 931, 4764, 4154)
//
// hello world lwe
// seqInput :[000101101010011000110110001101101111011000000100111011101111011001001110001101100010011000000100001101101110111010100110]
// seqDecrypt:[000101101010011000110110001101101111011000000100111011101111011001001110001101100010011000000100001101101110111010100110]
// hello world lwe
// SUCCESS
}
43 changes: 22 additions & 21 deletions FastGoat/UserGroup/Lattice/Regev.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace FastGoat.UserGroup.Lattice;
public class Regev
{
public int N { get; }
public int Q { get; }
public int P { get; }
public int M { get; }
public double A { get; }
private Vec<ZnInt64> SK { get; }
Expand All @@ -27,44 +27,45 @@ public Regev(int n)
{
// All conditions

// 1) m = (1+ε)*n*log2(n) ε=1
var m = (int)(2 * n * double.Log2(n));
// 1) lim a(n)*log2(n)*√n = lim 1/log2(n) = 0
var a = 1.0 / (double.Log2(n) * double.Log2(n) * double.Sqrt(n));

// 2) lim a(n)*log2(n)*√n = lim 1 / (4*log10(n)) = 0
var a = 1.0 / (4 * double.Log10(n) * double.Log2(n) * double.Sqrt(n));
// 2) a*p > 2*√n
var p = Primes10000.First(p => a * p > 2 * double.Sqrt(n));

// 3) a*q > 2*√n
var q = Primes10000.First(q => a * q > 2 * double.Sqrt(n));
(N, Q, M, A) = (n, q, m, a);
// 3) m = (1+ε)*n*log2(p) ε=1
var m = (int)(2 * n * double.Log2(p));
(N, P, M, A) = (n, p, m, a);

// 4) Discrete gaussian sample with s = a*q
var err = Err = DiscGauss(m, q, s: a * q);
// 4) Discrete gaussian sample with s = a*p
var err = Err = DiscGauss(m, p, s: a * p);

var sk = SK = Unif(n, q);
var ai = m.SeqLazy().Select(_ => Unif(n, q)).ToArray();
var sk = SK = Unif(n, p);
var ai = m.SeqLazy().Select(_ => Unif(n, p)).ToArray();
var b = ai.Zip(err).Select(e => (e.First * sk).Sum() + e.Second).ToVec();
PK = (ai, b);
}

public RegevCipher EncryptBit(int m)
{
var _m = m == 0 ? 0 : 1;
var acc0 = (a: PK.A[0].Zero, b: new ZnInt64(P, _m * (P / 2)));

// 5) Set S ⊂ [0..M-1]
var r = DistributionExt.DiceSample(M, [true, false]).Zip(M.Range())
.Where(e => e.First).Select(e => e.Second).ToArray();
var m1 = m == 0 ? 0 : 1;
var acc0 = (a: PK.A[0].Zero, b: new ZnInt64(Q, m1 * (Q / 2)));
return r.Select(e => (a: PK.A[e], b: PK.B[e])).Aggregate(acc0, (acc, e) => (acc.a + e.a, acc.b + e.b));
return DistributionExt.DiceSample(M, [true, false]).Zip(M.Range())
.Where(e => e.First)
.Select(e => (a: PK.A[e.Second], b: PK.B[e.Second]))
.Aggregate(acc0, (acc, e) => (acc.a + e.a, acc.b + e.b));
}

public int DecryptBit(RegevCipher cipher)
{
// 6) b - <s,a> distance to 0 and to Q/2
// 6) b - <s,a> distance to 0 and to P/2
var d = cipher.B - (cipher.A * SK).Sum();
return long.Abs(d.Signed) < Q / 4 ? 0 : 1;
return long.Abs(d.Signed) < P / 4 ? 0 : 1;
}

public string Params =>
$"Regev N:{N,-4} Q:{Q,-6} M:{M,-6} a:{A:F4} aq:{A * Q:F4} Q/M:{Q / M}";
public string Params => $"Regev N:{N,-4} P:{P,-6} M:{M,-6} A:{A:F4} A*P:{A * P:F4}";

public void Show()
{
Expand Down

0 comments on commit 1bc31b6

Please sign in to comment.