Skip to content

Commit

Permalink
更新pir算法 (#35)
Browse files Browse the repository at this point in the history
* 更新PIR算法

* 更新PIR算法,隐藏部分敏感信息

* 清理ide自动编译class文件

* 清理所有.class

* 更新PIR算法

* 更新PIR算法,隐藏部分敏感信息

* 清理ide自动编译class文件

* 清理所有.class

---------

Co-authored-by: zachma(马晨) <[email protected]>
  • Loading branch information
zachma-820 and zachma(马晨) authored Sep 13, 2024
1 parent 97726a1 commit af6f5d9
Show file tree
Hide file tree
Showing 36 changed files with 756 additions and 85 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ cmake-build-debug
*.exe
*.out
*.app
**/*.class

build/**
GPATH
GRTAGS
GTAGS
deps
deps/**
**/bin

d1-encrypted
d2-encrypted
Expand Down Expand Up @@ -137,6 +139,7 @@ ENV/
*.crt
*/bin
*/logs
*/bin
*.csv
*/*/logs
*/start.out
Expand Down
8 changes: 8 additions & 0 deletions java/ppc-pir-services/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,16 @@ dependencies {
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.2.2'
implementation 'org.apache.logging.log4j:log4j-slf4j-impl:2.19.0'
implementation 'commons-codec:commons-codec:1.15'
implementation("org.apache.hadoop:hadoop-common:2.7.5"){
exclude group: 'javax.servlet', module: 'servlet-api'
}
implementation("org.apache.hadoop:hadoop-client:2.7.5"){
exclude group: 'javax.servlet', module: 'servlet-api'
}
implementation 'com.opencsv:opencsv:5.9'
implementation 'com.squareup.okhttp3:okhttp:4.9.1'
implementation project(":ppc-pir-sdk")
implementation 'com.google.code.gson:gson:2.8.8'

compileOnly 'org.projectlombok:lombok:1.18.24'
annotationProcessor 'org.projectlombok:lombok:1.18.24'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@ public static void main(String[] args) throws WedprException {

private static void testIdFilter() throws WedprException {
PirJobParam pirJobParam = new PirJobParam();
pirJobParam.setJobId("j-12345678912");
pirJobParam.setServiceId("s-123456789");
pirJobParam.setAccessKeyId("a-123456789");
pirJobParam.setJobType(ParamEnum.JobType.SearchValue.getValue());
pirJobParam.setJobAlgorithmType(ParamEnum.AlgorithmType.idFilter.getValue());
pirJobParam.setParticipateAgencyId("1002");
pirJobParam.setDatasetId("t_login_token");
pirJobParam.setJobCreator("1001");
pirJobParam.setGatewayUrl("http://localhost:5831/api/pir/v3/server");
pirJobParam.setSearchIdList(Arrays.asList("1", "20", "3", "456"));
pirJobParam.setGatewayUrl("localhost:5831");
pirJobParam.setSearchIdList(Arrays.asList("1", "2", "3"));

PirClient pirClient = new PirClient(pirJobParam);
PirResultResponse pirResultResponse = pirClient.executePirJob();
Expand All @@ -35,14 +33,12 @@ private static void testIdFilter() throws WedprException {

private static void testIdObfuscation() throws WedprException {
PirJobParam pirJobParam = new PirJobParam();
pirJobParam.setJobId("j-12345678912");
pirJobParam.setServiceId("s-123456789");
pirJobParam.setAccessKeyId("a-123456789");
pirJobParam.setJobType(ParamEnum.JobType.SearchValue.getValue());
pirJobParam.setJobAlgorithmType(ParamEnum.AlgorithmType.idObfuscation.getValue());
pirJobParam.setParticipateAgencyId("1002");
pirJobParam.setDatasetId("t_login_token");
pirJobParam.setJobCreator("1001");
pirJobParam.setGatewayUrl("http://localhost:5831/api/pir/v3/server");
pirJobParam.setSearchIdList(Arrays.asList("1", "20", "3", "456"));
pirJobParam.setGatewayUrl("localhost:5831");
pirJobParam.setSearchIdList(Arrays.asList("3", "2", "1"));

PirClient pirClient = new PirClient(pirJobParam);
PirResultResponse pirResultResponse = pirClient.executePirJob();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package com.wedpr.pir.sdk;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.wedpr.pir.sdk.entity.body.ServiceConfigBody;
import com.wedpr.pir.sdk.entity.body.SimpleEntity;
import com.wedpr.pir.sdk.entity.param.PirJobParam;
import com.wedpr.pir.sdk.entity.request.ClientAuthRequest;
import com.wedpr.pir.sdk.entity.request.ServerJobRequest;
import com.wedpr.pir.sdk.entity.response.ClientAuthResponse;
import com.wedpr.pir.sdk.entity.response.ClientOTResponse;
import com.wedpr.pir.sdk.entity.response.PirResultResponse;
import com.wedpr.pir.sdk.enums.ParamEnum;
import com.wedpr.pir.sdk.exception.WedprException;
import com.wedpr.pir.sdk.exception.WedprStatusEnum;
import com.wedpr.pir.sdk.gateway.GatewayClient;
import com.wedpr.pir.sdk.helper.Constant;
import com.wedpr.pir.sdk.service.PirJobService;
import java.util.Objects;

Expand All @@ -26,13 +31,33 @@ public PirClient(PirJobParam pirJobParam) throws WedprException {

public PirResultResponse executePirJob() throws WedprException {
try {
ClientOTResponse otParamResponse = pirJobService.requesterOtCipher();
ObjectMapper objectMapper = new ObjectMapper();
// 认证
PirJobParam pirJobParam = pirJobService.getPirJobParam();
ServiceConfigBody serviceConfigBody = pirJobParam.getServiceConfigBody();
Integer invokeType = pirJobParam.getPirInvokeType();
if (invokeType.equals(ParamEnum.JobMode.SDKMode.getValue())) {
ClientAuthRequest clientAuthRequest = new ClientAuthRequest();
clientAuthRequest.setAccessKeyId(pirJobParam.getAccessKeyId());
clientAuthRequest.setServiceId(pirJobParam.getServiceId());
String authUrl = pirJobParam.getGatewayUrl() + Constant.PERMISSION;
String body = objectMapper.writeValueAsString(clientAuthRequest);
ClientAuthResponse authResult =
GatewayClient.sendPostRequestWithRetry(
authUrl, body, ClientAuthResponse.class);
if (!authResult.getCode().equals(WedprStatusEnum.SUCCESS.getCode())) {
throw new WedprException(
WedprStatusEnum.CALL_PSI_ERROR, "认证失败:" + authResult.getMessage());
}
pirJobParam.setServiceConfigBody(authResult.getData());
}

// 执行任务
ClientOTResponse otParamResponse = pirJobService.requesterOtCipher();
ServerJobRequest serverJobRequest =
generatePirJobRequestFromJobAndOTParam(pirJobParam, otParamResponse);

String pirUrl = pirJobParam.getGatewayUrl();
ObjectMapper objectMapper = new ObjectMapper();
String pirUrl = pirJobParam.getGatewayUrl() + Constant.SERVER;
String body = objectMapper.writeValueAsString(serverJobRequest);
SimpleEntity otResult =
GatewayClient.sendPostRequestWithRetry(pirUrl, body, SimpleEntity.class);
Expand All @@ -42,18 +67,17 @@ public PirResultResponse executePirJob() throws WedprException {
}
return pirJobService.requesterOtRecover(otParamResponse, pirJobParam, otResult);
} catch (Exception e) {
throw new WedprException(WedprStatusEnum.CALL_PSI_ERROR);
throw new WedprException(WedprStatusEnum.CALL_PSI_ERROR, e.getMessage());
}
}

/** 根据 ClientOTResponse 和 PirJobParam 构建PirJobRequest */
private ServerJobRequest generatePirJobRequestFromJobAndOTParam(
PirJobParam pirJobParam, ClientOTResponse otParamResponse) {
ServerJobRequest serverJobRequest = new ServerJobRequest();
serverJobRequest.setJobId(pirJobParam.getJobId());
serverJobRequest.setParticipateAgencyId(pirJobParam.getParticipateAgencyId());
serverJobRequest.setDatasetId(pirJobParam.getDatasetId());
serverJobRequest.setJobCreator(pirJobParam.getJobCreator());
serverJobRequest.setServiceId(pirJobParam.getServiceId());
serverJobRequest.setServiceConfigBody(pirJobParam.getServiceConfigBody());
serverJobRequest.setAccessKeyId(pirJobParam.getAccessKeyId());
serverJobRequest.setJobAlgorithmType(pirJobParam.getJobAlgorithmType());
serverJobRequest.setX(otParamResponse.getX());
serverJobRequest.setY(otParamResponse.getY());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.wedpr.pir.sdk.entity.body;

import lombok.Data;

/**
* @author zachma
* @date 2024/9/3
*/
@Data
public class ServiceConfigBody {
private String datasetId;
private String[] exists;
private String[] values;
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.wedpr.pir.sdk.entity.param;

import com.wedpr.pir.sdk.entity.body.ServiceConfigBody;
import com.wedpr.pir.sdk.entity.request.PirBaseRequest;
import com.wedpr.pir.sdk.enums.ParamEnum;
import com.wedpr.pir.sdk.exception.WedprException;
import com.wedpr.pir.sdk.exception.WedprStatusEnum;
import com.wedpr.pir.sdk.helper.BasicTypeHelper;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import lombok.Data;
import lombok.EqualsAndHashCode;

Expand All @@ -24,42 +26,42 @@ public class PirJobParam extends PirBaseRequest {

private List<String> searchIdList;

private ServiceConfigBody serviceConfigBody;

public void check() throws WedprException {
if (BasicTypeHelper.isStringEmpty(gatewayUrl)) {
throw new WedprException(WedprStatusEnum.CLIENT_PARAM_EXCEPTION, "访问gatewayUrl不能为空");
}

if (BasicTypeHelper.isStringEmpty(getJobId())) {
throw new WedprException(WedprStatusEnum.CLIENT_PARAM_EXCEPTION, "jobId 不能为空");
if (getPirInvokeType().equals(ParamEnum.JobMode.SDKMode.getValue())
&& BasicTypeHelper.isStringEmpty(getServiceId())) {
throw new WedprException(WedprStatusEnum.CLIENT_PARAM_EXCEPTION, "serviceId 不能为空");
}

if (getPirInvokeType().equals(ParamEnum.JobMode.SDKMode.getValue())
&& BasicTypeHelper.isStringEmpty(getAccessKeyId())) {
throw new WedprException(WedprStatusEnum.CLIENT_PARAM_EXCEPTION, "accessKeyId 不能为空");
}

if (Arrays.stream(ParamEnum.JobType.values())
.noneMatch(enumValue -> enumValue.getValue().equals(getJobType()))) {
throw new WedprException(WedprStatusEnum.CLIENT_PARAM_EXCEPTION, "jobType输入错误");
}

if (BasicTypeHelper.isStringEmpty(getParticipateAgencyId())) {
throw new WedprException(WedprStatusEnum.CLIENT_PARAM_EXCEPTION, "数据机构Id 不能为空");
}

if (BasicTypeHelper.isStringEmpty(getDatasetId())) {
throw new WedprException(WedprStatusEnum.CLIENT_PARAM_EXCEPTION, "datasetId 不能为空");
}

if (Arrays.stream(ParamEnum.AlgorithmType.values())
.noneMatch(enumValue -> enumValue.getValue().equals(getJobAlgorithmType()))) {
throw new WedprException(
WedprStatusEnum.CLIENT_PARAM_EXCEPTION, "jobAlgorithmType输入错误");
}

if (searchIdList == null || searchIdList.size() == 0) {
if (Objects.isNull(searchIdList) || searchIdList.size() == 0) {
throw new WedprException(WedprStatusEnum.CLIENT_PARAM_EXCEPTION, "searchId列表不能为空");
}

if (getJobAlgorithmType().equals(ParamEnum.AlgorithmType.idObfuscation.getValue())
&& getObfuscationOrder() == -1) {
if (getPirInvokeType().equals(ParamEnum.JobMode.DirectorMode.getValue())
&& Objects.isNull(serviceConfigBody)) {
throw new WedprException(
WedprStatusEnum.CLIENT_PARAM_EXCEPTION, "哈希披露算法中, obfuscationOrder未设置");
WedprStatusEnum.CLIENT_PARAM_EXCEPTION, "向导模式下serviceConfigBody不能为空");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.wedpr.pir.sdk.entity.request;

import lombok.Data;

/**
* @author zachma
* @date 2024/9/3
*/
@Data
public class ClientAuthRequest {
String serviceId;
String accessKeyId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.wedpr.pir.sdk.entity.request;

import com.wedpr.pir.sdk.entity.body.ServiceConfigBody;
import java.util.List;
import lombok.Data;

/**
* @author zachma
* @date 2024/9/5
*/
@Data
public class ClientDirectRequest {
private String algorithmType;
private List<String> searchIds;
private ServiceConfigBody serviceConfigBody;
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
package com.wedpr.pir.sdk.entity.request;

import com.wedpr.pir.sdk.enums.ParamEnum;
import lombok.Data;

@Data
public class PirBaseRequest {
// 任务ID
String jobId;
// 任务类型(0: 查询存在性, 1: 查询对应值)
String serviceId;

String accessKeyId;

String jobType;
// 数据方机构id
String participateAgencyId;
// 数据集id
String datasetId;
// 发起方用户名
String jobCreator;
// 匿踪算法类型(0: hash披露算法, 1: hash混淆算法)
String jobAlgorithmType;
// 查询范围
Integer obfuscationOrder = 9;

Integer pirInvokeType = ParamEnum.JobMode.SDKMode.getValue();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.wedpr.pir.sdk.entity.request;

import com.wedpr.pir.sdk.entity.body.PirDataBody;
import com.wedpr.pir.sdk.entity.body.ServiceConfigBody;
import java.math.BigInteger;
import java.util.List;
import lombok.Data;
Expand All @@ -16,4 +17,5 @@ public class ServerJobRequest extends PirBaseRequest {
BigInteger x;
BigInteger y;
List<PirDataBody> dataBodyList;
ServiceConfigBody serviceConfigBody;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
public class ServerOTRequest {
String jobType;
String datasetId;
String[] params;
BigInteger x;
BigInteger y;
List<PirDataBody> dataBodyList;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.wedpr.pir.sdk.entity.response;

import com.wedpr.pir.sdk.entity.body.ServiceConfigBody;
import lombok.Data;

/**
* @author zachma
* @date 2024/9/3
*/
@Data
public class ClientAuthResponse {
String code;
String message;
ServiceConfigBody data;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/
@Data
public class PirResultResponse {
String jobId;
String jobType;
// List<PirResultBody> detail;
ClientDecryptResponse detail;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,15 @@ public enum AlgorithmType {
@AllArgsConstructor
public enum HttpType {
HttpTimeout(180),
HttpMaxRetries(4);
HttpMaxRetries(1);
private Integer value;
}

@Getter
@AllArgsConstructor
public enum JobMode {
SDKMode(0),
DirectorMode(1);
private Integer value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public enum WedprStatusEnum {
INVALID_TRANS_TYPE("2009", "不合法的交易类型"),
INVALID_FILE_PATH("2010", "不合法交集文件路径"),
INVALID_ROLE_VALUE("2011", "pir.Role设置错误"),
CSV_ERROR("2012", "csv解析是错误"),

// 3000 db错误
DB_ERROR("3000", "DB查询失败"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ private static String sendGetRequest(String url) throws IOException, WedprExcept
}

private static Response sendPostRequest(String url, String jsonBody) throws IOException {
if (!url.startsWith("http")) {
url = "http://" + url;
}
OkHttpClient httpClient = new OkHttpClient().newBuilder().build();
RequestBody body = RequestBody.create(jsonBody, JSON_MEDIA_TYPE);
Request request = new Request.Builder().post(body).url(url).build();
Expand All @@ -69,6 +72,7 @@ public static <T> T sendPostRequestWithRetry(String url, String body, Class<T> r
}

} catch (Exception e) {
e.printStackTrace();
retries++;
if (retries > maxRetries) {
throw new WedprException(WedprStatusEnum.HTTP_CALL_ERROR);
Expand Down
Loading

0 comments on commit af6f5d9

Please sign in to comment.