Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion .github/workflows/go.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ jobs:
echo "sonarqube_golangci_report_paths=$(find -type f -name 'golangci-lint-report.xml' -printf "%p,")" >> $GITHUB_OUTPUT

- name: SonarQube Scan
uses: sonarsource/sonarqube-scan-action@69c1a75940dec6249b86dace6b630d3a2ae9d2a7 # v2.0.1
uses: sonarsource/sonarqube-scan-action@aecaf43ae57e412bd97d70ef9ce6076e672fe0a9 # v2.3.0
with:
args: >
-Dsonar.go.coverage.reportPaths=${{ steps.sonarqube_report_paths.outputs.sonarqube_coverage_report_paths }}
Expand Down
12 changes: 7 additions & 5 deletions go/tdh2/lib/group/nist/curve.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ func (p *curvePoint) Equal(p2 group.Point) bool {
// Make sure both coordinates are normalized.
// Apparently Go's elliptic curve code doesn't always ensure this.
M := p.c.p.P
p.x.Mod(p.x, M)
p.y.Mod(p.y, M)
cp2.x.Mod(cp2.x, M)
cp2.y.Mod(cp2.y, M)
x1 := new(big.Int).Mod(p.x, M)
y1 := new(big.Int).Mod(p.y, M)
x2 := new(big.Int).Mod(cp2.x, M)
y2 := new(big.Int).Mod(cp2.y, M)

return p.x.Cmp(cp2.x) == 0 && p.y.Cmp(cp2.y) == 0
return x1.Cmp(x2) == 0 && y1.Cmp(y2) == 0
}

