sum-check protocol合集

这篇博客深入解读了2008年至2021年间关于zkSNARKs的重要论文,包括Delegating Computation的互动证明,Hyrax的双效zkSNARKs,无信任设置的Spartan,以及Libra和Thaler论文中sumcheck协议的实现。它涵盖了从理论到实践的进展,展示了如何优化证明效率并去除信任前提。
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netinet/ip.h> #include <netinet/udp.h> // 计算校验和函数 unsigned short checksum(void *b, int len) { unsigned short *buf = b; unsigned int sum = 0; unsigned short result; for (sum = 0; len > 1; len -= 2) sum += *buf++; if (len == 1) sum += *(unsigned char *)buf; sum = (sum >> 16) + (sum & 0xFFFF); sum += (sum >> 16); result = ~sum; return result; } int main() { int sockfd; char buffer[1024]; struct sockaddr_in dest_addr; // 创建原始套接字 if ((sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) { perror("socket() error"); exit(EXIT_FAILURE); } // 设置目标地址 memset(&dest_addr, 0, sizeof(dest_addr)); dest_addr.sin_family = AF_INET; dest_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); // 目标IP // 构建IP头部 struct iphdr *ip_header = (struct iphdr *)buffer; ip_header->ihl = 5; // 头部长度(32位字) ip_header->version = 4; // IPv4 ip_header->tos = 0; // 服务类型 ip_header->tot_len = sizeof(struct iphdr) + sizeof(struct udphdr) + 5; // 总长度 ip_header->id = htons(54321); // 标识 ip_header->frag_off = 0; // 分片偏移 ip_header->ttl = 64; // 生存时间 ip_header->protocol = IPPROTO_UDP; // 协议类型 ip_header->saddr = inet_addr("127.0.0.1"); // 源IP ip_header->daddr = dest_addr.sin_addr.s_addr; // 目标IP ip_header->check = 0; // 先置0再计算校验和 ip_header->check = checksum((unsigned short *)ip_header, ip_header->ihl*4); // 构建UDP头部 struct udphdr *udp_header = (struct udphdr *)(buffer + sizeof(struct iphdr)); udp_header->source = htons(12345); // 源端口 udp_header->dest = htons(54321); // 目标端口 udp_header->len = htons(sizeof(struct udphdr) + 5); // UDP总长度 udp_header->check = 0; // 先置0 // 添加UDP数据 char *data = buffer + sizeof(struct iphdr) + sizeof(struct udphdr); strcpy(data, "TEST"); // 计算UDP校验和(包含伪头部) char pseudo[1024]; struct pseudo_udp_header { u_int32_t src_ip; u_int32_t dst_ip; u_int8_t zero; u_int8_t protocol; u_int16_t length; } *pseudo_header = (struct pseudo_udp_header *)pseudo; pseudo_header->src_ip = ip_header->saddr; pseudo_header->dst_ip = ip_header->daddr; pseudo_header->zero = 0; pseudo_header->protocol = IPPROTO_UDP; pseudo_header->length = udp_header->len; // 拼接伪头部+UDP头部+数据 memcpy(pseudo + sizeof(struct pseudo_udp_header), udp_header, ntohs(udp_header->len)); udp_header->check = checksum(pseudo, sizeof(struct pseudo_udp_header) + ntohs(udp_header->len)); // 发送数据包 if (sendto(sockfd, buffer, ip_header->tot_len, 0, (struct sockaddr *)&dest_addr, sizeof(dest_addr)) < 0) { perror("sendto() error"); close(sockfd); exit(EXIT_FAILURE); } printf("UDP packet sent. Check Wireshark for verification.\n"); close(sockfd); return 0; } 分析
08-08
static inline int virtio_net_hdr_to_skb(struct sk_buff *skb, const struct virtio_net_hdr *hdr, bool little_endian) { unsigned int gso_type = 0; unsigned int thlen = 0; unsigned int p_off = 0; unsigned int ip_proto; if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) { switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) { case VIRTIO_NET_HDR_GSO_TCPV4: gso_type = SKB_GSO_TCPV4; ip_proto = IPPROTO_TCP; thlen = sizeof(struct tcphdr); break; case VIRTIO_NET_HDR_GSO_TCPV6: gso_type = SKB_GSO_TCPV6; ip_proto = IPPROTO_TCP; thlen = sizeof(struct tcphdr); break; case VIRTIO_NET_HDR_GSO_UDP: gso_type = SKB_GSO_UDP; ip_proto = IPPROTO_UDP; thlen = sizeof(struct udphdr); break; case VIRTIO_NET_HDR_GSO_UDP_L4: gso_type = SKB_GSO_UDP_L4; ip_proto = IPPROTO_UDP; thlen = sizeof(struct udphdr); break; default: return -EINVAL; } if (hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN) gso_type |= SKB_GSO_TCP_ECN; if (hdr->gso_size == 0) return -EINVAL; } skb_reset_mac_header(skb); if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) { u32 start = __virtio16_to_cpu(little_endian, hdr->csum_start); u32 off = __virtio16_to_cpu(little_endian, hdr->csum_offset); u32 needed = start + max_t(u32, thlen, off + sizeof(__sum16)); if (!pskb_may_pull(skb, needed)) return -EINVAL; if (!skb_partial_csum_set(skb, start, off)) return -EINVAL; p_off = skb_transport_offset(skb) + thlen; if (!pskb_may_pull(skb, p_off)) return -EINVAL; } else { /* gso packets without NEEDS_CSUM do not set transport_offset. * probe and drop if does not match one of the above types. */ if (gso_type && skb->network_header) { struct flow_keys_basic keys; if (!skb->protocol) { __be16 protocol = dev_parse_header_protocol(skb); if (!protocol) virtio_net_hdr_set_proto(skb, hdr); else if (!virtio_net_hdr_match_proto(protocol, hdr->gso_type)) return -EINVAL; else skb->protocol = protocol; } retry: if (!skb_flow_dissect_flow_keys_basic(NULL, skb, &keys, NULL, 0, 0, 0, 0)) { /* UFO does not specify ipv4 or 6: try both */ if (gso_type & SKB_GSO_UDP && skb->protocol == htons(ETH_P_IP)) { skb->protocol = htons(ETH_P_IPV6); goto retry; } return -EINVAL; } p_off = keys.control.thoff + thlen; if (!pskb_may_pull(skb, p_off) || keys.basic.ip_proto != ip_proto) return -EINVAL; skb_set_transport_header(skb, keys.control.thoff); } else if (gso_type) { p_off = thlen; if (!pskb_may_pull(skb, p_off)) return -EINVAL; } } if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) { u16 gso_size = __virtio16_to_cpu(little_endian, hdr->gso_size); unsigned int nh_off = p_off; struct skb_shared_info *shinfo = skb_shinfo(skb); switch (gso_type & ~SKB_GSO_TCP_ECN) { case SKB_GSO_UDP: /* UFO may not include transport header in gso_size. */ nh_off -= thlen; break; case SKB_GSO_UDP_L4: if (!(hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM)) return -EINVAL; if (skb->csum_offset != offsetof(struct udphdr, check)) return -EINVAL; if (skb->len - p_off > gso_size * UDP_MAX_SEGMENTS) return -EINVAL; if (gso_type != SKB_GSO_UDP_L4) return -EINVAL; break; case SKB_GSO_TCPV4: case SKB_GSO_TCPV6: if (skb->csum_offset != offsetof(struct tcphdr, check)) return -EINVAL; break; } /* Too small packets are not really GSO ones. */ if (skb->len - nh_off > gso_size) { shinfo->gso_size = gso_size; shinfo->gso_type = gso_type; /* Header must be checked, and gso_segs computed. */ shinfo->gso_type |= SKB_GSO_DODGY; shinfo->gso_segs = 0; } } return 0; }
最新发布
10-28
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值