Skip to content

Commit

Permalink
Python (Chia-Network#12)
Browse files Browse the repository at this point in the history
* Point compression greatest y, python aggregation
* Aggregating aggregates, and verifying aggregates python
* Uncomment tests
* Fix bug in point compression
  • Loading branch information
mariano54 authored Aug 31, 2018
1 parent 04a978e commit 92be1b6
Show file tree
Hide file tree
Showing 12 changed files with 625 additions and 157 deletions.
37 changes: 34 additions & 3 deletions contrib/relic/src/ep/relic_ep_pck.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,23 @@
/*============================================================================*/

void ep_pck(ep_t r, const ep_t p) {
int b = fp_get_bit(p->y, 0);
bn_t halfQ;
bn_null(halfQ);
bn_new(halfQ);
halfQ->used = FP_DIGS;
dv_copy(halfQ->dp, fp_prime_get(), FP_DIGS);
bn_hlv(halfQ, halfQ);

bn_t yValue;
bn_null(yValue);
bn_new(yValue);
fp_prime_back(yValue, p->y);

int b = bn_cmp(yValue, halfQ) == CMP_GT;

bn_free(yValue);
bn_free(halfQ);

fp_copy(r->x, p->x);
fp_zero(r->y);
fp_set_bit(r->y, 0, b);
Expand All @@ -58,9 +74,22 @@ int ep_upk(ep_t r, const ep_t p) {
result = fp_srt(t, t);

if (result) {
/* Verify if least significant bit of the result matches the
/* Verify whether the y coordinate is the larger one, matches the
* compressed y-coordinate. */
if (fp_get_bit(t, 0) != fp_get_bit(p->y, 0)) {
bn_t halfQ;
bn_null(halfQ);
bn_new(halfQ);
halfQ->used = FP_DIGS;
dv_copy(halfQ->dp, fp_prime_get(), FP_DIGS);
bn_hlv(halfQ, halfQ);

bn_t yValue;
bn_null(yValue);
bn_new(yValue);
fp_prime_back(yValue, t);
int b = bn_cmp(yValue, halfQ) == CMP_GT;

if (b != fp_get_bit(p->y, 0)) {
fp_neg(t, t);
}
fp_copy(r->x, p->x);
Expand All @@ -74,6 +103,8 @@ int ep_upk(ep_t r, const ep_t p) {
}
FINALLY {
fp_free(t);
bn_free(yValue);
bn_free(halfQ);
}
return result;
}
38 changes: 35 additions & 3 deletions contrib/relic/src/epx/relic_ep2_pck.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,30 @@
/*============================================================================*/

void ep2_pck(ep2_t r, ep2_t p) {
int b = fp_get_bit(p->y[0], 0);
bn_t halfQ;
bn_null(halfQ);
bn_new(halfQ);
halfQ->used = FP_DIGS;
dv_copy(halfQ->dp, fp_prime_get(), FP_DIGS);
bn_hlv(halfQ, halfQ);

bn_t yValue;
bn_null(yValue);
bn_new(yValue);
fp_prime_back(yValue, p->y[1]);

int b = bn_cmp(yValue, halfQ) == CMP_GT;

fp2_copy(r->x, p->x);
fp2_zero(r->y);
fp_set_bit(r->y[0], 0, b);
fp_zero(r->y[1]);
fp_set_dig(r->z[0], 1);
fp_zero(r->z[1]);
r->norm = 1;

bn_free(yValue);
bn_free(halfQ);
}

int ep2_upk(ep2_t r, ep2_t p) {
Expand All @@ -61,9 +77,23 @@ int ep2_upk(ep2_t r, ep2_t p) {
result = fp2_srt(t, t);

if (result) {
/* Verify if least significant bit of the result matches the
/* Verify whether the y coordinate is the larger one, matches the
* compressed y-coordinate. */
if (fp_get_bit(t[0], 0) != fp_get_bit(p->y[0], 0)) {
bn_t halfQ;
bn_null(halfQ);
bn_new(halfQ);
halfQ->used = FP_DIGS;
dv_copy(halfQ->dp, fp_prime_get(), FP_DIGS);
bn_hlv(halfQ, halfQ);

bn_t yValue;
bn_null(yValue);
bn_new(yValue);
fp_prime_back(yValue, t[1]);

int b = bn_cmp(yValue, halfQ) == CMP_GT;

if (b != fp_get_bit(p->y[0], 0)) {
fp2_neg(t, t);
}
fp2_copy(r->x, p->x);
Expand All @@ -78,6 +108,8 @@ int ep2_upk(ep2_t r, ep2_t p) {
}
FINALLY {
fp2_free(t);
bn_free(yValue);
bn_free(halfQ);
}
return result;
}
14 changes: 14 additions & 0 deletions python-bindings/pythonbindings.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// Copyright 2018 Chia Network Inc

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/operators.h>
Expand Down
18 changes: 17 additions & 1 deletion python-bindings/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,20 @@ def test2():


test1()
test2()
test2()

"""
Copyright 2018 Chia Network Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
Loading

0 comments on commit 92be1b6

Please sign in to comment.