func (p *curvePoint) Null() group.Point {
Expand Down Expand Up @@ -134,6 +134,8 @@ func (p *curvePoint) UnmarshalBinary(buf []byte) error {
if p.x == nil || !p.Valid() {
return errors.New("invalid elliptic curve point")
}
} else if buf[0] != 0x04 {
return errors.New("invalid elliptic curve point: non-canonical identity encoding")
} else {
// All bytes are 0, so we initialize x and y
p.x = big.NewInt(0)
Expand Down
2 changes: 1 addition & 1 deletion go/tdh2/tdh2easy/tdh2easy.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (m *MasterSecret) Unmarshal(data []byte) error {
return m.m.Unmarshal(data)
}

func (m MasterSecret) UnmarshalJSON(data []byte) error {
func (m *MasterSecret) UnmarshalJSON(data []byte) error {
return m.Unmarshal(data)
}

Expand Down
202 changes: 191 additions & 11 deletions go/tdh2/tdh2easy/tdh2easy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,73 @@ func TestShareIndex(t *testing.T) {
}
}

func TestPrivateShareMarshal(t *testing.T) {
_, _, want, err := GenerateKeys(2, 3)
func testPrivateShare(t *testing.T) *PrivateShare {
_, _, sh, err := GenerateKeys(2, 3)
if err != nil {
t.Fatalf("GenerateKeys: %v", err)
}
b, err := want[0].Marshal()
return sh[0]
}

func TestPrivateShareMarshal(t *testing.T) {
want := testPrivateShare(t)
b, err := want.Marshal()
if err != nil {
t.Fatalf("Marshal: %v", err)
}
var got PrivateShare
if err := got.Unmarshal(b); err != nil {
t.Fatalf("Unmarshal: %v", err)
}
if !reflect.DeepEqual(got.p, want[0].p) {
t.Errorf("got=%v want=%v", got, want[0])
if !reflect.DeepEqual(got.p, want.p) {
t.Errorf("got=%v want=%v", got, want)
}
if err := got.Unmarshal([]byte("broken")); err == nil {
t.Errorf("Unmarshal did not fail")
}
}

func TestDecryptionShareMarshal(t *testing.T) {
func TestPrivateShareMarshalJSON(t *testing.T) {
want := testPrivateShare(t)
b, err := want.MarshalJSON()
if err != nil {
t.Fatalf("MarshalJSON: %v", err)
}
var got PrivateShare
if err := got.UnmarshalJSON(b); err != nil {
t.Fatalf("UnmarshalJSON: %v", err)
}
if !reflect.DeepEqual(got.p, want.p) {
t.Errorf("got=%v want=%v", got, want)
}
if err := got.UnmarshalJSON([]byte("broken")); err == nil {
t.Errorf("Unmarshal did not fail")
}
}

func TestPrivateShareJSONMarshal(t *testing.T) {
var wrap = struct {
Share *PrivateShare `json:"share"`
}{Share: testPrivateShare(t)}
b, err := json.Marshal(wrap)
if err != nil {
t.Fatalf("Marshal: %v", err)
}
var got struct {
Share *PrivateShare `json:"share"`
}
if err := json.Unmarshal(b, &got); err != nil {
t.Fatalf("Unmarshal: %v", err)
}
if !reflect.DeepEqual(got.Share, wrap.Share) {
t.Errorf("got=%v want=%v", got.Share, wrap.Share)
}
if err := json.Unmarshal([]byte("broken"), &got); err == nil {
t.Errorf("Unmarshal did not fail")
}
}

func testDecryptionShare(t *testing.T) *DecryptionShare {
_, pk, sh, err := GenerateKeys(2, 3)
if err != nil {
t.Fatalf("GenerateKeys: %v", err)
Expand All @@ -67,10 +112,15 @@ func TestDecryptionShareMarshal(t *testing.T) {
if err != nil {
t.Fatalf("Encrypt: %v", err)
}
want, err := Decrypt(c, sh[0])
ds, err := Decrypt(c, sh[0])
if err != nil {
t.Fatalf("Decrypt: %v", err)
}
return ds
}

func TestDecryptionShareMarshal(t *testing.T) {
want := testDecryptionShare(t)
b, err := want.Marshal()
if err != nil {
t.Fatalf("Marshal: %v", err)
Expand All @@ -87,11 +137,56 @@ func TestDecryptionShareMarshal(t *testing.T) {
}
}

func TestPublicKeyMarshal(t *testing.T) {
_, want, _, err := GenerateKeys(2, 3)
func TestDecryptionShareMarshalJSON(t *testing.T) {
want := testDecryptionShare(t)
b, err := want.MarshalJSON()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Would it make more sense to define a data structure that contains a DecyptionShare, then invoke json.Marshal/json.Unmarshal on it to make sure that the json package interacts correctly with how we defined the interface functions? One typically doesn't directly call MarshalJSON, it's mediated through the json package.
Same for other json tests.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

if err != nil {
t.Fatalf("MarshalJSON: %v", err)
}
var got DecryptionShare
if err := got.UnmarshalJSON(b); err != nil {
t.Fatalf("UnmarshalJSON: %v", err)
}
if !reflect.DeepEqual(got.d, want.d) {
t.Errorf("got=%v want=%v", got, want)
}
if err := got.UnmarshalJSON([]byte("broken")); err == nil {
t.Errorf("UnmarshalJSON did not fail")
}
}

func TestDecryptionShareJSONMarshal(t *testing.T) {
var wrap = struct {
Share *DecryptionShare `json:"share"`
}{Share: testDecryptionShare(t)}
b, err := json.Marshal(wrap)
if err != nil {
t.Fatalf("Marshal: %v", err)
}
var got struct {
Share *DecryptionShare `json:"share"`
}
if err := json.Unmarshal(b, &got); err != nil {
t.Fatalf("Unmarshal: %v", err)
}
if !reflect.DeepEqual(got.Share, wrap.Share) {
t.Errorf("got=%v want=%v", got.Share, wrap.Share)
}
if err := json.Unmarshal([]byte("broken"), &got); err == nil {
t.Errorf("Unmarshal did not fail")
}
}

func testPublicKey(t *testing.T) *PublicKey {
_, pk, _, err := GenerateKeys(2, 3)
if err != nil {
t.Fatalf("GenerateKeys: %v", err)
}
return pk
}

func TestPublicKeyMarshal(t *testing.T) {
want := testPublicKey(t)
b, err := want.Marshal()
if err != nil {
t.Fatalf("Marshal: %v", err)
Expand All @@ -108,11 +203,56 @@ func TestPublicKeyMarshal(t *testing.T) {
}
}

func TestMasterSecretMarshal(t *testing.T) {
want, _, _, err := GenerateKeys(2, 3)
func TestPublicKeyMarshalJSON(t *testing.T) {
want := testPublicKey(t)
b, err := want.MarshalJSON()
if err != nil {
t.Fatalf("MarshalJSON: %v", err)
}
var got PublicKey
if err := got.UnmarshalJSON(b); err != nil {
t.Fatalf("UnmarshalJSON: %v", err)
}
if !got.p.Equal(want.p) {
t.Errorf("got=%v want=%v", got, want)
}
if err := got.UnmarshalJSON([]byte("broken")); err == nil {
t.Errorf("UnmarshalJSON did not fail")
}
}

func TestPublicKeyJSONMarshal(t *testing.T) {
var wrap = struct {
Key *PublicKey `json:"key"`
}{Key: testPublicKey(t)}
b, err := json.Marshal(wrap)
if err != nil {
t.Fatalf("Marshal: %v", err)
}
var got struct {
Key *PublicKey `json:"key"`
}
if err := json.Unmarshal(b, &got); err != nil {
t.Fatalf("Unmarshal: %v", err)
}
if !got.Key.p.Equal(wrap.Key.p) {
t.Errorf("got=%v want=%v", got.Key, wrap.Key)
}
if err := json.Unmarshal([]byte("broken"), &got); err == nil {
t.Errorf("Unmarshal did not fail")
}
}

func testMasterSecret(t *testing.T) *MasterSecret {
ms, _, _, err := GenerateKeys(2, 3)
if err != nil {
t.Fatalf("GenerateKeys: %v", err)
}
return ms
}

func TestMasterSecretMarshal(t *testing.T) {
want := testMasterSecret(t)
b, err := want.Marshal()
if err != nil {
t.Fatalf("Marshal: %v", err)
Expand All @@ -129,6 +269,46 @@ func TestMasterSecretMarshal(t *testing.T) {
}
}

func TestMasterSecretMarshalJSON(t *testing.T) {
want := testMasterSecret(t)
b, err := want.MarshalJSON()
if err != nil {
t.Fatalf("MarshalJSON: %v", err)
}
var got MasterSecret
if err := got.UnmarshalJSON(b); err != nil {
t.Fatalf("UnmarshalJSON: %v", err)
}
if !reflect.DeepEqual(got.m, want.m) {
t.Errorf("got=%v want=%v", got, want)
}
if err := got.UnmarshalJSON([]byte("broken")); err == nil {
t.Errorf("UnmarshalJSON did not fail")
}
}

func TestMasterSecretJSONMarshal(t *testing.T) {
var wrap = struct {
Secret *MasterSecret `json:"secret"`
}{Secret: testMasterSecret(t)}
b, err := json.Marshal(wrap)
if err != nil {
t.Fatalf("Marshal: %v", err)
}
var got struct {
Secret *MasterSecret `json:"secret"`
}
if err := json.Unmarshal(b, &got); err != nil {
t.Fatalf("Unmarshal: %v", err)
}
if !reflect.DeepEqual(got.Secret, wrap.Secret) {
t.Errorf("got=%v want=%v", got.Secret, wrap.Secret)
}
if err := json.Unmarshal([]byte("broken"), &got); err == nil {
t.Errorf("Unmarshal did not fail")
}
}

func TestCiphertextDecrypt(t *testing.T) {
_, pk, share, err := GenerateKeys(1, 1)
if err != nil {
Expand Down
Loading