Skip to content

Commit 3b2cbc5

Browse files
author
zhdllwyc
committed
fmul
1 parent 10a0004 commit 3b2cbc5

File tree

10 files changed

+1132
-0
lines changed

10 files changed

+1132
-0
lines changed

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ go 1.16
55
require (
66
github.com/bwesterb/go-ristretto v1.2.2
77
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa
8+
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect
89
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10
910
)

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ github.com/bwesterb/go-ristretto v1.2.2/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7N
33
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa h1:zuSxTR4o9y82ebqCUJYNGJbGPo6sKVl54f/TVDObg1c=
44
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
55
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
6+
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 h1:uVc8UZUe6tr40fFVnUP5Oj+veunVezqYl9z7DYw9xzw=
7+
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
68
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
79
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
810
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

ot/simot/simot_test.go

+231
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
// Reference: https://eprint.iacr.org/2015/267.pdf (1 out of 2 OT case)
2+
// Sender has 2 messages m0, m1
3+
// Receiver receives mc based on the choice bit c
4+
5+
package simot
6+
7+
import (
8+
"bytes"
9+
"crypto/rand"
10+
"testing"
11+
12+
"github.com/cloudflare/circl/group"
13+
)
14+
15+
const testSimOTCount = 100
16+
17+
func simOT(myGroup group.Group, sender *SenderSimOT, receiver *ReceiverSimOT, m0, m1 []byte, choice, index int) error {
18+
// Initialization
19+
A := sender.InitSender(myGroup, m0, m1, index)
20+
21+
// Round 1
22+
// Sender sends A to receiver
23+
B := receiver.Round1Receiver(myGroup, choice, index, A)
24+
25+
// Round 2
26+
// Receiver sends B to sender
27+
e0, e1 := sender.Round2Sender(B)
28+
29+
// Round 3
30+
// Sender sends e0 e1 to receiver
31+
errDec := receiver.Round3Receiver(e0, e1, receiver.c)
32+
if errDec != nil {
33+
return errDec
34+
}
35+
36+
return nil
37+
}
38+
39+
func testNegativeSimOT(t *testing.T, myGroup group.Group, choice int) {
40+
var sender SenderSimOT
41+
var receiver ReceiverSimOT
42+
m0 := make([]byte, myGroup.Params().ScalarLength)
43+
m1 := make([]byte, myGroup.Params().ScalarLength)
44+
_, errRand := rand.Read(m0)
45+
if errRand != nil {
46+
panic(errRand)
47+
}
48+
_, errRand = rand.Read(m1)
49+
if errRand != nil {
50+
panic(errRand)
51+
}
52+
53+
// Initialization
54+
A := sender.InitSender(myGroup, m0, m1, 0)
55+
56+
// Round 1
57+
B := receiver.Round1Receiver(myGroup, choice, 0, A)
58+
59+
// Round 2
60+
e0, e1 := sender.Round2Sender(B)
61+
// Round 3
62+
63+
// Here we pass in the flipped choice bit, to prove the decryption will fail
64+
// The receiver will not learn anything about m_{1-c}
65+
errDec := receiver.Round3Receiver(e0, e1, 1-choice)
66+
if errDec == nil {
67+
t.Error("SimOT decryption failed", errDec)
68+
}
69+
70+
if choice == 0 {
71+
equal0 := bytes.Compare(sender.m0, receiver.mc)
72+
if equal0 == 0 {
73+
t.Error("Receiver decryption should fail")
74+
}
75+
equal1 := bytes.Compare(sender.m1, receiver.mc)
76+
if equal1 == 0 {
77+
t.Error("Receiver decryption should fail")
78+
}
79+
} else {
80+
equal0 := bytes.Compare(sender.m0, receiver.mc)
81+
if equal0 == 0 {
82+
t.Error("Receiver decryption should fail")
83+
}
84+
equal1 := bytes.Compare(sender.m1, receiver.mc)
85+
if equal1 == 0 {
86+
t.Error("Receiver decryption should fail")
87+
}
88+
}
89+
}
90+
91+
// Input: myGroup, the group we operate in
92+
func testSimOT(t *testing.T, myGroup group.Group, choice int) {
93+
var sender SenderSimOT
94+
var receiver ReceiverSimOT
95+
96+
m0 := make([]byte, myGroup.Params().ScalarLength)
97+
m1 := make([]byte, myGroup.Params().ScalarLength)
98+
_, errRand := rand.Read(m0)
99+
if errRand != nil {
100+
panic(errRand)
101+
}
102+
_, errRand = rand.Read(m1)
103+
if errRand != nil {
104+
panic(errRand)
105+
}
106+
107+
errDec := simOT(myGroup, &sender, &receiver, m0, m1, choice, 0)
108+
if errDec != nil {
109+
t.Error("AES GCM Decryption failed")
110+
}
111+
112+
if choice == 0 {
113+
equal0 := bytes.Compare(sender.m0, receiver.mc)
114+
if equal0 != 0 {
115+
t.Error("Receiver decryption failed")
116+
}
117+
} else {
118+
equal1 := bytes.Compare(sender.m1, receiver.mc)
119+
if equal1 != 0 {
120+
t.Error("Receiver decryption failed")
121+
}
122+
}
123+
}
124+
125+
func benchmarSimOT(b *testing.B, myGroup group.Group) {
126+
var sender SenderSimOT
127+
var receiver ReceiverSimOT
128+
m0 := make([]byte, myGroup.Params().ScalarLength)
129+
m1 := make([]byte, myGroup.Params().ScalarLength)
130+
_, errRand := rand.Read(m0)
131+
if errRand != nil {
132+
panic(errRand)
133+
}
134+
_, errRand = rand.Read(m1)
135+
if errRand != nil {
136+
panic(errRand)
137+
}
138+
139+
for iter := 0; iter < b.N; iter++ {
140+
errDec := simOT(myGroup, &sender, &receiver, m0, m1, iter%2, 0)
141+
if errDec != nil {
142+
b.Error("AES GCM Decryption failed")
143+
}
144+
}
145+
}
146+
147+
func benchmarkSimOTRound(b *testing.B, myGroup group.Group) {
148+
var sender SenderSimOT
149+
var receiver ReceiverSimOT
150+
m0 := make([]byte, myGroup.Params().ScalarLength)
151+
m1 := make([]byte, myGroup.Params().ScalarLength)
152+
_, errRand := rand.Read(m0)
153+
if errRand != nil {
154+
panic(errRand)
155+
}
156+
_, errRand = rand.Read(m1)
157+
if errRand != nil {
158+
panic(errRand)
159+
}
160+
161+
b.Run("Sender-Initialization", func(b *testing.B) {
162+
for i := 0; i < b.N; i++ {
163+
sender.InitSender(myGroup, m0, m1, 0)
164+
}
165+
})
166+
167+
A := sender.InitSender(myGroup, m0, m1, 0)
168+
169+
b.Run("Receiver-Round1", func(b *testing.B) {
170+
for i := 0; i < b.N; i++ {
171+
receiver.Round1Receiver(myGroup, 0, 0, A)
172+
}
173+
})
174+
175+
B := receiver.Round1Receiver(myGroup, 0, 0, A)
176+
177+
b.Run("Sender-Round2", func(b *testing.B) {
178+
for i := 0; i < b.N; i++ {
179+
sender.Round2Sender(B)
180+
}
181+
})
182+
183+
e0, e1 := sender.Round2Sender(B)
184+
185+
b.Run("Receiver-Round3", func(b *testing.B) {
186+
for i := 0; i < b.N; i++ {
187+
errDec := receiver.Round3Receiver(e0, e1, receiver.c)
188+
if errDec != nil {
189+
b.Error("Receiver-Round3 decryption failed")
190+
}
191+
}
192+
})
193+
194+
errDec := receiver.Round3Receiver(e0, e1, receiver.c)
195+
if errDec != nil {
196+
b.Error("Receiver-Round3 decryption failed")
197+
}
198+
199+
// Confirm
200+
equal0 := bytes.Compare(sender.m0, receiver.mc)
201+
if equal0 != 0 {
202+
b.Error("Receiver decryption failed")
203+
}
204+
}
205+
206+
func TestSimOT(t *testing.T) {
207+
t.Run("SimOT", func(t *testing.T) {
208+
for i := 0; i < testSimOTCount; i++ {
209+
currGroup := group.P256
210+
choice := i % 2
211+
testSimOT(t, currGroup, choice)
212+
}
213+
})
214+
t.Run("SimOTNegative", func(t *testing.T) {
215+
for i := 0; i < testSimOTCount; i++ {
216+
currGroup := group.P256
217+
choice := i % 2
218+
testNegativeSimOT(t, currGroup, choice)
219+
}
220+
})
221+
}
222+
223+
func BenchmarkSimOT(b *testing.B) {
224+
currGroup := group.P256
225+
benchmarSimOT(b, currGroup)
226+
}
227+
228+
func BenchmarkSimOTRound(b *testing.B) {
229+
currGroup := group.P256
230+
benchmarkSimOTRound(b, currGroup)
231+
}

0 commit comments

Comments
 (0)