I have this code, that uses PBC Library:
element_t pk, pk_temp;
element_init_G2(pk, pairing);
element_init_G2(pk_temp, pairing);
element_init_Zr(ci, pairing);
element_pow_zn(pk_temp, pk_temp, ci);
element_set1(pk);
element_add(pk, pk, pk);
element_mul_zn(pk, pk, pk_temp);
When I run this program(ci has a value from earlier calculations), this is the output that I get :
pk_temp
[116278406325033872100813200201193617766578695181932793603160637278854742066192092884782310233584512588249536578523628847229234460071209045611450183651531, 2021454548422679707182594138446448992982063147118097540714810473185383559710078393323207940613550542761869670939719707936590719813436809712827363459486303]
ci
557018308384393102708847545615423648196401851115
After pk_temp^ci
pk_temp
[108256843552655255908398113161102639677286937761571877242159361199581616450078081163742350174144405610156719380747507503987165257266343606269839543701390, 315460454942637140235438889718432767280220200962474346118962958364243678526968323118117335000004382683381426774251553048778733132443252812268528626451784]
After pk = pk + pk
pk
0
After pk = pk * pk_temp
pk
O
UPDATE
If pk is initialized as an element in Zr, the addition works.
element_mul_zn(pk, pk, pk_temp);
But in fact, your pk_temp is in G2 rather than Zn.
See the definition in include/pbc_field.h
static inline void element_mul_zn(element_t c, element_t a, element_t z) {
mpz_t z0;
PBC_ASSERT_MATCH2(c, a);
//TODO: check z->field is Zn
mpz_init(z0);
element_to_mpz(z0, z);
element_mul_mpz(c, a, z0);
mpz_clear(z0);
}
The code does not check if z->field is Zn and will just converts G2 to mpz.
It does not make sense if you want to convert G2 to mpz, and element_to_mpz just gives you 0.
I'm not sure which type of pairing do you use, so I suppose you use type A pairing. The function
element_set1(pk);
sets pk to O, the point at infinity, since pk is an element of G2. So the result should look like this
element_set1(pk);
pk = O
After pk=pk+pk
pk = O
After pk = pk * pk_temp
pk
O
the result of pk*pk_temp is O because for all scalar number c, O = c * O.
We generally do not accept code-review questions here; perhaps stackexchange (not crypto) would be better.
On the other hand, I do notice that you do:
element_set1(pk);
Would that set pk to 1? If so, why would it be surprising that pk would be set to 2 after you add pk to itself?
Related
Quoting the general expression of Tuple Relational Calculus (Fundamentals of Database Systems - Elmasri, Navathe; 6th edition)
A general expression of the tuple relational calculus is of the form
{t1.Aj, t2.Ak, ..., tn.Am | COND(t1, t2, ..., tn, tn+1, tn+2, ..., tn+m)}
where t1, t2, ..., tn, tn+1, ..., tn+m are tuple variables, each Ai is an attribute of the relation on which ti ranges, and COND is a condition or formula of the tuple relational calculus. A formula is made up of predicate calculus atoms, which can be one of the following:
1. An atom of the form R(ti), where R is a relation name and ti is a tuple variable. This atom identifies the range of the tuple variable ti as the relation whose name is R. It evaluates to TRUE if ti is a tuple in the relation R, and evaluates to FALSE otherwise.
2. An atom of the form ti.A op tj.B, where op is one of the comparison operators in the set {=, <, ≤, >, ≥, ≠}, ti and tj are tuple variables, A is an attribute of the relation on which ti ranges, and B is an attribute of the relation on which t ranges. An atom of the form ti.A op c or c op tj.B, where op is one of the comparison operators in the set {=, <, ≤, >, ≥, ≠}, ti and tj are tuple variables, A is an attribute of the relation on which ti ranges, B is an attribute of the relation on which tj ranges, and c is a constant value.
*Edit(Thanks to philipxy): The meaning of a query in TRC with respect to the above general expression would be,
For {t|p}--"The result of such a query is the set of all tuples t that evaluate COND(t) to TRUE". For {t.a1,t.a2,...|p}--"we first specify the requested attributes […] for each selected tuple t. Then we specify the condition for selecting a tuple following the bar".
There is also a mention of,
The only free tuple variables in a tuple relational calculus expression should be those that appear to the left of the bar (|).
Consider for example, a relation Students(id, grade) and we would like to find the "id's of all students who have secured the highest grade". The query specified in Tuple Relational Calculus could be
Q1 = {s1. id | students(s1) ^ ¬(∃ s2, students(s2) ^ ( s2. grade > s1.grade) )}
Here, s1 is the free variable.
Q1 can be interpreted as all id values of tuple variable s1, where s1 ranges within the relation student (i.e. s1 belongs to student) AND there does not exist a variable s2 such that s2 belongs to student and s2.grade > s1.grade.
Consider the queries,
Q2 = {s1. id | ∃ s1, students(s1) ^ ¬(∃ s2, students(s2) ^ ( s2. grade > s1.grade) )}
and
Q3 = {s1. id | ∀ s1, students(s1) ^ ¬(∃ s2, students(s2) ^ ( s2. grade > s1.grade) )}
As we can see, s1 (variable on the left of the bar) in Q2 and Q3 is also bounded by ∃ and ∀ respectively.
How is the interpretation of Q2 and Q3 different from Q1 assuming Q2 and Q3 are even possible ?
Note:
Queries Q2 and Q3 were made up from Q1 with the purpose of trying to understand what the queries would mean if the variables on the left side of '|' were bound by existential or universal quantifiers.
(Edit, after thinking a bit more) My interpretation of Q2 and Q3 is that the result of Q2 and Q1 will be the same will not be the same as Q2 will produce all id values of s1 if there exists atleast one s1 that belongs to student and for which there does not exist s2 such that s2 belongs to student and s2.grade > s1.grade (meaning, the result of Q2 is the "set of all student id if there exists atleast one student who got the highest grade"). Q3 will produce all id values of s1 if for every s1 that belongs to student and for which there does not exist s2 such that s2 belongs to student and s2.grade > s1.grade (meaning, the result of Q3 is the "set of all student id if every student got the highest grade"). But, I'm not sure if the queries Q2 and Q3 are even possible or even if such a scenario where the variables on the left of the bar are also bounded by quantifiers could occur in general.
I have 3 tables: A,B,C.
A consists of a column D.
B consists of columns E,F,G,H,I,J (PK is J).
C consists of foreign key K to table B.
now I need to have F,G,H unique, but if G is null then have F,H unique and I and E unique. (and G XOR I must be null).
is there a way I can do it in db and not programatically?
Thanks.
I'm pretty sure you can do this with unique indexes and the fact that NULL is ignored for a unique index.
First, create an index on F, G, and H:
create unique index idx_b_f_g_h on b(f, g, h)
This handles the "F, G, H unique" case. To handle the "G is null, then F, H unique" do:
create unique index idx_b_f_h_j on b(f, h, (case when G is null then 0 else j end));
This replaces a non-NULL values of G with the primary key -- ensuring uniqueness. It uses an arbitrary "constant" value when G is null. (Note the constant should be of the same type as the primary key.)
To handle, I and E unique, you can also use a functional index. I think you mean:
create unique index idx_b_i_e_j on b(coalesce(i, e));
You can handle the fact that i or e is NULL using a check constraint.
What is wrong with the commented out part in the SELECT below ?
If I uncomment the whole CASE...END, the SELECT becomes invalid.
What I want is, depending on wether there is a delivery address or not, take one group of address fields or another, ideally WITHOUT repeating the condition or using a COALESCE for each field. I am using SQL Server 2008 R2.
Thanks !
SELECT a, b, c,
-- CASE x is null
-- THEN d, e, f,
-- ELSE g, h, i,
-- END
k, l, m
FROM sometable
You're missing a WHEN, and you're only allowed to return a single value from a CASE expression:
SELECT a, b, c,
CASE WHEN x is null
THEN d
ELSE g
END,
CASE WHEN x is null
THEN e
ELSE h
END,
CASE WHEN x is null
THEN f
ELSE i
END,
k, l, m
FROM sometable
This also assumes the types of d and g are compatible, as are (e,h) and (f,i)
This query will give you the right answer and it's also easy to modify
select
s.a, s.b, s.c,
a.f1, a.f2, a.f3,
s.k, s.l, s.m
from sometable as s
outer apply (
select s.d as f1, s.e as f2, s.f as f3 where s.x is null
union all
select s.g as f1, s.h as f2, s.i as f3 where s.x is not null
) as a
SQL FIDDLE EXAMPLE
consider to put the condition for x in the where clause and then union
select a,b,c,d,e,f from table where x is null
union
select a,b,c,g,h,i from table where x is not null
the case thing needs a WHEN. I always use this structure
CASE ...
WHEN ...
THEN ...
ELSE ...
END AS Fieldname
all 5 must be present. it can be used to define one ouputfield. within it you could of course coallesce, but it will not give back an array of fields
EDIT: the ELSE is in fact not necessary, read informed comment below
I'm working with PostgreSQL to create some data types written in C.
For example, I have:
typedef struct Point3D
{
char id[50];
double x;
double y;
double z;
Point3D;
}
The input and output functions are working properly.
But the problem is the following:
Every id of Point3D must be unique (and can be NULL), so I have decided to create an unique index on this field id, but is that possible?
I'm thinking in something like this:
CREATE UNIQUE INDEX test_point3d_idx ON test_point3d (( getID(columname) ));
where getID returns the field ID of columname.
But I need to implement getID and I am really blocked.
Any advice?
The Postgres manual section "Interfacing Extensions To Indexes" explains indexes on user-defined types like your Point3D. That requires a fair amount of work. I don't know any shortcuts.
Unrelated to your question: are you sure you need this C-language Point3D datatype? Mistakes in such a datatype definition can "confuse or even crash the server". I presume the same applies to C-language operator functions supporting it.
Could you create tables with four columns, one for each Point3D field? Otherwise, could you forego C in favor of a simple CREATE TYPE point3d AS (id char(50), x float8, y float8, z float8)? Perhaps not, but worth a shot...
A unique column will allow multiple values of NULL because NULL is an unknown value so one null compared to another can never really be considered to be equal. Now logically you might consider NULL = NULL to be true, but unique constraint doesn't work that way.
Simple example to prove it.
CREATE TABLE test2
(
unq_id integer NULL,
CONSTRAINT uq_test2 UNIQUE (unq_id)
);
INSERT INTO test2
SElECT NULL;
INSERT INTO test2
SElECT NULL;
INSERT INTO test2
SElECT NULL;
SELECT *
FROM test2;
Does anyone have a process or approach to use for determining how to resove a many-to-many relationship in a relational database? Here is my scenario. I have a group of contacts and a group of phone numbers. Each contact can be associated with multiple phone numbers and each phone number can be associated with multiple contacts.
A simple example of this situation would be an office with two employess (e1 & e2), one main voice line (v1), one private voice line (v2). e1 is the CEO so they have thier own private voice line, v1, but they can also be reached by calling the main line, v2, and asking for the CEO. e2 is just an employee and can only be reached by calling v2.
So, e1 should be related to v1 & v2. e2 should be related to v2. Conversly, v1 should be related to e1 and v2 should be related to e1 & e2.
The goal here is to ge able to run queries like "what numbers can e1 be reached at" and "what employees can be reached at v2", etc.. I know the answer will involve an intermediate table or tables but I just can't seem to nail down the exact architecture.
You don't need any temp tables for the query. There is an intermediary table for the mapping.
numbers_tbl
-----------
nid int
number varchar
employees_tbl
-----------
eid int
name varchar
employee_to_phone_tbl
-----------
eid int
nid int
How can I call Bob?
select *
from employees_tbl e
inner join employee_to_phone_tbl m
on e.eid = m.eid
inner join numbers_tbl n
on m.nid = n.nid
where e.name = 'Bob'
Who might pickup if I call this number?
select *
from numbers_tbl n
inner join employee_to_phone_tbl m
on m.nid = n.nid
inner join employees_tbl e
on e.eid = m.eid
where n.number = '555-5555'
Employees:
eID, eName
1, e1
2, e2
PhoneNumbers:
pID, pNumber
1, v1
2, v2
EmployeePhones:
eID, pID
1, 1
1, 2
2, 2
then you inner join. if you need to find out what number(s) e1 can be reached at (t-sql):
SELECT E.eName, P.pNumber
FROM dbo.Employees E
INNER JOIN dbo.EmployeePhones EP ON E.eID = EP.eID
INNER JOIN dbo.PhoneNumbers P ON EP.pID = P.eID
WHERE E.eName = 'e1'
I believe this should work (testing it right now...)
EDIT: Took me a few minutes to type up, sorry for duplication...
Others have explained the schema, but I'm going to explain the concept. What they're building for you, the table named EmployeePhones and employee_to_phone_tbl, is called an Associative Entity, which is a type of Weak Entity.
A Weak Entity does not have its own natural key and must instead be defined in terms of its foreign keys. An Associative Entity exists for the sole purpose of mapping a many-to-many relationship in a database that does not support the concept. Its primary key is the grouped foreign keys to the tables it maps.
For further information on relational theory, see this link
Normalize
Best Practices on Referential Integrity
Check this - http://statisticsio.com/Home/tabid/36/articleType/ArticleView/articleId/327/Need-Some-More-Sample-Databases.aspx
After just a little more thought, here is what I came up with. It probably goes along with the approach AviewAnew is thinking of.
employees
id (index)
name
numbers
id (index)
number
relations
employees.id (index)
numbers.id (index)
employees
1 : e1
2 : e2
numbers
1 : v1
2 : v2
relations
1 : 1
1 : 2
2 : 1
Is this the best/only approach?